diff options
-rw-r--r-- | sca-java-2.x/trunk/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java | 86 |
1 files changed, 56 insertions, 30 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java b/sca-java-2.x/trunk/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java index 4ab2f5ab2c..7e56fb2329 100644 --- a/sca-java-2.x/trunk/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java +++ b/sca-java-2.x/trunk/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java @@ -28,6 +28,7 @@ import javax.jms.Session; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import javax.naming.NoInitialContextException; import javax.resource.spi.ActivationSpec; import org.apache.tuscany.sca.binding.jms.JMSBindingException; @@ -142,7 +143,6 @@ public class JMSResourceFactoryImpl implements JMSResourceFactory { connection = connectionFactory.createConnection(); } - static final String ACTIVEMQ_FACTORY = "org.apache.activemq.jndi.ActiveMQInitialContextFactory"; protected synchronized Context getInitialContext() throws NamingException { if (context == null) { Properties props = new Properties(); @@ -156,40 +156,66 @@ public class JMSResourceFactoryImpl implements JMSResourceFactory { initJREEnvironment(props); - /** - * 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...) - */ - ClassLoader ActiveMQCl = null; try { - if( initialContextFactoryName == null || ACTIVEMQ_FACTORY.equals(initialContextFactoryName) ) { - ActiveMQCl = ActiveMQInitialContextFactory.class.getClassLoader(); - props.setProperty(Context.INITIAL_CONTEXT_FACTORY, ACTIVEMQ_FACTORY); - } // 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) context = new InitialContext(props); - } finally { - // Restore the TCCL if we changed it - if( tccl != null ) Thread.currentThread().setContextClassLoader(tccl); - } // end try + 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... + + } 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 + /** * If using the WAS JMS Client with a non-IBM JRE then an additional |