Author: jstrachan
Date: Fri Oct 13 09:05:03 2006
New Revision: 463724

URL: http://svn.apache.org/viewvc?view=rev&rev=463724
Log:
Added support for the @Content annotation on method parameters to allow the 
message body to be injected into a method call

Added:
    
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/Content.java
Modified:
    
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/support/BeanInfo.java
    
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/PlainBeanEndpointTest.java
    
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/UriTest.java
    
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/beans/PlainBean.java

Added: 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/Content.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/Content.java?view=auto&rev=463724
==============================================================================
--- 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/Content.java
 (added)
+++ 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/Content.java
 Fri Oct 13 09:05:03 2006
@@ -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.servicemix.bean;
+
+import org.apache.servicemix.jbi.messaging.PojoMarshaler;
+import org.apache.servicemix.jbi.messaging.DefaultMarshaler;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+
+/**
+ * Binds a method parameter to the payload of a message exchange
+ *
+ * @version $Revision: $
+ */
[EMAIL PROTECTED](RetentionPolicy.RUNTIME)
[EMAIL PROTECTED]({ElementType.PARAMETER})
+public @interface Content {
+
+    /**
+     * Specifies the type of the marshaller to be used
+     */
+    Class<? extends PojoMarshaler> marshalType() default 
DefaultMarshaler.class;
+}

Modified: 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/support/BeanInfo.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/support/BeanInfo.java?view=diff&rev=463724&r1=463723&r2=463724
==============================================================================
--- 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/support/BeanInfo.java
 (original)
+++ 
incubator/servicemix/trunk/servicemix-bean/src/main/java/org/apache/servicemix/bean/support/BeanInfo.java
 Fri Oct 13 09:05:03 2006
@@ -19,11 +19,14 @@
 import org.aopalliance.intercept.MethodInvocation;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.bean.Content;
+import org.apache.servicemix.bean.Property;
+import org.apache.servicemix.bean.XPath;
+import org.apache.servicemix.components.util.MessageHelper;
 import org.apache.servicemix.expression.Expression;
 import org.apache.servicemix.expression.JAXPStringXPathExpression;
 import org.apache.servicemix.expression.PropertyExpression;
-import org.apache.servicemix.bean.Property;
-import org.apache.servicemix.bean.XPath;
+import org.apache.servicemix.jbi.messaging.PojoMarshaler;
 
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
@@ -146,10 +149,35 @@
             Property propertyAnnotation = (Property) annotation;
             return new PropertyExpression(propertyAnnotation.name());
         }
-        if (annotation instanceof XPath) {
+        else if (annotation instanceof Content) {
+            Content content = (Content) annotation;
+            final PojoMarshaler marshaller = newInstance(content);
+            return createContentExpression(marshaller);
+        }
+        else if (annotation instanceof XPath) {
             XPath xpathAnnotation = (XPath) annotation;
             return new JAXPStringXPathExpression(xpathAnnotation.xpath());
         }
         return null;
+    }
+
+    protected Expression createContentExpression(final PojoMarshaler 
marshaller) {
+        return new Expression() {
+            public Object evaluate(MessageExchange exchange, NormalizedMessage 
message) throws MessagingException {
+                return MessageHelper.getBody(message, marshaller);
+            }
+        };
+    }
+
+    protected PojoMarshaler newInstance(Content content) {
+        try {
+            return content.marshalType().newInstance();
+        }
+        catch (InstantiationException e) {
+            throw new RuntimeException(e);
+        }
+        catch (IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
     }
 }

Modified: 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/PlainBeanEndpointTest.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/PlainBeanEndpointTest.java?view=diff&rev=463724&r1=463723&r2=463724
==============================================================================
--- 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/PlainBeanEndpointTest.java
 (original)
+++ 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/PlainBeanEndpointTest.java
 Fri Oct 13 09:05:03 2006
