View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Alexander Wolf-Reber, IBM, a.wolf-reber@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1678915    2007-03-12  lupusalex    Integrated WBEM service discovery via SLP
17   * 1729361    2007-06-04  lupusalex    Multicast discovery is broken in DiscovererSLP
18   * 1804402    2007-09-28  ebak         IPv6 ready SLP
19   * 1892103    2008-02-13  ebak         SLP improvements
20   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
21   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
23   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
24   */
25  
26  package org.metricshub.wbem.sblim.cimclient.internal.discovery.slp;
27  
28  /*-
29   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
30   * WBEM Java Client
31   * ჻჻჻჻჻჻
32   * Copyright 2023 - 2025 MetricsHub
33   * ჻჻჻჻჻჻
34   * Licensed under the Apache License, Version 2.0 (the "License");
35   * you may not use this file except in compliance with the License.
36   * You may obtain a copy of the License at
37   *
38   *      http://www.apache.org/licenses/LICENSE-2.0
39   *
40   * Unless required by applicable law or agreed to in writing, software
41   * distributed under the License is distributed on an "AS IS" BASIS,
42   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43   * See the License for the specific language governing permissions and
44   * limitations under the License.
45   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
46   */
47  
48  import java.net.InetAddress;
49  import java.util.ArrayList;
50  import java.util.Enumeration;
51  import java.util.LinkedList;
52  import java.util.List;
53  import java.util.Locale;
54  import java.util.Vector;
55  import java.util.logging.Level;
56  import org.metricshub.wbem.sblim.cimclient.discovery.Discoverer;
57  import org.metricshub.wbem.sblim.cimclient.discovery.WBEMServiceAdvertisement;
58  import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
59  import org.metricshub.wbem.sblim.slp.Locator;
60  import org.metricshub.wbem.sblim.slp.ServiceLocationException;
61  import org.metricshub.wbem.sblim.slp.ServiceLocationManager;
62  import org.metricshub.wbem.sblim.slp.ServiceType;
63  import org.metricshub.wbem.sblim.slp.ServiceURL;
64  import org.metricshub.wbem.sblim.slp.internal.SLPDefaults;
65  
66  /**
67   * Class DiscovererSLP is the SLP specific implementation of the Discoverer
68   * interface.
69   *
70   * @since 2.0.2
71   */
72  public class DiscovererSLP implements Discoverer {
73  	private static final String SERVICE_WBEM = "service:wbem";
74  
75  	private static final ServiceType SERVICE_TYPE = new ServiceType(SERVICE_WBEM);
76  
77  	private static final Vector<String> ATTRIBUTES = new Vector<String>();
78  
79  	private Locale iLocale;
80  
81  	private Vector<String> iScopes;
82  
83  	/**
84  	 * Ctor.
85  	 *
86  	 * @param pLocale
87  	 *            The locale setting to use for the Locator
88  	 */
89  	public DiscovererSLP(Locale pLocale) {
90  		this.iLocale = pLocale;
91  		this.iScopes = new Vector<String>();
92  		this.iScopes.add(SLPDefaults.DEFAULT_SCOPE);
93  	}
94  
95  	/**
96  	 * Returns locale
97  	 *
98  	 * @return The value of locale.
99  	 */
100 	public Locale getLocale() {
101 		return this.iLocale;
102 	}
103 
104 	/**
105 	 * Sets locale
106 	 *
107 	 * @param pLocale
108 	 *            The new value of locale.
109 	 */
110 	public void setLocale(Locale pLocale) {
111 		this.iLocale = pLocale;
112 	}
113 
114 	/**
115 	 * Returns scopes
116 	 *
117 	 * @return The value of scopes.
118 	 */
119 	public Vector<String> getScopes() {
120 		return this.iScopes;
121 	}
122 
123 	/**
124 	 * Sets scopes
125 	 *
126 	 * @param pScopes
127 	 *            The new value of scopes.
128 	 */
129 	public void setScopes(Vector<String> pScopes) {
130 		this.iScopes = pScopes;
131 	}
132 
133 	/*
134 	 * (non-Javadoc)
135 	 *
136 	 * @see org.sblim.cimclient.discovery.Discoverer#findWbemServices(String[])
137 	 */
138 	public WBEMServiceAdvertisement[] findWbemServices(final String[] pDirectoryUrls) {
139 		List<WBEMServiceAdvertisement> advertisements = new ArrayList<WBEMServiceAdvertisement>();
140 
141 		if (pDirectoryUrls != null && pDirectoryUrls.length > 0) {
142 			for (int i = 0; i < pDirectoryUrls.length; ++i) {
143 				advertisements.addAll(findWbemServices(pDirectoryUrls[i]));
144 			}
145 		} else {
146 			advertisements.addAll(findWbemServices((String) null));
147 		}
148 
149 		return advertisements.toArray(new WBEMServiceAdvertisement[advertisements.size()]);
150 	}
151 
152 	private List<WBEMServiceAdvertisement> findWbemServices(final String pDA) {
153 		final LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
154 		logger.entry();
155 		logger.trace(Level.FINEST, "SLP discovery started on DA " + pDA);
156 		try {
157 			Vector<InetAddress> agents;
158 			if (pDA != null) {
159 				agents = new Vector<InetAddress>();
160 				InetAddress address = InetAddress.getByName(pDA);
161 				if (address == null) {
162 					return new LinkedList<WBEMServiceAdvertisement>();
163 				}
164 				agents.add(address);
165 			} else {
166 				agents = null;
167 			}
168 
169 			final Locator locator = ServiceLocationManager.getLocator(this.iLocale);
170 
171 			List<WBEMServiceAdvertisement> advertisements = new ArrayList<WBEMServiceAdvertisement>();
172 			Enumeration<?> serviceEnum = locator.findServices(SERVICE_TYPE, this.iScopes, "", agents);
173 
174 			while (serviceEnum != null && serviceEnum.hasMoreElements()) {
175 				ServiceURL url;
176 				try {
177 					url = (ServiceURL) serviceEnum.nextElement();
178 				} catch (RuntimeException e) {
179 					logger.trace(Level.FINE, e.getMessage(), e);
180 					continue;
181 				}
182 				Enumeration<?> attributeEnum = locator.findAttributes(url, this.iScopes, ATTRIBUTES, agents);
183 				List<String> attributes = new ArrayList<String>();
184 				while (attributeEnum != null && attributeEnum.hasMoreElements()) {
185 					try {
186 						attributes.add(attributeEnum.nextElement().toString());
187 					} catch (RuntimeException e) {
188 						logger.trace(Level.FINE, e.getMessage(), e);
189 						continue;
190 					}
191 				}
192 				advertisements.add(new WBEMServiceAdvertisementSLP(pDA, url, attributes));
193 			}
194 			logger.trace(
195 				Level.FINEST,
196 				"SLP discovery completed on DA " + pDA + ". " + advertisements.size() + " WBEM services found"
197 			);
198 			logger.exit();
199 			return advertisements;
200 		} catch (ServiceLocationException e) {
201 			LogAndTraceBroker.getBroker().trace(Level.FINE, "SLP discovery failed with error", e);
202 		} catch (Exception e) {
203 			LogAndTraceBroker.getBroker().trace(Level.FINE, "Exception during service discovery", e);
204 		}
205 		logger.exit();
206 		return new LinkedList<WBEMServiceAdvertisement>();
207 	}
208 
209 	public String[] findDirectoryServices() {
210 		final LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
211 		logger.entry();
212 		try {
213 			ArrayList<String> agents = new ArrayList<String>();
214 
215 			final Locator locator = ServiceLocationManager.getLocator(this.iLocale);
216 
217 			Enumeration<?> serviceEnum = locator.findServices(SLPDefaults.DA_SERVICE_TYPE, this.iScopes, "");
218 
219 			while (serviceEnum != null && serviceEnum.hasMoreElements()) {
220 				ServiceURL url = (ServiceURL) serviceEnum.nextElement();
221 				agents.add(url.getHost());
222 			}
223 
224 			logger.trace(
225 				Level.FINER,
226 				"SLP DA discovery completed in local subnet. " + agents.size() + " DAs found:" + agents.toString()
227 			);
228 			if (agents.size() == 0) {
229 				serviceEnum = locator.findServices(SLPDefaults.SA_SERVICE_TYPE, this.iScopes, "");
230 
231 				while (serviceEnum != null && serviceEnum.hasMoreElements()) {
232 					ServiceURL url = (ServiceURL) serviceEnum.nextElement();
233 					agents.add(url.getHost());
234 				}
235 				logger.trace(
236 					Level.FINER,
237 					"SLP SA discovery completed in local subnet. " + agents.size() + " SAs found:" + agents.toString()
238 				);
239 			}
240 
241 			logger.exit();
242 			return agents.toArray(new String[agents.size()]);
243 		} catch (ServiceLocationException e) {
244 			LogAndTraceBroker.getBroker().trace(Level.FINE, "SLP discovery failed with error", e);
245 		} catch (Exception e) {
246 			LogAndTraceBroker.getBroker().trace(Level.FINE, "Exception during service discovery", e);
247 		}
248 		logger.exit();
249 		return new String[0];
250 	}
251 }