You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mu...@apache.org on 2007/11/07 23:42:29 UTC

svn commit: r592932 - in /struts/sandbox/trunk/struts2-osgi-plugin/src/main: java/org/apache/struts2/osgi/ resources/

Author: musachy
Date: Wed Nov  7 14:42:29 2007
New Revision: 592932

URL: http://svn.apache.org/viewvc?rev=592932&view=rev
Log:
Add Spring and FreeMarker support
* The Spring support is hacky and kind of a mess right now

Added:
    struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleFreemarkerManager.java
    struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleTemplateLoader.java
    struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/SpringOSGiUtil.java
    struts/sandbox/trunk/struts2-osgi-plugin/src/main/resources/struts-osgi.properties

Added: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleFreemarkerManager.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleFreemarkerManager.java?rev=592932&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleFreemarkerManager.java (added)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleFreemarkerManager.java Wed Nov  7 14:42:29 2007
@@ -0,0 +1,56 @@
+package org.apache.struts2.osgi;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+
+import org.apache.struts2.views.freemarker.FreemarkerManager;
+import org.apache.struts2.views.freemarker.StrutsClassTemplateLoader;
+
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+
+import freemarker.cache.FileTemplateLoader;
+import freemarker.cache.MultiTemplateLoader;
+import freemarker.cache.TemplateLoader;
+import freemarker.cache.WebappTemplateLoader;
+
+public class BundleFreemarkerManager extends FreemarkerManager{
+    private static final Logger LOG = LoggerFactory.getLogger(BundleFreemarkerManager.class);
+    
+    @Override
+    protected TemplateLoader getTemplateLoader(ServletContext servletContext) {
+        // construct a FileTemplateLoader for the init-param 'TemplatePath'
+        FileTemplateLoader templatePathLoader = null;
+
+        String templatePath = servletContext.getInitParameter("TemplatePath");
+        if (templatePath == null) {
+            templatePath = servletContext.getInitParameter("templatePath");
+        }
+
+        if (templatePath != null) {
+            try {
+                templatePathLoader = new FileTemplateLoader(new File(templatePath));
+            } catch (IOException e) {
+                LOG.error("Invalid template path specified: " + e.getMessage(), e);
+            }
+        }
+
+        // presume that most apps will require the class and webapp template loader
+        // if people wish to
+        return templatePathLoader != null ?
+                new MultiTemplateLoader(new TemplateLoader[]{
+                        templatePathLoader,
+                        new WebappTemplateLoader(servletContext),
+                        new StrutsClassTemplateLoader(),
+                        new BundleTemplateLoader()
+                })
+                : new MultiTemplateLoader(new TemplateLoader[]{
+                    new WebappTemplateLoader(servletContext),
+                    new StrutsClassTemplateLoader(),
+                    new BundleTemplateLoader()
+                });
+    }
+    
+}

Added: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleTemplateLoader.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleTemplateLoader.java?rev=592932&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleTemplateLoader.java (added)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleTemplateLoader.java Wed Nov  7 14:42:29 2007
@@ -0,0 +1,17 @@
+package org.apache.struts2.osgi;
+
+import java.net.URL;
+
+import freemarker.cache.URLTemplateLoader;
+
+/**
+ * Finds FreeMarker templates in bundles
+ */
+public class BundleTemplateLoader extends URLTemplateLoader {
+
+    @Override
+    protected URL getURL(String name) {
+        return DefaultBundleAccessor.getInstance().loadResource(name);
+    }
+
+}

Added: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/SpringOSGiUtil.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/SpringOSGiUtil.java?rev=592932&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/SpringOSGiUtil.java (added)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/SpringOSGiUtil.java Wed Nov  7 14:42:29 2007
@@ -0,0 +1,32 @@
+package org.apache.struts2.osgi;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+public class SpringOSGiUtil {
+    public static boolean isValidBean(BundleContext bundleContext, String beanId) throws InvalidSyntaxException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+        return getBean(bundleContext, beanId) != null;
+    }
+    
+    public static Object getBean(BundleContext bundleContext, String beanId) throws InvalidSyntaxException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+        ServiceReference[] references = bundleContext.getAllServiceReferences(
+            "org.springframework.context.ApplicationContext", null);
+        if (references != null && references.length > 0) {
+            Object beanFactory = bundleContext.getService(references[0]);
+            //this class and the BeanFactory service are loaded by different classloaders
+            //so we cannot cast to a common interface (is there any other (nice) way of doing this?)
+            return getBean(beanFactory, beanId);
+        }
+        
+        return null;
+    }
+    
+    private static Object getBean(Object beanFactory, String beanId) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+        Method getBeanMethod = beanFactory.getClass().getMethod("getBean", String.class);
+        return getBeanMethod.invoke(beanFactory, beanId);
+    }
+}

Added: struts/sandbox/trunk/struts2-osgi-plugin/src/main/resources/struts-osgi.properties
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/resources/struts-osgi.properties?rev=592932&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/resources/struts-osgi.properties (added)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/resources/struts-osgi.properties Wed Nov  7 14:42:29 2007
@@ -0,0 +1,19 @@
+# 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.
+
+xwork=, \
+ com.opensymphony.xwork2