@@ -134,6 +134,34 @@
         assertEquals("xpath parameter", "London", xpath);
     }
 
+    public void 
testSendingToDynamicEndpointForPlainBeanWithPropertyAndContentParamameter() 
throws Exception {
+        // now lets make a request on this endpoint
+        DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
+
+        DocumentFragment epr = URIResolver.createWSAEPR("bean:plainBean");
+        ServiceEndpoint se = client.getContext().resolveEndpointReference(epr);
+        assertNotNull("We should find a service endpoint!", se);
+
+        InOnly exchange = client.createInOnlyExchange();
+        exchange.setEndpoint(se);
+        exchange.setOperation(new 
QName("methodWithPropertyParameterAndContent"));
+        NormalizedMessage inMessage = exchange.getInMessage();
+        inMessage.setProperty("person", "James");
+        inMessage.setContent(new StringSource("<hello 
address='London'>world</hello>"));
+        client.sendSync(exchange);
+
+        assertExchangeWorked(exchange);
+
+        PlainBean bean = (PlainBean) getBean("plainBean");
+        Object property = bean.getPropertyParameter();
+        Object body = bean.getBody();
+        log.info("Bean's methodWithPropertyParameterAndContent() method has 
been with property: " + property + " and body: " + body);
+
+        assertEquals("property parameter", "James", property);
+        // TODO need to add a marshalling example
+        //assertEquals("content parameter", "London", body);
+    }
+
     protected void assertExchangeWorked(MessageExchange me) throws Exception {
         if (me.getStatus() == ExchangeStatus.ERROR) {
             if (me.getError() != null) {

Modified: 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/UriTest.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/UriTest.java?view=diff&rev=463724&r1=463723&r2=463724
==============================================================================
--- 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/UriTest.java
 (original)
+++ 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/UriTest.java
 Fri Oct 13 09:05:03 2006
@@ -49,8 +49,14 @@
         dump(uri);
     }
 
+
     public void testHierarchialWithAbsolutePath() throws Exception {
         URI uri = new URI("file:///cheese/path?foo=123");
+        dump(uri);
+    }
+
+    public void testUserNameAndPassword() throws Exception {
+        URI uri = new URI("jabber://[EMAIL PROTECTED]/[EMAIL PROTECTED]");
         dump(uri);
     }
 

Modified: 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/beans/PlainBean.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/beans/PlainBean.java?view=diff&rev=463724&r1=463723&r2=463724
==============================================================================
--- 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/beans/PlainBean.java
 (original)
+++ 
incubator/servicemix/trunk/servicemix-bean/src/test/java/org/apache/servicemix/bean/beans/PlainBean.java
 Fri Oct 13 09:05:03 2006
@@ -20,6 +20,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.bean.Property;
 import org.apache.servicemix.bean.XPath;
+import org.apache.servicemix.bean.Content;
 
 import javax.jbi.messaging.MessageExchange;
 
@@ -36,6 +37,7 @@
     private MessageExchange bar;
     private String propertyParameter;
     private String xpathParameter;
+    private Object body;
 
     public void foo(MessageExchange messageExchange) {
         this.foo = messageExchange;
@@ -59,6 +61,12 @@
         log.info("methodWithPropertyParameterAndXPath() called with parameter: 
" + address);
     }
 
+    public void methodWithPropertyParameterAndContent(@Property(name = 
"person") String name, @Content Object body) {
+        this.propertyParameter = name;
+        this.body = body;
+        log.info("methodWithPropertyParameterAndContent() called with body: " 
+ body);
+    }
+
 
     public MessageExchange getFoo() {
         return foo;
@@ -90,5 +98,13 @@
 
     public void setXpathParameter(String xpathParameter) {
         this.xpathParameter = xpathParameter;
+    }
+
+    public Object getBody() {
+        return body;
+    }
+
+    public void setBody(Object body) {
+        this.body = body;
     }
 }


Reply via email to