From 54a87028541800f6a72fc470a02d1bd4aa53d494 Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 15 Oct 2008 09:27:02 +0000 Subject: Rename JMSResourceFactory to JMSResourceFactoryImpl in preperation for making it plugable git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@704820 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding/jms/provider/JMSBindingInvoker.java | 4 +- .../binding/jms/provider/JMSBindingListener.java | 4 +- .../JMSBindingReferenceBindingProvider.java | 4 +- .../provider/JMSBindingServiceBindingProvider.java | 4 +- .../binding/jms/provider/JMSResourceFactory.java | 220 --------------------- .../jms/provider/JMSResourceFactoryImpl.java | 220 +++++++++++++++++++++ .../jms/OperationSelectionTestCaseFIXME.java | 4 +- 7 files changed, 230 insertions(+), 230 deletions(-) delete mode 100644 java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactory.java create mode 100644 java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java (limited to 'java/sca') diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingInvoker.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingInvoker.java index 9c5fba5de6..1d2067055a 100644 --- a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingInvoker.java +++ b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingInvoker.java @@ -61,7 +61,7 @@ public class JMSBindingInvoker implements Invoker, DataExchangeSemantics { protected String operationName; protected JMSBinding jmsBinding; - protected JMSResourceFactory jmsResourceFactory; + protected JMSResourceFactoryImpl jmsResourceFactory; protected JMSMessageProcessor requestMessageProcessor; protected JMSMessageProcessor responseMessageProcessor; protected Destination bindingRequestDest; @@ -70,7 +70,7 @@ public class JMSBindingInvoker implements Invoker, DataExchangeSemantics { protected JMSTokenAuthenticationPolicy jmsTokenAuthenticationPolicy = null; protected JMSHeaderPolicy jmsHeaderPolicy = null; - public JMSBindingInvoker(JMSBinding jmsBinding, Operation operation, JMSResourceFactory jmsResourceFactory, RuntimeComponentReference reference) { + public JMSBindingInvoker(JMSBinding jmsBinding, Operation operation, JMSResourceFactoryImpl jmsResourceFactory, RuntimeComponentReference reference) { this.operation = operation; operationName = operation.getName(); diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingListener.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingListener.java index 87b1bb21e2..1cf9d57bc9 100644 --- a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingListener.java +++ b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingListener.java @@ -62,7 +62,7 @@ public class JMSBindingListener implements MessageListener { private static final String ON_MESSAGE_METHOD_NAME = "onMessage"; private JMSBinding jmsBinding; private Binding targetBinding; - private JMSResourceFactory jmsResourceFactory; + private JMSResourceFactoryImpl jmsResourceFactory; private RuntimeComponentService service; private JMSMessageProcessor requestMessageProcessor; private JMSMessageProcessor responseMessageProcessor; @@ -70,7 +70,7 @@ public class JMSBindingListener implements MessageListener { private List serviceOperations; protected JMSTokenAuthenticationPolicy jmsTokenAuthenticationPolicy = null; - public JMSBindingListener(JMSBinding jmsBinding, JMSResourceFactory jmsResourceFactory, RuntimeComponentService service, Binding targetBinding) throws NamingException { + public JMSBindingListener(JMSBinding jmsBinding, JMSResourceFactoryImpl jmsResourceFactory, RuntimeComponentService service, Binding targetBinding) throws NamingException { this.jmsBinding = jmsBinding; this.jmsResourceFactory = jmsResourceFactory; this.service = service; diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingReferenceBindingProvider.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingReferenceBindingProvider.java index 8de4e0a7d5..9e40456d0b 100644 --- a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingReferenceBindingProvider.java +++ b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingReferenceBindingProvider.java @@ -49,7 +49,7 @@ public class JMSBindingReferenceBindingProvider implements ReferenceBindingProvi private RuntimeComponentReference reference; private JMSBinding jmsBinding; private List jmsBindingInvokers = new ArrayList(); - private JMSResourceFactory jmsResourceFactory; + private JMSResourceFactoryImpl jmsResourceFactory; private RuntimeComponent component; private InterfaceContract wsdlInterfaceContract; private ExtensionPointRegistry extensions; @@ -59,7 +59,7 @@ public class JMSBindingReferenceBindingProvider implements ReferenceBindingProvi this.jmsBinding = binding; this.extensions = extensions; this.component = component; - jmsResourceFactory = new JMSResourceFactory(binding.getConnectionFactoryName(), binding.getInitialContextFactoryName(), binding.getJndiURL()); + jmsResourceFactory = new JMSResourceFactoryImpl(binding.getConnectionFactoryName(), binding.getInitialContextFactoryName(), binding.getJndiURL()); if (XMLTextMessageProcessor.class.isAssignableFrom(JMSMessageProcessorUtil.getRequestMessageProcessor(jmsBinding).getClass())) { setXMLDataBinding(reference); diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingServiceBindingProvider.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingServiceBindingProvider.java index 7db640b7d1..e97fef0f45 100644 --- a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingServiceBindingProvider.java +++ b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSBindingServiceBindingProvider.java @@ -57,7 +57,7 @@ public class JMSBindingServiceBindingProvider implements ServiceBindingProvider private RuntimeComponentService service; private Binding targetBinding; private JMSBinding jmsBinding; - private JMSResourceFactory jmsResourceFactory; + private JMSResourceFactoryImpl jmsResourceFactory; private MessageConsumer consumer; private WorkScheduler workScheduler; private boolean running; @@ -77,7 +77,7 @@ public class JMSBindingServiceBindingProvider implements ServiceBindingProvider this.targetBinding = targetBinding; this.extensionPoints = extensionPoints; - jmsResourceFactory = new JMSResourceFactory(binding.getConnectionFactoryName(), binding.getInitialContextFactoryName(), binding.getJndiURL()); + jmsResourceFactory = new JMSResourceFactoryImpl(binding.getConnectionFactoryName(), binding.getInitialContextFactoryName(), binding.getJndiURL()); if (jmsBinding.getDestinationName().equals(JMSBindingConstants.DEFAULT_DESTINATION_NAME)) { if (!service.isCallback()) { diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactory.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactory.java deleted file mode 100644 index a1308d98cf..0000000000 --- a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactory.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.binding.jms.provider; - -import java.util.Properties; - -import javax.jms.Connection; -import javax.jms.ConnectionFactory; -import javax.jms.Destination; -import javax.jms.JMSException; -import javax.jms.Session; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import org.apache.tuscany.sca.binding.jms.impl.JMSBindingConstants; -import org.apache.tuscany.sca.binding.jms.impl.JMSBindingException; - -/** - * Abstracts away any JMS provide specific feature from the JMS binding - * - * @version $Rev$ $Date$ - */ -public class JMSResourceFactory { - - private String initialContextFactoryName; - private String connectionFactoryName = "ConnectionFactory"; - private String jndiURL; - - private Connection connection; - private Context context; - private boolean isConnectionStarted; - - public JMSResourceFactory(String connectionFactoryName, String initialContextFactoryName, String jndiURL) { - if (connectionFactoryName != null && connectionFactoryName.trim().length() > 0) { - this.connectionFactoryName = connectionFactoryName.trim(); - } - if (initialContextFactoryName != null && initialContextFactoryName.trim().length() > 0) { - this.initialContextFactoryName = initialContextFactoryName.trim(); - } - if (jndiURL != null) { - this.jndiURL = jndiURL.trim(); - } - } - - /* - * This is a simple implementation where a connection is created per binding Ideally the resource factory should be - * able to leverage the host environment to provide connection pooling if it can. E.g. if Tuscany is running inside - * an AppServer Then we could leverage the JMS resources it provides - * - * @see org.apache.tuscany.binding.jms.JMSResourceFactory#getConnection() - */ - public Connection getConnection() throws NamingException, JMSException { - if (connection == null) { - createConnection(); - } - return connection; - } - - /* - * (non-Javadoc) - * - * @see org.apache.tuscany.binding.jms.JMSResourceFactory#createSession() - */ - public Session createSession() throws JMSException, NamingException { - return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); - } - - /* - * (non-Javadoc) - * - * @see org.apache.tuscany.binding.jms.JMSResourceFactory#startConnection() - */ - public void startConnection() throws JMSException, NamingException { - if (!isConnectionStarted) { - getConnection().start(); - isConnectionStarted = true; - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.tuscany.binding.jms.JMSResourceFactory#closeConnection() - */ - public void closeConnection() throws JMSException { - if (connection != null) { - try { - connection.close(); - } catch (JMSException e) { - // if using an embedded broker then when shutting down Tuscany the broker may get closed - // before this stop method is called. I can't see how to detect that so for now just - // ignore the exception if the message is that the transport is already disposed - if (!e.getMessage().contains("disposed")) { - throw e; - } - } - } - } - - private void createConnection() throws NamingException, JMSException { - ConnectionFactory connectionFactory = (ConnectionFactory)jndiLookUp(connectionFactoryName); - if (connectionFactory == null) { - throw new JMSBindingException("connection factory not found: " + connectionFactoryName); - } - connection = connectionFactory.createConnection(); - } - - private synchronized Context getInitialContext() throws NamingException { - if (context == null) { - Properties props = new Properties(); - - if (initialContextFactoryName != null) { - props.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName); - } - if (jndiURL != null) { - props.setProperty(Context.PROVIDER_URL, jndiURL); - } - - initJREEnvironment(props); - - context = new InitialContext(props); - } - return context; - } - - /** - * If using the WAS JMS Client with a non-IBM JRE then an additional - * environment property needs to be set to initialize the ORB correctly. - * See: http://www-1.ibm.com/support/docview.wss?uid=swg24012804 - */ - private void initJREEnvironment(Properties props) { - if ("com.ibm.websphere.naming.WsnInitialContextFactory".equals(props.get(Context.INITIAL_CONTEXT_FACTORY))) { - String vendor = System.getProperty("java.vendor"); - if (vendor == null || !vendor.contains("IBM")) { - props.setProperty("com.ibm.CORBA.ORBInit", "com.ibm.ws.sib.client.ORB"); - } - } - } - - public Destination lookupDestination(String destName) throws NamingException { - if (JMSBindingConstants.DEFAULT_DESTINATION_NAME.equals(destName)) { - return null; - } - - Destination dest = (Destination)jndiLookUp(destName); - if (dest == null) { - dest = lookupPhysical(destName); - } - return dest; - } - - protected Destination lookupPhysical(String jndiName) { - - // TODO: the SCA JMS spec says a destination name may be a non-jndi plain destination name - -// Session session = null; -// try { -// -// Destination dest; -// session = createSession(); -// dest = session.createQueue(jndiName); -// return dest; -// -// } catch (JMSException e) { -// throw new JMSBindingException(e); -// } catch (NamingException e) { -// throw new JMSBindingException(e); -// } finally { -// if (session != null) { -// try { -// session.close(); -// } catch (JMSException e) { -// throw new JMSBindingException(e); -// } -// } -// } - return null; - } - - /** - * You can create a destination in ActiveMQ (and have it appear in JNDI) by putting "dynamicQueues/" in front of the queue name being looked up - */ - public Destination createDestination(String jndiName) throws NamingException { - return lookupDestination("dynamicQueues/" + jndiName); - } - - protected Object jndiLookUp(String name) { - Object o = null; - try { - o = getInitialContext().lookup("java:comp/env/" + name); - } catch (NamingException ex) { - // ignore - } - if (o == null) { - try { - o = getInitialContext().lookup(name); - } catch (NamingException ex) { - // ignore - } - } - return o; - } -} diff --git a/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java new file mode 100644 index 0000000000..eced286a20 --- /dev/null +++ b/java/sca/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/JMSResourceFactoryImpl.java @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.jms.provider; + +import java.util.Properties; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.tuscany.sca.binding.jms.impl.JMSBindingConstants; +import org.apache.tuscany.sca.binding.jms.impl.JMSBindingException; + +/** + * Abstracts away any JMS provide specific feature from the JMS binding + * + * @version $Rev$ $Date$ + */ +public class JMSResourceFactoryImpl { + + private String initialContextFactoryName; + private String connectionFactoryName = "ConnectionFactory"; + private String jndiURL; + + private Connection connection; + private Context context; + private boolean isConnectionStarted; + + public JMSResourceFactoryImpl(String connectionFactoryName, String initialContextFactoryName, String jndiURL) { + if (connectionFactoryName != null && connectionFactoryName.trim().length() > 0) { + this.connectionFactoryName = connectionFactoryName.trim(); + } + if (initialContextFactoryName != null && initialContextFactoryName.trim().length() > 0) { + this.initialContextFactoryName = initialContextFactoryName.trim(); + } + if (jndiURL != null) { + this.jndiURL = jndiURL.trim(); + } + } + + /* + * This is a simple implementation where a connection is created per binding Ideally the resource factory should be + * able to leverage the host environment to provide connection pooling if it can. E.g. if Tuscany is running inside + * an AppServer Then we could leverage the JMS resources it provides + * + * @see org.apache.tuscany.binding.jms.JMSResourceFactory#getConnection() + */ + public Connection getConnection() throws NamingException, JMSException { + if (connection == null) { + createConnection(); + } + return connection; + } + + /* + * (non-Javadoc) + * + * @see org.apache.tuscany.binding.jms.JMSResourceFactory#createSession() + */ + public Session createSession() throws JMSException, NamingException { + return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); + } + + /* + * (non-Javadoc) + * + * @see org.apache.tuscany.binding.jms.JMSResourceFactory#startConnection() + */ + public void startConnection() throws JMSException, NamingException { + if (!isConnectionStarted) { + getConnection().start(); + isConnectionStarted = true; + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.tuscany.binding.jms.JMSResourceFactory#closeConnection() + */ + public void closeConnection() throws JMSException { + if (connection != null) { + try { + connection.close(); + } catch (JMSException e) { + // if using an embedded broker then when shutting down Tuscany the broker may get closed + // before this stop method is called. I can't see how to detect that so for now just + // ignore the exception if the message is that the transport is already disposed + if (!e.getMessage().contains("disposed")) { + throw e; + } + } + } + } + + private void createConnection() throws NamingException, JMSException { + ConnectionFactory connectionFactory = (ConnectionFactory)jndiLookUp(connectionFactoryName); + if (connectionFactory == null) { + throw new JMSBindingException("connection factory not found: " + connectionFactoryName); + } + connection = connectionFactory.createConnection(); + } + + private synchronized Context getInitialContext() throws NamingException { + if (context == null) { + Properties props = new Properties(); + + if (initialContextFactoryName != null) { + props.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName); + } + if (jndiURL != null) { + props.setProperty(Context.PROVIDER_URL, jndiURL); + } + + initJREEnvironment(props); + + context = new InitialContext(props); + } + return context; + } + + /** + * If using the WAS JMS Client with a non-IBM JRE then an additional + * environment property needs to be set to initialize the ORB correctly. + * See: http://www-1.ibm.com/support/docview.wss?uid=swg24012804 + */ + private void initJREEnvironment(Properties props) { + if ("com.ibm.websphere.naming.WsnInitialContextFactory".equals(props.get(Context.INITIAL_CONTEXT_FACTORY))) { + String vendor = System.getProperty("java.vendor"); + if (vendor == null || !vendor.contains("IBM")) { + props.setProperty("com.ibm.CORBA.ORBInit", "com.ibm.ws.sib.client.ORB"); + } + } + } + + public Destination lookupDestination(String destName) throws NamingException { + if (JMSBindingConstants.DEFAULT_DESTINATION_NAME.equals(destName)) { + return null; + } + + Destination dest = (Destination)jndiLookUp(destName); + if (dest == null) { + dest = lookupPhysical(destName); + } + return dest; + } + + protected Destination lookupPhysical(String jndiName) { + + // TODO: the SCA JMS spec says a destination name may be a non-jndi plain destination name + +// Session session = null; +// try { +// +// Destination dest; +// session = createSession(); +// dest = session.createQueue(jndiName); +// return dest; +// +// } catch (JMSException e) { +// throw new JMSBindingException(e); +// } catch (NamingException e) { +// throw new JMSBindingException(e); +// } finally { +// if (session != null) { +// try { +// session.close(); +// } catch (JMSException e) { +// throw new JMSBindingException(e); +// } +// } +// } + return null; + } + + /** + * You can create a destination in ActiveMQ (and have it appear in JNDI) by putting "dynamicQueues/" in front of the queue name being looked up + */ + public Destination createDestination(String jndiName) throws NamingException { + return lookupDestination("dynamicQueues/" + jndiName); + } + + protected Object jndiLookUp(String name) { + Object o = null; + try { + o = getInitialContext().lookup("java:comp/env/" + name); + } catch (NamingException ex) { + // ignore + } + if (o == null) { + try { + o = getInitialContext().lookup(name); + } catch (NamingException ex) { + // ignore + } + } + return o; + } +} diff --git a/java/sca/modules/binding-jms-runtime/src/test/java/org/apache/tuscany/sca/binding/jms/OperationSelectionTestCaseFIXME.java b/java/sca/modules/binding-jms-runtime/src/test/java/org/apache/tuscany/sca/binding/jms/OperationSelectionTestCaseFIXME.java index 03cc0bf1c4..7d8f9fd2fc 100644 --- a/java/sca/modules/binding-jms-runtime/src/test/java/org/apache/tuscany/sca/binding/jms/OperationSelectionTestCaseFIXME.java +++ b/java/sca/modules/binding-jms-runtime/src/test/java/org/apache/tuscany/sca/binding/jms/OperationSelectionTestCaseFIXME.java @@ -25,7 +25,7 @@ import javax.jms.TextMessage; import org.apache.tuscany.sca.binding.jms.impl.JMSBinding; import org.apache.tuscany.sca.binding.jms.provider.JMSBindingListener; -import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactoryImpl; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; @@ -181,7 +181,7 @@ public class OperationSelectionTestCaseFIXME { throws Exception { // Create the test JMS Binding final JMSBinding jmsBinding = new JMSBinding(); - JMSResourceFactory jmsResourceFactory = null; + JMSResourceFactoryImpl jmsResourceFactory = null; // Extra information for the method we are invoking final String operationParams = "Hello"; -- cgit v1.2.3