You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by my...@apache.org on 2010/12/01 16:27:08 UTC
svn commit: r1041052 - in
/sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity:
SlingResourceLoader.java VelocityScriptEngineService.java
VelocityTemplatesScriptEngine.java
Author: mykee
Date: Wed Dec 1 15:27:08 2010
New Revision: 1041052
URL: http://svn.apache.org/viewvc?rev=1041052&view=rev
Log:
SLING-1708 contrib/scripting/velocity is missing suitable resourceloader for the JCR
Added:
sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java (with props)
sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java (with props)
Modified:
sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
Added: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java?rev=1041052&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java (added)
+++ sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java Wed Dec 1 15:27:08 2010
@@ -0,0 +1,83 @@
+/*
+ * 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.sling.scripting.velocity;
+
+import java.io.InputStream;
+
+import org.apache.commons.collections.ExtendedProperties;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.runtime.resource.loader.ResourceLoader;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+
+public class SlingResourceLoader extends ResourceLoader {
+
+ private VelocityScriptEngineService velocityScriptEngineService;
+
+ private ResourceResolver resourceResolver;
+
+ @Override
+ public long getLastModified(Resource arg0) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public InputStream getResourceStream(String absPath )
+ throws ResourceNotFoundException {
+ InputStream returnValue = null;
+
+ if ( absPath != null && absPath.startsWith( "/" ) && resourceResolver != null )
+ {
+ org.apache.sling.api.resource.Resource resource = resourceResolver.resolve( absPath );
+ if ( resource != null )
+ {
+ returnValue = resource.adaptTo( InputStream.class );
+ }
+ }
+
+ return returnValue;
+ }
+
+ @Override
+ public void init(ExtendedProperties arg0) {
+ Bundle bundle = FrameworkUtil.getBundle( VelocityScriptEngineService.class );
+ BundleContext bundleContext = bundle.getBundleContext();
+ ServiceReference serviceRef = bundleContext.getServiceReference( VelocityScriptEngineService.class.getName() );
+
+ if ( serviceRef != null )
+ {
+ velocityScriptEngineService = (VelocityScriptEngineService) bundleContext.getService( serviceRef );
+ if ( velocityScriptEngineService != null )
+ {
+ resourceResolver = velocityScriptEngineService.getResourceResolver();
+ }
+ }
+ }
+
+ @Override
+ public boolean isSourceModified(Resource arg0) {
+ return true;
+ }
+
+
+
+}
Propchange: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/SlingResourceLoader.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java?rev=1041052&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java (added)
+++ sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java Wed Dec 1 15:27:08 2010
@@ -0,0 +1,80 @@
+/*
+ * 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.sling.scripting.velocity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.osgi.framework.BundleContext;
+
+@Component(name = "org.apache.sling.scripting.velocity.VelocityScriptEngineService", label = "%auth.name", description = "%auth.description")
+@Service( value = VelocityScriptEngineService.class )
+public class VelocityScriptEngineService {
+
+ @Reference
+ private ResourceResolverFactory resourceResolverFactory;
+
+ private ResourceResolver resourceResolver;
+
+ public static final String PROP_RESOURCE_LOADER_USER = "velocityresourceloader.user";
+
+ @Property( name = PROP_RESOURCE_LOADER_USER )
+ public static final String DEFAULT_RESOURCE_LOADER_USER = null;
+
+ public ResourceResolver getResourceResolver ()
+ {
+ return resourceResolver;
+ }
+
+ @SuppressWarnings("unused")
+ @Activate
+ private void activate(final BundleContext bundleContext,
+ final Map<String, Object> properties) throws LoginException {
+ if ( resourceResolverFactory != null )
+ {
+ Map<String, Object> authInfo = new HashMap<String, Object>();
+ // Use the user which is configured to access the resources if available
+ Object prop = properties.get(PROP_RESOURCE_LOADER_USER);
+ final String resourceLoaderUser = ( prop != null ) ? prop.toString() : null;
+ if (resourceLoaderUser != null && resourceLoaderUser.length() > 0) {
+ authInfo.put(ResourceResolverFactory.USER_IMPERSONATION, resourceLoaderUser);
+ }
+
+ resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver( authInfo );
+ }
+ }
+
+ @SuppressWarnings("unused")
+ @Deactivate
+ private void deactivate(final BundleContext bundleContext) {
+ }
+
+
+}
Propchange: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityScriptEngineService.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java?rev=1041052&r1=1041051&r2=1041052&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java (original)
+++ sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java Wed Dec 1 15:27:08 2010
@@ -48,7 +48,14 @@ public class VelocityTemplatesScriptEngi
velocity = new VelocityEngine();
- velocity.init();
+ // include the Sling resource loader for Velocity
+ Properties props = new Properties();
+ props.put("sling.resource.loader.description", "Sling Resource Loader for Velocity");
+ props.put("sling.resource.loader.class", "org.apache.sling.scripting.velocity.SlingResourceLoader");
+ props.put("sling.resource.loader.cache", "false");
+ props.put("resource.loader","file,sling");
+
+ velocity.init( props );
} catch (Exception e) {
throw new RuntimeException("Exception in Velocity.init() "
+ e.getMessage(), e);