1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.metricshub.wbem.sblim.cimclient.internal.discovery.slp;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.Map.Entry;
50 import java.util.Set;
51 import java.util.logging.Level;
52 import java.util.regex.Matcher;
53 import java.util.regex.Pattern;
54 import javax.security.auth.Subject;
55 import org.metricshub.wbem.javax.cim.CIMObjectPath;
56 import org.metricshub.wbem.javax.wbem.client.WBEMClient;
57 import org.metricshub.wbem.javax.wbem.client.WBEMClientFactory;
58 import org.metricshub.wbem.sblim.cimclient.discovery.WBEMServiceAdvertisement;
59 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
60 import org.metricshub.wbem.sblim.slp.ServiceURL;
61
62
63
64
65
66
67
68
69 public class WBEMServiceAdvertisementSLP implements WBEMServiceAdvertisement {
70 private static final Pattern ATTR_PATTERN = Pattern.compile("[(]?(.+)=([^)]+)[)]?");
71
72 private String iDA;
73
74 private ServiceURL iServiceUrl;
75
76 private Map<String, String> iAttributeMap;
77
78 private boolean iExpired = false;
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public WBEMServiceAdvertisementSLP(String pDA, ServiceURL pUrl, List<String> pAttributes) {
93 if (pDA == null || pUrl == null) throw new IllegalArgumentException("Directory and URL must not be null");
94 this.iDA = pDA;
95 this.iServiceUrl = pUrl;
96 parseAttributes(pAttributes);
97 }
98
99 private void parseAttributes(List<String> pAttributes) {
100 this.iAttributeMap = new HashMap<String, String>();
101 Iterator<String> iter = pAttributes.iterator();
102 while (iter.hasNext()) {
103 String attribute = iter.next();
104 Matcher matcher = ATTR_PATTERN.matcher(attribute);
105 if (matcher.matches()) {
106 String key = matcher.group(1).trim();
107 String value = matcher.group(2).trim();
108 this.iAttributeMap.put(key, value);
109 } else {
110 LogAndTraceBroker.getBroker().trace(Level.FINE, "SLP discovery returned invalid attribute: " + attribute);
111 }
112 }
113 }
114
115
116
117
118
119
120
121 public String getAttribute(String pAttributeName) {
122 return this.iAttributeMap.get(pAttributeName);
123 }
124
125
126
127
128
129
130 public Set<Entry<String, String>> getAttributes() {
131 return this.iAttributeMap.entrySet();
132 }
133
134
135
136
137
138
139 public String getConcreteServiceType() {
140 return this.iServiceUrl.getServiceType().getConcreteTypeName();
141 }
142
143
144
145
146
147
148 public String getServiceUrl() {
149 return (
150 getConcreteServiceType() + "://" + this.iServiceUrl.getHost() + ":" + String.valueOf(this.iServiceUrl.getPort())
151 );
152 }
153
154
155
156
157
158
159
160
161 public WBEMClient createClient(Subject pSubject, Locale[] pLocales) throws Exception {
162 String communication = getAttribute(COMM_MECHANISM);
163 if (communication.equalsIgnoreCase("OTHER")) {
164 communication = getAttribute(OTHER_COMM_MECHN_DESC);
165 }
166 WBEMClient client = WBEMClientFactory.getClient(communication);
167 CIMObjectPath path = new CIMObjectPath(
168 this.iServiceUrl.getServiceType().getConcreteTypeName(),
169 this.iServiceUrl.getHost(),
170 String.valueOf(this.iServiceUrl.getPort()),
171 null,
172 null,
173 null
174 );
175 client.initialize(path, pSubject, pLocales);
176 return client;
177 }
178
179 public String getDirectory() {
180 return this.iDA;
181 }
182
183
184
185
186
187
188 @Override
189 public String toString() {
190 StringBuffer buffer = new StringBuffer();
191
192
193
194 buffer.append(getServiceUrl());
195 buffer.append("{slp da=\"");
196 buffer.append(this.iDA);
197 buffer.append("\", attributes=[");
198 Iterator<Entry<String, String>> iter = this.iAttributeMap.entrySet().iterator();
199 while (iter.hasNext()) {
200 Entry<String, String> entry = iter.next();
201 buffer.append("\"");
202 buffer.append(entry.getKey());
203 buffer.append("=");
204 buffer.append(entry.getValue());
205 buffer.append("\"");
206 if (iter.hasNext()) buffer.append(", ");
207 }
208 buffer.append("]}");
209 return buffer.toString();
210 }
211
212 public String[] getInteropNamespaces() {
213 return parseCommaSeparatedList(getAttribute(INTEROP_NS));
214 }
215
216 private String[] parseCommaSeparatedList(String pAttribute) {
217 String[] split = pAttribute.split("[,\\n]");
218 int emptyCount = 0;
219 for (int i = 0; i < split.length; ++i) {
220 split[i] = split[i].trim();
221 if (split[i].length() == 0) {
222 ++emptyCount;
223 }
224 }
225 if (emptyCount > 0) {
226 String[] result = new String[split.length - emptyCount];
227 int j = 0;
228 for (int i = 0; i < split.length; ++i) {
229 if (split[i].length() > 0) {
230 result[j++] = split[i];
231 }
232 }
233 return result;
234 }
235 return split;
236 }
237
238
239
240
241
242
243
244 public String getServiceId() {
245 return getAttribute(SERVICE_ID);
246 }
247
248
249
250
251
252
253 public boolean isExpired() {
254 return this.iExpired;
255 }
256
257
258
259
260
261
262
263
264 public void setExpired(boolean pExpired) {
265 this.iExpired = pExpired;
266 }
267
268
269
270
271
272
273 @Override
274 public boolean equals(Object pObj) {
275 if (pObj == this) {
276 return true;
277 }
278 if (pObj != null && pObj instanceof WBEMServiceAdvertisement) {
279 WBEMServiceAdvertisement that = (WBEMServiceAdvertisement) pObj;
280 return getServiceUrl().equals(that.getServiceUrl()) && getDirectory().equals(that.getDirectory());
281 }
282 return false;
283 }
284
285
286
287
288
289
290 @Override
291 public int hashCode() {
292 return this.iServiceUrl.hashCode() + this.iDA.hashCode();
293 }
294 }