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   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2) 
20   */
21  
22  package org.metricshub.wbem.sblim.cimclient.internal.discovery.slp;
23  
24  /*-
25   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
26   * WBEM Java Client
27   * ჻჻჻჻჻჻
28   * Copyright 2023 - 2025 MetricsHub
29   * ჻჻჻჻჻჻
30   * Licensed under the Apache License, Version 2.0 (the "License");
31   * you may not use this file except in compliance with the License.
32   * You may obtain a copy of the License at
33   *
34   *      http://www.apache.org/licenses/LICENSE-2.0
35   *
36   * Unless required by applicable law or agreed to in writing, software
37   * distributed under the License is distributed on an "AS IS" BASIS,
38   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39   * See the License for the specific language governing permissions and
40   * limitations under the License.
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   * Class WBEMServiceAdvertisementSLP is the SLP specific implementation if the
64   * WBEMServiceAdvertisement interface.
65   *
66   *
67   * @since 2.0.2
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  	 * Ctor.
82  	 *
83  	 * @param pDA
84  	 *            The Directory Agent from which this advertisement was received
85  	 * @param pUrl
86  	 *            The SLP service url returned by Locator.findServices().
87  	 * @param pAttributes
88  	 *            The attribute list (List&lt;String&gt;) where each entry looks
89  	 *            either like this <code>key=value</code> or this
90  	 *            <code>(key=value)</code>.
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 	 * (non-Javadoc)
117 	 *
118 	 * @see
119 	 * org.sblim.cimclient.discovery.WbemService#getAttribute(java.lang.String)
120 	 */
121 	public String getAttribute(String pAttributeName) {
122 		return this.iAttributeMap.get(pAttributeName);
123 	}
124 
125 	/*
126 	 * (non-Javadoc)
127 	 *
128 	 * @see org.sblim.cimclient.discovery.WbemService#getAttributes()
129 	 */
130 	public Set<Entry<String, String>> getAttributes() {
131 		return this.iAttributeMap.entrySet();
132 	}
133 
134 	/*
135 	 * (non-Javadoc)
136 	 *
137 	 * @see org.sblim.cimclient.discovery.WbemService#getConcreteServiceType()
138 	 */
139 	public String getConcreteServiceType() {
140 		return this.iServiceUrl.getServiceType().getConcreteTypeName();
141 	}
142 
143 	/*
144 	 * (non-Javadoc)
145 	 *
146 	 * @see org.sblim.cimclient.discovery.WbemService#getServiceUrl()
147 	 */
148 	public String getServiceUrl() {
149 		return (
150 			getConcreteServiceType() + "://" + this.iServiceUrl.getHost() + ":" + String.valueOf(this.iServiceUrl.getPort())
151 		);
152 	}
153 
154 	/*
155 	 * (non-Javadoc)
156 	 *
157 	 * @see
158 	 * org.sblim.cimclient.discovery.WBEMServiceAdvertisement#createClient(javax
159 	 * .security.auth.Subject, java.util.Locale[])
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 	 * (non-Javadoc)
185 	 *
186 	 * @see java.lang.Object#toString()
187 	 */
188 	@Override
189 	public String toString() {
190 		StringBuffer buffer = new StringBuffer();
191 		// buffer.append(iServiceUrl.getServiceType());
192 		// buffer.append("://");
193 		// buffer.append(iServiceUrl.getHost());
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 	 * (non-Javadoc)
240 	 *
241 	 * @see
242 	 * org.sblim.cimclient.discovery.WBEMServiceAdvertisement#getServiceId()
243 	 */
244 	public String getServiceId() {
245 		return getAttribute(SERVICE_ID);
246 	}
247 
248 	/*
249 	 * (non-Javadoc)
250 	 *
251 	 * @see org.sblim.cimclient.discovery.WBEMServiceAdvertisement#isExpired()
252 	 */
253 	public boolean isExpired() {
254 		return this.iExpired;
255 	}
256 
257 	/*
258 	 * (non-Javadoc)
259 	 *
260 	 * @see
261 	 * org.sblim.cimclient.discovery.WBEMServiceAdvertisement#setExpired(boolean
262 	 * )
263 	 */
264 	public void setExpired(boolean pExpired) {
265 		this.iExpired = pExpired;
266 	}
267 
268 	/*
269 	 * (non-Javadoc)
270 	 *
271 	 * @see java.lang.Object#equals(java.lang.Object)
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 	 * (non-Javadoc)
287 	 *
288 	 * @see java.lang.Object#hashCode()
289 	 */
290 	@Override
291 	public int hashCode() {
292 		return this.iServiceUrl.hashCode() + this.iDA.hashCode();
293 	}
294 }