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 2007/10/08 13:30:15 UTC
svn commit: r582783 - in
/incubator/servicemix/trunk/core/servicemix-audit/src:
main/java/org/apache/servicemix/jbi/audit/file/
test/java/org/apache/servicemix/jbi/audit/file/
Author: gertv
Date: Mon Oct 8 04:30:10 2007
New Revision: 582783
URL: http://svn.apache.org/viewvc?rev=582783&view=rev
Log:
SM-1096: First attempt at creating a file-based auditor implementation
Added:
incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/
incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java
incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/TeeInputStream.java
incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/
incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/TeeInputStreamTest.java
Added: incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java?rev=582783&view=auto
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java (added)
+++ incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/FileAuditor.java Mon Oct 8 04:30:10 2007
@@ -0,0 +1,135 @@
+/*
+ * 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.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+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.MessageUtil;
+
+/**
+ * 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
+ *
+ * @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 {
+
+ private static final Log LOG = LogFactory.getLog(FileAuditor.class);
+ private File directory;
+
+ /**
+ * The directory used for storing the audited messages
+ *
+ * @param directory
+ * the directory
+ */
+ public void setDirectory(File directory) {
+ if (!directory.exists()) {
+ LOG.info("Creating directory " + directory);
+ directory.mkdirs();
+ }
+ this.directory = directory;
+ }
+
+ public void exchangeSent(ExchangeEvent event) {
+ try {
+ MessageExchange exchange = event.getExchange();
+ if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+ OutputStream os = getOutputStream(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);
+ exchange.getMessage("in").setContent(new StreamSource(tis));
+ } else {
+ MessageUtil.enableContentRereadability(in);
+ SourceTransformer transformer = new SourceTransformer();
+ transformer.toResult(in.getContent(), new StreamResult(os));
+ }
+ }
+ } catch (IOException e) {
+ LOG.error(String.format("Error occurred while storing message %s", event.getExchange().getExchangeId()), e);
+ } catch (TransformerException e) {
+ LOG.error(String.format("Error occurred while storing message %s", event.getExchange().getExchangeId()), e);
+ } catch (MessagingException e) {
+ LOG.error(String.format("Error occurred while storing message %s", event.getExchange().getExchangeId()), e);
+ }
+ }
+
+ private OutputStream getOutputStream(MessageExchange exchange) throws FileNotFoundException {
+ String name = getNameForId(exchange.getExchangeId());
+ return new BufferedOutputStream(new FileOutputStream(new File(directory, name)));
+ }
+
+ private String getNameForId(String id) {
+ return id.replaceAll("[:\\.]", "_");
+ }
+
+ @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;
+ }
+
+ @Override
+ public int getExchangeCount() throws AuditorException {
+ return directory.listFiles().length;
+ }
+
+ @Override
+ public String[] getExchangeIdsByRange(int fromIndex, int toIndex) throws AuditorException {
+ throw new AuditorException("getExchangeIdsByRange currently unsupported by FileAuditor");
+ }
+
+ @Override
+ public MessageExchange[] getExchangesByIds(String[] ids) throws AuditorException {
+ throw new AuditorException("getExchangeByIds currently unsupported by FileAuditor");
+ }
+
+ public String getDescription() {
+ return "A file-based auditor implementation: archives files to a specified target directory";
+ }
+}
Added: incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/TeeInputStream.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/TeeInputStream.java?rev=582783&view=auto
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/TeeInputStream.java (added)
+++ incubator/servicemix/trunk/core/servicemix-audit/src/main/java/org/apache/servicemix/jbi/audit/file/TeeInputStream.java Mon Oct 8 04:30:10 2007
@@ -0,0 +1,78 @@
+/*
+ * 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.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * {@link FilterInputStream} implementation that sends a copy of all data being processed to the specified OutputStream.
+ *
+ * @author Gert Vanthienen (gertv)
+ * @since 3.2
+ */
+public class TeeInputStream extends FilterInputStream {
+
+ private final OutputStream os;
+
+ /**
+ * Create a new TeeInputStream
+ *
+ * @param is the InputStream to read from
+ * @param os the OuputStream to copy data to
+ */
+ public TeeInputStream(InputStream is, OutputStream os) {
+ super(is);
+ this.os = os;
+ }
+
+ /**
+ * Reads a single byte from the underlying InputStream. In addition, it also write the same byte to the OutputStream.
+ */
+ @Override
+ public int read() throws IOException {
+ int read = super.read();
+ if (read != -1) {
+ os.write(read);
+ }
+ return read;
+ }
+
+ /**
+ * Read a block of bytes from the underlying InputStream. In addition, write the same data to the OutputStream.
+ */
+ @Override
+ public int read(byte[] bytes, int offset, int length) throws IOException {
+ int read = super.read(bytes, offset, length);
+ if (read != -1) {
+ os.write(bytes, offset, read);
+ }
+ return read;
+ }
+
+ /**
+ * Close the underlying InputStream. Also, flush and close the underlying OutputStream.
+ */
+ @Override
+ public void close() throws IOException {
+ super.close();
+ os.flush();
+ os.close();
+ }
+}
\ No newline at end of file
Added: incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/TeeInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/TeeInputStreamTest.java?rev=582783&view=auto
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/TeeInputStreamTest.java (added)
+++ incubator/servicemix/trunk/core/servicemix-audit/src/test/java/org/apache/servicemix/jbi/audit/file/TeeInputStreamTest.java Mon Oct 8 04:30:10 2007
@@ -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.jbi.audit.file;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link TeeInputStream}
+ *
+ * @author Gert Vanthienen (gertv)
+ * @since 3.2
+ */
+public class TeeInputStreamTest extends TestCase {
+
+ private static final String TEXT = "Apache ServiceMix is an Open Source ESB (Enterprise Service Bus) "
+ + "that combines the functionality of a Service Oriented Architecture (SOA) and an Event Driven Architecture (EDA) "
+ + "to create an agile, enterprise ESB";
+
+ /**
+ * Test for reading byte-by-byte
+ */
+ public void testReadByByte() throws Exception {
+ InputStream is = new ByteArrayInputStream(TEXT.getBytes());
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ TeeInputStream tis = new TeeInputStream(is, bos);
+ while (tis.read() >= 0) {
+ // nothing to do
+ }
+ is.close();
+ assertResult(bos.toByteArray());
+ }
+
+ /**
+ * Test for reading blocks of bytes
+ */
+ public void testReadBlock() throws Exception {
+ InputStream is = new ByteArrayInputStream(TEXT.getBytes());
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ TeeInputStream tis = new TeeInputStream(is, bos);
+ byte[] data = new byte[4096];
+ assertEquals(TEXT.length(), tis.read(data, 0, data.length));
+ tis.close();
+ assertResult(bos.toByteArray());
+ }
+
+ private void assertResult(byte[] bytes) {
+ assertEquals(TEXT.length(), bytes.length);
+ for (int i = 0; i < bytes.length; i++) {
+ assertEquals("Characters on position " + (i + 1) + " should match", TEXT.getBytes()[i], bytes[i]);
+ }
+ }
+}