incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r782904 - in /incubator/sling/trunk/contrib/scripting/jsp-taglib-atom: ./ src/main/java/org/apache/sling/atom/taglib/ src/main/java/org/apache/sling/atom/taglib/internal/ src/main/java/org/apache/sling/atom/taglib/media/
Date Tue, 09 Jun 2009 08:08:16 GMT
Author: fmeschbe
Date: Tue Jun  9 08:08:13 2009
New Revision: 782904

URL: http://svn.apache.org/viewvc?rev=782904&view=rev
Log:
SLING-985 Add BundleActivator managing the Abdera instance as
a ServletContext attribute and modify the AbstractAbderaHandler.getAbdera
methods to use the JSP PageContext instead of the servlet request.

Added:
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
  (with props)
Modified:
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/pom.xml
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/AbstractAbderaHandler.java
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/EntryTagHandler.java
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/FeedTagHandler.java
    incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/media/MediaGroupTagHandler.java

Modified: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/pom.xml?rev=782904&r1=782903&r2=782904&view=diff
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/pom.xml (original)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/pom.xml Tue Jun  9 08:08:13 2009
@@ -1,20 +1,20 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
-<!--
-    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.
--->
+    <!--
+        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.
+    -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
     <modelVersion>4.0.0</modelVersion>
@@ -51,14 +51,21 @@
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
-                        <Export-Package>
-                            org.apache.sling.atom.taglib.*;version=${pom.version}
-                        </Export-Package>
                         <Import-Package>
                             javax.servlet;version=2.4,
                             javax.servlet.jsp.*;version=2.0,
                             *
                         </Import-Package>
+                        <Export-Package>
+                            org.apache.sling.atom.taglib.*;version=${pom.version}
+                        </Export-Package>
+                        <Private-Package>
+                            org.apache.sling.atom.taglib.internal.*,
+                            org.osgi.util.tracker
+                        </Private-Package>
+                        <Bundle-Activator>
+                            org.apache.sling.atom.taglib.internal.AbderaActivator
+                        </Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>
@@ -83,15 +90,24 @@
         </dependency>
         <dependency>
             <groupId>org.apache.abdera</groupId>
-            <artifactId>abdera-extensions-media
-            </artifactId>
+            <artifactId>abdera-extensions-media</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>org.apache.abdera</groupId>
-            <artifactId>abdera-extensions-opensearch
-            </artifactId>
+            <artifactId>abdera-extensions-opensearch</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>compile</scope>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 </project>

Modified: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/AbstractAbderaHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/AbstractAbderaHandler.java?rev=782904&r1=782903&r2=782904&view=diff
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/AbstractAbderaHandler.java
(original)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/AbstractAbderaHandler.java
Tue Jun  9 08:08:13 2009
@@ -18,61 +18,43 @@
  */
 package org.apache.sling.atom.taglib;
 
+import javax.servlet.ServletContext;
 import javax.servlet.ServletRequest;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
 import javax.servlet.jsp.tagext.BodyTagSupport;
 
 import org.apache.abdera.Abdera;
-import org.apache.abdera.ext.media.MediaExtensionFactory;
-import org.apache.abdera.ext.opensearch.model.OpenSearchExtensionFactory;
-import org.apache.abdera.factory.Factory;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
-import org.apache.abdera.parser.stax.FOMFactory;
 
 public class AbstractAbderaHandler extends BodyTagSupport {
 
+    public static final String ABDERA_ATTRIBUTE = "org.apache.abdera.Abdera";
+
     private static final long serialVersionUID = 1L;
 
     public AbstractAbderaHandler() {
         super();
     }
 
-    protected Abdera getAbdera() {
-        final ServletRequest request = pageContext.getRequest();
-        return getAbdera(request);
+    protected Abdera getAbdera() throws JspException {
+        return getAbdera(pageContext);
     }
 
-    protected Abdera getAbdera(ServletRequest request) {
-        Abdera abdera;
-        if (request.getAttribute("abdera") != null
-            && (request.getAttribute("abdera") instanceof Abdera)) {
-            abdera = (Abdera) request.getAttribute("abdera");
-        } else {
-
-            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-            ClassLoader osgiClassloader = getClass().getClassLoader();
+    protected Abdera getAbdera(PageContext pageContexxt) throws JspException {
+        ServletContext context = pageContext.getServletContext();
 
-            Thread.currentThread().setContextClassLoader(osgiClassloader);
-
-            abdera = new Abdera();
-            new FOMFactory();
-            Factory f = abdera.getFactory();
-            if (f instanceof FOMFactory) {
-                FOMFactory ff = (FOMFactory) f;
-                // I know this sucks, but due to the OSGi-fication Abdera does
-                // not pick up extension factories automatically.
-                ff.registerExtension(new MediaExtensionFactory());
-                ff.registerExtension(new OpenSearchExtensionFactory());
-            }
-
-            Thread.currentThread().setContextClassLoader(classLoader);
-
-            request.setAttribute("abdera", abdera);
+        Object obj = context.getAttribute(ABDERA_ATTRIBUTE);
+        if (obj instanceof Abdera) {
+            return (Abdera) obj;
         }
-        return abdera;
+
+        // fallback to failure !!
+        throw new JspException("Abdera not available");
     }
 
-    protected Feed getFeed(ServletRequest request) {
+    protected Feed getFeed(ServletRequest request) throws JspException {
         Feed feed;
         if (request.getAttribute("feed") != null
             && (request.getAttribute("feed") instanceof Feed)) {
@@ -84,7 +66,7 @@
         return feed;
     }
 
-    protected Feed getFeed() {
+    protected Feed getFeed() throws JspException {
         final ServletRequest request = pageContext.getRequest();
         return getFeed(request);
     }

Modified: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/EntryTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/EntryTagHandler.java?rev=782904&r1=782903&r2=782904&view=diff
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/EntryTagHandler.java
(original)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/EntryTagHandler.java
Tue Jun  9 08:08:13 2009
@@ -54,7 +54,7 @@
     }
 
     @Override
-    public int doStartTag() {
+    public int doStartTag() throws JspException {
         Entry entry = getFeed().addEntry();
 
         setEntry(entry);

Modified: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/FeedTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/FeedTagHandler.java?rev=782904&r1=782903&r2=782904&view=diff
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/FeedTagHandler.java
(original)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/FeedTagHandler.java
Tue Jun  9 08:08:13 2009
@@ -124,7 +124,7 @@
     }
 
     @Override
-    public int doStartTag() {
+    public int doStartTag() throws JspException {
         final ServletRequest request = pageContext.getRequest();
         // create a feed
         Feed feed = getFeed(request);

Added: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java?rev=782904&view=auto
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
(added)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
Tue Jun  9 08:08:13 2009
@@ -0,0 +1,121 @@
+/*
+ * 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.atom.taglib.internal;
+
+import javax.servlet.ServletContext;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.ext.media.MediaExtensionFactory;
+import org.apache.abdera.ext.opensearch.model.OpenSearchExtensionFactory;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.parser.stax.FOMFactory;
+import org.apache.sling.atom.taglib.AbstractAbderaHandler;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class AbderaActivator implements BundleActivator {
+
+    private static final String SERVLET_CONTEXT_SERVICE = "javax.servlet.ServletContext";
+
+    private ServiceTracker servletContext;
+
+    private Abdera abdera;
+
+    public void start(BundleContext context) {
+        servletContext = new ServletContextTracker(context);
+        servletContext.open();
+    }
+
+    public void stop(BundleContext context) {
+        if (servletContext != null) {
+            servletContext.close();
+            servletContext = null;
+        }
+
+        abdera = null;
+    }
+
+    private class ServletContextTracker extends ServiceTracker {
+
+        private ServletContextTracker(BundleContext context) {
+            super(context, SERVLET_CONTEXT_SERVICE, null);
+        }
+
+        @Override
+        public Object addingService(ServiceReference reference) {
+            Object service = super.addingService(reference);
+            if (service instanceof ServletContext) {
+                ServletContext ctx = (ServletContext) service;
+                Object attr = ctx.getAttribute(AbstractAbderaHandler.ABDERA_ATTRIBUTE);
+                if (attr instanceof Abdera) {
+                    // nothing to do, strange that it already is here, though...
+                } else if (attr != null) {
+                    // some other attribute of the same name ??
+                    // might log this and do nothing !
+                } else {
+                    // create Abdera
+                    final Abdera abdera = getAbdera();
+                    ctx.setAttribute(AbstractAbderaHandler.ABDERA_ATTRIBUTE,
+                        abdera);
+                }
+            }
+            return service;
+        }
+
+        @Override
+        public void removedService(ServiceReference reference, Object service) {
+            if (service instanceof ServletContext) {
+                ServletContext ctx = (ServletContext) service;
+                Object attr = ctx.getAttribute(AbstractAbderaHandler.ABDERA_ATTRIBUTE);
+                if (attr == abdera) {
+                    ctx.removeAttribute(AbstractAbderaHandler.ABDERA_ATTRIBUTE);
+                }
+            }
+
+            super.removedService(reference, service);
+        }
+    }
+
+    private Abdera getAbdera() {
+        if (abdera == null) {
+            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+            try {
+                ClassLoader osgiClassloader = getClass().getClassLoader();
+                Thread.currentThread().setContextClassLoader(osgiClassloader);
+
+                abdera = new Abdera();
+                Factory f = abdera.getFactory();
+                if (f instanceof FOMFactory) {
+                    FOMFactory ff = (FOMFactory) f;
+                    ff.registerExtension(new MediaExtensionFactory());
+                    ff.registerExtension(new OpenSearchExtensionFactory());
+                }
+
+            } finally {
+
+                Thread.currentThread().setContextClassLoader(classLoader);
+            }
+        }
+
+        return abdera;
+    }
+}

Propchange: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/internal/AbderaActivator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Modified: incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/media/MediaGroupTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/media/MediaGroupTagHandler.java?rev=782904&r1=782903&r2=782904&view=diff
==============================================================================
--- incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/media/MediaGroupTagHandler.java
(original)
+++ incubator/sling/trunk/contrib/scripting/jsp-taglib-atom/src/main/java/org/apache/sling/atom/taglib/media/MediaGroupTagHandler.java
Tue Jun  9 08:08:13 2009
@@ -39,7 +39,7 @@
     }
 
     @Override
-    public int doStartTag() {
+    public int doStartTag() throws JspException {
         Entry entry = getEntry();
         // create the group element
 



Mime
View raw message