synapse-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r600517 - in /webservices/synapse/trunk/java/modules/core/src: main/java/org/apache/synapse/config/xml/ main/java/org/apache/synapse/mediators/annotations/ main/java/org/apache/synapse/mediators/ext/ test/java/org/apache/synapse/mediators/ext/
Date Mon, 03 Dec 2007 13:10:23 GMT
Author: antelder
Date: Mon Dec  3 05:10:22 2007
New Revision: 600517

URL: http://svn.apache.org/viewvc?rev=600517&view=rev
Log:
Straw man for the annotation command mediator being discussed on the ML

Added:
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
  (with props)
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
  (with props)
Modified:
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,118 @@
+/*
+ *  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.synapse.config.xml;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.mediators.ext.AnnotatedCommandMediator;
+import org.apache.synapse.mediators.ext.POJOCommandMediator;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+
+/**
+ * Creates an instance of a AnnotatedCommand mediator using XML configuration specified
+ * <p/>
+ * <pre>
+ * &lt;annotatedCommand name=&quot;class-name&quot;&gt;
+ *   &lt;property name=&quot;string&quot; value=&quot;literal&quot;&gt;
+ *      either literal or XML child
+ *   &lt;/property&gt;
+ *   &lt;property name=&quot;string&quot; expression=&quot;XPATH expression&quot;/&gt;
+ * &lt;/annoatedCommand&gt;
+ * </pre>
+ */
+public class AnnotatedCommandMediatorFactory extends AbstractMediatorFactory {
+
+    private static final QName ANNOTATED_COMMAND_Q =
+        new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "annotatedCommand");
+
+    public Mediator createMediator(OMElement elem) {
+
+        AnnotatedCommandMediator pojoMediator = new AnnotatedCommandMediator();
+
+        // Class name of the Command object should be present
+        OMAttribute name = elem.getAttribute(ATT_NAME);
+        if (name == null) {
+            String msg = "The name of the actual POJO command implementation class" +
+                    " is a required attribute";
+            log.error(msg);
+            throw new SynapseException(msg);
+        }
+
+        // load the class for the command object
+        try {
+            pojoMediator.setCommand(
+                    getClass().getClassLoader().loadClass(name.getAttributeValue()));
+        } catch (ClassNotFoundException e) {
+            handleException("Unable to load the class specified as the command "
+                    + name.getAttributeValue(), e);
+        }
+
+        // setting the properties to the command. these properties will be instantiated
+        // at the mediation time
+        for (Iterator it = elem.getChildElements(); it.hasNext();) {
+            OMElement child = (OMElement) it.next();
+            if("property".equals(child.getLocalName())) {
+
+                String propName = child.getAttribute(ATT_NAME).getAttributeValue();
+                if (propName == null) {
+                    handleException(
+                        "A POJO command mediator property must specify the name attribute");
+                } else {
+                    if (child.getAttribute(ATT_EXPRN) != null) {
+                        AXIOMXPath xpath = null;
+                        try {
+                            xpath = new AXIOMXPath(
+                                child.getAttribute(ATT_EXPRN).getAttributeValue());
+                            OMElementUtils.addNameSpaces(xpath, child, log);
+                            pojoMediator.addDynamicProperty(propName, xpath);
+                        } catch (JaxenException e) {
+                            handleException("Error instantiating XPath expression : " +
+                                child.getAttribute(ATT_EXPRN), e);
+                        }
+                    } else {
+                        if (child.getAttribute(ATT_VALUE) != null) {
+                            pojoMediator.addStaticProperty(propName,
+                                child.getAttribute(ATT_VALUE).getAttributeValue());
+                        } else {
+                            handleException("A POJO mediator property must specify either
" +
+                                "name and expression attributes, or name and value attributes");
+                        }
+                    }
+                }
+            }
+        }
+
+        return pojoMediator;
+    }
+
+    public QName getTagQName() {
+        return ANNOTATED_COMMAND_Q;
+    }
+
+}
+

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AnnotatedCommandMediatorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java?rev=600517&r1=600516&r2=600517&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
(original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
Mon Dec  3 05:10:22 2007
@@ -62,6 +62,7 @@
         ClassMediatorFactory.class,
         ValidateMediatorFactory.class,
         XSLTMediatorFactory.class,
+        AnnotatedCommandMediatorFactory.class,
         POJOCommandMediatorFactory.class,
         CloneMediatorFactory.class,
         IterateMediatorFactory.class,

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,32 @@
+/*
+ *  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.synapse.mediators.annotations;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Target({METHOD})
+@Retention(RUNTIME)
+public @interface Execute {
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Execute.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,46 @@
+/*
+ *  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.synapse.mediators.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to declare namespaces available to be used in XPATH expressions
+ */
+@Target({TYPE, FIELD, METHOD})
+@Retention(RUNTIME)
+public @interface Namespaces {
+
+    String[] value() default {"",""};
+
+    String ns() default "";
+    String ns1() default "";
+    String ns2() default "";
+    String ns3() default "";
+    String ns4() default "";
+    String ns5() default "";
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/Namespaces.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,34 @@
+/*
+ *  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.synapse.mediators.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Target({FIELD})
+@Retention(RUNTIME)
+public @interface ReadAndUpdate {
+
+    String value();
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadAndUpdate.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,35 @@
+/*
+ *  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.synapse.mediators.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Target({FIELD, METHOD})
+@Retention(RUNTIME)
+public @interface ReadFromMessage {
+
+    String value();
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/ReadFromMessage.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,35 @@
+/*
+ *  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.synapse.mediators.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Target({FIELD, METHOD})
+@Retention(RUNTIME)
+public @interface UpdateMessage {
+
+    String value();
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/annotations/UpdateMessage.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,287 @@
+/*
+ *  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.synapse.mediators.ext;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.synapse.Command;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.annotations.Namespaces;
+import org.apache.synapse.mediators.annotations.ReadAndUpdate;
+import org.apache.synapse.mediators.annotations.ReadFromMessage;
+import org.apache.synapse.mediators.annotations.UpdateMessage;
+import org.jaxen.JaxenException;
+
+/**
+ */
+public class AnnotatedCommandMediator extends POJOCommandMediator {
+
+    protected Map<Field, AXIOMXPath> beforeFields;
+    protected Map<Method, AXIOMXPath> beforeMethods;
+    protected Map<Field, AXIOMXPath> afterFields;
+    protected Map<Method, AXIOMXPath> afterMethods;
+    
+    @Override
+    public boolean mediate(MessageContext synCtx) {
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : POJOCommand mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx.getEnvelope());
+            }
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Creating a new instance of POJO class : " + getCommand().getClass());
+        }
+
+        Object commandObject = null;
+        try {
+            // instantiate a new command object each time
+            commandObject = getCommand().newInstance();
+        } catch (Exception e) {
+            handleException("Error creating an instance of the POJO command class : " +
+                            getCommand().getClass(), e, synCtx);
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Instance created, setting static and dynamic properties");
+        }
+
+        // then set the static/constant properties first
+        for (Iterator iter = getStaticProps().keySet().iterator(); iter.hasNext(); ) {
+            String name = (String) iter.next();
+            setInstanceProperty(name, (String) getStaticProps().get(name), commandObject,
synCtx);
+        }
+        
+        
+        for (Field f : beforeFields.keySet()) {
+            AXIOMXPath xpath = beforeFields.get(f);
+            Object v;
+            if (f.getType().equals(String.class)) {
+                v = Axis2MessageContext.getStringValue(xpath, synCtx);
+            } else {
+                throw new UnsupportedOperationException("non-String types not supportted
yet");
+            }
+            try {
+                f.set(commandObject, v);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        for (Method m : beforeMethods.keySet()) {
+            AXIOMXPath xpath = beforeMethods.get(m);
+            Object v;
+            if (m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(String.class))
{
+                v = Axis2MessageContext.getStringValue(xpath, synCtx);
+            } else {
+                throw new UnsupportedOperationException("non-String types not supportted
yet");
+            }
+            try {
+                m.invoke(commandObject, v);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "POJO initialized successfully, invoking the execute()
method");
+        }
+
+        // then call the execute method if the Command interface is implemented
+        if (commandObject instanceof Command) {
+            try {
+                ((Command) commandObject).execute();
+            } catch (Exception e) {
+                handleException("Error invoking POJO command class : "
+                    + getCommand().getClass(), e, synCtx);
+            }
+
+        } else {
+
+            Method exeMethod = null;
+            try {
+                exeMethod = getCommand().getMethod("execute", new Class[]{});
+                exeMethod.invoke(commandObject, new Object[]{});
+            } catch (NoSuchMethodException e) {
+                handleException("Cannot locate an execute() method on POJO class : " +
+                                getCommand().getClass(), e, synCtx);
+            } catch (Exception e) {
+                handleException("Error invoking the execute() method on POJO class : " +
+                                getCommand().getClass(), e, synCtx);
+            }
+        }
+
+        // TODO: now update the MessageContext from the commandObject
+        
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : POJOCommand mediator");
+        }
+        return true;
+    }
+    
+    @Override
+    public void setCommand(Class commandClass) {
+        super.setCommand(commandClass);
+        introspectClass(commandClass);
+    }
+
+    /**
+     * Introspect the command class annotations
+     */
+    protected void introspectClass(Class<?> commandClass) {
+
+        beforeFields = new HashMap<Field, AXIOMXPath>();
+        afterFields = new HashMap<Field, AXIOMXPath>();
+        beforeMethods = new HashMap<Method, AXIOMXPath>();
+        afterMethods = new HashMap<Method, AXIOMXPath>();
+
+        for (Field f : commandClass.getDeclaredFields()) {
+
+            ReadFromMessage readFromMessage = f.getAnnotation(ReadFromMessage.class);
+            if (readFromMessage != null) {
+                AXIOMXPath axiomXpath = createAxiomXPATH(readFromMessage.value(), f.getAnnotation(Namespaces.class));
+                beforeFields.put(f, axiomXpath);
+            }
+
+            UpdateMessage updateMessage = f.getAnnotation(UpdateMessage.class);
+            if (updateMessage != null) {
+                AXIOMXPath axiomXpath = createAxiomXPATH(updateMessage.value(), f.getAnnotation(Namespaces.class));
+                afterFields.put(f, axiomXpath);
+            }
+
+            ReadAndUpdate readAndUpdate = f.getAnnotation(ReadAndUpdate.class);
+            if (readAndUpdate != null) {
+                AXIOMXPath axiomXpath = createAxiomXPATH(readAndUpdate.value(), f.getAnnotation(Namespaces.class));
+                beforeFields.put(f, axiomXpath);
+                afterFields.put(f, axiomXpath);
+            }
+        }
+
+        for (Method m : commandClass.getDeclaredMethods()) {
+
+            ReadFromMessage readFromMessage = m.getAnnotation(ReadFromMessage.class);
+            if (readFromMessage != null) {
+                AXIOMXPath axiomXpath = createAxiomXPATH(readFromMessage.value(), m.getAnnotation(Namespaces.class));
+                beforeMethods.put(m, axiomXpath);
+            }
+
+            UpdateMessage updateMessage = m.getAnnotation(UpdateMessage.class);
+            if (updateMessage != null) {
+                AXIOMXPath axiomXpath = createAxiomXPATH(updateMessage.value(), m.getAnnotation(Namespaces.class));
+                afterMethods.put(m, axiomXpath);
+            }
+
+        }
+    }
+
+    /**
+     * Create an AXIOMXPath from an xpath string 
+     */
+    protected AXIOMXPath createAxiomXPATH(String xpath, Namespaces nsAnnotation) {
+        
+        Map<String, String> namespaces = getNamespaces(nsAnnotation);     
+        try {
+
+            AXIOMXPath axiomXPath = new AXIOMXPath(xpath);
+            
+            for (String prefix : namespaces.keySet()) {
+                axiomXPath.addNamespace(prefix, namespaces.get(prefix));
+            }
+            
+            return axiomXPath;
+
+        } catch (JaxenException e) {
+            throw new RuntimeException("Error creating AXIOMXPath: " + xpath, e);
+        }
+    }
+
+    /**
+     * Creates a Map of namespace prefixes and namespaces from a Namespace annotation
+     * and the default Namespace annotation on the command class.
+     */
+    protected Map<String, String> getNamespaces(Namespaces namespaces) {
+        Map<String, String> allNamespaces = new HashMap<String, String>();
+        
+        Namespaces defaultNamespaces = ((Class<?>)getCommand()).getAnnotation(Namespaces.class);
+
+        // First add any default namespaces
+        if (defaultNamespaces != null) {
+            if (defaultNamespaces.value()[0].length()>0) {
+                allNamespaces.put(defaultNamespaces.value()[0], defaultNamespaces.value()[1]);
+            }
+            if (defaultNamespaces.ns() != null && defaultNamespaces.ns().length()
> 0) {
+                allNamespaces.put("ns", defaultNamespaces.ns());
+            }
+            if (defaultNamespaces.ns1() != null && defaultNamespaces.ns1().length()
> 0) {
+                allNamespaces.put("ns1", defaultNamespaces.ns());
+            }
+            if (defaultNamespaces.ns2() != null && defaultNamespaces.ns2().length()
> 0) {
+                allNamespaces.put("ns2", defaultNamespaces.ns());
+            }
+            if (defaultNamespaces.ns3() != null && defaultNamespaces.ns3().length()
> 0) {
+                allNamespaces.put("ns3", defaultNamespaces.ns());
+            }
+            if (defaultNamespaces.ns4() != null && defaultNamespaces.ns4().length()
> 0) {
+                allNamespaces.put("ns4", defaultNamespaces.ns());
+            }
+            if (defaultNamespaces.ns5() != null && defaultNamespaces.ns5().length()
> 0) {
+                allNamespaces.put("ns5", defaultNamespaces.ns());
+            }
+        }
+
+        // add any namespaces which will overwrite any previously added default namespaces
+        if (namespaces != null) {
+            if (namespaces.value()[0].length()>0) {
+                allNamespaces.put(namespaces.value()[0], namespaces.value()[1]);
+            }
+            if (namespaces.ns() != null && namespaces.ns().length() > 0) {
+                allNamespaces.put("ns", namespaces.ns());
+            }
+            if (namespaces.ns1() != null && namespaces.ns1().length() > 0) {
+                allNamespaces.put("ns1", namespaces.ns());
+            }
+            if (namespaces.ns2() != null && namespaces.ns2().length() > 0) {
+                allNamespaces.put("ns2", namespaces.ns());
+            }
+            if (namespaces.ns3() != null && namespaces.ns3().length() > 0) {
+                allNamespaces.put("ns3", namespaces.ns());
+            }
+            if (namespaces.ns4() != null && namespaces.ns4().length() > 0) {
+                allNamespaces.put("ns4", namespaces.ns());
+            }
+            if (namespaces.ns5() != null && namespaces.ns5().length() > 0) {
+                allNamespaces.put("ns5", namespaces.ns());
+            }
+        }
+        return allNamespaces;
+    }
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java?rev=600517&r1=600516&r2=600517&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java
(original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java
Mon Dec  3 05:10:22 2007
@@ -19,14 +19,16 @@
 
 package org.apache.synapse.mediators.ext;
 
-import org.apache.synapse.mediators.AbstractMediator;
-import org.apache.synapse.*;
-import org.apache.synapse.core.axis2.Axis2MessageContext;
-import org.apache.axiom.om.xpath.AXIOMXPath;
-
-import java.util.*;
 import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.synapse.Command;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
 
 /**
  * This mediator will use the specified command object and execute the command after setting
@@ -150,7 +152,7 @@
      * @param obj POJO instance
      * @param synCtx current message
      */
-    private void setInstanceProperty(String name, String value, Object obj, MessageContext
synCtx) {
+    protected void setInstanceProperty(String name, String value, Object obj, MessageContext
synCtx) {
 
         String mName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
         Method method = null;

Added: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,47 @@
+/*
+ *  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.synapse.mediators.ext;
+
+import org.apache.synapse.mediators.annotations.Namespaces;
+import org.apache.synapse.mediators.annotations.ReadAndUpdate;
+import org.apache.synapse.mediators.annotations.ReadFromMessage;
+import org.apache.synapse.mediators.annotations.UpdateMessage;
+
+@Namespaces({"myns", "http://myns"})
+public class AnnotatedCommand {
+
+    @ReadFromMessage("/beforeField")
+    String beforeField;
+
+    @UpdateMessage("/afterField")
+    String afterField;
+
+    @ReadAndUpdate("/@ReadAndUpdateField")
+    String ReadAndUpdateField;
+    
+    @Namespaces(ns="http://ns")
+    @UpdateMessage("/afterField")
+    String nsTest1;
+
+    @Namespaces({"xns", "http://xns"})
+    @UpdateMessage("/afterField")
+    String nsTest2;
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,46 @@
+/*
+ *  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.synapse.mediators.ext;
+
+import org.apache.synapse.mediators.annotations.Namespaces;
+import org.apache.synapse.mediators.annotations.ReadFromMessage;
+
+@Namespaces({"soapenv", "http://schemas.xmlsoap.org/soap/envelope/"})
+public class AnnotatedCommand2 {
+
+    static String fieldResult;
+    static String methodResult;
+    
+    @Namespaces({"m", "http://services.samples/xsd"})
+    @ReadFromMessage("/soapenv:Envelope/soapenv:Body/m:getQuote/m:request/m:symbol")
+    String beforeField;
+
+    @Namespaces({"m", "http://services.samples/xsd"})
+    @ReadFromMessage("/soapenv:Envelope/soapenv:Body/m:getQuote/m:request/m:symbol")
+    public void setSymbol(String s) {
+        methodResult = s;
+    }
+    
+    public void execute() {
+        System.out.println("beforeField: " + beforeField);
+        fieldResult = beforeField.toString();    
+    }
+
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommand2.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java?rev=600517&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
Mon Dec  3 05:10:22 2007
@@ -0,0 +1,82 @@
+/*
+ *  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.synapse.mediators.ext;
+
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.xml.MediatorFactoryFinder;
+import org.apache.synapse.mediators.AbstractMediatorTestCase;
+import org.apache.synapse.mediators.TestUtils;
+
+/**
+ */
+public class AnnotatedCommandMediatorTest extends AbstractMediatorTestCase {
+
+    public void testAnnotations() throws Exception {
+        AnnotatedCommandMediator m = new AnnotatedCommandMediator();
+        m.setCommand(AnnotatedCommand.class);
+
+        assertEquals(2, m.beforeFields.size());
+        assertEquals(4, m.afterFields.size());
+
+        assertTrue(m.beforeFields.containsKey(AnnotatedCommand.class.getDeclaredField("beforeField")));
+        assertTrue(m.beforeFields.containsKey(AnnotatedCommand.class.getDeclaredField("ReadAndUpdateField")));
+        assertTrue(m.afterFields.containsKey(AnnotatedCommand.class.getDeclaredField("afterField")));
+        assertTrue(m.afterFields.containsKey(AnnotatedCommand.class.getDeclaredField("ReadAndUpdateField")));
+    }
+
+    public void testNamspaces() throws Exception {
+        AnnotatedCommandMediator m = new AnnotatedCommandMediator();
+        m.setCommand(AnnotatedCommand.class);
+
+        AXIOMXPath ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("ReadAndUpdateField"));
+        assertEquals(1, ax.getNamespaces().size());
+        assertEquals("http://myns", ax.getNamespaces().values().iterator().next());
+        assertEquals("myns", ax.getNamespaces().keySet().iterator().next());
+
+        ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("nsTest1"));
+        assertEquals(2, ax.getNamespaces().size());
+        assertTrue(ax.getNamespaces().keySet().contains("myns"));
+        assertTrue(ax.getNamespaces().keySet().contains("ns"));
+        assertTrue(ax.getNamespaces().values().contains("http://myns"));
+        assertTrue(ax.getNamespaces().values().contains("http://ns"));
+
+        ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("nsTest2"));
+        assertEquals(2, ax.getNamespaces().size());
+        assertTrue(ax.getNamespaces().keySet().contains("myns"));
+        assertTrue(ax.getNamespaces().keySet().contains("xns"));
+        assertTrue(ax.getNamespaces().values().contains("http://myns"));
+        assertTrue(ax.getNamespaces().values().contains("http://xns"));
+    }
+
+    public void testBasicExecute() throws Exception {
+        AnnotatedCommandMediator m = new AnnotatedCommandMediator();
+        m.setCommand(AnnotatedCommand.class);
+
+        Mediator pcm = MediatorFactoryFinder.getInstance().getMediator(createOMElement(
+           "<annotatedCommand name='org.apache.synapse.mediators.ext.AnnotatedCommand2'
xmlns='http://ws.apache.org/ns/synapse'/>"));
+
+        MessageContext mc = TestUtils.getTestContext("<m:getQuote xmlns:m=\"http://services.samples/xsd\"><m:request><m:symbol>IBM</m:symbol></m:request></m:getQuote>");
+        pcm.mediate(mc);
+        assertEquals("IBM", AnnotatedCommand2.fieldResult);
+        assertEquals("IBM", AnnotatedCommand2.methodResult);
+    }
+}

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/ext/AnnotatedCommandMediatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org


Mime
View raw message