servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r702756 - in /servicemix/components/engines/servicemix-bean/trunk/src: main/java/org/apache/servicemix/bean/ main/java/org/apache/servicemix/bean/pojos/ test/java/org/apache/servicemix/bean/
Date Wed, 08 Oct 2008 08:49:54 GMT
Author: gnodet
Date: Wed Oct  8 01:49:53 2008
New Revision: 702756

URL: http://svn.apache.org/viewvc?rev=702756&view=rev
Log:
SM-1618: Given that the lwcontainer is not ported to smx4, we need a replacement for the log
component which could be done inside servicemix-bean

Added:
    servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/
    servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/LoggingPojo.java
    servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/LoggingPojoTest.java
Modified:
    servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/BeanEndpoint.java

Modified: servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/BeanEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/BeanEndpoint.java?rev=702756&r1=702755&r2=702756&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/BeanEndpoint.java
(original)
+++ servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/BeanEndpoint.java
Wed Oct  8 01:49:53 2008
@@ -306,12 +306,12 @@
     }
 
     protected Object createBean() throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
-        if (beanName == null && beanType == null) {
-            throw new IllegalArgumentException("Property 'beanName' has not been set!");
-        }
         if (beanType == null && beanClassName != null) {
             beanType = Class.forName(beanClassName, true, getServiceUnit().getConfigurationClassLoader());
         }
+        if (beanName == null && beanType == null) {
+            throw new IllegalArgumentException("Property 'bean', 'beanName' or 'beanClassName'
has not been set!");
+        }
         if (beanType != null) {
             return beanType.newInstance();
         } else if (beanName == null) {

Added: servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/LoggingPojo.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/LoggingPojo.java?rev=702756&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/LoggingPojo.java
(added)
+++ servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/pojos/LoggingPojo.java
Wed Oct  8 01:49:53 2008
@@ -0,0 +1,181 @@
+/*
+ * 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.servicemix.bean.pojos;
+
+import java.util.Set;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Node;
+
+import org.apache.servicemix.bean.support.BeanSupport;
+import org.apache.servicemix.jbi.listener.MessageExchangeListener;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.common.util.MessageUtil;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A simple tracing component which can be placed inside a pipeline
+ * to trace the message exchange though the component.
+ * If an InOut exchange is received by this component, it will log the
+ * input message and copy it to the output message.
+ *
+ * @version $Revision: 648504 $
+ */
+public class LoggingPojo extends BeanSupport implements MessageExchangeListener {
+
+    private Log log = LogFactory.getLog(LoggingPojo.class);
+
+    private SourceTransformer sourceTransformer = new SourceTransformer();
+
+    private int maxMsgDisplaySize = 1500;
+
+    public Log getLog() {
+        return log;
+    }
+
+    public void setLog(Log log) {
+        this.log = log;
+    }
+
+    public SourceTransformer getSourceTransformer() {
+        return sourceTransformer;
+    }
+
+    public void setSourceTransformer(SourceTransformer sourceTransformer) {
+        this.sourceTransformer = sourceTransformer;
+    }
+
+    public int getMaxMsgDisplaySize() {
+        return maxMsgDisplaySize;
+    }
+
+    public void setMaxMsgDisplaySize(int maxMsgDisplaySize) {
+        this.maxMsgDisplaySize = maxMsgDisplaySize;
+    }
+
+    /**
+     * Intercepts the {@link MessageExchange} to output the message and its
+     * properties for debugging purposes.
+     *
+     * @param exchange A JBI {@link MessageExchange} between two endpoints
+     */
+    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
+        if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+            // lets dump the incoming message
+            NormalizedMessage message = exchange.getMessage("in");
+            StringBuffer sb = new StringBuffer();
+            sb.append("[\n");
+            sb.append("  id: ").append(exchange.getExchangeId()).append('\n');
+            sb.append("  mep: ").append(exchange.getPattern()).append('\n');
+            sb.append("  status: ").append(exchange.getStatus()).append('\n');
+            sb.append("  role: ").append(exchange.getRole() == MessageExchange.Role.CONSUMER
? "consumer" : "provider").append('\n');
+            if (exchange.getInterfaceName() != null) {
+                sb.append("  interface: ").append(exchange.getInterfaceName()).append('\n');
+            }
+            if (exchange.getService() != null) {
+                sb.append("  service: ").append(exchange.getService()).append('\n');
+            }
+            if (exchange.getEndpoint() != null) {
+                sb.append("  endpoint: ").append(exchange.getEndpoint().getEndpointName()).append('\n');
+            }
+            if (exchange.getOperation() != null) {
+                sb.append("  operation: ").append(exchange.getOperation()).append('\n');
+            }
+            if (exchange.getPropertyNames().size() > 0) {
+                sb.append("  properties: [").append('\n');
+                for (String key : (Set<String>) exchange.getPropertyNames()) {
+                    sb.append("      ").append(key).append(" = ");
+                    Object contents = exchange.getProperty(key);
+                    if (contents instanceof Source) {
+                        try {
+                            contents = sourceTransformer.toString((Source) contents);
+                        } catch (Exception e) { }
+                    }
+                    sb.append(contents);
+                    sb.append('\n');
+                }
+                sb.append("  ]").append('\n');
+            }
+            display(exchange, "in", sb);
+            log.info("Exchange received " + sb.toString());
+            if (exchange instanceof InOut) {
+                MessageUtil.transferInToOut(exchange, exchange);
+                send(exchange);
+            } else {
+                done(exchange);
+            }
+        }
+    }
+
+    private void display(MessageExchange exchange, String msg, StringBuffer sb) {
+        NormalizedMessage message = exchange.getMessage(msg);
+        if (message != null) {
+            sb.append("  ").append(msg).append(": [").append('\n');
+            sb.append("    content: ");
+            try {
+                if (message.getContent() != null) {
+                    Node node = sourceTransformer.toDOMNode(message.getContent());
+                    message.setContent(new DOMSource(node));
+                    String str = sourceTransformer.toString(node);
+                    if (str.length() > maxMsgDisplaySize) {
+                        sb.append(str.substring(0, maxMsgDisplaySize)).append("...");
+                    } else {
+                        sb.append(str);
+                    }
+                } else {
+                    sb.append("null");
+                }
+            } catch (Exception e) {
+                sb.append("Unable to display: ").append(e);
+            }
+            sb.append('\n');
+            if (message.getAttachmentNames().size() > 0) {
+                sb.append("    attachments: [").append('\n');
+                for (String key : (Set<String>) message.getAttachmentNames()) {
+                    sb.append("      ").append(key).append(" = ").append(message.getAttachment(key)).append('\n');
+                }
+                sb.append("    ]").append('\n');
+            }
+            if (message.getPropertyNames().size() > 0) {
+                sb.append("    properties: [").append('\n');
+                for (String key : (Set<String>) message.getPropertyNames()) {
+                    sb.append("      ").append(key).append(" = ");
+                    Object contents = message.getProperty(key);
+                    if (contents instanceof Source) {
+                        try {
+                            contents = sourceTransformer.toString((Source) contents);
+                        } catch (Exception e) { }
+                    }
+                    sb.append(contents);
+                    sb.append('\n');
+                }
+                sb.append("    ]").append('\n');
+            }
+            sb.append("  ]").append('\n');
+        }
+    }
+
+}

