servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r391800 - in /incubator/servicemix/trunk/servicemix-jms/src/main: java/org/apache/servicemix/jms/standard/ resources/META-INF/services/org/apache/servicemix/jms/
Date Wed, 05 Apr 2006 20:46:50 GMT
Author: gnodet
Date: Wed Apr  5 13:46:49 2006
New Revision: 391800

URL: http://svn.apache.org/viewcvs?rev=391800&view=rev
Log:
SM-378: Add a fully JMS compliant processor set

Added:
    incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/
    incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardConsumerProcessor.java
    incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardProviderProcessor.java
    incubator/servicemix/trunk/servicemix-jms/src/main/resources/META-INF/services/org/apache/servicemix/jms/standard

Added: incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardConsumerProcessor.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardConsumerProcessor.java?rev=391800&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardConsumerProcessor.java
(added)
+++ incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardConsumerProcessor.java
Wed Apr  5 13:46:49 2006
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2005-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.servicemix.jms.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import javax.jbi.messaging.DeliveryChannel;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jms.BytesMessage;
+import javax.jms.Destination;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+import javax.resource.spi.work.Work;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jms.AbstractJmsProcessor;
+import org.apache.servicemix.jms.JmsEndpoint;
+import org.apache.servicemix.soap.Context;
+import org.apache.servicemix.soap.SoapFault;
+import org.apache.servicemix.soap.SoapHelper;
+import org.apache.servicemix.soap.handlers.AddressingHandler;
+import org.apache.servicemix.soap.marshalers.SoapMarshaler;
+import org.apache.servicemix.soap.marshalers.SoapMessage;
+import org.apache.servicemix.soap.marshalers.SoapWriter;
+
+import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
+
+public class StandardConsumerProcessor extends AbstractJmsProcessor {
+
+    private static final Log log = LogFactory.getLog(StandardConsumerProcessor.class);
+    
+    protected Session session;
+    protected Destination destination;
+    protected DeliveryChannel channel;
+    protected SoapHelper soapHelper;
+    protected SoapMarshaler soapMarshaler;
+    protected AtomicBoolean running = new AtomicBoolean(false);
+    
+    public StandardConsumerProcessor(JmsEndpoint endpoint) {
+        super(endpoint);
+        this.soapMarshaler = new SoapMarshaler(endpoint.isSoap());
+        if (endpoint.isSoap() && "1.1".equals(endpoint.getSoapVersion())) {
+            this.soapMarshaler.setSoapUri(SoapMarshaler.SOAP_11_URI);
+        }
+        this.soapHelper = new SoapHelper(endpoint);
+        this.soapHelper.addPolicy(new AddressingHandler());
+    }
+
+    protected void doStart(InitialContext ctx) throws Exception {
+        destination = endpoint.getDestination();
+        if (destination == null) {
+            if (endpoint.getJndiDestinationName() != null) {
+                destination = (Destination) ctx.lookup(endpoint.getJndiDestinationName());
+            } else if (endpoint.getJmsProviderDestinationName() == null) {
+                throw new IllegalStateException("No destination provided");
+            }
+        }
+        channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
+        synchronized (running) {
+            endpoint.getServiceUnit().getComponent().getWorkManager().startWork(new Work()
{
+                public void release() {
+                }
+                public void run() {
+                    StandardConsumerProcessor.this.poll();
+                }
+            });
+            running.wait();
+        }
+    }
+
+
+    protected void doStop() throws Exception {
+        if (running.get()) {
+            synchronized (running) {
+                if (session != null) {
+                    session.close();
+                }
+                running.wait();
+            }
+        }
+        session = null;
+        destination = null;
+    }
+    
+    protected void poll() {
+        synchronized (running) {
+            running.set(true);
+            running.notify();
+        }
+        try {
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            if (destination == null) {
+                if (STYLE_QUEUE.equals(endpoint.getDestinationStyle())) {
+                    destination = session.createQueue(endpoint.getJmsProviderDestinationName());
+                } else {
+                    destination = session.createTopic(endpoint.getJmsProviderDestinationName());
+                }
+            }
+            MessageConsumer consumer = session.createConsumer(destination);
+            while (running.get()) {
+                Message message = consumer.receive();
+                if (message != null) {
+                    onMessage(message);
+                }
+            }
+        } catch (Exception e) {
+            log.error("", e);
+        } finally {
+            synchronized (running) {
+                running.set(false);
+                running.notify();
+            }
+        }
+    }
+
+    public void onMessage(final Message message) {
+        try {
+            if (log.isDebugEnabled()) {
+                log.debug("Received jms message " + message);
+            }
+            InputStream is = null;
+            if (message instanceof TextMessage) {
+                is = new ByteArrayInputStream(((TextMessage) message).getText().getBytes());
+            } else if (message instanceof BytesMessage) {
+                int length = (int) ((BytesMessage) message).getBodyLength();
+                byte[] bytes = new byte[length];
+                ((BytesMessage) message).readBytes(bytes);
+                is = new ByteArrayInputStream(bytes);
+            } else {
+                throw new IllegalArgumentException("JMS message should be a text or bytes
message");
+            }
+            String contentType = message.getStringProperty("Content-Type");
+            SoapMessage soap = soapMarshaler.createReader().read(is, contentType);
+            Context context = soapHelper.createContext(soap);
+            MessageExchange exchange = soapHelper.onReceive(context);
+            context.setProperty(Message.class.getName(), message);
+            // TODO: copy protocol messages
+            //inMessage.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(message));
+            if (!channel.sendSync(exchange)) {
+                throw new IllegalStateException("Exchange has been aborted");
+            }
+            MessageProducer producer = null;
+            Message response = null;
+            try {
+                if (exchange.getStatus() == ExchangeStatus.ERROR) {
+                    Exception e = exchange.getError();
+                    if (e == null) {
+                        e = new Exception("Unkown error");
+                    }
+                    response = session.createObjectMessage(e);
+                } else if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+                    if (exchange.getFault() != null) {
+                        SoapFault fault = new SoapFault(SoapFault.RECEIVER, null, null, null,
exchange.getFault().getContent());
+                        SoapMessage soapFault = soapHelper.onFault(context, fault);
+                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                        SoapWriter writer = soapMarshaler.createWriter(soapFault);
+                        writer.write(baos);
+                        response = session.createTextMessage(baos.toString());
+                        response.setStringProperty("Content-Type", writer.getContentType());
+                        // TODO: Copy other properties from fault
+                    } else {
+                        NormalizedMessage outMsg = exchange.getMessage("out");
+                        if (outMsg != null) {
+                            SoapMessage out = soapHelper.onReply(context, outMsg);
+                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                            SoapWriter writer = soapMarshaler.createWriter(out);
+                            writer.write(baos);
+                            response = session.createTextMessage(baos.toString());
+                            response.setStringProperty("Content-Type", writer.getContentType());
+                            // TODO: Copy other properties from response
+                        }
+                    }
+                }
+                if (response != null) {
+                    producer = session.createProducer(message.getJMSReplyTo());
+                    response.setJMSCorrelationID(message.getJMSCorrelationID());
+                    producer.send(response);
+                }
+            } finally {
+                if (producer != null) {
+                    producer.close();
+                }
+                if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+                    exchange.setStatus(ExchangeStatus.DONE);
+                    channel.send(exchange);
+                }
+            }
+        } catch (Throwable e) {
+            log.error("Error while handling jms message", e);
+        }
+    }
+
+    public void process(MessageExchange exchange) throws Exception {
+        throw new IllegalStateException();
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardProviderProcessor.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardProviderProcessor.java?rev=391800&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardProviderProcessor.java
(added)
+++ incubator/servicemix/trunk/servicemix-jms/src/main/java/org/apache/servicemix/jms/standard/StandardProviderProcessor.java
Wed Apr  5 13:46:49 2006
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2005-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.servicemix.jms.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jbi.messaging.DeliveryChannel;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.RobustInOnly;
+import javax.jms.BytesMessage;
+import javax.jms.Destination;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.JbiConstants;
+import org.apache.servicemix.jms.AbstractJmsProcessor;
+import org.apache.servicemix.jms.JmsEndpoint;
+import org.apache.servicemix.soap.marshalers.JBIMarshaler;
+import org.apache.servicemix.soap.marshalers.SoapMarshaler;
+import org.apache.servicemix.soap.marshalers.SoapMessage;
+import org.apache.servicemix.soap.marshalers.SoapWriter;
+
+public class StandardProviderProcessor extends AbstractJmsProcessor {
+
+    private static final Log log = LogFactory.getLog(StandardProviderProcessor.class);
+    
+    protected Session session;
+    protected Destination destination;
+    protected Destination replyToDestination;
+    protected MessageConsumer consumer;
+    protected MessageProducer producer;
+    protected DeliveryChannel channel;
+    protected SoapMarshaler soapMarshaler;
+    protected JBIMarshaler jbiMarshaler;
+    
+    public StandardProviderProcessor(JmsEndpoint endpoint) {
+        super(endpoint);
+        this.soapMarshaler = new SoapMarshaler(endpoint.isSoap());
+        this.jbiMarshaler = new JBIMarshaler();
+    }
+
+    protected void doStart(InitialContext ctx) throws Exception {
+        channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
+        destination = endpoint.getDestination();
+        if (destination == null) {
+            if (endpoint.getJndiDestinationName() != null) {
+                destination = (Destination) ctx.lookup(endpoint.getJndiDestinationName());
+            } else if (endpoint.getJmsProviderDestinationName() == null) {
+                throw new IllegalStateException("No destination provided");
+            }
+        }
+    }
+
+
+    protected void doStop() throws Exception {
+        session = null;
+        destination = null;
+        consumer = null;
+        producer = null;
+        replyToDestination = null;
+    }
+
+    public void process(MessageExchange exchange) throws Exception {
+        if (exchange.getStatus() == ExchangeStatus.DONE) {
+            return;
+        } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
+            return;
+        }
+        try {
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            if (destination == null) {
+                if (STYLE_QUEUE.equals(endpoint.getDestinationStyle())) {
+                    destination = session.createQueue(endpoint.getJmsProviderDestinationName());
+                } else {
+                    destination = session.createTopic(endpoint.getJmsProviderDestinationName());
+                }
+            }
+            producer = session.createProducer(destination);
+            
+            SoapMessage soapMessage = new SoapMessage();
+            NormalizedMessage nm = exchange.getMessage("in");
+            jbiMarshaler.fromNMS(soapMessage, nm);
+            SoapWriter writer = soapMarshaler.createWriter(soapMessage);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            writer.write(baos);
+            Message msg = session.createTextMessage(baos.toString());
+            msg.setStringProperty("Content-Type", writer.getContentType());
+            Map headers = (Map) nm.getProperty(JbiConstants.PROTOCOL_HEADERS);
+            if (headers != null) {
+                for (Iterator it = headers.keySet().iterator(); it.hasNext();) {
+                    String name = (String) it.next();
+                    String value = (String) headers.get(name);
+                    msg.setStringProperty(name, value);
+                }
+            }
+    
+            if (exchange instanceof InOnly || exchange instanceof RobustInOnly) {
+                producer.send(msg);
+            } else if (exchange instanceof InOut) {
+                if (destination instanceof Queue) {
+                    replyToDestination = session.createTemporaryQueue();
+                } else {
+                    replyToDestination = session.createTemporaryTopic();
+                }
+                consumer = session.createConsumer(replyToDestination);
+                msg.setJMSCorrelationID(exchange.getExchangeId());
+                msg.setJMSReplyTo(replyToDestination);
+                producer.send(msg);
+                Message message = consumer.receive();
+                if (message instanceof ObjectMessage) {
+                    Object o = ((ObjectMessage) message).getObject();
+                    if (o instanceof Exception) {
+                        exchange.setError((Exception) o);
+                    } else {
+                        throw new UnsupportedOperationException("Can not handle objects of
type " + o.getClass().getName());
+                    }
+                } else {
+                    InputStream is = null;
+                    if (message instanceof TextMessage) {
+                        is = new ByteArrayInputStream(((TextMessage) message).getText().getBytes());
+                    } else if (message instanceof BytesMessage) {
+                        int length = (int) ((BytesMessage) message).getBodyLength();
+                        byte[] bytes = new byte[length];
+                        ((BytesMessage) message).readBytes(bytes);
+                        is = new ByteArrayInputStream(bytes);
+                    } else {
+                        throw new IllegalArgumentException("JMS message should be a text
or bytes message");
+                    }
+                    String contentType = message.getStringProperty("Content-Type");
+                    SoapMessage soap = soapMarshaler.createReader().read(is, contentType);
+                    NormalizedMessage out = exchange.createMessage();
+                    jbiMarshaler.toNMS(out, soap);
+                    ((InOut) exchange).setOutMessage(out);
+                }
+                channel.send(exchange);
+            } else {
+                throw new IllegalStateException(exchange.getPattern() + " not implemented");
+            }
+        } finally {
+            if (session != null) {
+                session.close();
+            }
+        }
+    }
+
+    
+}

Added: incubator/servicemix/trunk/servicemix-jms/src/main/resources/META-INF/services/org/apache/servicemix/jms/standard
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-jms/src/main/resources/META-INF/services/org/apache/servicemix/jms/standard?rev=391800&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-jms/src/main/resources/META-INF/services/org/apache/servicemix/jms/standard
(added)
+++ incubator/servicemix/trunk/servicemix-jms/src/main/resources/META-INF/services/org/apache/servicemix/jms/standard
Wed Apr  5 13:46:49 2006
@@ -0,0 +1,2 @@
+consumer=org.apache.servicemix.jms.standard.StandardConsumerProcessor
+provider=org.apache.servicemix.jms.standard.StandardProviderProcessor
\ No newline at end of file



Mime
View raw message