Author: jbonofre
Date: Fri May  1 06:22:24 2009
New Revision: 770546

URL: http://svn.apache.org/viewvc?rev=770546&view=rev
Log:
[SMXCOMP-508] The user can now provided its own implementation of the 
ExecMarshalerSupport interface to constructs the exec command using the in 
normalized message.

Added:
    
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/
    
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
   (with props)
    
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
   (with props)
    
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/
    
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java
   (with props)
Removed:
    
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecMarshaler.java
Modified:
    
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java

Modified: 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java?rev=770546&r1=770545&r2=770546&view=diff
==============================================================================
--- 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
 (original)
+++ 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
 Fri May  1 06:22:24 2009
@@ -23,7 +23,8 @@
 import javax.jbi.messaging.NormalizedMessage;
 
 import org.apache.servicemix.common.endpoints.ProviderEndpoint;
-import org.apache.servicemix.exec.utils.ExecMarshaler;
+import org.apache.servicemix.exec.marshaler.DefaultExecMarshaler;
+import org.apache.servicemix.exec.marshaler.ExecMarshalerSupport;
 import org.apache.servicemix.exec.utils.ExecUtils;
 import org.apache.servicemix.jbi.jaxp.StringSource;
 
@@ -37,15 +38,49 @@
 
     private String command; // the command can be static (define in the
                             // descriptor) or provided in the incoming message
+    private ExecMarshalerSupport marshaler = new DefaultExecMarshaler(); // 
the marshaler that parse the in message
 
     public String getCommand() {
         return command;
     }
 
+    /**
+     * <p>
+     * This attribute specifies the default command to use if no is provided
+     * in the incoming message.
+     * </p>
+     * <i>&nbsp;&nbsp;&nbsp;The default value is <code>null</code>. 
+     * 
+     * @param command
+     */
     public void setCommand(String command) {
         this.command = command;
     }
+    
+    public ExecMarshalerSupport getMarshaler() {
+        return marshaler;
+    }
+    
+    /**     
+     * <p>
+     * With this method you can specify a marshaler class which provides the
+     * logic for converting a message into a execution command. This class
+     * has to implement the interface class <code>ExecMarshalerSupport</code>. 
+     * If you don't specify a marshaler, the <code>DefaultExecMarshaler</code> 
+     * will be used.
+     * </p>
+     * 
+     * @param marshaler a <code>ExecMarshalerSupport</code> class representing 
the
+     *            marshaler.
+     */
+    public void setMarshaler(ExecMarshalerSupport marshaler) {
+        this.marshaler = marshaler;
+    }
 