Added: servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/LoggingPojoTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/LoggingPojoTest.java?rev=702756&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/LoggingPojoTest.java
(added)
+++ servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/LoggingPojoTest.java
Wed Oct  8 01:49:53 2008
@@ -0,0 +1,73 @@
+/*
+ * 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.servicemix.bean;
+
+import javax.xml.namespace.QName;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.InOut;
+
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.bean.pojos.LoggingPojo;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.client.ServiceMixClientFacade;
+import junit.framework.TestCase;
+
+public class LoggingPojoTest extends TestCase {
+
+    protected JBIContainer container;
+    protected BeanComponent component;
+
+    protected void setUp() throws Exception {
+        container = new JBIContainer();
+        container.setEmbedded(true);
+        container.init();
+
+        component = new BeanComponent();
+        BeanEndpoint loggingEndpoint = new BeanEndpoint();
+        loggingEndpoint.setBeanClassName(LoggingPojo.class.getName());
+        loggingEndpoint.setService(new QName("logging"));
+        loggingEndpoint.setEndpoint("endpoint");
+        component.addEndpoint(loggingEndpoint);
+        container.activateComponent(component, "servicemix-bean");
+
+        container.start();
+    }
+
+    protected void tearDown() throws Exception {
+        container.shutDown();
+    }
+
+    public void testInOnly() throws Exception {
+        ServiceMixClient client = new ServiceMixClientFacade(component.getComponentContext());
+        InOnly exchange = client.createInOnlyExchange();
+        exchange.setService(new QName("logging"));
+        exchange.getInMessage().setContent(new StringSource("<hello/>"));
+        exchange.getInMessage().setProperty("key", "value");
+        client.sendSync(exchange);
+    }
+
+    public void testInOut() throws Exception {
+        ServiceMixClient client = new ServiceMixClientFacade(component.getComponentContext());
+        InOut exchange = client.createInOutExchange();
+        exchange.setService(new QName("logging"));
+        exchange.getInMessage().setContent(new StringSource("<hello/>"));
+        exchange.getInMessage().setProperty("key", "value");
+        client.sendSync(exchange);
+        client.done(exchange);
+    }
+}



Mime
View raw message