Author: jstrachan
Date: Mon Oct 2 12:41:54 2006
New Revision: 452180
URL: http://svn.apache.org/viewvc?view=rev&rev=452180
Log:
minor refactor so that the saxon and file components can reuse more logic from
the ProviderEndpoint - making it easier for derived endpoints to just implement
specific methods (rather like the default methods on the Servlet classes to
allow you to overload a GET or POST etc). Also added a test case for
servicemix-file which shows the file marshaler / xpath expressions being used
Modified:
incubator/servicemix/trunk/pom.xml
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ProviderEndpoint.java
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/SpringComponentTest.java
incubator/servicemix/trunk/servicemix-file/src/test/resources/spring.xml
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonEndpoint.java
Modified: incubator/servicemix/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/pom.xml?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
--- incubator/servicemix/trunk/pom.xml (original)
+++ incubator/servicemix/trunk/pom.xml Mon Oct 2 12:41:54 2006
@@ -1455,7 +1455,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
- <skip>true</skip>
<useFile>true</useFile>
<forkMode>once</forkMode>
<childDelegation>false</childDelegation>
Modified:
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ProviderEndpoint.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ProviderEndpoint.java?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
---
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ProviderEndpoint.java
(original)
+++
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ProviderEndpoint.java
Mon Oct 2 12:41:54 2006
@@ -6,8 +6,12 @@
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessageExchangeFactory;
import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.RobustInOnly;
import javax.jbi.messaging.MessageExchange.Role;
import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.jbi.JBIException;
public abstract class ProviderEndpoint extends Endpoint implements
ExchangeProcessor {
@@ -75,4 +79,66 @@
public void stop() throws Exception {
}
+
+ /**
+ * A default implementation of the message processor which checks the
status of the exchange
+ * and if its valid will dispatch to either [EMAIL PROTECTED]
#processInOnly(MessageExchange,NormalizedMessage)} for
+ * an [EMAIL PROTECTED] InOnly} or [EMAIL PROTECTED] RobustInOnly} message
exchange otherwise the
+ * [EMAIL PROTECTED]
#processInOut(MessageExchange,NormalizedMessage,NormalizedMessage)}
+ * method will be invoked
+ *
+ * @param exchange the message exchange
+ * @throws Exception
+ */
+ public void process(MessageExchange exchange) throws Exception {
+ // The component acts as a provider, this means that another component
has requested our service
+ // As this exchange is active, this is either an in or a fault (out
are sent by this component)
+ if (exchange.getRole() == Role.PROVIDER) {
+ // Exchange is finished
+ if (exchange.getStatus() == ExchangeStatus.DONE) {
+ return;
+ // Exchange has been aborted with an exception
+ } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
+ return;
+ // Exchange is active
+ } else {
+ // In message
+ NormalizedMessage in = exchange.getMessage("in");
+ if (in != null) {
+ if (exchange instanceof InOnly || exchange instanceof
RobustInOnly) {
+ processInOnly(exchange, in);
+ }
+ else {
+ NormalizedMessage out = exchange.getMessage("out");
+ if (out == null) {
+ out = exchange.createMessage();
+ exchange.setMessage(out, "out");
+ }
+ processInOut(exchange, in, out);
+ }
+ send(exchange);
+
+ // Fault message
+ } else if (exchange.getFault() != null) {
+ done(exchange);
+ // This is not compliant with the default MEPs
+ } else {
+ throw new IllegalStateException("Provider exchange is
ACTIVE, but no in or fault is provided");
+ }
+ }
+ // Unsupported role: this should never happen has we never create
exchanges
+ } else {
+ throw new IllegalStateException("Unsupported role: " +
exchange.getRole());
+ }
+ }
+
+
+ protected void processInOnly(MessageExchange exchange, NormalizedMessage
in) throws Exception {
+ throw new UnsupportedOperationException("Unsupported MEP: " +
exchange.getPattern());
+ }
+
+ protected void processInOut(MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws Exception {
+ throw new UnsupportedOperationException("Unsupported MEP: " +
exchange.getPattern());
+ }
+
}
Modified:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
(original)
+++
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
Mon Oct 2 12:41:54 2006
@@ -16,8 +16,6 @@
*/
package org.apache.servicemix.file;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.apache.servicemix.common.ProviderEndpoint;
import org.apache.servicemix.components.util.DefaultFileMarshaler;
import org.apache.servicemix.components.util.FileMarshaler;
@@ -25,8 +23,6 @@
import javax.jbi.management.DeploymentException;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
-import javax.jbi.messaging.ExchangeStatus;
-import javax.jbi.messaging.InOut;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
@@ -40,8 +36,6 @@
* @org.apache.xbean.XBean element="endpoint"
*/
public class FileEndpoint extends ProviderEndpoint {
- private static final Log log = LogFactory.getLog(FileEndpoint.class);
-
private File directory;
private FileMarshaler marshaler = new DefaultFileMarshaler();
private String tempFilePrefix = "servicemix-";
@@ -49,21 +43,36 @@
private boolean autoCreateDirectory = true;
- public void process(MessageExchange exchange) throws Exception {
+ public void validate() throws DeploymentException {
+ super.validate();
+
+ if (directory == null) {
+ throw new DeploymentException("You must specify the directory
property");
+ }
+ if (isAutoCreateDirectory()) {
+ directory.mkdirs();
+ }
+ if (!directory.isDirectory()) {
+ throw new DeploymentException("The directory property must be a
directory but was: " + directory);
+ }
+ }
+
+ protected void processInOnly(MessageExchange exchange, NormalizedMessage
in) throws Exception {
OutputStream out = null;
try {
- NormalizedMessage message = exchange.getMessage("in");
- String name = marshaler.getOutputName(exchange, message);
+ String name = marshaler.getOutputName(exchange, in);
File newFile = null;
- System.out.println("Creating file in directory: " +
directory.getCanonicalPath());
if (name == null) {
newFile = File.createTempFile(tempFilePrefix, tempFileSuffix,
directory);
}
else {
newFile = new File(directory, name);
}
+ if (logger.isDebugEnabled()) {
+ logger.debug("Writing to file: " + newFile.getCanonicalPath());
+ }
out = new BufferedOutputStream(new FileOutputStream(newFile));
- marshaler.writeMessage(exchange, message, out, name);
+ marshaler.writeMessage(exchange, in, out, name);
done(exchange);
}
finally {
@@ -72,26 +81,15 @@
out.close();
}
catch (IOException e) {
- log.error("Caught exception while closing stream on error:
" + e, e);
+ logger.error("Caught exception while closing stream on
error: " + e, e);
}
}
}
}
-
- public void validate() throws DeploymentException {
- super.validate();
- if (directory == null) {
- throw new DeploymentException("You must specify the directory
property");
- }
- if (isAutoCreateDirectory()) {
- directory.mkdirs();
- }
- if (!directory.isDirectory()) {
- throw new DeploymentException("The directory property must be a
directory but was: " + directory);
- }
+ protected void processInOut(MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws Exception {
+ super.processInOut(exchange, in, out); /** TODO */
}
-
// Properties
//-------------------------------------------------------------------------
Modified:
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/SpringComponentTest.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/SpringComponentTest.java?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/SpringComponentTest.java
(original)
+++
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/SpringComponentTest.java
Mon Oct 2 12:41:54 2006
@@ -19,6 +19,7 @@
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;
import org.apache.servicemix.client.DefaultServiceMixClient;
@@ -34,7 +35,10 @@
DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
InOnly me = client.createInOnlyExchange();
me.setService(new QName("urn:test", "service"));
- me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
+ NormalizedMessage message = me.getInMessage();
+ message.setProperty("name", "cheese");
+ message.setContent(new StringSource("<hello>world</hello>"));
+
client.sendSync(me);
if (me.getStatus() == ExchangeStatus.ERROR) {
if (me.getError() != null) {
Modified:
incubator/servicemix/trunk/servicemix-file/src/test/resources/spring.xml
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/resources/spring.xml?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
--- incubator/servicemix/trunk/servicemix-file/src/test/resources/spring.xml
(original)
+++ incubator/servicemix/trunk/servicemix-file/src/test/resources/spring.xml
Mon Oct 2 12:41:54 2006
@@ -29,7 +29,16 @@
<sm:component>
<file:component>
<file:endpoints>
- <file:endpoint service="test:service"
endpoint="endpoint" directory="file:target/componentOutput" />
+ <file:endpoint service="test:service"
endpoint="endpoint" directory="file:target/componentOutput">
+ <file:marshaler>
+ <sm:defaultFileMarshaler>
+ <sm:fileName>
+ <!-- lets use a header from the message -->
+ <sm:xpathString xpath="concat($name, '.xml')"/>
+ </sm:fileName>
+ </sm:defaultFileMarshaler>
+ </file:marshaler>
+ </file:endpoint>
</file:endpoints>
</file:component>
</sm:component>
Modified:
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonEndpoint.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonEndpoint.java?view=diff&rev=452180&r1=452179&r2=452180
==============================================================================
---
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonEndpoint.java
(original)
+++
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonEndpoint.java
Mon Oct 2 12:41:54 2006
@@ -10,6 +10,9 @@
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.RobustInOnly;
+import javax.jbi.JBIException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -159,48 +162,20 @@
}
super.activate();
}
-
- public void process(MessageExchange exchange) throws Exception {
- // The component acts as a provider, this means that another component
has requested our service
- // As this exchange is active, this is either an in or a fault (out
are send by this component)
- if (exchange.getRole() == MessageExchange.Role.PROVIDER) {
- // Exchange is finished
- if (exchange.getStatus() == ExchangeStatus.DONE) {
- return;
- // Exchange has been aborted with an exception
- } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
- return;
- // Exchange is active
- } else {
- // Check here if the mep is supported by this component
- if (exchange instanceof InOut == false) {
- throw new UnsupportedOperationException("Unsupported MEP: "
+ exchange.getPattern());
- }
- // In message
- if (exchange.getMessage("in") != null) {
- NormalizedMessage in = exchange.getMessage("in");
- // Transform the content and send it back
- NormalizedMessage out = exchange.createMessage();
- copyPropertiesAndAttachments(in, out);
- transform(exchange, in, out);
- exchange.setMessage(out, "out");
- send(exchange);
- // Fault message
- } else if (exchange.getFault() != null) {
- done(exchange);
- // This is not compliant with the default MEPs
- } else {
- throw new IllegalStateException("Provider exchange is
ACTIVE, but no in or fault is provided");
- }
- }
- // Unsupported role: this should never happen has we never create
exchanges
- } else {
- throw new IllegalStateException("Unsupported role: " +
exchange.getRole());
- }
- }
-
+
+
// Implementation methods
//
-------------------------------------------------------------------------
+
+ /**
+ * Transform the content and send it back
+ */
+ protected void processInOut(MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws Exception {
+ copyPropertiesAndAttachments(in, out);
+ transform(exchange, in, out);
+ }
+
+
/**
* If enabled the properties and attachments are copied to the destination
message
*/
@@ -256,4 +231,5 @@
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(res.getInputStream(), url != null ?
url.toExternalForm() : null);
}
-}
+
+}
\ No newline at end of file