You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/05/21 22:44:19 UTC

svn commit: r658857 - in /servicemix/smx3/branches/servicemix-3.2: core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/ core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/ core/servicemix-core/src/main/java/or...

Author: gertv
Date: Wed May 21 13:44:18 2008
New Revision: 658857

URL: http://svn.apache.org/viewvc?rev=658857&view=rev
Log:
SM-1096: File-based message auditor

Added:
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditorStrategy.java
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/FileAuditorTest.java
Modified:
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/FileUtil.java
    servicemix/smx3/branches/servicemix-3.2/deployables/sharedlibraries/servicemix-shared-compat/   (props changed)
    servicemix/smx3/branches/servicemix-3.2/samples/camel/camel-sa/   (props changed)
    servicemix/smx3/branches/servicemix-3.2/samples/camel/camel-sa-itest/   (props changed)
    servicemix/smx3/branches/servicemix-3.2/samples/cxf-wsdl-first/wsdl-first-cxfbc-su/   (props changed)

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java?rev=658857&r1=658856&r2=658857&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java Wed May 21 13:44:18 2008
@@ -22,6 +22,10 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.MessageExchange;
@@ -33,26 +37,29 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.components.util.FileMarshaler;
 import org.apache.servicemix.jbi.audit.AbstractAuditor;
 import org.apache.servicemix.jbi.audit.AuditorException;
 import org.apache.servicemix.jbi.event.ExchangeEvent;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.util.FileUtil;
 import org.apache.servicemix.jbi.util.MessageUtil;
+import org.springframework.beans.factory.InitializingBean;
 
 /**
  * Simple implementation of a ServiceMix auditor that stores messages in files in a directory.
+ * Shows usage of {@link TeeInputStream} for auditing {@link StreamSource} message content. 
  * 
- * Shows usage of {@link TeeInputStream} for auditing {@link StreamSource} message content 
+ * Currently, the file auditor will only store the message body for ACTIVE exchanges.
  * 
  * @org.apache.xbean.XBean element="fileAuditor" description="The Auditor of message exchanges to a directory"
- * 
- * @author Gert Vanthienen (gertv)
- * @since 3.2
  */
