Author: davsclaus
Date: Tue Sep 27 08:05:28 2011
New Revision: 1176262

URL: http://svn.apache.org/viewvc?rev=1176262&view=rev
Log:
CAMEL-4490: Restored ResourceBasedEndpoint in camel-spring to be backwards 
compatible.

Added:
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelClassPathResource.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelResourceLoader.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/package.html
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProcessorEndpoint.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProcessorEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProcessorEndpoint.java?rev=1176262&r1=1176261&r2=1176262&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProcessorEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProcessorEndpoint.java
 Tue Sep 27 08:05:28 2011
@@ -59,6 +59,12 @@ public class ProcessorEndpoint extends D
         super(endpointUri, component);
     }
 
+    @Deprecated
+    public ProcessorEndpoint(String endpointUri, Processor processor) {
+        super(endpointUri);
+        this.processor = processor;
+    }
+
     public Producer createProducer() throws Exception {
         return new DefaultProducer(this) {
             public void process(Exchange exchange) throws Exception {

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelClassPathResource.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelClassPathResource.java?rev=1176262&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelClassPathResource.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelClassPathResource.java
 Tue Sep 27 08:05:28 2011
@@ -0,0 +1,62 @@
+/**
+ * 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.camel.component;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.camel.spi.ClassResolver;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.Assert;
+
+/**
+ * Camel specific {@link ClassPathResource} which uses the {@link 
ClassResolver} to load resources from the classpath.
+ *
+ * @deprecated use {@link org.apache.camel.component.ResourceEndpoint}. Will 
be removed in Camel 3.0.
+ */
+@Deprecated
+public class CamelClassPathResource extends ClassPathResource {
+
+    private final ClassResolver resolver;
+
+    public CamelClassPathResource(ClassResolver resolver, String path, 
ClassLoader classLoader) {
+        super(path, classLoader);
+        Assert.notNull(resolver, "Resolver must not be null");
+        this.resolver = resolver;
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        InputStream is = resolver.loadResourceAsStream(getPath());
+        if (is == null) {
+            return super.getInputStream();
+        } else {
+            return is;
+        }
+    }
+
+    @Override
+    public URL getURL() throws IOException {
+        URL url = resolver.loadResourceAsURL(getPath());
+        if (url == null) {
+            return super.getURL();
+        } else {
+            return url;
+        }
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelResourceLoader.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelResourceLoader.java?rev=1176262&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelResourceLoader.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/CamelResourceLoader.java
 Tue Sep 27 08:05:28 2011
@@ -0,0 +1,53 @@
+/**
+ * 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.camel.component;
+
+import org.apache.camel.CamelContext;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.util.Assert;
+
+/**
+ * A Camel specific {@link org.springframework.core.io.ResourceLoader} which 
can load
+ * resources from classpath using the Camel {@link 
org.apache.camel.spi.ClassResolver}.
+ *
+ * @deprecated use {@link org.apache.camel.component.ResourceEndpoint}. Will 
be removed in Camel 3.0.
+ */
+@Deprecated
+public class CamelResourceLoader extends DefaultResourceLoader {
+
+    private final CamelContext camelContext;
+
+    public CamelResourceLoader(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public Resource getResource(String location) {
+        Assert.notNull(location, "Location must not be null");
+        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
+            return new CamelClassPathResource(camelContext.getClassResolver(), 
location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
+        }
+        return super.getResource(location);
+    }
+
+    @Override
+    protected Resource getResourceByPath(String path) {
+        return new CamelClassPathResource(camelContext.getClassResolver(), 
path, getClassLoader());
+    }
+
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java?rev=1176262&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
 Tue Sep 27 08:05:28 2011
@@ -0,0 +1,56 @@
+/**
+ * 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.camel.component;
+
+import org.apache.camel.impl.DefaultComponent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+/**
+ * A useful base class for components which depend on a resource
+ * such as things like Velocity or XQuery based components.
+ *
+ * @deprecated use {@link DefaultComponent}. Will be removed in Camel 3.0.
+ */
+@Deprecated
+public abstract class ResourceBasedComponent extends DefaultComponent {
+    protected final transient Logger log = LoggerFactory.getLogger(getClass());
+    private ResourceLoader resourceLoader;
+
+    public ResourceLoader getResourceLoader() {
+        if (resourceLoader == null) {
+            resourceLoader = new CamelResourceLoader(getCamelContext());
+        }
+        return resourceLoader;
+    }
+
+    public void setResourceLoader(ResourceLoader resourceLoader) {
+        this.resourceLoader = resourceLoader;
+    }
+
+    protected Resource resolveMandatoryResource(String uri) {
+        Resource resource = getResourceLoader().getResource(uri);
+        if (resource == null) {
+            throw new IllegalArgumentException("Could not find resource for 
URI: " + uri + " using: " + getResourceLoader());
+        } else {
+            return resource;
+        }
+    }
+
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java?rev=1176262&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
 Tue Sep 27 08:05:28 2011
@@ -0,0 +1,152 @@
+/**
+ * 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.camel.component;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.camel.Component;
+import org.apache.camel.Processor;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.impl.ProcessorEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+/**
+ * A useful base class for endpoints which depend on a resource
+ * such as things like Velocity or XQuery based components.
+ *
+ * @deprecated use {@link org.apache.camel.component.ResourceEndpoint}. Will 
be removed in Camel 3.0.
+ */
+@Deprecated
+public abstract class ResourceBasedEndpoint extends ProcessorEndpoint {
+    protected final transient Logger log = LoggerFactory.getLogger(getClass());
+    private String resourceUri;
+    private ResourceLoader resourceLoader;
+    private Resource resource;
+    private boolean contentCache;
+    private byte[] buffer;
+
+    public ResourceBasedEndpoint() {
+    }
+
+    public ResourceBasedEndpoint(String endpointUri, Component component, 
String resourceUri, Processor processor) {
+        super(endpointUri, component, processor);
+        this.resourceUri = resourceUri;
+        if (component instanceof ResourceBasedComponent) {
+            this.resourceLoader = ((ResourceBasedComponent) 
component).getResourceLoader();
+        }
+    }
+
+    protected ResourceBasedEndpoint(String endpointUri, Processor processor, 
String resourceUri) {
+        super(endpointUri, processor);
+        this.resourceUri = resourceUri;
+    }
+
+    public Resource getResource() {
+        if (resource == null) {
+            log.debug("Loading resource: {} using: {}", resourceUri, 
getResourceLoader());
+            resource = getResourceLoader().getResource(resourceUri);
+            if (resource == null) {
+                throw new IllegalArgumentException("Could not find resource 
for URI: " + resourceUri + " using: " + getResourceLoader());
+            }
+        }
+        return resource;
+    }
+
+    /**
+     * Gets the resource as an input stream considering the cache flag as well.
+     * <p/>
+     * If cache is enabled then the resource content is cached in an internal 
buffer and this content is
+     * returned to avoid loading the resource over and over again.
+     *
+     * @return  the input stream
+     * @throws IOException is thrown if error loading the content of the 
resource to the local cache buffer
+     */
+    public InputStream getResourceAsInputStream() throws IOException {
+        if (resource == null) {
+            // get the resource if not already done
+            resource = getResource();
+        }
+        // try to get the resource input stream
+        InputStream is = null;
+        if (contentCache) {
+            synchronized (resource) {
+                if (buffer == null) {
+                    log.debug("Reading resource: {} into the content cache", 
resourceUri);
+                    is = getResourceAsInputStreamWithoutCache();
+                    buffer = IOConverter.toBytes(is);
+                }
+            }
+            log.debug("Using resource: {} from the content cache", 
resourceUri);
+            return new ByteArrayInputStream(buffer);
+        } 
+
+        return getResourceAsInputStreamWithoutCache();
+    }
+
+    protected InputStream getResourceAsInputStreamWithoutCache() throws 
IOException {
+        InputStream result;
+        try {
+            result = resource.getInputStream();
+        } catch (IOException exception) {
+            // Using the camelContext classResolver to load the resource as a 
fall back
+            result = 
getCamelContext().getClassResolver().loadResourceAsStream(resourceUri);
+            if (result == null) {
+                log.warn("Cannot get the resource: " + resourceUri + "from the 
camelContext ClassResolver");
+                throw exception;   
+            }
+        }
+        return result;
+    }
+
+    public boolean isContentCache() {
+        return contentCache;
+    }
+
+    /**
+     * Sets whether to use resource content cache or not - default is 
<tt>false</tt>.
+     *
+     * @see #getResourceAsInputStream()
+     */
+    public void setContentCache(boolean contentCache) {
+        this.contentCache = contentCache;
+    }
+
+    public ResourceLoader getResourceLoader() {
+        if (resourceLoader == null) {
+            resourceLoader = new CamelResourceLoader(getCamelContext());
+        }
+        return resourceLoader;
+    }
+
+    public void setResourceLoader(ResourceLoader resourceLoader) {
+        this.resourceLoader = resourceLoader;
+    }
+
+    public String getResourceUri() {
+        return resourceUri;
+    }
+
+    public void setResourceUri(String resourceUri) {
+        this.resourceUri = resourceUri;
+    }
+
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/package.html
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/package.html?rev=1176262&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/package.html
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/package.html
 Tue Sep 27 08:05:28 2011
@@ -0,0 +1,25 @@
+<!--
+    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>
+
+Base classes for Spring based Components
+
+</body>
+</html>


Reply via email to