1 22 package org.objectweb.petals.binding.jms.su; 23 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URLClassLoader ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.spi.InitialContextFactory ; 34 35 import org.objectweb.petals.binding.jms.JMSConnection; 36 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 37 import org.objectweb.petals.component.common.su.ServiceUnitListener; 38 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil; 39 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions; 40 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 41 42 import static org.objectweb.petals.component.common.util.ClassLoaderUtil.createClassLoader; 43 44 52 public class JMSServiceUnitListener implements ServiceUnitListener { 53 54 private static final String CONNECTION_FACTORY = "connection-factory"; 55 56 private static final String ADDRESS = "address"; 57 58 private static final String INITIAL_CONTEXT_FACTORY = "initial-context-factory"; 59 60 private static final String USER = "user"; 61 62 private static final String PASSWORD = "password"; 63 64 private static final String TRANSACTED = "transacted"; 65 66 70 private Map <String , JMSConnection> jmsProducerConnections; 71 72 76 private Map <String , JMSConnection> jmsConsumerConnections; 77 78 81 private Map <String , String > addresses; 82 83 93 public JMSServiceUnitListener( 94 Map <String , JMSConnection> jmsProducerConnections, 95 Map <String , JMSConnection> jmsConsumerConnections) { 96 this.jmsProducerConnections = jmsProducerConnections; 97 this.jmsConsumerConnections = jmsConsumerConnections; 98 addresses = new HashMap <String , String >(); 99 } 100 101 106 public void onSUDeployed(String serviceUnitName, String suRootPath, 107 JBIDescriptor descriptor) throws PEtALSComponentSDKException { 108 109 int providesCount = descriptor.getServices().getProvides().size(); 110 int consumesCount = descriptor.getServices().getConsumes().size(); 111 112 if (providesCount + consumesCount != 1) { 113 throw new PEtALSComponentSDKException( 114 "Only one 'Consumes' or 'Provides' is allowed per JMS-BC ServiceUnit."); 115 116 } 117 JMSConnection.Role role = null; 118 Extensions extensions = null; 119 120 if (providesCount == 1) { 121 role = JMSConnection.Role.JMS_PRODUCER; 122 extensions = descriptor.getServices().getProvides().get(0) 123 .getExtensions(); 124 125 } else { 126 role = JMSConnection.Role.JMS_CONSUMER; 127 extensions = descriptor.getServices().getConsumes().get(0) 128 .getExtensions(); 129 } 130 131 Context context = setupContext(suRootPath, extensions); 134 135 JMSConnection connection = setupConnection(context, extensions, role); 137 138 String address = PetalsExtensionsUtil 140 .extractValueFromKeyValueExtension(extensions, ADDRESS); 141 142 addresses.put(serviceUnitName, address); 144 if (role == JMSConnection.Role.JMS_PRODUCER) { 145 jmsProducerConnections.put(address, connection); 146 } else { 147 jmsConsumerConnections.put(address, connection); 148 } 149 } 150 151 154 public void onSUStarted(String serviceUnitName) 155 throws PEtALSComponentSDKException { 156 157 String address = addresses.get(serviceUnitName); 158 159 if (address != null) { 160 JMSConnection jmsConnection = jmsProducerConnections.get(address); 161 if (jmsConnection == null) { 162 jmsConnection = jmsConsumerConnections.get(address); 163 } 164 jmsConnection.start(); 165 } 166 } 167 168 171 public void onSUStopped(String serviceUnitName) 172 throws PEtALSComponentSDKException { 173 174 String address = addresses.get(serviceUnitName); 175 176 if (address != null) { 177 JMSConnection jmsConnection = jmsProducerConnections.get(address); 178 if (jmsConnection == null) { 179 jmsConnection = jmsConsumerConnections.get(address); 180 } 181 jmsConnection.stop(); 182 } 183 } 184 185 public void onSUUndeployed(String serviceUnitName, String suRootPath, 186 JBIDescriptor descriptor) throws PEtALSComponentSDKException { 187 188 String address = addresses.remove(serviceUnitName); 189 190 if (address != null) { 191 JMSConnection jmsConnection = jmsProducerConnections 192 .remove(address); 193 if (jmsConnection == null) { 194 jmsConsumerConnections.remove(address); 195 } 196 } 197 } 198 199 209 private Context setupContext(String suRootPath, Extensions extensions) 210 throws PEtALSComponentSDKException { 211 212 URLClassLoader classLoader = createClassLoader(suRootPath, this 216 .getClass().getClassLoader()); 217 218 Properties jndiProperties = createJNDIProperties(extensions); 220 221 return createJNDIContext(classLoader, jndiProperties); 223 } 224 225 233 private Properties createJNDIProperties(Extensions extensions) 234 throws PEtALSComponentSDKException { 235 Properties outProperties = new Properties (); 236 237 String url = null; 238 try { 239 URI address = new URI (PetalsExtensionsUtil 240 .extractValueFromKeyValueExtension(extensions, ADDRESS)); 241 url = address.getScheme() + ":" + address.getSchemeSpecificPart(); 242 } catch (URISyntaxException e) { 243 throw new PEtALSComponentSDKException( 244 ADDRESS 245 + " property not found in the Service Unit descriptor file." 246 + e); 247 } 248 String factory = PetalsExtensionsUtil 249 .extractValueFromKeyValueExtension(extensions, 250 INITIAL_CONTEXT_FACTORY); 251 252 outProperties.setProperty(InitialContext.PROVIDER_URL, url); 253 outProperties.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, 254 factory); 255 return outProperties; 256 } 257 258 272 private Context createJNDIContext(ClassLoader classLoader, 273 Properties properties) throws PEtALSComponentSDKException { 274 Context ctx = null; 275 try { 276 InitialContextFactory icf = (InitialContextFactory ) classLoader 277 .loadClass( 278 properties 279 .getProperty(InitialContext.INITIAL_CONTEXT_FACTORY)) 280 .newInstance(); 281 ctx = icf.getInitialContext(properties); 282 } catch (Exception e) { 283 throw new PEtALSComponentSDKException( 284 "JNDI context can not be created." + e); 285 } 286 return ctx; 287 } 288 289 304 private JMSConnection setupConnection(Context context, 305 Extensions extensions, JMSConnection.Role role) 306 throws PEtALSComponentSDKException { 307 String connName = PetalsExtensionsUtil 308 .extractValueFromKeyValueExtension(extensions, 309 CONNECTION_FACTORY); 310 311 String destName = null; 312 try { 313 URI address = new URI (PetalsExtensionsUtil 314 .extractValueFromKeyValueExtension(extensions, ADDRESS)); 315 destName = address.getFragment(); 316 } catch (URISyntaxException e) { 317 throw new PEtALSComponentSDKException( 318 ADDRESS 319 + " property not found in the Service Unit descriptor file." 320 + e); 321 } 322 323 String user = PetalsExtensionsUtil.extractValueFromKeyValueExtension( 324 extensions, USER); 325 326 String password = PetalsExtensionsUtil 327 .extractValueFromKeyValueExtension(extensions, PASSWORD); 328 329 boolean transacted = false; 330 String transac = PetalsExtensionsUtil 331 .extractValueFromKeyValueExtension(extensions, TRANSACTED); 332 transacted = Boolean.valueOf(transac); 333 334 return new JMSConnection(context, connName, destName, transacted, user, 335 password, role); 336 337 } 338 339 } | Popular Tags |