diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2011-11-03 13:42:04 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2011-11-03 13:42:04 +0000 |
commit | b58f1db61886c798c00e5849d38e74107e220bf0 (patch) | |
tree | 57b78601f458f11c42e418a64dcf870e625be77d /sca-java-2.x/trunk/modules | |
parent | 6a41868b23c2cbc21eed31ae02302d2d35d4f6c3 (diff) |
Move the ActiveMQ specific code out from the JMS binding runtime module to binding-jms-runtime-activemq
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1197112 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules')
2 files changed, 74 insertions, 6 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/pom.xml b/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/pom.xml index 556fd74d56..32262c82da 100644 --- a/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/pom.xml @@ -57,6 +57,21 @@ <scope>provided</scope> </dependency> + <!-- Add the dependency for ActiveMQ as the JMS runtime to use --> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-core</artifactId> + <version>5.3.0</version> + <exclusions> + <exclusion> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + </exclusion> + </exclusions> + </dependency> + <!-- end of addition for ActiveMQ --> + + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> diff --git a/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/src/main/java/org/apache/tuscany/sca/binding/jms/runtime/activemq/ActiveMQJMSResourceFactory.java b/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/src/main/java/org/apache/tuscany/sca/binding/jms/runtime/activemq/ActiveMQJMSResourceFactory.java index 4dbe6dca06..f8c06058a0 100644 --- a/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/src/main/java/org/apache/tuscany/sca/binding/jms/runtime/activemq/ActiveMQJMSResourceFactory.java +++ b/sca-java-2.x/trunk/modules/binding-jms-runtime-activemq/src/main/java/org/apache/tuscany/sca/binding/jms/runtime/activemq/ActiveMQJMSResourceFactory.java @@ -25,7 +25,9 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.apache.activemq.jndi.ActiveMQInitialContextFactory; import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactoryImpl; +import org.apache.tuscany.sca.extensibility.ClassLoaderContext; public class ActiveMQJMSResourceFactory extends JMSResourceFactoryImpl { @@ -36,11 +38,7 @@ public class ActiveMQJMSResourceFactory extends JMSResourceFactoryImpl { super(connectionFactoryName, responseConnectionFactoryName, initialContextFactoryName, jndiURL); } - @Override protected synchronized Context getInitialContext() throws NamingException { - - System.out.println("************************** ActiveMQJMSResourceFactory.getInitialContext"); - if (context == null) { Properties props = new Properties(); @@ -52,9 +50,64 @@ public class ActiveMQJMSResourceFactory extends JMSResourceFactoryImpl { } initJREEnvironment(props); + + try { + // Load the JNDI InitialContext (will load the InitialContextFactory, if present) + context = new InitialContext(props); + if( context == null ) { + throw new NamingException(); + } else if ( context.getEnvironment().get(InitialContext.INITIAL_CONTEXT_FACTORY) == null ) { + throw new NamingException(); + } // end if + } catch (NamingException e ) { + context = getInitialContextOsgi( props ); + } // end try + // In the case where the InitialContext fails, check whether performing an OSGi based load succeeds... - context = new InitialContext(props); + } return context; - } + } // end method getInitialContext + + static final String ACTIVEMQ_FACTORY = "org.apache.activemq.jndi.ActiveMQInitialContextFactory"; + private Context getInitialContextOsgi( Properties props ) throws NamingException { + /** + * For OSGi, need to provide access to the InitialContextFactory for the JMS provider that is going to be used. + * + * The situation is that the InitialContext constructor instantiates an instance of the InitialContextFactory by + * calling "new" using the TCCL - thus there is a need to prepare the TCCL. + * 03/12/2010 MJE - for the present, only worry about ActiveMQ - other providers can be added later + * 10/12/2010 MJE - the following code attempts to get the classloader for the ActiveMQ initial context factory + * it will fail if the ActiveMQ classes are not available in the runtime, but the code will still + * execute (although under OSGi the new InitialContext() operation will fail to find a suitable + * InitialContextFactory object...) + */ + + String contextFactoryName = (String)props.get(Context.INITIAL_CONTEXT_FACTORY); + + ClassLoader ActiveMQCl = null; + try { + if( contextFactoryName == null || ACTIVEMQ_FACTORY.equals(contextFactoryName) ) { + ActiveMQCl = ActiveMQInitialContextFactory.class.getClassLoader(); + props.setProperty(Context.INITIAL_CONTEXT_FACTORY, ACTIVEMQ_FACTORY); + if( props.getProperty(Context.PROVIDER_URL) == null ) { + props.setProperty(Context.PROVIDER_URL, "vm://localhost?broker.persistent=false" ); + } // end if + } // end if + } catch (Exception e) { + // Nothing to do in this case - the ActiveMQCl classloader will simply be null + } // end try + + ClassLoader tccl = ClassLoaderContext.setContextClassLoader(JMSResourceFactoryImpl.class.getClassLoader(), + ActiveMQCl, + Thread.currentThread().getContextClassLoader() ); + try { + // Load the JNDI InitialContext (will load the InitialContextFactory, if present) + return new InitialContext(props); + } finally { + // Restore the TCCL if we changed it + if( tccl != null ) Thread.currentThread().setContextClassLoader(tccl); + } // end try + + } // end method getInitialContextOsgi } |