Author: reto
Date: Wed Feb 13 17:04:02 2013
New Revision: 1445739

URL: http://svn.apache.org/r1445739
Log:
STANBOL-930: Moved BundleTemplateLoader to freemarker bundle

Added:
    stanbol/trunk/commons/freemarker/   (with props)
    stanbol/trunk/commons/freemarker/src/
    stanbol/trunk/commons/freemarker/src/license/
    stanbol/trunk/commons/freemarker/src/license/THIRD-PARTY.properties
    stanbol/trunk/commons/freemarker/src/main/
    stanbol/trunk/commons/freemarker/src/main/java/
    stanbol/trunk/commons/freemarker/src/main/java/org/
    stanbol/trunk/commons/freemarker/src/main/java/org/apache/
    stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/
    stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/
    
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/
    
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/
    
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/BundleTemplateLoader.java
Removed:
    
stanbol/trunk/commons/web/viewable/src/main/java/org/apache/stanbol/commons/web/viewable/impl/BundleTemplateLoader.java
Modified:
    stanbol/trunk/commons/pom.xml
    stanbol/trunk/launchers/bundlelists/stanbolcommons/src/main/bundles/list.xml

Propchange: stanbol/trunk/commons/freemarker/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Feb 13 17:04:02 2013
@@ -0,0 +1 @@
+target

Added: stanbol/trunk/commons/freemarker/src/license/THIRD-PARTY.properties
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/freemarker/src/license/THIRD-PARTY.properties?rev=1445739&view=auto
==============================================================================
--- stanbol/trunk/commons/freemarker/src/license/THIRD-PARTY.properties (added)
+++ stanbol/trunk/commons/freemarker/src/license/THIRD-PARTY.properties Wed Feb 
13 17:04:02 2013
@@ -0,0 +1,25 @@
+# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
+#-------------------------------------------------------------------------------
+# Already used licenses in project :
+# - Apache Software License
+# - Apache Software License, Version 2.0
+# - BSD License
+# - Common Development And Distribution License (CDDL), Version 1.0
+# - Common Development And Distribution License (CDDL), Version 1.1
+# - Common Public License, Version 1.0
+# - Eclipse Public License, Version 1.0
+# - GNU General Public License (GPL), Version 2 with classpath exception
+# - GNU Lesser General Public License (LGPL)
+# - GNU Lesser General Public License (LGPL), Version 2.1
+# - ICU License
+# - MIT License
+# - New BSD License
+# - Public Domain License
+# - Similar to Apache License but with the acknowledgment clause removed
+#-------------------------------------------------------------------------------
+# Please fill the missing licenses for dependencies :
+#
+#
+#Thu Dec 13 13:41:29 CET 2012
+org.osgi--org.osgi.compendium--4.1.0=The Apache Software License, Version 2.0
+org.osgi--org.osgi.core--4.1.0=The Apache Software License, Version 2.0

Added: 
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/BundleTemplateLoader.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/BundleTemplateLoader.java?rev=1445739&view=auto
==============================================================================
--- 
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/BundleTemplateLoader.java
 (added)
+++ 
stanbol/trunk/commons/freemarker/src/main/java/org/apache/stanbol/commons/freemarker/impl/BundleTemplateLoader.java
 Wed Feb 13 17:04:02 2013