-public class FileAuditor extends AbstractAuditor {
+public class FileAuditor extends AbstractAuditor implements InitializingBean {
 
     private static final Log LOG = LogFactory.getLog(FileAuditor.class);
     private File directory;
+    private FileAuditorStrategy strategy = new FileAuditorStrategyImpl();
+    private boolean autostart = true;
 
     /**
      * The directory used for storing the audited messages
@@ -68,12 +75,16 @@
         this.directory = directory;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void exchangeSent(ExchangeEvent event) {
         try {
             MessageExchange exchange = event.getExchange();
             if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
                 OutputStream os = getOutputStream(exchange);
-                NormalizedMessage in = exchange.getMessage("in");
+                writeFileHeader(os, exchange);
+                NormalizedMessage in = exchange.getMessage("in");                
                 if (StreamSource.class.isAssignableFrom(in.getContent().getClass())) {
                     StreamSource original = (StreamSource) exchange.getMessage("in").getContent();
                     TeeInputStream tis = new TeeInputStream(original.getInputStream(), os);
@@ -93,30 +104,34 @@
         }
     }
 
-    private OutputStream getOutputStream(MessageExchange exchange) throws FileNotFoundException {
-        String name = getNameForId(exchange.getExchangeId());
-        return new BufferedOutputStream(new FileOutputStream(new File(directory, name)));
+    private void writeFileHeader(OutputStream os, MessageExchange exchange) {
+        MessageExchangeWriter writer = new MessageExchangeWriter(os);
+        writer.writeMessageExchange(exchange);
+        writer.println(); 
+        writer.println("-- Normalized message (in) --");
+        writer.writeNormalizedMessage(exchange.getMessage("in"));
+        writer.flush();
     }
 
-    private String getNameForId(String id) {
-        return id.replaceAll("[:\\.]", "_");
+    /*
+     * Get the outputstream for writing the message content
+     */
+    private OutputStream getOutputStream(MessageExchange exchange) throws FileNotFoundException {
+        File file = new File(directory, strategy.getFileName(exchange));
+        if (!file.getParentFile().exists()) {
+            file.getParentFile().mkdirs();
+        }
+        return new BufferedOutputStream(new FileOutputStream(file));
     }
 
     @Override
     public int deleteExchangesByIds(String[] ids) throws AuditorException {
-        int count = 0;
-        for (String id : ids) {
-            File file = new File(directory, getNameForId(id));
-            if (file.delete()) {
-                count++;
-            }
-        }
-        return count;
+        throw new AuditorException("deleteExchangesById(s) currently unsupported by FileAuditor");
     }
 
     @Override
     public int getExchangeCount() throws AuditorException {
-        return directory.listFiles().length;
+        return FileUtil.countFilesInDirectory(directory);
     }
 
     @Override
@@ -129,7 +144,62 @@
         throw new AuditorException("getExchangeByIds currently unsupported by FileAuditor");
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public String getDescription() {
-        return "A file-based auditor implementation: archives files to a specified target directory";
+        return "File-based auditing service";
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        init(getContainer());
+        if (autostart) {
+            start();
+        } else {
+            stop();
+        }
+    }
+    
+    /*
+     * Convenience PrintWriter implementation
+     */
+    private class MessageExchangeWriter extends PrintWriter {
+      
+        private MessageExchangeWriter(OutputStream os) {
+            super(os);
+        }
+        
+        private void writeMessageExchange(MessageExchange exchange) {
+            println("-- Exchange " + exchange.getExchangeId() + " --");
+            writeProperty("endpoint", exchange.getEndpoint());
+            writeProperty("MEP", exchange.getPattern());
+            for (Object key : exchange.getPropertyNames()) {
+                writeProperty(key, exchange.getProperty(key.toString()));
+            }
+        }
+        
+        private void writeNormalizedMessage(NormalizedMessage message) {
+            for (Object key : message.getPropertyNames()) {
+                writeProperty(key, message.getProperty(key.toString()));
+            }
+            println();println("- content -");
+        }
+
+        
+        private void writeProperty(Object key, Object value) {
+            println(String.format(" %s : %s", key, value));
+        }
+    }
+    
+    /*
+     * Default FileAuditorStrategy implementation, writing audit files in a folder per day
+     */
+    private class FileAuditorStrategyImpl implements FileAuditorStrategy {
+        
+        private final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");
+        
+        public String getFileName(MessageExchange exchange) {
+            return DATEFORMAT.format(new Date()) + File.separatorChar + exchange.getExchangeId().replaceAll("[:\\.]", "_");
+        }
     }
 }

Added: servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditorStrategy.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditorStrategy.java?rev=658857&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditorStrategy.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditorStrategy.java Wed May 21 13:44:18 2008
@@ -0,0 +1,31 @@
+/*
+ * 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.jbi.audit.file;
+
+import javax.jbi.messaging.MessageExchange;
+
+/**
+ * Interface that defines the name of the file written by a {@link FileAuditor} 
+ */
+public interface FileAuditorStrategy {
+    
+    /**
+     * Get the file name for writing the given {@link MessageExchange}
+     */
+    public String getFileName(MessageExchange exchange);
+
+}

Added: servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/FileAuditorTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/FileAuditorTest.java?rev=658857&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/FileAuditorTest.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/FileAuditorTest.java Wed May 21 13:44:18 2008
@@ -0,0 +1,80 @@
+/*
+ * 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.jbi.audit.file;
+
+import java.io.File;
+import java.util.Locale;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchange;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.audit.file.FileAuditor;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.util.FileUtil;
+import org.apache.servicemix.tck.ReceiverComponent;
+import org.apache.servicemix.tck.SenderComponent;
+import org.hsqldb.jdbc.jdbcDataSource;
+
+public class FileAuditorTest extends TestCase {
+
+    private static final File directory = new File("target/tests/FileAuditor");
+
+    private JBIContainer jbi;
+
+    protected void setUp() throws Exception {
+        jbi = new JBIContainer();
+        jbi.setFlowName("st");
+        jbi.setEmbedded(true);
+        jbi.init();
+        FileUtil.deleteFile(directory); 
+        directory.mkdirs();
+    }
+
+    protected void tearDown() throws Exception {
+        if (jbi != null) {
+            jbi.shutDown();
+        }
+    }
+
+    public void testFileAuditor() throws Exception {
+        jbi.start();
+        SenderComponent sender = new SenderComponent();
+        ReceiverComponent receiver = new ReceiverComponent();
+        jbi.activateComponent(sender, "sender");
+        jbi.activateComponent(receiver, "receiver");
+
+        FileAuditor auditor = new FileAuditor();
+        auditor.setContainer(jbi);
+        auditor.setDirectory(directory);
+        auditor.afterPropertiesSet();
+
+        InOnly inonly = sender.createInOnlyExchange(ReceiverComponent.SERVICE, null, null);
+        inonly.setInMessage(inonly.createMessage());
+        inonly.getInMessage().setContent(new StringSource("<hello>world</hello>"));
+        inonly.getInMessage().setProperty("from", Locale.getDefault().getCountry());
+        sender.send(inonly);
+
+        //check if a message has been audited
+        int nbMessages = auditor.getExchangeCount();
+        assertEquals(1, nbMessages);
+    }
+
+}

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/FileUtil.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/FileUtil.java?rev=658857&r1=658856&r2=658857&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/FileUtil.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/FileUtil.java Wed May 21 13:44:18 2008
@@ -83,6 +83,24 @@
     public static boolean buildDirectory(File file) {
         return file.exists() || file.mkdirs();
     }
+    
+    /**
+     * Count files in a directory (including files in all subdirectories)
+     * @param directory the directory to start in
+     * @return the total number of files
+     */
+    public static int countFilesInDirectory(File directory) {
+        int count = 0;
+        for (File file : directory.listFiles()) {
+            if (file.isFile()) {
+                count++;
+            }
+            if (file.isDirectory()) {
+                count += countFilesInDirectory(file);
+            }
+        }
+        return count;
+    }
 
     /**
      * Copy in stream to an out stream
@@ -294,4 +312,6 @@
             }
         }
     }
+    
+    
 }
\ No newline at end of file

Propchange: servicemix/smx3/branches/servicemix-3.2/deployables/sharedlibraries/servicemix-shared-compat/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed May 21 13:44:18 2008
@@ -4,3 +4,4 @@
 .pmd
 .classpath
 .project
+.settings

Propchange: servicemix/smx3/branches/servicemix-3.2/samples/camel/camel-sa/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed May 21 13:44:18 2008
@@ -1,3 +1,4 @@
 target
 .classpath
 .project
+.settings

Propchange: servicemix/smx3/branches/servicemix-3.2/samples/camel/camel-sa-itest/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed May 21 13:44:18 2008
@@ -1,3 +1,5 @@
 target
 bin
 .project
+.classpath
+.settings

Propchange: servicemix/smx3/branches/servicemix-3.2/samples/cxf-wsdl-first/wsdl-first-cxfbc-su/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed May 21 13:44:18 2008
@@ -1,3 +1,4 @@
 target
 .classpath
 .project
+.settings