+    /*
+     * (non-Javadoc)
+     * @see 
org.apache.servicemix.common.endpoints.ProviderEndpoint#process(javax.jbi.messaging.MessageExchange)
+     */
     @Override
     public void process(MessageExchange exchange) throws Exception {
         // The component acts as a provider, this means that another component
@@ -84,7 +119,7 @@
             // gets the in message
             NormalizedMessage in = exchange.getMessage("in");
             // parses the in message and get the execution command
-            String exec = ExecMarshaler.constructExecCommand(in);
+            String exec = marshaler.constructExecCommand(in);
             if (exec == null || exec.trim().length() < 1) {
                 exec = command;
             }

Added: 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java?rev=770546&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
 (added)
+++ 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
 Fri May  1 06:22:24 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.exec.marshaler;
+
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.TransformerException;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+/**
+ * Default exec marshaler.
+ * 
+ * @author jbonofre
+ */
+public class DefaultExecMarshaler implements ExecMarshalerSupport {
+    
+    private static final String TAG_COMMAND = "command";
+    private static final String TAG_ARGUMENT = "argument";
+    
+    /*
+     * (non-Javadoc)
+     * @see 
org.apache.servicemix.exec.marshaler.ExecMarshalerSupport#constructExecCommand(javax.jbi.messaging.NormalizedMessage)
+     */
+    public String constructExecCommand(NormalizedMessage message) throws 
TransformerException {
+        String execString = null;
+        // create a source transformer
+        SourceTransformer transformer = new SourceTransformer();
+        try {
+            // transform the message content into a DOM document
+            Document document = transformer.toDOMDocument(message);
+            document.getDocumentElement().normalize();
+            
+            // get the command node
+            NodeList commandNode = document.getElementsByTagName(TAG_COMMAND);
+            if (commandNode != null && commandNode.getLength() > 1) {
+                throw new TransformerException("Invalid message content. Only 
one command tag is supported.");
+            }
+            if (commandNode != null && commandNode.item(0) != null) {
+                execString = 
commandNode.item(0).getChildNodes().item(0).getNodeValue();
+            }
+            
+            // get the argument nodes
+            NodeList argumentNodes = 
document.getElementsByTagName(TAG_ARGUMENT);
+            for (int i = 0; i < argumentNodes.getLength(); i++) {
+                execString = execString + " " + 
argumentNodes.item(i).getChildNodes().item(0).getNodeValue();
+            }
+            
+        } catch (Exception e) {
+            throw new TransformerException(e);
+        }
+        return execString;
+    }
+
+}

Propchange: 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java?rev=770546&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
 (added)
+++ 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
 Fri May  1 06:22:24 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.exec.marshaler;
+
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.TransformerException;
+
+/**
+ * This interface describes the behavior of an exec marshaler.
+ * 
+ * @author jbonofre
+ */
+public interface ExecMarshalerSupport {
+    
+    /**
+     * <p>
+     * Parses the content of the <code>NormalizedMessage</code>, extracts 
command and arguments
+     * to constructs the execution command.
+     * </p>
+     * 
+     * @param message the <code>NormalizedMessage</code>.
+     * @return the execution command.
+     * @throws TransformerException in case of error during command 
construction.
+     */
+    public String constructExecCommand(NormalizedMessage message) throws 
TransformerException;
+
+}

Propchange: 
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java?rev=770546&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java
 (added)
+++ 
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java
 Fri May  1 06:22:24 2009
@@ -0,0 +1,74 @@
+/*
+ * 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.exec.marshaler;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessageExchangeFactory;
+import javax.jbi.messaging.NormalizedMessage;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.id.IdGenerator;
+import org.apache.servicemix.jbi.helper.MessageExchangePattern;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.messaging.MessageExchangeFactoryImpl;
+
+/**
+ * Unit tests on the default exec marshaler.
+ * 
+ * @author jbonofre
+ */
+public class DefaultExecMarshalerTest extends TestCase {
+    
+    private static final String COMMAND = "ls";
+    private static final String FIRST_ARG = "-lt";
+    private static final String SECOND_ARG = "/tmp";
+    
+    private static final String MSG_VALID = "<message>"
+        + "<command>" + COMMAND + "</command>"
+        + "<arguments>"
+        + "<argument>" + FIRST_ARG + "</argument>"
+        + "<argument>" + SECOND_ARG + "</argument>"
+        + "</arguments>"
+        + "</message>";
+    
+    private ExecMarshalerSupport marshaler;
+    private MessageExchangeFactory factory;
+    
+    /*
+     * (non-Javadoc)
+     * @see junit.framework.TestCase#setUp()
+     */
+    public void setUp() throws Exception {
+        this.marshaler = new DefaultExecMarshaler();
+        this.factory = new MessageExchangeFactoryImpl(new IdGenerator(), new 
AtomicBoolean(false));
+    }
+    
+    public void testValidMessage() throws Exception {
+        MessageExchange exchange = 
this.factory.createExchange(MessageExchangePattern.IN_ONLY);
+        NormalizedMessage message = exchange.createMessage();
+        message.setContent(new StringSource(MSG_VALID));
+        exchange.setMessage(message, "in");
+        
+        String execCommand = marshaler.constructExecCommand(message);
+        
+        assertEquals("ls -lt /tmp", execCommand);
+    }
+
+}

Propchange: 
servicemix/components/engines/servicemix-exec/trunk/src/test/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshalerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to