diff options
Diffstat (limited to 'sandbox/rajith/binding.jms/src/main/java')
12 files changed, 1058 insertions, 0 deletions
diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/DefaultOperationSelector.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/DefaultOperationSelector.java new file mode 100644 index 0000000000..fe472e917c --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/DefaultOperationSelector.java @@ -0,0 +1,32 @@ +package org.apache.tuscany.binding.jms; + +import javax.jms.JMSException; +import javax.jms.Message; + +public class DefaultOperationSelector implements OperationSelector{ + + private JMSBinding jmsBinding; + + public DefaultOperationSelector(JMSBinding jmsBinding){ + this.jmsBinding = jmsBinding; + } + + public String getOperationName(Message message) { + try { + return message.getStringProperty(jmsBinding.getOperationSelectorPropertyName()); + } catch (JMSException e) { + e.printStackTrace(); + return null; + } + } + + public void setOperationName(String operationName,Message message) { + try { + message.setStringProperty(jmsBinding.getOperationSelectorPropertyName(),operationName); + } catch (JMSException e) { + e.printStackTrace(); + } + } + + +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBinding.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBinding.java new file mode 100644 index 0000000000..8bc8bc2c36 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBinding.java @@ -0,0 +1,167 @@ +package org.apache.tuscany.binding.jms; + +import javax.jms.DeliveryMode; + +import org.apache.tuscany.spi.model.Binding; + +/** + * Represents a binding to a JMS resource. + */ + +public class JMSBinding extends Binding { + + public final static int DESTINATION_TYPE_QUEUE = 0; + public final static int DESTINATION_TYPE_TOPIC = 1; + + private int destinationType = DESTINATION_TYPE_QUEUE; + + private String destinationName; + + // Topic or Query factory name + private String connectionFactoryName; + + private String activationSpecName; + + private String initialContextFactoryName; // "org.apache.activemq.jndi.ActiveMQInitialContextFactory" + + private String jNDIProviderURL; // "tcp://hostname:61616" + + // Maps to javax.jms.DeliveryMode + private int deliveryMode = DeliveryMode.NON_PERSISTENT; + + private int timeToLive = 1000 ; // in mili seconds + + private int priority; + + private String replyTo; + + private String jmsResourceFactoryName; + + private String operationSelectorName; + + private String operationSelectorPropertyName = "OpName"; + + public JMSBinding(int destinationType, String destinationName, String connectionFactoryName, String activationSpecName, String initialContextFactoryName, String providerURL, int deliveryMode, int timeToLive, int priority, String replyTo) { + super(); + this.destinationType = destinationType; + this.destinationName = destinationName; + this.connectionFactoryName = connectionFactoryName; + this.activationSpecName = activationSpecName; + this.initialContextFactoryName = initialContextFactoryName; + jNDIProviderURL = providerURL; + this.deliveryMode = deliveryMode; + this.timeToLive = timeToLive; + this.priority = priority; + this.replyTo = replyTo; + } + + public JMSBinding(){ + super(); + } + + public String getActivationSpecName() { + return activationSpecName; + } + + public void setActivationSpecName(String activationSpecName) { + this.activationSpecName = activationSpecName; + } + + public String getConnectionFactoryName() { + return connectionFactoryName; + } + + public void setConnectionFactoryName(String connectionFactoryName) { + this.connectionFactoryName = connectionFactoryName; + } + + public int getDeliveryMode() { + return deliveryMode; + } + + public void setDeliveryMode(int deliveryMode) { + this.deliveryMode = deliveryMode; + } + + public String getDestinationName() { + return destinationName; + } + + public void setDestinationName(String destinationName) { + this.destinationName = destinationName; + } + + public String getInitialContextFactoryName() { + return initialContextFactoryName; + } + + public void setInitialContextFactoryName(String initialContextFactoryName) { + this.initialContextFactoryName = initialContextFactoryName; + } + + public String getJNDIProviderURL() { + return jNDIProviderURL; + } + + public void setJNDIProviderURL(String providerURL) { + jNDIProviderURL = providerURL; + } + + public int getPriority() { + return priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } + + public String getReplyTo() { + return replyTo; + } + + public void setReplyTo(String replyTo) { + this.replyTo = replyTo; + } + + public int getTimeToLive() { + return timeToLive; + } + + public void setTimeToLive(int timeToLive) { + this.timeToLive = timeToLive; + } + + public int getDestinationType() { + return destinationType; + } + + public void setDestinationType(int destinationType) { + this.destinationType = destinationType; + } + + public String getJmsResourceFactoryName() { + return jmsResourceFactoryName; + } + + public void setJmsResourceFactoryName(String jmsResourceFactoryName) { + this.jmsResourceFactoryName = jmsResourceFactoryName; + } + + public String getOperationSelectorName() { + return operationSelectorName; + } + + public void setOperationSelectorName(String operationSelectorName) { + this.operationSelectorName = operationSelectorName; + } + + public String getOperationSelectorPropertyName() { + return operationSelectorPropertyName; + } + + public void setOperationSelectorPropertyName( + String operationSelectorPropertyName) { + this.operationSelectorPropertyName = operationSelectorPropertyName; + } + +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingBuilder.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingBuilder.java new file mode 100644 index 0000000000..f1359ed492 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingBuilder.java @@ -0,0 +1,137 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed 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.binding.jms; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Reference; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +import commonj.sdo.helper.TypeHelper; + +/** + * Builds a Service or Reference for an RMI binding. + * + * @version $Rev$ $Date$ + */ + +public class JMSBindingBuilder extends BindingBuilderExtension<JMSBinding> { + + private static String DEFAULT_JMS_RESOURCE_FACTORY = "org.apache.tuscany.binding.jms.SimpleJMSResourceFactory"; + private static String DEFAULT_OPERATION_SELECTOR = "org.apache.tuscany.binding.jms.DefaultOperationSelector"; + + protected Class<JMSBinding> getBindingType() { + return JMSBinding.class; + } + + @SuppressWarnings({"unchecked"}) + public Service build(CompositeComponent parent, + BoundServiceDefinition<JMSBinding> serviceDefinition, + DeploymentContext deploymentContext) { + + JMSBinding jmsBinding = serviceDefinition.getBinding(); + Class<?> interfaze = serviceDefinition.getServiceContract().getInterfaceClass(); + TypeHelper typeHelper = (TypeHelper) deploymentContext.getExtension(TypeHelper.class.getName()); + if(typeHelper==null) typeHelper = TypeHelper.INSTANCE; + + JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding); + OperationSelector opSec = getOperationSelector(jmsBinding); + + return new JMSService(serviceDefinition.getName(),parent, wireService, jmsBinding, jmsResourceFactory,opSec,interfaze,typeHelper); + } + + @SuppressWarnings({"unchecked"}) + public Reference build(CompositeComponent parent, + BoundReferenceDefinition<JMSBinding> referenceDefinition, + DeploymentContext deploymentContext) { + + String name = referenceDefinition.getName(); + Class<?> interfaze = referenceDefinition.getServiceContract().getInterfaceClass(); + + JMSBinding jmsBinding = referenceDefinition.getBinding(); + JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding); + OperationSelector opSec = getOperationSelector(jmsBinding); + + return new JMSReference(name,parent,wireService, jmsBinding, jmsResourceFactory,opSec,interfaze); + + } + + private JMSResourceFactory getJMSResourceFactory(JMSBinding jmsBinding) { + String className = jmsBinding.getJmsResourceFactoryName(); + if (className != null && !className.equals("")){ + try { + Class factoryClass = Class.forName(className != null ? className : DEFAULT_JMS_RESOURCE_FACTORY); + Constructor constructor = factoryClass.getDeclaredConstructor(new Class[]{JMSBinding.class}); + return (JMSResourceFactory) constructor.newInstance(jmsBinding); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return new SimpleJMSResourceFactory(jmsBinding); + }else{ + return new SimpleJMSResourceFactory(jmsBinding); + } + + } + + + private OperationSelector getOperationSelector(JMSBinding jmsBinding) { + String className = jmsBinding.getOperationSelectorName(); + + if (className != null && !className.equals("")){ + try { + Class factoryClass = Class.forName(className != null ? className : DEFAULT_OPERATION_SELECTOR); + Constructor constructor = factoryClass.getDeclaredConstructor(new Class[]{JMSBinding.class}); + return (OperationSelector) constructor.newInstance(jmsBinding); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return new DefaultOperationSelector(jmsBinding); + }else{ + return new DefaultOperationSelector(jmsBinding); + } + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingLoader.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingLoader.java new file mode 100644 index 0000000000..c419df81f0 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingLoader.java @@ -0,0 +1,119 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed 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.binding.jms; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.annotations.Scope; + +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMXMLParserWrapper; +import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory; +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; + +/** + * Loader for handling <binding.jms> elements. + * + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class JMSBindingLoader extends LoaderExtension<JMSBinding> { + public static final QName BINDING_JMS = new QName( + "http://tuscany.apache.org/xmlns/binding/jms/1.0-SNAPSHOT", "binding.jms"); + + public JMSBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return BINDING_JMS; + } + + public JMSBinding load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + + OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), reader); + OMElement omElement = builder.getDocumentElement(); + + OMElement connectionOM = omElement.getFirstChildWithName(new QName("connection.jms")); + + String activationSpecName = connectionOM.getAttributeValue(new QName("activationSpecName")); + String destinationName = connectionOM.getAttributeValue(new QName("destinationName")); + String connectionFactoryName = connectionOM.getAttributeValue(new QName("connectionFactoryName")) ; + String initialContextFactoryName = connectionOM.getAttributeValue(new QName("initialContextFactoryName")) ; + String jNDIProviderURL = connectionOM.getAttributeValue(new QName("providerURL")) ; + String deliveryMode = connectionOM.getAttributeValue(new QName(null, "deliveryMode")) ; + String timeToLive = connectionOM.getAttributeValue(new QName(null, "timeToLive")) ; + String priority = connectionOM.getAttributeValue(new QName(null, "priority")) ; + String replyTo = connectionOM.getAttributeValue(new QName(null, "replyTo")) ; + String jmsResourceFactoryName = connectionOM.getAttributeValue(new QName(null, "jmsResourceFactory")) ; + + OMElement opSecOM = omElement.getFirstChildWithName(new QName("operationSelector")); + String operationSelector = opSecOM.getAttributeValue(new QName("name")); + OMElement opSecPropertyOM = opSecOM.getFirstChildWithName(new QName("property")); + String jmsOpSecPropertyName = opSecPropertyOM.getText(); + + LoaderUtil.skipToEndElement(reader); + + JMSBinding binding = new JMSBinding(); + binding.setActivationSpecName(activationSpecName); + binding.setConnectionFactoryName(connectionFactoryName); + binding.setDestinationName(destinationName); + binding.setInitialContextFactoryName(initialContextFactoryName); + binding.setJNDIProviderURL(jNDIProviderURL); + binding.setReplyTo(replyTo); + binding.setJmsResourceFactoryName(jmsResourceFactoryName); + binding.setOperationSelectorName(operationSelector); + binding.setOperationSelectorPropertyName(jmsOpSecPropertyName); + + if (deliveryMode != null && deliveryMode.trim().equals("")){ + try{ + binding.setDeliveryMode(Integer.parseInt(deliveryMode)); + }catch (Exception e){ + + } + } + + if (priority != null && priority.trim().equals("")){ + try{ + binding.setPriority(Integer.parseInt(priority)); + }catch (Exception e){ + + } + } + + if (timeToLive != null && timeToLive.trim().equals("")){ + try{ + binding.setTimeToLive(Integer.parseInt(timeToLive)); + }catch (Exception e){ + + } + } + + return binding; + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSProxy.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSProxy.java new file mode 100644 index 0000000000..d088e0f288 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSProxy.java @@ -0,0 +1,80 @@ +package org.apache.tuscany.binding.jms; + +import java.lang.reflect.Method; + +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.NamingException; + +public class JMSProxy implements MessageListener{ + + protected Method operationMethod; + protected SDODataBinding dataBinding; + private Object entryPointProxy; + private JMSBinding jmsBinding; + private Context context; + private JMSResourceFactory jmsResourceFactory; + private OperationSelector operationSelector; + + public JMSProxy(Object entryPointProxy,JMSResourceFactory jmsResourceFactory, JMSBinding jmsBinding,SDODataBinding dataBinding, OperationSelector operationSelector) throws NamingException{ + + this.entryPointProxy = entryPointProxy; + this.dataBinding = dataBinding; + this.jmsBinding = jmsBinding; + this.jmsResourceFactory = jmsResourceFactory; + this.operationSelector = operationSelector; + } + + public void onMessage(Message msg){ + + String operationName = operationSelector.getOperationName(msg); + + try { + + Object[] args = dataBinding.fromXML(((TextMessage)msg).getText()); + + operationMethod = getOperationMethod(entryPointProxy,operationName,args); + + Object result = operationMethod.invoke(entryPointProxy,args); + + // if result is null then the method can be assumed as oneway + if (result != null){ + String response = dataBinding.toXML(new Object[] {result} ); + sendReply(operationName, response); + } + } catch (Exception e) { + // need to do proper error handling + e.printStackTrace(); + } + } + + private Method getOperationMethod(Object entryPointProxy2, String operationName, Object[] args) { + // TODO Auto-generated method stub + return null; + } + + private void sendReply(String operationName,String payload) throws JMSException, NamingException{ + + Session session = jmsResourceFactory.createSession(); + + javax.jms.Message message = jmsResourceFactory.createMessage(session); + message.setJMSCorrelationID(operationName); // I am using the existing header instead of adding a new JMS property. + + ((javax.jms.TextMessage)message).setText((String)payload); + + Destination destination = null; + Object obj = context.lookup(jmsBinding.getDestinationName()); + destination = (Destination) obj; + + MessageProducer producer = session.createProducer(destination); + producer.send(message); + producer.close(); + session.close(); + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSReference.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSReference.java new file mode 100644 index 0000000000..ac669653b8 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSReference.java @@ -0,0 +1,43 @@ +package org.apache.tuscany.binding.jms; + +import javax.naming.NamingException; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +/** + * @version $Rev$ $Date$ + */ +public class JMSReference<T> extends ReferenceExtension { + + private JMSBinding jmsBinding; + private JMSResourceFactory jmsResourceFactory; + private OperationSelector operationSelector; + + public JMSReference(String name, + CompositeComponent parent, + WireService wireService, + JMSBinding jmsBinding, + JMSResourceFactory jmsResourceFactory, + OperationSelector operationSelector, + Class<?> service) { + + super(name, service, parent, wireService); + + this.jmsBinding = jmsBinding; + this.jmsResourceFactory = jmsResourceFactory; + this.operationSelector = operationSelector; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + try { + return new JMSTargetInvoker(jmsResourceFactory, jmsBinding, operation.getName(),operationSelector); + } catch (NamingException e) { + throw new RuntimeException("Unable to create JMS resources for the invocation",e); + } + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSResourceFactory.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSResourceFactory.java new file mode 100644 index 0000000000..5633cbed16 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSResourceFactory.java @@ -0,0 +1,24 @@ +package org.apache.tuscany.binding.jms; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.Session; +import javax.naming.NamingException; + +public interface JMSResourceFactory { + + public abstract Connection getConnection() throws NamingException, + JMSException; + + public abstract Session createSession() throws JMSException, + NamingException; + + public abstract void startConnection() throws JMSException, NamingException; + + public abstract void closeConnection() throws JMSException, NamingException; + + public abstract Message createMessage(Session session) + throws JMSException; + +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSService.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSService.java new file mode 100644 index 0000000000..d92303145e --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSService.java @@ -0,0 +1,82 @@ +package org.apache.tuscany.binding.jms; + + +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; +import javax.naming.NamingException; +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceExtension; +import org.apache.tuscany.spi.wire.WireService; + +import commonj.sdo.helper.TypeHelper; + +/** + * @version $Rev$ $Date$ + */ +public class JMSService extends ServiceExtension { + + private JMSBinding jmsBinding; + private TypeHelper typeHelper; + private JMSResourceFactory jmsResourceFactory; + private MessageConsumer consumer; + private OperationSelector operationSelector; + + public JMSService(String name, + CompositeComponent parent, + WireService wireService, + JMSBinding jmsBinding, + JMSResourceFactory jmsResourceFactory, + OperationSelector operationSelector, + Class<?> service, + TypeHelper typeHelper) { + super(name, service, parent, wireService); + + this.jmsBinding = jmsBinding; + this.typeHelper = typeHelper; + this.jmsResourceFactory = jmsResourceFactory; + this.operationSelector = operationSelector; + } + + public void start() { + super.start(); + try { + registerListerner(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void stop() { + + try { + consumer.close(); + jmsResourceFactory.closeConnection(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + super.stop(); + } + + private void registerListerner() throws NamingException, JMSException{ + + Object entryPointProxy = this.getServiceInstance(); + QName responseQN = new QName("payload"); + SDODataBinding dataBinding = new SDODataBinding(typeHelper, false, responseQN); + + Session session = jmsResourceFactory.createSession(); + Destination destination = session.createQueue(jmsBinding.getDestinationName()); + + consumer = session.createConsumer(destination); + consumer.setMessageListener(new JMSProxy(entryPointProxy,jmsResourceFactory,jmsBinding,dataBinding,operationSelector)); + + jmsResourceFactory.startConnection(); + + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSTargetInvoker.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSTargetInvoker.java new file mode 100644 index 0000000000..97b2dbef83 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSTargetInvoker.java @@ -0,0 +1,111 @@ +package org.apache.tuscany.binding.jms; + + +import java.lang.reflect.InvocationTargetException; +import java.util.Properties; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Invoke a JMS reference. + * + * @version $Rev$ $Date$ + */ +public class JMSTargetInvoker implements TargetInvoker { + private JMSBinding jmsBinding; + private Context context; + private String operationName; + private JMSResourceFactory jmsResourceFactory; + private OperationSelector operationSelector; + + public JMSTargetInvoker(JMSResourceFactory jmsResourceFactory,JMSBinding jmsBinding, String operationName, OperationSelector operationSelector) throws NamingException { + this.jmsBinding = jmsBinding; + this.jmsResourceFactory = jmsResourceFactory; + this.operationName = operationName; + this.operationSelector = operationSelector; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBody(e.getCause()); + } + return msg; + } + + public Object invokeTarget(Object payload) throws InvocationTargetException { + try { + + return sendReceiveMessage(payload); + + } catch (Exception e) { // catch JMS specific error + + throw new AssertionError(e); + } + + } + + public Object clone() throws CloneNotSupportedException { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); + } + } + + public boolean isOptimizable() { + return false; + } + + public boolean isCacheable() { + return false; + } + + public void setCacheable(boolean cacheable) { + } + + private Object sendReceiveMessage(Object payload) throws JMSException, NamingException{ + + Session session = jmsResourceFactory.createSession(); + + javax.jms.Message message = jmsResourceFactory.createMessage(session); + operationSelector.setOperationName(operationName,message); + + ((javax.jms.TextMessage)message).setText((String)payload); + + Destination destination = (Destination) context.lookup(jmsBinding.getDestinationName()); + + MessageProducer producer = session.createProducer(destination); + + // create a temporary queue and listen to the response + Destination replyDest = session.createTemporaryQueue(); + message.setJMSReplyTo(replyDest); + + producer.send(message); + producer.close(); + + MessageConsumer consumer = session.createConsumer(replyDest); + jmsResourceFactory.startConnection(); + javax.jms.Message reply = consumer.receive(jmsBinding.getTimeToLive()); + consumer.close(); + + session.close(); + + return ((javax.jms.TextMessage)reply).getText(); + } +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/OperationSelector.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/OperationSelector.java new file mode 100644 index 0000000000..2b510e6eb7 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/OperationSelector.java @@ -0,0 +1,12 @@ +package org.apache.tuscany.binding.jms; + +/* + * Defined by the SCA Messaging binding spec + */ +public interface OperationSelector { + + public String getOperationName(javax.jms.Message message); + + public void setOperationName(String operationName,javax.jms.Message message); + +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SDODataBinding.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SDODataBinding.java new file mode 100644 index 0000000000..f45a3f68d4 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SDODataBinding.java @@ -0,0 +1,156 @@ +/* + * 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.binding.jms; + +import java.io.ByteArrayInputStream; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.FactoryConfigurationError; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMXMLParserWrapper; +import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory; +import org.apache.tuscany.core.databinding.impl.TransformationContextImpl; +import org.apache.tuscany.databinding.sdo.XMLDocument2XMLStreamReader; +import org.apache.tuscany.databinding.sdo.XMLStreamReader2XMLDocument; +import org.apache.tuscany.sdo.util.SDOUtil; +import org.apache.tuscany.spi.databinding.TransformationContext; +import org.apache.tuscany.spi.model.DataType; +import org.apache.tuscany.spi.wire.InvocationRuntimeException; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +/** + * DataBinding for converting between AXIOM OMElement and Java Objects + */ +public class SDODataBinding { + + private TypeHelper typeHelper; + + private boolean isWrapped; + + private QName elementName; + + public SDODataBinding(TypeHelper typeHelper, boolean isWrapped, QName elementName) { + this.typeHelper = typeHelper; + this.isWrapped = isWrapped; + this.elementName = elementName; + } + + public Object[] fromXML(String input) throws XMLStreamException, FactoryConfigurationError { + + ByteArrayInputStream bIn = new ByteArrayInputStream(input.getBytes()); + + XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bIn); + + XMLStreamReader2XMLDocument transformer = new XMLStreamReader2XMLDocument(); + TransformationContext context = new TransformationContextImpl(); + DataType<QName> binding = new DataType<QName>(DataObject.class, null); + binding.setMetadata(TypeHelper.class.getName(), typeHelper); + context.setTargetDataType(binding); + XMLDocument document = transformer.transform(reader, context); + return toObjects(document, isWrapped); + } + + public String toXML(Object[] os) { + XMLDocument document = toXMLDocument(typeHelper, os, elementName, isWrapped); + XMLDocument2XMLStreamReader transformer = new XMLDocument2XMLStreamReader(); + XMLStreamReader reader = transformer.transform(document, null); + OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), reader); + OMElement omElement = builder.getDocumentElement(); + return omElement.toString(); + } + + /** + * Convert a typed DataObject to Java objects + * + * @param dataObject + * @param isWrapped + * @return the array of Objects from the DataObject + */ + public static Object[] toObjects(XMLDocument document, boolean isWrapped) { + DataObject dataObject = document.getRootObject(); + if (isWrapped) { + List ips = dataObject.getInstanceProperties(); + Object[] os = new Object[ips.size()]; + for (int i = 0; i < ips.size(); i++) { + os[i] = dataObject.get((Property) ips.get(i)); + } + return os; + } else { + Object object = dataObject; + Type type = dataObject.getType(); + if (type.isSequenced()) { + object = dataObject.getSequence().getValue(0); + } + return new Object[] { object }; + } + } + + /** + * Convert objects to typed DataObject + * + * @param typeNS + * @param typeName + * @param os + * @return the DataObject + */ + private static XMLDocument toXMLDocument(TypeHelper typeHelper, Object[] os, QName elementQName, boolean isWrapped) { + XSDHelper xsdHelper = SDOUtil.createXSDHelper(typeHelper); + + Property property = xsdHelper.getGlobalProperty(elementQName.getNamespaceURI(), elementQName.getLocalPart(), true); + if (null == property) { + throw new InvocationRuntimeException("Type '" + elementQName.toString() + "' not found in registered SDO types."); + } + DataObject dataObject = null; + if (isWrapped) { + DataFactory dataFactory = SDOUtil.createDataFactory(typeHelper); + dataObject = dataFactory.create(property.getType()); + List ips = dataObject.getInstanceProperties(); + for (int i = 0; i < ips.size(); i++) { + dataObject.set(i, os[i]); + } + } else { + Object value = os[0]; + Type type = property.getType(); + if (!type.isDataType()) { + dataObject = (DataObject) value; + } else { + dataObject = SDOUtil.createDataTypeWrapper(type, value); + } + } + + XMLHelper xmlHelper = SDOUtil.createXMLHelper(typeHelper); + return xmlHelper.createDocument(dataObject, elementQName.getNamespaceURI(), elementQName.getLocalPart()); + + } + +} diff --git a/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SimpleJMSResourceFactory.java b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SimpleJMSResourceFactory.java new file mode 100644 index 0000000000..399e80e4b5 --- /dev/null +++ b/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/SimpleJMSResourceFactory.java @@ -0,0 +1,95 @@ +package org.apache.tuscany.binding.jms; + +import java.util.Properties; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class SimpleJMSResourceFactory implements JMSResourceFactory { + + private JMSBinding jmsBinding; + private Connection con; + private Context context; + private boolean isConnectionStarted; + + public SimpleJMSResourceFactory(JMSBinding jmsBinding){ + this.jmsBinding = jmsBinding; + } + + /* + * 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. + * + * For ex 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 (con == null){ + createConnection(); + } + return con; + } + + /* (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, NamingException{ + if(con != null){ + con.close(); + } + } + + /* (non-Javadoc) + * @see org.apache.tuscany.binding.jms.JMSResourceFactory#createTextMessage(javax.jms.Session) + */ + public Message createMessage(Session session) throws JMSException{ + javax.jms.Message message = session.createTextMessage(); // default + message.setJMSDeliveryMode(jmsBinding.getDeliveryMode()); + message.setJMSPriority(jmsBinding.getPriority()); + + return message; + } + + private void createConnection() throws NamingException, JMSException { + if(context == null){ + createInitialContext(); + } + ConnectionFactory conFac = (ConnectionFactory)context.lookup(jmsBinding.getConnectionFactoryName()); + con = conFac.createConnection(); + } + + private void createInitialContext() throws NamingException{ + Properties props = new Properties(); + props.setProperty(Context.INITIAL_CONTEXT_FACTORY,jmsBinding.getInitialContextFactoryName()); + props.setProperty(Context.PROVIDER_URL,jmsBinding.getJNDIProviderURL()); + + context = new InitialContext(props); + } + +} |