1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.metricshub.wbem.sblim.cimclient.internal.discovery.slp;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
68
69
70
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
85
86
87
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
97
98
99
100 public Locale getLocale() {
101 return this.iLocale;
102 }
103
104
105
106
107
108
109
110 public void setLocale(Locale pLocale) {
111 this.iLocale = pLocale;
112 }
113
114
115
116
117
118
119 public Vector<String> getScopes() {
120 return this.iScopes;
121 }
122
123
124
125
126
127
128
129 public void setScopes(Vector<String> pScopes) {
130 this.iScopes = pScopes;
131 }
132
133
134
135
136
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 }