PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/sblim-cim-client2-2.1.12-src/src/org/sblim/cimclient/WBEMListenerSBLIM.java

#
Java | 354 lines | 122 code | 36 blank | 196 comment | 12 complexity | 858523086988ad00ff7e737b5f786e9a MD5 | raw file
Possible License(s): EPL-1.0
  1. /**
  2. * (C) Copyright IBM Corp. 2006, 2012
  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 : Endre Bak, IBM, ebak@de.ibm.com
  12. *
  13. * Change History
  14. * Flag Date Prog Description
  15. *-------------------------------------------------------------------------------
  16. * 1565892 2007-01-08 ebak Make SBLIM client JSR48 compliant
  17. * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
  18. * 2204488 2008-10-28 raman_arora Fix code to remove compiler warnings
  19. * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
  20. * 2531371 2009-02-10 raman_arora Upgrade client to JDK 1.5 (Phase 2)
  21. * 2763216 2009-04-14 blaschke-oss Code cleanup: visible spelling/grammar errors
  22. * 3023348 2010-07-02 blaschke-oss Listener uses # constructor instead of valueOf
  23. * 3400209 2011-08-31 blaschke-oss Highlighted Static Analysis (PMD) issues
  24. * 3469018 2012-01-03 blaschke-oss Properties not passed to CIMIndicationHandler
  25. * 3477087 2012-01-23 blaschke-oss Need Access to an Indication Sender's IP Address
  26. * 3496385 2012-03-02 blaschke-oss JSR48 1.0.0: add WBEMListener get/setProperty
  27. */
  28. package org.sblim.cimclient;
  29. import java.io.IOException;
  30. import java.net.BindException;
  31. import java.util.EventListener;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import java.util.Properties;
  35. import javax.wbem.listener.IndicationListener;
  36. import javax.wbem.listener.WBEMListener;
  37. import org.sblim.cimclient.internal.http.HttpConnectionHandler;
  38. import org.sblim.cimclient.internal.http.HttpServerConnection;
  39. import org.sblim.cimclient.internal.util.WBEMConfiguration;
  40. import org.sblim.cimclient.internal.util.WBEMConstants;
  41. import org.sblim.cimclient.internal.wbem.indications.CIMEventDispatcher;
  42. import org.sblim.cimclient.internal.wbem.indications.CIMIndicationHandler;
  43. /**
  44. * Class WBEMListenerSBLIM is the SBLIM implementation of the WBEMListener
  45. * interface.
  46. *
  47. */
  48. public class WBEMListenerSBLIM implements WBEMListener {
  49. /**
  50. * The real implementation of a listener that starts a HTTP server and
  51. * processes incoming indications
  52. *
  53. */
  54. public static class WBEMListenerImpl {
  55. private EventListener iIndicationListener;
  56. private HttpServerConnection iConnection;
  57. /**
  58. * Ctor.
  59. *
  60. * @param pLocalAddress
  61. * The local address to bind the port to. If null the port is
  62. * bound to all local addresses. For use on multi-homed
  63. * systems.
  64. * @param pPort
  65. * The port to listen on. If zero any free port will be
  66. * chosen.
  67. * @param pSSL
  68. * SSL secured connection?
  69. * @param pIndicationListener
  70. * The indication listener to forward the incoming
  71. * indications to (an instance of IndicationListener or
  72. * IndicationListenerSBLIM).
  73. * @param pProperties
  74. * The configuration.
  75. * @throws IOException
  76. */
  77. public WBEMListenerImpl(String pLocalAddress, int pPort, boolean pSSL,
  78. EventListener pIndicationListener, Properties pProperties) throws IOException {
  79. WBEMConfiguration config = (pProperties != null ? new WBEMConfiguration(pProperties)
  80. : WBEMConfiguration.getGlobalConfiguration());
  81. if (!(pIndicationListener instanceof IndicationListener)
  82. && !(pIndicationListener instanceof IndicationListenerSBLIM)) throw new IllegalArgumentException(
  83. "Listener must be instance of IndicationListener or IndicationListenerSBLIM");
  84. this.iIndicationListener = pIndicationListener;
  85. CIMEventDispatcher eventDispatcher = new CIMEventDispatcher(this.iIndicationListener);
  86. CIMIndicationHandler indicationHandler = new CIMIndicationHandler(eventDispatcher,
  87. config);
  88. this.iConnection = new HttpServerConnection(
  89. new HttpConnectionHandler(indicationHandler), pLocalAddress, pPort, pSSL,
  90. config);
  91. }
  92. @Override
  93. protected void finalize() throws Throwable {
  94. stop();
  95. super.finalize();
  96. }
  97. /**
  98. * Starts the HTTP server connection receiving the indications.
  99. */
  100. public void start() {
  101. this.iConnection.start();
  102. }
  103. /**
  104. * Stops the HTTP server connection receiving the indications.
  105. */
  106. public void stop() {
  107. this.iConnection.close();
  108. }
  109. /**
  110. * Returns the listener we forward the indications to.
  111. *
  112. * @return The listener.
  113. */
  114. public IndicationListener getIndicationListener() {
  115. return (this.iIndicationListener instanceof IndicationListener) ? (IndicationListener) this.iIndicationListener
  116. : null;
  117. }
  118. /**
  119. * Returns the listener we forward the indications to.
  120. *
  121. * @return The listener.
  122. */
  123. public IndicationListenerSBLIM getIndicationListenerSBLIM() {
  124. return (this.iIndicationListener instanceof IndicationListenerSBLIM) ? (IndicationListenerSBLIM) this.iIndicationListener
  125. : null;
  126. }
  127. /**
  128. * Returns the listener port.
  129. *
  130. * @return The listener port.
  131. */
  132. public int getListenerPort() {
  133. return this.iConnection.getPort();
  134. }
  135. }
  136. private final static WBEMListenerSBLIM INSTANCE = new WBEMListenerSBLIM();
  137. /**
  138. * Returns the singleton instance
  139. *
  140. * @return The instance
  141. */
  142. public static WBEMListenerSBLIM getInstance() {
  143. return INSTANCE;
  144. }
  145. private Map<Integer, WBEMListenerImpl> iPortMap = new HashMap<Integer, WBEMListenerImpl>();
  146. /**
  147. * Ctor.
  148. */
  149. private WBEMListenerSBLIM() {
  150. // empty
  151. }
  152. /*
  153. * (non-Javadoc)
  154. *
  155. * @see javax.wbem.listener.WBEMListener#addListener(javax.wbem.listener.
  156. * IndicationListener, int, java.lang.String)
  157. */
  158. public int addListener(IndicationListener pListener, int pPort, String pTransport)
  159. throws IOException {
  160. return addListener((EventListener) pListener, pPort, pTransport, null, null);
  161. }
  162. /*
  163. * (non-Javadoc)
  164. *
  165. * @see javax.wbem.listener.WBEMListener#addListener(javax.wbem.listener.
  166. * IndicationListener, int, java.lang.String, java.lang.String)
  167. */
  168. public int addListener(IndicationListener pListener, int pPort, String pTransport,
  169. String pLocalAddr) throws IOException {
  170. return addListener((EventListener) pListener, pPort, pTransport, pLocalAddr, null);
  171. }
  172. /**
  173. * Add a new listener using the specified port.
  174. *
  175. * @param pListener
  176. * The Indication Listener that will be called when an indication
  177. * is received.
  178. * @param pPort
  179. * The port to listen on. Use 0 to specify any available port.
  180. * @param pTransport
  181. * The transport to use (e.g. HTTP or HTTPS).
  182. * @param pLocalAddr
  183. * The local IP address to bind to. This is only needed in
  184. * multi-homed systems. A value of <code>null</code> will bind to
  185. * all IP addresses.
  186. * @param pConfigurationProperties
  187. * The individual configuration properties for this listener.
  188. * @return The port that was used.
  189. * @throws IOException
  190. * This exception is thrown when binding to pPort fails.
  191. */
  192. public int addListener(IndicationListener pListener, int pPort, String pTransport,
  193. String pLocalAddr, Properties pConfigurationProperties) throws IOException {
  194. return addListener((EventListener) pListener, pPort, pTransport, pLocalAddr,
  195. pConfigurationProperties);
  196. }
  197. /**
  198. * Add a new listener using the specified port.
  199. *
  200. * @param pListener
  201. * The SBLIM Indication Listener that will be called when an
  202. * indication is received.
  203. * @param pPort
  204. * The port to listen on. Use 0 to specify any available port.
  205. * @param pTransport
  206. * The transport to use (e.g. HTTP or HTTPS).
  207. * @return The port that was used.
  208. * @throws IOException
  209. * This exception is thrown when binding to pPort fails.
  210. */
  211. public int addListener(IndicationListenerSBLIM pListener, int pPort, String pTransport)
  212. throws IOException {
  213. return addListener((EventListener) pListener, pPort, pTransport, null, null);
  214. }
  215. /**
  216. * Add a new listener using the specified port and local address.
  217. *
  218. * @param pListener
  219. * The SBLIM Indication Listener that will be called when an
  220. * indication is received.
  221. * @param pPort
  222. * The port to listen on. Use 0 to specify any available port.
  223. * @param pTransport
  224. * The transport to use (e.g. HTTP or HTTPS).
  225. * @param pLocalAddr
  226. * The local IP address to bind to. This is only needed in
  227. * multi-homed systems. A value of <code>null</code> will bind to
  228. * all IP addresses.
  229. * @return The port that was used.
  230. * @throws IOException
  231. * This exception is thrown when binding to pPort fails.
  232. */
  233. public int addListener(IndicationListenerSBLIM pListener, int pPort, String pTransport,
  234. String pLocalAddr) throws IOException {
  235. return addListener((EventListener) pListener, pPort, pTransport, pLocalAddr, null);
  236. }
  237. /**
  238. * Add a new listener using the specified port, local address and
  239. * properties.
  240. *
  241. * @param pListener
  242. * The SBLIM Indication Listener that will be called when an
  243. * indication is received.
  244. * @param pPort
  245. * The port to listen on. Use 0 to specify any available port.
  246. * @param pTransport
  247. * The transport to use (e.g. HTTP or HTTPS).
  248. * @param pLocalAddr
  249. * The local IP address to bind to. This is only needed in
  250. * multi-homed systems. A value of <code>null</code> will bind to
  251. * all IP addresses.
  252. * @param pConfigurationProperties
  253. * The individual configuration properties for this listener.
  254. * @return The port that was used.
  255. * @throws IOException
  256. * This exception is thrown when binding to pPort fails.
  257. */
  258. public int addListener(IndicationListenerSBLIM pListener, int pPort, String pTransport,
  259. String pLocalAddr, Properties pConfigurationProperties) throws IOException {
  260. return addListener((EventListener) pListener, pPort, pTransport, pLocalAddr,
  261. pConfigurationProperties);
  262. }
  263. /**
  264. * Add a new listener using the specified port, local address and
  265. * properties. This is the worker routine for all public addListener
  266. * methods.
  267. *
  268. * @param pListener
  269. * The indication listener (<code>IndicationListener</code> or
  270. * <code>IndicationListenerSBLIM</code>) that will be called when
  271. * an indication is received.
  272. * @param pPort
  273. * The port to listen on. Use 0 to specify any available port.
  274. * @param pTransport
  275. * The transport to use (e.g. HTTP or HTTPS).
  276. * @param pLocalAddr
  277. * The local IP address to bind to. This is only needed in
  278. * multi-homed systems. A value of <code>null</code> will bind to
  279. * all IP addresses.
  280. * @param pConfigurationProperties
  281. * The individual configuration properties for this listener.
  282. * @return The port that was used.
  283. * @throws IOException
  284. * This exception is thrown when binding to pPort fails.
  285. */
  286. private synchronized int addListener(EventListener pListener, int pPort, String pTransport,
  287. String pLocalAddr, Properties pConfigurationProperties) throws IOException {
  288. if (pPort > 0 && this.iPortMap.containsKey(Integer.valueOf(pPort))) { throw new BindException(
  289. "Port already in use."); }
  290. boolean ssl;
  291. if (pTransport.equalsIgnoreCase(WBEMConstants.HTTP)) ssl = false;
  292. else if (pTransport.equalsIgnoreCase(WBEMConstants.HTTPS)) ssl = true;
  293. else throw new IllegalArgumentException("Unknown transport: " + pTransport
  294. + "! Valid values are http and https.");
  295. WBEMListenerImpl listener = new WBEMListenerImpl(pLocalAddr, pPort, ssl, pListener,
  296. pConfigurationProperties);
  297. listener.start();
  298. this.iPortMap.put(Integer.valueOf(listener.getListenerPort()), listener);
  299. return listener.getListenerPort();
  300. }
  301. public String getProperty(String pName) {
  302. if (pName.startsWith("javax.wbem.")) {
  303. // Process JSR48 properties
  304. return null;
  305. }
  306. return null;
  307. }
  308. public synchronized void removeListener(int pPort) {
  309. WBEMListenerImpl listener = this.iPortMap.remove(Integer.valueOf(pPort));
  310. if (listener != null) {
  311. listener.stop();
  312. }
  313. }
  314. public void setProperty(String pName, String pValue) {
  315. throw new IllegalArgumentException("Property " + pName + " with value " + pValue
  316. + " not supported!");
  317. }
  318. }