camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/4] camel git commit: CAMEL-9349: ResourceHelper - Allow to load resource from registry
Date Sat, 28 Nov 2015 08:00:04 GMT
Repository: camel
Updated Branches:
  refs/heads/master b93511e61 -> 17e222db0


CAMEL-9349: ResourceHelper - Allow to load resource from registry


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/14784c1f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/14784c1f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/14784c1f

Branch: refs/heads/master
Commit: 14784c1f4428b198dc28e1be2b92bc080bf6019c
Parents: b93511e
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Nov 27 19:50:39 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Nov 27 19:50:39 2015 +0100

----------------------------------------------------------------------
 .../camel/component/ResourceEndpoint.java       |  6 +--
 .../component/language/LanguageProducer.java    |  2 +-
 .../validator/DefaultLSResourceResolver.java    |  4 +-
 .../component/validator/ValidatorEndpoint.java  |  8 ++--
 .../apache/camel/support/LanguageSupport.java   |  2 +-
 .../org/apache/camel/util/ResourceHelper.java   | 39 +++++++++++++++++++-
 .../xslt/XsltCustomizeURIResolverTest.java      |  2 +-
 .../apache/camel/util/ResourceHelperTest.java   | 30 ++++++++++++---
 8 files changed, 75 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
index a8e7169..f99441a 100644
--- a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
@@ -43,7 +43,7 @@ import org.slf4j.LoggerFactory;
 public abstract class ResourceEndpoint extends ProcessorEndpoint implements ManagedResourceEndpointMBean
{
     protected final Logger log = LoggerFactory.getLogger(getClass());
     private volatile byte[] buffer;
-    @UriPath(description = "Path to the resource") @Metadata(required = "true")
+    @UriPath(description = "Path to the resource, or a reference to lookup a bean in the
Registry to use as the resource") @Metadata(required = "true")
     private String resourceUri;
     @UriParam(defaultValue = "false", description = "Sets whether to use resource content
cache or not")
     private boolean contentCache;
@@ -96,7 +96,7 @@ public abstract class ResourceEndpoint extends ProcessorEndpoint implements
Mana
      * @throws IOException is thrown if resource is not found or cannot be loaded
      */
     protected InputStream loadResource(String uri) throws IOException {
-        return ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(),
uri);
+        return ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), uri);
     }
 
     @ManagedAttribute(description = "Whether the resource is cached")
