Author: jstrachan
Date: Sun Oct 8 08:16:54 2006
New Revision: 454153
URL: http://svn.apache.org/viewvc?view=rev&rev=454153
Log:
minor change of API to expose configured endpoints as a List rather than
Endpoint[] so that multiple types of endpoints can be configured in a typesafe
way so they work nice with xbean - e.g. FileComponent can take 2 Endpoint[]
properties for different kinds of endpoints
Added:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
(contents, props changed)
- copied, changed from r454126,
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
Removed:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
Modified:
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/DefaultComponent.java
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileComponent.java
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/DynamicEndpointTest.java
incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonComponent.java
Modified:
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/DefaultComponent.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/DefaultComponent.java?view=diff&rev=454153&r1=454152&r2=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/DefaultComponent.java
(original)
+++
incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/DefaultComponent.java
Sun Oct 8 08:16:54 2006
@@ -31,6 +31,9 @@
import javax.jbi.messaging.MessageExchange;
import javax.jbi.servicedesc.ServiceEndpoint;
import java.util.Arrays;
+import java.util.List;
+import java.util.Iterator;
+import java.util.Collections;
/**
* A useful base class for writing new JBI components which includes the
[EMAIL PROTECTED] ComponentLifeCycle} interface methods so that
@@ -45,7 +48,6 @@
protected Registry registry;
protected BaseServiceUnitManager serviceUnitManager;
protected ServiceUnit serviceUnit;
- private Endpoint[] endpoints;
public DefaultComponent() {
setComponent(this);
@@ -185,40 +187,6 @@
return registry;
}
- /**
- * Returns the statically defined endpoints of this component
- */
- public Endpoint[] getEndpoints() {
- return endpoints;
- }
-
- public void setEndpoints(Endpoint[] endpoints) throws DeploymentException {
- for (int i = 0; i < endpoints.length; i++) {
- Endpoint endpoint = endpoints[i];
- validateEndpoint(endpoint);
- }
- this.endpoints = endpoints;
- }
-
- /**
- * Provides a hook to validate the statically configured endpoint
- */
- protected void validateEndpoint(Endpoint endpoint) throws
DeploymentException {
- Class[] endpointClasses = getEndpointClasses();
- if (endpointClasses != null) {
- boolean valid = false;
- for (int i = 0; i < endpointClasses.length; i++) {
- Class endpointClass = endpointClasses[i];
- if (endpointClass.isInstance(endpoint)) {
- valid = true;
- }
- }
- if (!valid) {
- throw new DeploymentException("The endpoint: " + endpoint
- + " is not an instance of any of the allowable types:
" + Arrays.asList(endpointClasses));
- }
- }
- }
/**
* Returns the service unit, lazily creating one on demand
@@ -234,6 +202,11 @@
}
/**
+ * Returns an array of configured endpoints for the component or null if
there are no configured endpoints
+ */
+ protected abstract List getConfiguredEndpoints();
+
+ /**
* Returns a list of valid endpoint classes or null if the component does
not wish to programmatically
* restrict the list of possible endpoint classes
*
@@ -241,22 +214,61 @@
*/
protected abstract Class[] getEndpointClasses();
+ /**
+ * A little helper method to turn a possibly null list of endpoitns into a
list of endpoints
+ */
+ protected List asList(Endpoint[] endpoints) {
+ if (endpoints == null) {
+ return Collections.EMPTY_LIST;
+ }
+ return Arrays.asList(endpoints);
+ }
+
/* (non-Javadoc)
* @see org.servicemix.common.BaseLifeCycle#doInit()
*/
protected void doInit() throws Exception {
super.doInit();
- Endpoint[] endpoints = getEndpoints();
- if (endpoints != null && endpoints.length > 0) {
+ List endpoints = getConfiguredEndpoints();
+ if (endpoints != null && !endpoints.isEmpty()) {
ServiceUnit su = getServiceUnit();
- for (int i = 0; i < endpoints.length; i++) {
- endpoints[i].setServiceUnit(su);
- endpoints[i].validate();
- su.addEndpoint(endpoints[i]);
+ Iterator iter = endpoints.iterator();
+ while (iter.hasNext()) {
+ Endpoint endpoint = (Endpoint) iter.next();
+ if (endpoint == null) {
+ logger.warn("Ignoring null endpoint in list: " +
endpoints);
+ continue;
+ }
+ endpoint.setServiceUnit(su);
+ validateEndpoint(endpoint);
+ endpoint.validate();
+ su.addEndpoint(endpoint);
}
getRegistry().registerServiceUnit(su);
}
}
+
+
+ /**
+ * Provides a hook to validate the statically configured endpoint
+ */
+ protected void validateEndpoint(Endpoint endpoint) throws
DeploymentException {
+ Class[] endpointClasses = getEndpointClasses();
+ if (endpointClasses != null) {
+ boolean valid = false;
+ for (int i = 0; i < endpointClasses.length; i++) {
+ Class endpointClass = endpointClasses[i];
+ if (endpointClass.isInstance(endpoint)) {
+ valid = true;
+ }
+ }
+ if (!valid) {
+ throw new DeploymentException("The endpoint: " + endpoint
+ + " is not an instance of any of the allowable types:
" + Arrays.asList(endpointClasses));
+ }
+ }
+ }
+
/* (non-Javadoc)
* @see org.servicemix.common.BaseLifeCycle#doStart()
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=454153&r1=454152&r2=454153
==============================================================================
---
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
Sun Oct 8 08:16:54 2006
@@ -25,14 +25,17 @@
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.xml.namespace.QName;
+import java.io.File;
import java.net.URI;
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
-import java.io.File;
/**
- * @org.apache.xbean.XBean element="component"
- * description="File Component"
+ * A file based component
+ *
+ * @version $Revision: $
+ * @org.apache.xbean.XBean element="component" description="File Component"
*/
public class FileComponent extends DefaultComponent {
@@ -40,15 +43,41 @@
public final static QName EPR_SERVICE = new QName(EPR_URI,
"FileComponent");
public final static String EPR_NAME = "epr";
+ private FileEndpoint[] endpoints;
+ private FilePollingEndpoint[] pollingEndpoints;
- protected Class[] getEndpointClasses() {
- return new Class[]{FileEndpoint.class, FilePollEndpoint.class};
+ public FileEndpoint[] getEndpoints() {
+ return endpoints;
+ }
+
+ public void setEndpoints(FileEndpoint[] endpoints) {
+ this.endpoints = endpoints;
+ }
+
+
+ public FilePollingEndpoint[] getPollingEndpoints() {
+ return pollingEndpoints;
+ }
+
+ public void setPollingEndpoints(FilePollingEndpoint[] pollingEndpoints) {
+ this.pollingEndpoints = pollingEndpoints;
}
public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
return ResolvedEndpoint.resolveEndpoint(epr, EPR_URI, EPR_NAME,
EPR_SERVICE, "file:");
}
+ protected List getConfiguredEndpoints() {
+ List answer = new ArrayList();
+ answer.addAll(asList(getEndpoints()));
+ answer.addAll(asList(getPollingEndpoints()));
+ return answer;
+ }
+
+ protected Class[] getEndpointClasses() {
+ return new Class[]{FileEndpoint.class, FilePollingEndpoint.class};
+ }
+
protected QName getEPRServiceName() {
return EPR_SERVICE;
}
@@ -61,18 +90,38 @@
// TODO
//fileEp.setRole(MessageExchange.Role.PROVIDER);
- // lets use a URL to parse the path
- URL url = new URL(ep.getEndpointName());
+ URI uri = new URI(ep.getEndpointName());
+
+ String file = null;
+ String host = uri.getHost();
+ String path = uri.getPath();
+ if (host != null) {
+ if (path != null) {
+ // lets assume host really is a relative directory
+ file = host + File.separator + path;
+ }
+ else {
+ file = host;
+ }
+ }
+ else {
+ if (path != null) {
+ file = path;
+ }
+ else {
+ // must be an abssolute URI
+ file = uri.getSchemeSpecificPart();
+ }
+ }
- Map map = URISupport.parseQuery(url.getQuery());
- IntrospectionSupport.setProperties(fileEp, map, "file.");
+ Map map = URISupport.parseQuery(uri.getQuery());
+ IntrospectionSupport.setProperties(fileEp, map);
- String path = url.getPath();
- if (path != null) {
- fileEp.setDirectory(new File(path));
+ if (file != null) {
+ fileEp.setDirectory(new File(file));
}
else {
- throw new IllegalArgumentException("No path defined for URL: " +
url);
+ throw new IllegalArgumentException("No file defined for URL: " +
uri);
}
fileEp.activate();
return fileEp;
Added:
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=auto&rev=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
(added)
+++
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FileEndpoint.java
Sun Oct 8 08:16:54 2006
@@ -0,0 +1,141 @@
+/*
+ * 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.components.util.DefaultFileMarshaler;
+import org.apache.servicemix.components.util.FileMarshaler;
+import org.apache.servicemix.common.ProviderEndpoint;
+
+import javax.jbi.management.DeploymentException;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import java.io.*;
+
+/**
+ * An endpoint which receives a message and writes the content to a file.
+ *
+ * @org.apache.xbean.XBean element="endpoint"
+ *
+ * @version $Revision: $
+*/
+public class FileEndpoint extends ProviderEndpoint {
+ private File directory;
+ private FileMarshaler marshaler = new DefaultFileMarshaler();
+ private String tempFilePrefix = "servicemix-";
+ private String tempFileSuffix = ".xml";
+ private boolean autoCreateDirectory = true;
+
+
+ public FileEndpoint() {
+ }
+
+ public FileEndpoint(FileComponent component, ServiceEndpoint endpoint) {
+ super(component, endpoint);
+ }
+
+ 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 {
+ String name = marshaler.getOutputName(exchange, in);
+ File newFile = null;
+ 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, in, out, name);
+ }
+ finally {
+ if (out != null) {
+ try {
+ out.close();
+ }
+ catch (IOException e) {
+ logger.error("Caught exception while closing stream on
error: " + e, e);
+ }
+ }
+ }
+ }
+
+ protected void processInOut(MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws Exception {
+ /** TODO list the files? */
+ super.processInOut(exchange, in, out);
+ }
+
+ // Properties
+ //-------------------------------------------------------------------------
+ public File getDirectory() {
+ return directory;
+ }
+
+ public void setDirectory(File directory) {
+ this.directory = directory;
+ }
+
+ public FileMarshaler getMarshaler() {
+ return marshaler;
+ }
+
+ public void setMarshaler(FileMarshaler marshaler) {
+ this.marshaler = marshaler;
+ }
+
+ public String getTempFilePrefix() {
+ return tempFilePrefix;
+ }
+
+ public void setTempFilePrefix(String tempFilePrefix) {
+ this.tempFilePrefix = tempFilePrefix;
+ }
+
+ public String getTempFileSuffix() {
+ return tempFileSuffix;
+ }
+
+ public void setTempFileSuffix(String tempFileSuffix) {
+ this.tempFileSuffix = tempFileSuffix;
+ }
+
+ public boolean isAutoCreateDirectory() {
+ return autoCreateDirectory;
+ }
+
+ public void setAutoCreateDirectory(boolean autoCreateDirectory) {
+ this.autoCreateDirectory = autoCreateDirectory;
+ }
+
+}
Copied:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
(from r454126,
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/FilePollingEndpoint.java?view=diff&rev=454153&p1=incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java&r1=454126&p2=incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java&r2=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollEndpoint.java
(original)
+++
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
Sun Oct 8 08:16:54 2006
@@ -40,12 +40,12 @@
* 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"
+ * @org.apache.xbean.XBean element="pollingEndpoint"
*
* @version $Revision$
*/
-public class FilePollEndpoint extends PollingEndpoint {
- private static final Log log = LogFactory.getLog(FilePollEndpoint.class);
+public class FilePollingEndpoint extends PollingEndpoint {
+ private static final Log log =
LogFactory.getLog(FilePollingEndpoint.class);
private File file;
private FileFilter filter;
Propchange:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
incubator/servicemix/trunk/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollingEndpoint.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/DynamicEndpointTest.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/DynamicEndpointTest.java?view=diff&rev=454153&r1=454152&r2=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/DynamicEndpointTest.java
(original)
+++
incubator/servicemix/trunk/servicemix-file/src/test/java/org/apache/servicemix/file/DynamicEndpointTest.java
Sun Oct 8 08:16:54 2006
@@ -30,12 +30,10 @@
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.servicedesc.ServiceEndpoint;
-
import java.io.File;
-import java.net.URL;
public class DynamicEndpointTest extends SpringTestSupport {
- protected String dynamicURI =
"file:target/dynamicEndpoint?file.tempFilePrefix=dynamicEp-";
+ protected String dynamicURI =
"file://target/dynamicEndpoint?file.tempFilePrefix=dynamicEp-";
protected void setUp() throws Exception {
super.setUp();
@@ -43,12 +41,6 @@
}
public void testSendingToDynamicEndpoint() throws Exception {
- // lets check the path is parsable first
- URL url = new URL(dynamicURI);
- String path = url.getPath();
- assertEquals("target/dynamicEndpoint", path);
-
-
// now lets make a request on this endpoint
DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
Modified:
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=diff&rev=454153&r1=454152&r2=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
(original)
+++
incubator/servicemix/trunk/servicemix-file/src/test/resources/spring-polling.xml
Sun Oct 8 08:16:54 2006
@@ -29,12 +29,12 @@
<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:pollingEndpoints>
+ <file:pollingEndpoint service="test:poller" endpoint="poller"
file="file:target/pollerFiles"/>
+ </file:pollingEndpoints>
</file:component>
</sm:component>
</sm:activationSpec>
Modified:
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonComponent.java
URL:
http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonComponent.java?view=diff&rev=454153&r1=454152&r2=454153
==============================================================================
---
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonComponent.java
(original)
+++
incubator/servicemix/trunk/servicemix-saxon/src/main/java/org/apache/servicemix/saxon/SaxonComponent.java
Sun Oct 8 08:16:54 2006
@@ -16,15 +16,9 @@
*/
package org.apache.servicemix.saxon;
-import org.apache.servicemix.common.BaseComponent;
-import org.apache.servicemix.common.BaseLifeCycle;
-import org.apache.servicemix.common.BaseServiceUnitManager;
-import org.apache.servicemix.common.Deployer;
-import org.apache.servicemix.common.ServiceUnit;
import org.apache.servicemix.common.DefaultComponent;
-import org.apache.servicemix.common.Endpoint;
-import org.apache.servicemix.common.xbean.BaseXBeanDeployer;
-import org.apache.servicemix.common.xbean.XBeanServiceUnit;
+
+import java.util.List;
/**
*
@@ -34,8 +28,21 @@
*/
public class SaxonComponent extends DefaultComponent {
+ private SaxonEndpoint[] endpoints;
+
+ public SaxonEndpoint[] getEndpoints() {
+ return endpoints;
+ }
+
+ public void setEndpoints(SaxonEndpoint[] endpoints) {
+ this.endpoints = endpoints;
+ }
+
protected Class[] getEndpointClasses() {
return new Class[] { SaxonEndpoint.class };
}
+ protected List getConfiguredEndpoints() {
+ return asList(getEndpoints());
+ }
}