Author: davsclaus
Date: Mon May 30 17:01:20 2011
New Revision: 1129257

URL: http://svn.apache.org/viewvc?rev=1129257&view=rev
Log:
CAMEL-3934: Fixed resource based component to load from classpath using Camels 
ClassResolver which ensures they now work in OSGi blueprint.

Added:
    
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityClasspathResourceLoader.java
Modified:
    
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java

Added: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityClasspathResourceLoader.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityClasspathResourceLoader.java?rev=1129257&view=auto
==============================================================================
--- 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityClasspathResourceLoader.java
 (added)
+++ 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityClasspathResourceLoader.java
 Mon May 30 17:01:20 2011
@@ -0,0 +1,52 @@
+/**
+ * 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.velocity;
+
+import java.io.InputStream;
+
+import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.collections.ExtendedProperties;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
+
+/**
+ * Camel specific {@link ClasspathResourceLoader} that loads resources using 
the
+ * Camel {@link ClassResolver}
+ */
+public class CamelVelocityClasspathResourceLoader extends 
ClasspathResourceLoader {
+
+    private ClassResolver resolver;
+
+    @Override
+    public void init(ExtendedProperties configuration) {
+        super.init(configuration);
+        resolver = (ClassResolver) 
this.rsvc.getProperty("CamelClassResolver");;
+        ObjectHelper.notNull(resolver, "ClassResolver");
+    }
+
+    @Override
+    public InputStream getResourceStream(String name) throws 
ResourceNotFoundException {
+        InputStream is = resolver.loadResourceAsStream(name);
+        if (is == null) {
+            return super.getResourceStream(name);
+        } else {
+            return is;
+        }
+    }
+
+}

Modified: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=1129257&r1=1129256&r2=1129257&view=diff
==============================================================================
--- 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
 (original)
+++ 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
 Mon May 30 17:01:20 2011
@@ -81,14 +81,19 @@ public class VelocityEndpoint extends Re
                 log.info("Loaded the velocity configuration file " + 
getPropertiesFile());
             }
 
+            // set the class resolver as a property so we can access it from 
CamelVelocityClasspathResourceLoader
+            velocityEngine.addProperty("CamelClassResolver", 
getCamelContext().getClassResolver());
+
+            // set regular properties
             properties.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, 
isLoaderCache() ? "true" : "false");
             properties.setProperty(Velocity.RESOURCE_LOADER, "file, class");
-            properties.setProperty("class.resource.loader.description", 
"Velocity Classpath Resource Loader");
-            properties.setProperty("class.resource.loader.class", 
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+            properties.setProperty("class.resource.loader.description", "Camel 
Velocity Classpath Resource Loader");
+            properties.setProperty("class.resource.loader.class", 
CamelVelocityClasspathResourceLoader.class.getName());
             properties.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, 
CommonsLogLogChute.class.getName());
             
properties.setProperty(CommonsLogLogChute.LOGCHUTE_COMMONS_LOG_NAME, 
VelocityEndpoint.class.getName());
-            velocityEngine.init(properties);
 
+            log.debug("Initializing VelocityEngine with properties {}", 
properties);
+            velocityEngine.init(properties);
         }
         return velocityEngine;
     }


Reply via email to