@@ -0,0 +1,120 @@
+/*
+* 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.stanbol.commons.freemarker.impl;
+
+import freemarker.cache.TemplateLoader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import java.util.Collection;
+import java.util.HashSet;
+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.Service;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This Service provides an {@link TemplateLoader} that provides templates
+ * relative to the {@link #TEMPLATES_PATH_IN_BUNDLES}.
+ */
+@Component
+@Service(TemplateLoader.class)
+public class BundleTemplateLoader implements TemplateLoader{
+       
+       private static final String TEMPLATES_PATH_IN_BUNDLES = "templates/";
+
+       private static final Logger log = 
LoggerFactory.getLogger(BundleTemplateLoader.class);
+       
+       private final Collection<Bundle> bundles = new HashSet<Bundle>();
+       
+       private BundleListener bundleListener = new BundleListener() {
+               
+               @Override
+               public void bundleChanged(BundleEvent event) {
+                       if ((event.getType() == BundleEvent.STARTED) && 
containsTemplates(event.getBundle())) {
+                               bundles.add(event.getBundle());
+                       } else {
+                               bundles.remove(event.getBundle());
+                       }
+               }
+       };
+       
+               
+       @Override
+       public Reader getReader(Object templateSource, String encoding)
+                       throws IOException {
+               URL templateUrl = (URL) templateSource;
+               return new InputStreamReader(templateUrl.openStream(), 
encoding);
+       }
+       
+       @Override
+       public long getLastModified(Object templateSource) {
+               // not known
+               return -1;
+       }
+       
+       @Override
+       public Object findTemplateSource(String name) throws IOException {
+               if (!name.endsWith(".ftl")) {
+                       name = name +".ftl";
+               }
+               final String path = TEMPLATES_PATH_IN_BUNDLES+name;
+               for (Bundle bundle : bundles) {
+                       URL res = bundle.getResource(path);
+                       if (res != null) {
+                               return res;
+                       }
+               }
+               log.warn("Template "+name+" not known");
+               return null;
+       }
+       
+       @Override
+       public void closeTemplateSource(Object templateSource) throws 
IOException {
+
+               
+       }
+       
+       @Activate
+       protected void activate(final ComponentContext context) {
+               final Bundle[] registeredBundles = 
context.getBundleContext().getBundles();
+               for (int i = 0; i < registeredBundles.length; i++) {
+                       if ((registeredBundles[i].getState() == Bundle.ACTIVE) 
+                                       && 
containsTemplates(registeredBundles[i])) {
+                               bundles.add(registeredBundles[i]);
+                       }
+               }       
+               context.getBundleContext().addBundleListener(bundleListener);
+       }
+
+       @Deactivate
+       protected void deactivate(final ComponentContext context) {
+               context.getBundleContext().removeBundleListener(bundleListener);
+       }
+       
+       private boolean containsTemplates(Bundle bundle) {
+               return bundle.getResource(TEMPLATES_PATH_IN_BUNDLES) != null;
+       }
+       
+}

Modified: stanbol/trunk/commons/pom.xml
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/pom.xml?rev=1445739&r1=1445738&r2=1445739&view=diff
==============================================================================
--- stanbol/trunk/commons/pom.xml (original)
+++ stanbol/trunk/commons/pom.xml Wed Feb 13 17:04:02 2013
@@ -67,6 +67,7 @@
     <module>web/viewable-ldpath</module>
     <module>web/resources</module>
     <module>web/sparql</module>
+    <module>freemarker</module>
     <module>ldpathtemplate</module>
     <module>httpqueryheaders</module> <!-- allows CORS with MS Internet 
Explorer -->
        

Modified: 
stanbol/trunk/launchers/bundlelists/stanbolcommons/src/main/bundles/list.xml
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/launchers/bundlelists/stanbolcommons/src/main/bundles/list.xml?rev=1445739&r1=1445738&r2=1445739&view=diff
==============================================================================
--- 
stanbol/trunk/launchers/bundlelists/stanbolcommons/src/main/bundles/list.xml 
(original)
+++ 
stanbol/trunk/launchers/bundlelists/stanbolcommons/src/main/bundles/list.xml 
Wed Feb 13 17:04:02 2013
@@ -499,6 +499,11 @@
       <version>0.12.0-SNAPSHOT</version>
     </bundle>
     <bundle>
+       <groupId>org.apache.stanbol</groupId>
+       <artifactId>org.apache.stanbol.commons.freemarker</artifactId>
+       <version>0.12.0-SNAPSHOT</version>
+    </bundle>
+    <bundle>
       <groupId>org.apache.stanbol</groupId>
       <artifactId>org.apache.stanbol.commons.web.viewable</artifactId>
       <version>0.12.0-SNAPSHOT</version>


Reply via email to