@@ -141,7 +141,7 @@ public abstract class ResourceEndpoint extends ProcessorEndpoint implements
Mana
     }
 
     /**
-     * Path to the resource
+     * Path to the resource, or a reference to lookup a bean in the Registry to use as the
resource
      *
      * @param resourceUri  the resource path
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
index 7b5c73c..40cdab2 100644
--- a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
@@ -72,7 +72,7 @@ public class LanguageProducer extends DefaultProducer {
             if (script == null) {
                 is = getEndpoint().getResourceAsInputStream();
             } else if (ResourceHelper.hasScheme(script)) {
-                is = ResourceHelper.resolveMandatoryResourceAsInputStream(getEndpoint().getCamelContext().getClassResolver(),
script);
+                is = ResourceHelper.resolveMandatoryResourceAsInputStream(getEndpoint().getCamelContext(),
script);
             }
 
             if (is != null && !getEndpoint().isBinary()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
b/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
index 38fa704..2de0a9e 100644
--- a/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
+++ b/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
@@ -105,7 +105,7 @@ public class DefaultLSResourceResolver implements LSResourceResolver {
             // find the xsd with relative path
             if (ObjectHelper.isNotEmpty(relatedURI)) {
                 try {
-                    ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext.getClassResolver(),
relatedURI);
+                    ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, relatedURI);
                     return relatedURI;
                 } catch (IOException e) {
                    // ignore the exception
@@ -128,7 +128,7 @@ public class DefaultLSResourceResolver implements LSResourceResolver {
         @Override
         public InputStream getByteStream() {
             try {
-                return ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext.getClassResolver(),
uri);
+                return ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext,
uri);
             } catch (IOException e) {
                 throw ObjectHelper.wrapRuntimeCamelException(e);
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
index 9bbeafd..3eb3fd3 100644
--- a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
@@ -45,7 +45,8 @@ public class ValidatorEndpoint extends DefaultEndpoint {
 
     private static final Logger LOG = LoggerFactory.getLogger(ValidatorEndpoint.class);
 
-    @UriPath(description = "URL to a local resource on the classpath or a full URL to a remote
resource or resource on the file system which contains the XSD to validate against.")
+    @UriPath(description = "URL to a local resource on the classpath, or a reference to lookup
a bean in the Registry,"
+            + " or a full URL to a remote resource or resource on the file system which contains
the XSD to validate against.")
     @Metadata(required = "true")
     private String resourceUri;
     @UriParam(defaultValue = XMLConstants.W3C_XML_SCHEMA_NS_URI, label = "advanced",
@@ -81,7 +82,7 @@ public class ValidatorEndpoint extends DefaultEndpoint {
     public Producer createProducer() throws Exception {
         ValidatingProcessor validator = new ValidatingProcessor();
 
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(),
resourceUri);
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(),
resourceUri);
         byte[] bytes = null;
         try {
             bytes = IOConverter.toBytes(is);
@@ -132,7 +133,8 @@ public class ValidatorEndpoint extends DefaultEndpoint {
     }
 
     /**
-     * URL to a local resource on the classpath or a full URL to a remote resource or resource
on the file system which contains the XSD to validate against.
+     * URL to a local resource on the classpath,or a reference to lookup a bean in the Registry,
+     * or a full URL to a remote resource or resource on the file system which contains the
XSD to validate against.
      */
     public void setResourceUri(String resourceUri) {
         this.resourceUri = resourceUri;

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/support/LanguageSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/support/LanguageSupport.java b/camel-core/src/main/java/org/apache/camel/support/LanguageSupport.java
index fed9521..2fc52f9 100644
--- a/camel-core/src/main/java/org/apache/camel/support/LanguageSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/support/LanguageSupport.java
@@ -64,7 +64,7 @@ public abstract class LanguageSupport implements Language, IsSingleton,
CamelCon
             String uri = expression.substring(RESOURCE.length());
             InputStream is = null;
             try {
-                is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext.getClassResolver(),
uri);
+                is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uri);
                 expression = camelContext.getTypeConverter().mandatoryConvertTo(String.class,
is);
             } catch (Exception e) {
                 throw new ExpressionIllegalSyntaxException(expression, e);

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
index 0cb50ef..cea7c38 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.util;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -74,7 +75,7 @@ public final class ResourceHelper {
             if (hasScheme(external)) {
                 InputStream is = null;
                 try {
-                    is = resolveMandatoryResourceAsInputStream(camelContext.getClassResolver(),
external);
+                    is = resolveMandatoryResourceAsInputStream(camelContext, external);
                     expression = camelContext.getTypeConverter().convertTo(String.class,
is);
                 } catch (IOException e) {
                     throw new RuntimeCamelException("Cannot load resource " + external, e);
@@ -118,13 +119,49 @@ public final class ResourceHelper {
     /**
      * Resolves the mandatory resource.
      * <p/>
+     * The resource uri can refer to the following systems to be loaded from
+     * <ul>
+     *     <il>file:nameOfFile - to refer to the file system</il>
+     *     <il>classpath:nameOfFile - to refer to the classpath (default)</il>
+     *     <il>http:uri - to load the resoufce using HTTP</il>
+     *     <il>ref:nameOfBean - to lookup the resource in the {@link org.apache.camel.spi.Registry}</il>
+     * </ul>
+     * If no prefix has been given, then the resource is loaded from the classpath
+     * <p/>
+     * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver,
String)}
+     *
+     * @param camelContext the Camel Context
+     * @param uri URI of the resource
+     * @return the resource as an {@link InputStream}.  Remember to close this stream after
usage.
+     * @throws java.io.IOException is thrown if the resource file could not be found or loaded
as {@link InputStream}
+     */
+    public static InputStream resolveMandatoryResourceAsInputStream(CamelContext camelContext,
String uri) throws IOException {
+        if (uri.startsWith("ref:")) {
+            String ref = uri.substring(4);
+            String value = CamelContextHelper.mandatoryLookup(camelContext, ref, String.class);
+            return new ByteArrayInputStream(value.getBytes());
+        }
+        InputStream is = resolveResourceAsInputStream(camelContext.getClassResolver(), uri);
+        if (is == null) {
+            String resolvedName = resolveUriPath(uri);
+            throw new FileNotFoundException("Cannot find resource: " + resolvedName + " in
classpath for URI: " + uri);
+        } else {
+            return is;
+        }
+    }
+
+    /**
+     * Resolves the mandatory resource.
+     * <p/>
      * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver,
String)}
      *
      * @param classResolver the class resolver to load the resource from the classpath
      * @param uri URI of the resource
      * @return the resource as an {@link InputStream}.  Remember to close this stream after
usage.
      * @throws java.io.IOException is thrown if the resource file could not be found or loaded
as {@link InputStream}
+     * @deprecated use {@link #resolveMandatoryResourceAsInputStream(CamelContext, String)}
      */
+    @Deprecated
     public static InputStream resolveMandatoryResourceAsInputStream(ClassResolver classResolver,
String uri) throws IOException {
         InputStream is = resolveResourceAsInputStream(classResolver, uri);
         if (is == null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
index 37bf103..959de9d 100644
--- a/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
@@ -64,7 +64,7 @@ public class XsltCustomizeURIResolverTest extends ContextTestSupport {
             public Source resolve(String href, String base) throws TransformerException {
                 if (href.equals("org/apache/camel/component/xslt/include_not_existing_resource.xsl"))
{
                     try {
-                        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
href);
+                        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context,
href);
                         return new StreamSource(is);
                     } catch (Exception e) {
                         throw new TransformerException(e);

http://git-wip-us.apache.org/repos/asf/camel/blob/14784c1f/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
index 145c747..f654aa6 100644
--- a/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
@@ -26,6 +26,7 @@ import java.util.Map;
 import org.apache.camel.CamelContext;
 import org.apache.camel.TestSupport;
 import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
 
 /**
  *
@@ -36,7 +37,7 @@ public class ResourceHelperTest extends TestSupport {
         CamelContext context = new DefaultCamelContext();
         context.start();
 
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"file:src/test/resources/log4j.properties");
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "file:src/test/resources/log4j.properties");
         assertNotNull(is);
 
         String text = context.getTypeConverter().convertTo(String.class, is);
@@ -54,7 +55,7 @@ public class ResourceHelperTest extends TestSupport {
         createDirectory("target/my space");
         FileUtil.copyFile(new File("src/test/resources/log4j.properties"), new File("target/my
space/log4j.properties"));
 
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"file:target/my%20space/log4j.properties");
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "file:target/my%20space/log4j.properties");
         assertNotNull(is);
 
         String text = context.getTypeConverter().convertTo(String.class, is);
@@ -69,7 +70,7 @@ public class ResourceHelperTest extends TestSupport {
         CamelContext context = new DefaultCamelContext();
         context.start();
 
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"classpath:log4j.properties");
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "classpath:log4j.properties");
         assertNotNull(is);
 
         String text = context.getTypeConverter().convertTo(String.class, is);
@@ -80,11 +81,28 @@ public class ResourceHelperTest extends TestSupport {
         context.stop();
     }
 
+    public void testLoadRegistry() throws Exception {
+        SimpleRegistry registry = new SimpleRegistry();
+        registry.put("myBean", "This is a log4j logging configuation file");
+
+        CamelContext context = new DefaultCamelContext(registry);
+        context.start();
+
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "ref:myBean");
+        assertNotNull(is);
+
+        String text = context.getTypeConverter().convertTo(String.class, is);
+        assertNotNull(text);
+        assertTrue(text.contains("log4j"));
+        is.close();
+
+        context.stop();
+    }
     public void testLoadClasspathDefault() throws Exception {
         CamelContext context = new DefaultCamelContext();
         context.start();
 
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"log4j.properties");
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "log4j.properties");
         assertNotNull(is);
 
         String text = context.getTypeConverter().convertTo(String.class, is);
@@ -100,7 +118,7 @@ public class ResourceHelperTest extends TestSupport {
         context.start();
 
         try {
-            ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"file:src/test/resources/notfound.txt");
+            ResourceHelper.resolveMandatoryResourceAsInputStream(context, "file:src/test/resources/notfound.txt");
             fail("Should not find file");
         } catch (FileNotFoundException e) {
             assertTrue(e.getMessage().contains("notfound.txt"));
@@ -114,7 +132,7 @@ public class ResourceHelperTest extends TestSupport {
         context.start();
 
         try {
-            ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
"classpath:notfound.txt");
+            ResourceHelper.resolveMandatoryResourceAsInputStream(context, "classpath:notfound.txt");
             fail("Should not find file");
         } catch (FileNotFoundException e) {
             assertEquals("Cannot find resource: classpath:notfound.txt in classpath for URI:
classpath:notfound.txt", e.getMessage());


Mime
View raw message