diff options
Diffstat (limited to '')
10 files changed, 978 insertions, 0 deletions
diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/AsyncResponseJMSServiceListener.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/AsyncResponseJMSServiceListener.java new file mode 100644 index 0000000000..1aa316f81b --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/AsyncResponseJMSServiceListener.java @@ -0,0 +1,272 @@ +/* + * 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.host; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.Topic; +import javax.naming.NamingException; +import javax.resource.spi.ActivationSpec; + +import org.apache.tuscany.sca.binding.jms.JMSBinding; +import org.apache.tuscany.sca.binding.jms.JMSBindingConstants; +import org.apache.tuscany.sca.binding.jms.JMSBindingException; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.work.WorkScheduler; + +/** + * Implementation of a JMS response queue handler for async responses + * + * @version $Rev$ $Date$ + */ +public class AsyncResponseJMSServiceListener implements JMSServiceListener { + private static final Logger logger = Logger.getLogger(AsyncResponseJMSServiceListener.class.getName()); + + private MessageListener listener; + private String serviceName; + private JMSBinding jmsBinding; + private WorkScheduler workScheduler; + + private JMSResourceFactory jmsResourceFactory; + private MessageConsumer consumer; + private boolean running; + + private Destination destination; + + public AsyncResponseJMSServiceListener(MessageListener listener, String serviceName, + JMSBinding jmsBinding, WorkScheduler workScheduler, JMSResourceFactory rf) { + this.listener = listener; + this.serviceName = serviceName; + this.jmsBinding = jmsBinding; + this.workScheduler = workScheduler; + this.jmsResourceFactory = rf; + } + + public void start() { + this.running = true; + + try { + registerListener(); + } catch (Exception e) { + if (e instanceof JMSBindingException) throw (JMSBindingException)e; + throw new JMSBindingException("Error starting JMSAsyncResponse endpoint", e); + } + } // end start + + public void stop() { + this.running = false; + try { + consumer.close(); + jmsResourceFactory.closeConnection(); + jmsResourceFactory.closeResponseConnection(); + } catch (Exception 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() == null) || !e.getMessage().contains("disposed")) { + throw new JMSBindingException("Error stopping JMSServiceBinding", e); + } + } + } // end stop + + private void registerListener() throws NamingException, JMSException { + + Session session = jmsResourceFactory.createSession(); + lookupActivationSpec(); + destination = lookupQueue( jmsBinding.getResponseDestinationName() ); + if (destination == null) { + throw new JMSBindingException("Unable to create Async Response queue"); + } // end if + + if (jmsBinding.getJMSSelector() != null) { + consumer = session.createConsumer(destination, jmsBinding.getJMSSelector()); + } else { + consumer = session.createConsumer(destination); + } // end if + + try { + + consumer.setMessageListener(listener); + jmsResourceFactory.startConnection(); + + } catch (javax.jms.JMSException e) { + + // setMessageListener not allowed in JEE container so use Tuscany threads + + jmsResourceFactory.startConnection(); + workScheduler.scheduleWork(new Runnable() { + public void run() { + try { + while (running) { + final Message msg = consumer.receive(); + workScheduler.scheduleWork(new Runnable() { + public void run() { + try { + listener.onMessage(msg); + } catch (Exception e) { + e.printStackTrace(); + } // end try + } // end method run + }); + } // end while + } catch (Exception e) { + e.printStackTrace(); + } // end try + } // end method run + }); + } // end try + logger.log(Level.INFO, "JMS AsyncResponse handler '" + + serviceName + + "' listening on destination " + + ((destination instanceof Queue) ? ((Queue)destination).getQueueName() : ((Topic)destination).getTopicName())); + } // end method registerListener + + // Stub code for ActivationSpec support that throws appropriate errors + private void lookupActivationSpec() { + if ( jmsBinding.getActivationSpecName() != null ) { + String createMode = jmsBinding.getActivationSpecCreate(); + if ( JMSBindingConstants.CREATE_ALWAYS.equals(createMode) ) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec != null ) { + throw new JMSBindingException("ActivationSpec specifies create mode of \"always\" but resource already exists."); + } + throw new JMSBindingException("Can not create ActivationSpec"); + } else if ( JMSBindingConstants.CREATE_IF_NOT_EXIST.equals(createMode)) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec == null ) { + throw new JMSBindingException("Can not create ActivationSpec"); + } + } else if ( JMSBindingConstants.CREATE_NEVER.equals(createMode)) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec == null ) + throw new JMSBindingException("ActivationSpec specifies create mode of \"never\" but resource does not exist at jndiName " + jmsBinding.getActivationSpecName()); + + } // end if + } // end if + } // end method lookupActivationSpec + + /** + * Looks up the Async Response Queue for the JMS Binding. + * <p> + * What happens in the look up will depend on the create mode specified for the JMS Binding: + * <ul> + * <li>always - the JMS queue is always created. It is an error if the queue already exists + * <li>ifnotexist - the JMS queue is created if it does not exist. It is not an error if the queue already exists + * <li>never - the JMS queue is never created. It is an error if the queue does not exist + * </ul> + * See the SCA JMS Binding specification for more information. + * <p> + * @param queueName - the name of the Async Response queue + * @return The Async Response queue. + * @throws NamingException Failed to lookup JMS queue + * @throws JMSBindingException Failed to lookup JMS Queue. Probable cause is that the JMS queue's current existence/non-existence is not + * compatible with the create mode specified on the binding + */ + private Destination lookupQueue(String queueName ) throws NamingException, JMSBindingException { + + Destination destination = jmsResourceFactory.lookupDestination(queueName); + + String qCreateMode = jmsBinding.getDestinationCreate(); + if (qCreateMode.equals(JMSBindingConstants.CREATE_ALWAYS)) { + // In this mode, the queue must not already exist as we are creating it + if (destination != null) { + throw new JMSBindingException("JMS Destination " + queueName + + " already exists but has create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } // end if + + // Create the queue + destination = jmsResourceFactory.createDestination(queueName); + + } else if (qCreateMode.equals(JMSBindingConstants.CREATE_IF_NOT_EXIST)) { + // In this mode, the queue may nor may not exist. It will be created if it does not exist + // but don't create when using jms:jndi uri format + if (destination == null && !"jndi".equals(jmsBinding.getDestinationType())) { + destination = jmsResourceFactory.createDestination(queueName); + } // end if + + } else if (qCreateMode.equals(JMSBindingConstants.CREATE_NEVER)) { + // In this mode, the queue must have already been created. + if (destination == null) { + throw new JMSBindingException("JMS Destination " + queueName + + " not found but create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } // end if + } // end if + + // Make sure we ended up with a queue + if (destination == null) { + throw new JMSBindingException("JMS Destination " + queueName + + " not found with create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } // end if + + // Make sure its the expected type (queue or topic) + String type = (destination instanceof Queue) ? JMSBindingConstants.DESTINATION_TYPE_QUEUE : JMSBindingConstants.DESTINATION_TYPE_TOPIC; + if ("jndi".equals(jmsBinding.getDestinationType())) { + jmsBinding.setDestinationType(type); + } else { + if (!type.equals(jmsBinding.getDestinationType())) { + throw new JMSBindingException("JMS Destination " + queueName + + " expecting type of " + + jmsBinding.getDestinationType() + + " but found " + + type + + " while registering service " + + serviceName + + " listener"); + } // end if + } // end if + + return destination; + } // end method lookupDestinationQueue(String) + + public String getDestinationName() { + try { + if (destination instanceof Queue) { + return ((Queue)destination).getQueueName(); + } else if (destination instanceof Topic) { + return ((Topic)destination).getTopicName(); + } else { + return null; + } + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } // end method getDestinationName + +} // end class AsyncResponseJMSServiceListener diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSHostExtensionPoint.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSHostExtensionPoint.java new file mode 100644 index 0000000000..cac346adf4 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSHostExtensionPoint.java @@ -0,0 +1,40 @@ +/* + * 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.host; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.work.WorkScheduler; + +public class DefaultJMSHostExtensionPoint implements JMSHostExtensionPoint { + + private JMSServiceListenerFactory jmsServiceListenerFactory; + + public DefaultJMSHostExtensionPoint(ExtensionPointRegistry extensionPoints) { + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + WorkScheduler workScheduler = utilities.getUtility(WorkScheduler.class); + this.jmsServiceListenerFactory = new DefaultJMSServiceListenerFactory(workScheduler); + } + + public JMSServiceListenerFactory getJMSServiceListenerFactory() { + return jmsServiceListenerFactory; + } + +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListener.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListener.java new file mode 100644 index 0000000000..94728699a2 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListener.java @@ -0,0 +1,283 @@ +/* + * 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.host; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.Topic; +import javax.naming.NamingException; +import javax.resource.spi.ActivationSpec; + +import org.apache.tuscany.sca.binding.jms.JMSBinding; +import org.apache.tuscany.sca.binding.jms.JMSBindingConstants; +import org.apache.tuscany.sca.binding.jms.JMSBindingException; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.work.WorkScheduler; + +/** + * Implementation of the JMS service binding provider. + * + * @version $Rev$ $Date$ + */ +public class DefaultJMSServiceListener implements JMSServiceListener { + private static final Logger logger = Logger.getLogger(DefaultJMSServiceListener.class.getName()); + + private MessageListener listener; + private String serviceName; + private boolean isCallbackService; + private JMSBinding jmsBinding; + private WorkScheduler workScheduler; + + private JMSResourceFactory jmsResourceFactory; + private MessageConsumer consumer; + private boolean running; + + private Destination destination; + + public DefaultJMSServiceListener(MessageListener listener, String serviceName, boolean isCallbackService, JMSBinding jmsBinding, WorkScheduler workScheduler, JMSResourceFactory rf) { + this.listener = listener; + this.serviceName = serviceName; + this.isCallbackService = isCallbackService; + this.jmsBinding = jmsBinding; + this.workScheduler = workScheduler; + this.jmsResourceFactory = rf; + } + + public void start() { + this.running = true; + + try { + registerListener(); + } catch (Exception e) { + if (e instanceof JMSBindingException) throw (JMSBindingException)e; + throw new JMSBindingException("Error starting JMSServiceBinding", e); + } + } + + public void stop() { + this.running = false; + try { + consumer.close(); + jmsResourceFactory.closeConnection(); + jmsResourceFactory.closeResponseConnection(); + } catch (Exception 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() == null) || !e.getMessage().contains("disposed")) { + throw new JMSBindingException("Error stopping JMSServiceBinding", e); + } + } + } + + private void registerListener() throws NamingException, JMSException { + + Session session = jmsResourceFactory.createSession(); + lookupActivationSpec(); + destination = lookupDestinationQueue(); + if (destination == null) { + destination = session.createTemporaryQueue(); + } + + if (jmsBinding.getJMSSelector() != null) { + consumer = session.createConsumer(destination, jmsBinding.getJMSSelector()); + } else { + consumer = session.createConsumer(destination); + } + + try { + + consumer.setMessageListener(listener); + jmsResourceFactory.startConnection(); + + } catch (javax.jms.JMSException e) { + + // setMessageListener not allowed in JEE container so use Tuscany threads + + jmsResourceFactory.startConnection(); + workScheduler.scheduleWork(new Runnable() { + public void run() { + try { + while (running) { + final Message msg = consumer.receive(); + workScheduler.scheduleWork(new Runnable() { + public void run() { + try { + listener.onMessage(msg); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + logger.log(Level.INFO, "JMS " + (isCallbackService ? "callback service" : "service") + + " '" + + serviceName + + "' listening on destination " + + ((destination instanceof Queue) ? ((Queue)destination).getQueueName() : ((Topic)destination).getTopicName())); + } + + // Stub code for ActivationSpec support that throws appropriate errors + private void lookupActivationSpec() { + if ( jmsBinding.getActivationSpecName() != null ) { + String createMode = jmsBinding.getActivationSpecCreate(); + if ( JMSBindingConstants.CREATE_ALWAYS.equals(createMode) ) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec != null ) { + throw new JMSBindingException("ActivationSpec specifies create mode of \"always\" but resource already exists."); + } + throw new JMSBindingException("Can not create ActivationSpec"); + } else if ( JMSBindingConstants.CREATE_IF_NOT_EXIST.equals(createMode)) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec == null ) { + throw new JMSBindingException("Can not create ActivationSpec"); + } + } else if ( JMSBindingConstants.CREATE_NEVER.equals(createMode)) { + ActivationSpec spec = jmsResourceFactory.lookupActivationSpec(jmsBinding.getActivationSpecName()); + if ( spec == null ) + throw new JMSBindingException("ActivationSpec specifies create mode of \"never\" but resource does not exist at jndiName " + jmsBinding.getActivationSpecName()); + + } + + + } + } + + /** + * Looks up the Destination Queue for the JMS Binding. + * <p> + * What happens in the look up will depend on the create mode specified for the JMS Binding: + * <ul> + * <li>always - the JMS queue is always created. It is an error if the queue already exists + * <li>ifnotexist - the JMS queue is created if it does not exist. It is not an error if the queue already exists + * <li>never - the JMS queue is never created. It is an error if the queue does not exist + * </ul> + * See the SCA JMS Binding specification for more information. + * <p> + * + * @return The Destination queue. + * @throws NamingException Failed to lookup JMS queue + * @throws JMSBindingException Failed to lookup JMS Queue. Probable cause is that the JMS queue's current existence/non-existence is not + * compatible with the create mode specified on the binding + */ + private Destination lookupDestinationQueue() throws NamingException, JMSBindingException { + + if (isCallbackService && (jmsBinding.getDestinationName() == null)) { + // if its a callback service returning null indicates to use a temporary queue + return null; + } + + Destination destination = jmsResourceFactory.lookupDestination(jmsBinding.getDestinationName()); + + String qCreateMode = jmsBinding.getDestinationCreate(); + if (qCreateMode.equals(JMSBindingConstants.CREATE_ALWAYS)) { + // In this mode, the queue must not already exist as we are creating it + if (destination != null) { + throw new JMSBindingException("JMS Destination " + jmsBinding.getDestinationName() + + " already exists but has create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } + + // Create the queue + destination = jmsResourceFactory.createDestination(jmsBinding.getDestinationName()); + + } else if (qCreateMode.equals(JMSBindingConstants.CREATE_IF_NOT_EXIST)) { + // In this mode, the queue may nor may not exist. It will be created if it does not exist + // but don't create when using jms:jndi uri format + if (destination == null && !"jndi".equals(jmsBinding.getDestinationType())) { + destination = jmsResourceFactory.createDestination(jmsBinding.getDestinationName()); + } + + } else if (qCreateMode.equals(JMSBindingConstants.CREATE_NEVER)) { + // In this mode, the queue must have already been created. + if (destination == null) { + throw new JMSBindingException("JMS Destination " + jmsBinding.getDestinationName() + + " not found but create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } + } + + // Make sure we ended up with a queue + if (destination == null) { + throw new JMSBindingException("JMS Destination " + jmsBinding.getDestinationName() + + " not found with create mode of \"" + + qCreateMode + + "\" while registering service " + + serviceName + + " listener"); + } + + // Make sure its the expected type (queue or topic) + String type = (destination instanceof Queue) ? JMSBindingConstants.DESTINATION_TYPE_QUEUE : JMSBindingConstants.DESTINATION_TYPE_TOPIC; + if ("jndi".equals(jmsBinding.getDestinationType())) { + jmsBinding.setDestinationType(type); + } else { + if (!type.equals(jmsBinding.getDestinationType())) { + throw new JMSBindingException("JMS Destination " + jmsBinding.getDestinationName() + + " expecting type of " + + jmsBinding.getDestinationType() + + " but found " + + type + + " while registering service " + + serviceName + + " listener"); + } + } + + return destination; + } + + + + public String getDestinationName() { + try { + if (destination instanceof Queue) { + return ((Queue)destination).getQueueName(); + } else if (destination instanceof Topic) { + return ((Topic)destination).getTopicName(); + } else { + return null; + } + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } + +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListenerFactory.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListenerFactory.java new file mode 100644 index 0000000000..f829e15f04 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultJMSServiceListenerFactory.java @@ -0,0 +1,53 @@ +/* + * 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.host; + +import javax.jms.MessageListener; +import javax.naming.NamingException; + +import org.apache.tuscany.sca.binding.jms.JMSBindingException; +import org.apache.tuscany.sca.binding.jms.provider.JMSBindingServiceBindingProvider; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.work.WorkScheduler; + +public class DefaultJMSServiceListenerFactory implements JMSServiceListenerFactory { + + private WorkScheduler workScheduler; + + public DefaultJMSServiceListenerFactory(WorkScheduler workScheduler) { + this.workScheduler = workScheduler; + } + + public JMSServiceListener createJMSServiceListener(JMSServiceListenerDetails jmsSLD) { + try { + + JMSResourceFactory rf = ((JMSBindingServiceBindingProvider)jmsSLD).getResourceFactory(); + + RuntimeComponentService service = (RuntimeComponentService) jmsSLD.getEndpoint().getService(); + MessageListener listener = new DefaultServiceInvoker(jmsSLD.getEndpoint(), jmsSLD.getTargetBinding(), jmsSLD.getMessageFactory(), rf); + + return new DefaultJMSServiceListener(listener, service.getName(), service.isForCallback(), jmsSLD.getJmsBinding(), workScheduler, rf); + + } catch (NamingException e) { + throw new JMSBindingException(e); + } + } +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultServiceInvoker.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultServiceInvoker.java new file mode 100644 index 0000000000..566284fda6 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/DefaultServiceInvoker.java @@ -0,0 +1,112 @@ +/* + * 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.host; + +import java.lang.reflect.InvocationTargetException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.naming.NamingException; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.binding.jms.JMSBinding; +import org.apache.tuscany.sca.binding.jms.context.JMSBindingContext; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * TODO RRB experiement + * Listener for the JMSBinding. + * + * @version $Rev$ $Date$ + */ +public class DefaultServiceInvoker implements MessageListener { + + private static final Logger logger = Logger.getLogger(DefaultServiceInvoker.class.getName()); + + private RuntimeEndpoint endpoint; + private JMSBinding jmsBinding; + private Binding targetBinding; + private JMSResourceFactory jmsResourceFactory; + private RuntimeComponentService service; + private MessageFactory messageFactory; + + public DefaultServiceInvoker(RuntimeEndpoint endpoint, Binding targetBinding, MessageFactory messageFactory, JMSResourceFactory rf) throws NamingException { + this.endpoint = endpoint; + this.jmsBinding = (JMSBinding) endpoint.getBinding(); + this.jmsResourceFactory = rf; + this.service = (RuntimeComponentService) endpoint.getService(); + this.targetBinding = targetBinding; + this.messageFactory = messageFactory; + + } + + public void onMessage(Message requestJMSMsg) { + logger.log(Level.FINE, "JMS service '" + service.getName() + "' received message " + requestJMSMsg); + try { + invokeService(requestJMSMsg); + } catch (Throwable e) { + logger.log(Level.SEVERE, "Exception send fault response '" + service.getName(), e); + } + } + + protected void invokeService(Message requestJMSMsg) throws JMSException, InvocationTargetException { + + // create the tuscany message + org.apache.tuscany.sca.invocation.Message tuscanyMsg = messageFactory.createMessage(); + + // populate the message context with JMS binding information + JMSBindingContext context = new JMSBindingContext(); + tuscanyMsg.setBindingContext(context); + + context.setJmsMsg(requestJMSMsg); + context.setJmsResourceFactory(jmsResourceFactory); + context.setReplyToDestination(requestJMSMsg.getJMSReplyTo()); + + // set the message body + tuscanyMsg.setBody(requestJMSMsg); + + // call the runtime wire - the response is handled by the + // transport interceptor + //getEndpoint(targetBinding).invoke(tuscanyMsg); + RuntimeEndpoint endpoint = getEndpoint(targetBinding); + if( endpoint.isAsyncInvocation() ) { + endpoint.invokeAsync(tuscanyMsg); + } else { + endpoint.invoke(tuscanyMsg); + } // end if + + } // end method invokeService + + private RuntimeEndpoint getEndpoint(Binding targetBinding) { + for(Endpoint ep: service.getEndpoints()) { + if(ep.getBinding() == targetBinding) { + return (RuntimeEndpoint) ep; + } + } + return endpoint; + } + +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSAsyncResponseInvoker.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSAsyncResponseInvoker.java new file mode 100644 index 0000000000..386cb54700 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSAsyncResponseInvoker.java @@ -0,0 +1,98 @@ +/* + * 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.host; + +import java.lang.reflect.InvocationTargetException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.naming.NamingException; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.binding.jms.JMSBinding; +import org.apache.tuscany.sca.binding.jms.context.JMSBindingContext; +import org.apache.tuscany.sca.binding.jms.provider.JMSResourceFactory; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +/** + * TODO RRB experiement + * Listener for the JMSBinding. + * + * @version $Rev$ $Date$ + */ +public class JMSAsyncResponseInvoker implements MessageListener { + + private static final Logger logger = Logger.getLogger(JMSAsyncResponseInvoker.class.getName()); + + private RuntimeEndpointReference endpointReference; + private JMSBinding jmsBinding; + private JMSResourceFactory jmsResourceFactory; + private RuntimeComponentReference reference; + private MessageFactory messageFactory; + + public JMSAsyncResponseInvoker(RuntimeEndpointReference endpointReference, + MessageFactory messageFactory, + JMSResourceFactory rf) throws NamingException { + this.endpointReference = endpointReference; + this.jmsBinding = (JMSBinding) endpointReference.getBinding(); + this.jmsResourceFactory = rf; + this.reference = (RuntimeComponentReference) endpointReference.getReference(); + this.messageFactory = messageFactory; + } + + public void onMessage(Message requestJMSMsg) { + logger.log(Level.FINE, "JMS reference '" + reference.getName() + "' received message " + requestJMSMsg); + try { + invokeReference(requestJMSMsg); + } catch (Throwable e) { + logger.log(Level.SEVERE, "Exception send fault response '" + reference.getName(), e); + } + } // end method onMessage + + protected void invokeReference(Message requestJMSMsg) throws JMSException, InvocationTargetException { + + // create the Tuscany message + org.apache.tuscany.sca.invocation.Message tuscanyMsg = messageFactory.createMessage(); + + // populate the message context with JMS binding information + JMSBindingContext context = new JMSBindingContext(); + tuscanyMsg.setBindingContext(context); + + context.setJmsMsg(requestJMSMsg); + context.setJmsResourceFactory(jmsResourceFactory); + context.setReplyToDestination(requestJMSMsg.getJMSReplyTo()); + + + + // set the message body + tuscanyMsg.setBody(requestJMSMsg); + + // call the runtime wire - the response is handled by the + // transport interceptor + endpointReference.invokeAsyncResponse(tuscanyMsg); + + } // end method invokeReference + +} // end class AsyncResponseInvoker diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSHostExtensionPoint.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSHostExtensionPoint.java new file mode 100644 index 0000000000..a15a84b60e --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSHostExtensionPoint.java @@ -0,0 +1,27 @@ +/* + * 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.host; + + +public interface JMSHostExtensionPoint { + + JMSServiceListenerFactory getJMSServiceListenerFactory(); + +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListener.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListener.java new file mode 100644 index 0000000000..444d796b12 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListener.java @@ -0,0 +1,29 @@ +/* + * 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.host; + +public interface JMSServiceListener { + + void start(); + + void stop(); + + String getDestinationName(); +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerDetails.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerDetails.java new file mode 100644 index 0000000000..451f3680d6 --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerDetails.java @@ -0,0 +1,37 @@ +/* + * 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.host; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.jms.JMSBinding; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +public interface JMSServiceListenerDetails { + + JMSBinding getJmsBinding(); + + Binding getTargetBinding(); + + RuntimeEndpoint getEndpoint(); + + MessageFactory getMessageFactory(); + +} diff --git a/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerFactory.java b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerFactory.java new file mode 100644 index 0000000000..9dc23f1d8d --- /dev/null +++ b/sca-java-2.x/tags/2.0/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/host/JMSServiceListenerFactory.java @@ -0,0 +1,27 @@ +/* + * 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.host; + + +public interface JMSServiceListenerFactory { + + JMSServiceListener createJMSServiceListener(JMSServiceListenerDetails jmsServiceBindingProvider); + +} |