You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by js...@apache.org on 2006/10/06 21:28:11 UTC

svn commit: r453735 - in /incubator/servicemix/trunk/servicemix-file/src: main/java/org/apache/servicemix/file/ test/java/org/apache/servicemix/file/ test/resources/

Author: jstrachan
Date: Fri Oct  6 12:28:10 2006
New Revision: 453735

URL: http://svn.apache.org/viewvc?view=rev&rev=453735
Log:
added the polling endpoint for the servicemix-file module, making servicemix-file as capable as the previous lightweight component - though we could do much better than this :)

Added:
    incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java   (with props)
    incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html   (with props)
    incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java   (with props)
    incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml   (with props)
Modified:
    incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java
    incubator/servicemix/trunk/servicemix-file/src/test/resources/log4j.properties

Modified: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java?view=diff&rev=453735&r1=453734&r2=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java (original)
+++ incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java Fri Oct  6 12:28:10 2006
@@ -42,7 +42,7 @@
 
 
     protected Class[] getEndpointClasses() {
-        return new Class[]{FileEndpoint.class};
+        return new Class[]{FileEndpoint.class, FilePollEndpoint.class};
     }
 
     public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {

Added: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java?view=auto&rev=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java (added)
+++ incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java Fri Oct  6 12:28:10 2006
@@ -0,0 +1,214 @@
+/**
+ *
+ * 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.file;
+
+import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.common.PollingEndpoint;
+import org.apache.servicemix.components.util.DefaultFileMarshaler;
+import org.apache.servicemix.components.util.FileMarshaler;
+
+import javax.jbi.management.DeploymentException;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.NormalizedMessage;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Set;
+
+/**
+ * A polling endpoint which looks for a file or files in a directory
+ * and sends the files into the JBI bus as messages, deleting the files
+ * by default when they are processed.
+ *
+ * @org.apache.xbean.XBean element="poller"
+ *
+ * @version $Revision$
+ */
+public class FilePollEndpoint extends PollingEndpoint {
+    private static final Log log = LogFactory.getLog(FilePollEndpoint.class);
+
+    private File file;
+    private FileFilter filter;
+    private boolean deleteFile = true;
+    private boolean recursive = true;
+    private boolean autoCreateDirectory = true;
+    private FileMarshaler marshaler = new DefaultFileMarshaler();
+    private Set workingSet = new CopyOnWriteArraySet();
+
+    public void poll() throws Exception {
+        pollFileOrDirectory(file);
+    }
+
+    public void validate() throws DeploymentException {
+        super.validate();
+        if (file == null) {
+            throw new DeploymentException("You must specify a file property");
+        }
+        if (isAutoCreateDirectory() && !file.exists()) {
+            file.mkdirs();
+        }
+    }
+
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public File getFile() {
+        return file;
+    }
+
+    /**
+     * Sets the file to poll, which can be a directory or a file.
+     *
+     * @param file
+     */
+    public void setFile(File file) {
+        this.file = file;
+    }
+
+    public FileFilter getFilter() {
+        return filter;
+    }
+
+    /**
+     * Sets the optional filter to choose which files to process
+     */
+    public void setFilter(FileFilter filter) {
+        this.filter = filter;
+    }
+
+    /**
+     * Returns whether or not we should delete the file when its processed
+     */
+    public boolean isDeleteFile() {
+        return deleteFile;
+    }
+
+    public void setDeleteFile(boolean deleteFile) {
+        this.deleteFile = deleteFile;
+    }
+
+    public boolean isRecursive() {
+        return recursive;
+    }
+
+    public void setRecursive(boolean recursive) {
+        this.recursive = recursive;
+    }
+
+    public boolean isAutoCreateDirectory() {
+        return autoCreateDirectory;
+    }
+
+    public void setAutoCreateDirectory(boolean autoCreateDirectory) {
+        this.autoCreateDirectory = autoCreateDirectory;
+    }
+
+    public FileMarshaler getMarshaler() {
+        return marshaler;
+    }
+
+    public void setMarshaler(FileMarshaler marshaler) {
+        this.marshaler = marshaler;
+    }
+
+    /**
+     * The set of FTPFiles that this component is currently working on
+     *
+     * @return
+     */
+    public Set getWorkingSet() {
+        return workingSet;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+
+    protected void pollFileOrDirectory(File fileOrDirectory) {
+        pollFileOrDirectory(fileOrDirectory, true);
+    }
+
+    protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) {
+        if (!fileOrDirectory.isDirectory()) {
+            pollFile(fileOrDirectory); // process the file
+        }
+        else if (processDir) {
+            logger.debug("Polling directory " + fileOrDirectory);
+            File[] files = fileOrDirectory.listFiles(getFilter());
+            for (int i = 0; i < files.length; i++) {
+                pollFileOrDirectory(files[i], isRecursive()); // self-recursion
+            }
+        }
+        else {
+            logger.debug("Skipping directory " + fileOrDirectory);
+        }
+    }
+
+    protected void pollFile(final File aFile) {
+        if (workingSet.add(aFile)) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Scheduling file " + aFile + " for processing");
+            }
+            getExecutor().execute(new Runnable() {
+                public void run() {
+                    try {
+                        processFileAndDelete(aFile);
+                    }
+                    finally {
+                        workingSet.remove(aFile);
+                    }
+                }
+            });
+        }
+    }
+
+    protected void processFileAndDelete(File aFile) {
+        try {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Processing file " + aFile);
+            }
+            if (aFile.exists()) {
+                processFile(aFile);
+                if (isDeleteFile()) {
+                    if (!aFile.delete()) {
+                        throw new IOException("Could not delete file " + aFile);
+                    }
+                }
+            }
+        }
+        catch (Exception e) {
+            logger.error("Failed to process file: " + aFile + ". Reason: " + e, e);
+        }
+    }
+
+    protected void processFile(File aFile) throws Exception {
+        String name = aFile.getCanonicalPath();
+        InputStream in = new BufferedInputStream(new FileInputStream(aFile));
+        InOnly exchange = getExchangeFactory().createInOnlyExchange();
+        NormalizedMessage message = exchange.createMessage();
+        exchange.setInMessage(message);
+        marshaler.readMessage(exchange, message, in, name);
+        getDeliveryChannel().sendSync(exchange);
+        in.close();
+    }
+}

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html?view=auto&rev=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html (added)
+++ incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html Fri Oct  6 12:28:10 2006
@@ -0,0 +1,27 @@
+<!--
+
+    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.
+
+-->
+<html>
+<head>
+</head>
+<body>
+
+A component for working with files such as writing to a directory or polling a directory for files.
+
+</body>
+</html>

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/package.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Added: incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java?view=auto&rev=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java (added)
+++ incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java Fri Oct  6 12:28:10 2006
@@ -0,0 +1,77 @@
+/**
+ *
+ * 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.file;
+
+import org.apache.servicemix.tck.SpringTestSupport;
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+import java.io.File;
+import java.io.FileWriter;
+
+public class PollDirectoryTest extends SpringTestSupport {
+    protected String directoryName = "target/pollDirectory";
+    protected String dynamicURI = "file:" + directoryName;
+
+
+    public void testSendToWriterSoItCanBePolled() throws Exception {
+        // now lets make a request on this endpoint
+        DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
+
+        // lets send a request to be written to a file
+        // which should then be polled
+        InOnly me = client.createInOnlyExchange();
+        me.setService(new QName("urn:test", "service"));
+        NormalizedMessage message = me.getInMessage();
+
+        message.setProperty("name", "cheese");
+        message.setContent(new StringSource("<hello>world</hello>"));
+
+        client.sendSync(me);
+
+
+        Thread.sleep(5000);
+    }
+
+    protected void assertExchangeWorked(MessageExchange me) throws Exception {
+        if (me.getStatus() == ExchangeStatus.ERROR) {
+            if (me.getError() != null) {
+                throw me.getError();
+            }
+            else {
+                fail("Received ERROR status");
+            }
+        }
+        else if (me.getFault() != null) {
+            fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
+        }
+    }
+
+    protected AbstractXmlApplicationContext createBeanFactory() {
+        return new ClassPathXmlApplicationContext("spring-polling.xml");
+    }
+
+}

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/servicemix/trunk/servicemix-file/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/resources/log4j.properties?view=diff&rev=453735&r1=453734&r2=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/test/resources/log4j.properties (original)
+++ incubator/servicemix/trunk/servicemix-file/src/test/resources/log4j.properties Fri Oct  6 12:28:10 2006
@@ -26,6 +26,7 @@
 log4j.logger.org.springframework=INFO
 log4j.logger.org.apache.activemq=INFO
 log4j.logger.org.apache.activemq.spring=WARN
+log4j.logger.org.apache.servicemix=DEBUG
 
 # CONSOLE appender 
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender

Added: incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml?view=auto&rev=453735
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml (added)
+++ incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml Fri Oct  6 12:28:10 2006
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns:sm="http://servicemix.apache.org/config/1.0" 
+	     xmlns:file="http://servicemix.apache.org/file/1.0"
+       xmlns:test="urn:test">
+
+  <sm:container id="jbi" embedded="true" createMBeanServer="false">
+    
+    <sm:activationSpecs>
+
+      <sm:activationSpec>
+      	<sm:component>
+            <file:component>
+            	<file:endpoints>
+
+                <file:endpoint service="test:service" endpoint="endpoint" directory="file:target/componentOutput"/>
+
+
+                <file:poller service="test:poller" endpoint="poller" file="file:target/pollerFiles"/>
+            	</file:endpoints>
+            </file:component>
+        </sm:component>
+      </sm:activationSpec>
+
+    </sm:activationSpecs>
+  </sm:container>
+
+</beans>

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml