Author: ogrisel
Date: Wed Mar 30 13:14:08 2011
New Revision: 1086943

URL: http://svn.apache.org/viewvc?rev=1086943&view=rev
Log:
STANBOL-120: better classloading context management for the JAX-RS application

Added:
    
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/BundleHttpContext.java
Removed:
    
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragmentHttpContext.java
    
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/resources/META-INF/templates/ajax/
Modified:
    
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java

Added: 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/BundleHttpContext.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/BundleHttpContext.java?rev=1086943&view=auto
==============================================================================
--- 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/BundleHttpContext.java
 (added)
+++ 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/BundleHttpContext.java
 Wed Mar 30 13:14:08 2011
@@ -0,0 +1,43 @@
+package org.apache.stanbol.commons.web;
+
+import java.net.URL;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * Custom HTTP Context to lookup the resources from the classloader of the 
OSGi bundle.
+ */
+public class BundleHttpContext implements HttpContext {
+
+    private Bundle bundle;
+
+    public BundleHttpContext(WebFragment fragment) {
+        this.bundle = fragment.getBundleContext().getBundle();
+    }
+
+    public BundleHttpContext(Bundle bundle) {
+        this.bundle = bundle;
+    }
+
+    public String getMimeType(String name) {
+        // someone in the chain seems to already be doing the Mime type mapping
+        return null;
+    }
+
+    public URL getResource(String name) {
+        if (name.startsWith("/")) {
+            name = name.substring(1);
+        }
+
+        return this.bundle.getResource(name);
+    }
+
+    public boolean handleSecurity(HttpServletRequest req, HttpServletResponse 
res) {
+        return true;
+    }
+
+}

Modified: 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java?rev=1086943&r1=1086942&r2=1086943&view=diff
==============================================================================
--- 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java
 (original)
+++ 
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java
 Wed Mar 30 13:14:08 2011
@@ -17,6 +17,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.stanbol.commons.web.resource.NavigationMixin;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.http.HttpService;
@@ -61,7 +62,7 @@ public class JerseyEndpoint {
 
     protected final List<WebFragment> webFragments = new 
ArrayList<WebFragment>();
 
-    protected ArrayList<String> registeredAlias;
+    protected ArrayList<String> registeredAliases;
 
     public Dictionary<String,String> getInitParams() {
         Dictionary<String,String> initParams = new Hashtable<String,String>();
@@ -74,7 +75,7 @@ public class JerseyEndpoint {
     @Activate
     protected void activate(ComponentContext ctx) throws IOException, 
ServletException, NamespaceException {
         this.componentContext = ctx;
-        this.registeredAlias = new ArrayList<String>();
+        this.registeredAliases = new ArrayList<String>();
 
         // register all the JAX-RS resources into a a JAX-RS application and 
bind it to a configurable URL
         // prefix
@@ -90,7 +91,7 @@ public class JerseyEndpoint {
         // register the root of static resources (TODO: move me in a dedicated 
fragment instead)
         String defaultStaticAlias = staticUrlRoot + "/default";
         httpService.registerResources(defaultStaticAlias, staticClasspath, 
null);
-        registeredAlias.add(defaultStaticAlias);
+        registeredAliases.add(defaultStaticAlias);
 
         // incrementally contribute fragment resources
         List<LinkResource> linkResources = new ArrayList<LinkResource>();
@@ -104,22 +105,17 @@ public class JerseyEndpoint {
             app.contributeTemplateLoader(fragment.getTemplateLoader());
             String resourceAlias = staticUrlRoot + '/' + fragment.getName();
             httpService.registerResources(resourceAlias, 
fragment.getStaticResourceClassPath(),
-                new WebFragmentHttpContext(fragment));
-            registeredAlias.add(resourceAlias);
+                new BundleHttpContext(fragment));
+            registeredAliases.add(resourceAlias);
         }
 
+        // bind the aggregate JAX-RS application to a dedicated servlet
         ServletContainer container = new ServletContainer(app);
-        String alias = (String) ctx.getProperties().get(ALIAS_PROPERTY);
-
-        // TODO: check whether this class-loading hack is still necessary or 
not
-        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
-        
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
-        try {
-            httpService.registerServlet(alias, container, getInitParams(), 
null);
-            registeredAlias.add(alias);
-        } finally {
-            Thread.currentThread().setContextClassLoader(classLoader);
-        }
+        String applicationAlias = (String) 
ctx.getProperties().get(ALIAS_PROPERTY);
+        Bundle appBundle = ctx.getBundleContext().getBundle();
+        httpService.registerServlet(applicationAlias, container, 
getInitParams(), new BundleHttpContext(
+                appBundle));
+        registeredAliases.add(applicationAlias);
 
         // forward the main Stanbol OSGi runtime context so that JAX-RS 
resources can lookup arbitrary
         // services
@@ -128,12 +124,12 @@ public class JerseyEndpoint {
         servletContext.setAttribute(NavigationMixin.STATIC_RESOURCES_ROOT_URL, 
staticUrlRoot);
         servletContext.setAttribute(NavigationMixin.LINK_RESOURCES, 
linkResources);
         servletContext.setAttribute(NavigationMixin.SCRIPT_RESOURCES, 
scriptResources);
-        log.info("JerseyEndpoint servlet registered at {}", alias);
+        log.info("JerseyEndpoint servlet registered at {}", applicationAlias);
     }
 
     @Deactivate
     protected void deactivate(ComponentContext ctx) {
-        for (String alias : registeredAlias) {
+        for (String alias : registeredAliases) {
             httpService.unregister(alias);
         }
         servletContext = null;


Reply via email to