You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by og...@apache.org on 2011/03/28 16:22:06 UTC

svn commit: r1086250 - in /incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web: JaxrsResource.java JerseyEndpoint.java JerseyEndpointApplication.java WebFragment.java

Author: ogrisel
Date: Mon Mar 28 14:22:06 2011
New Revision: 1086250

URL: http://svn.apache.org/viewvc?rev=1086250&view=rev
Log:
refactored JerseyEndpointApplication to make the list of contributed resources dynamicly contributed using WebFragments

Added:
    incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragment.java
Removed:
    incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JaxrsResource.java
Modified:
    incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpoint.java
    incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpointApplication.java

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=1086250&r1=1086249&r2=1086250&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 Mon Mar 28 14:22:06 2011
@@ -55,12 +55,7 @@ public class JerseyEndpoint {
     protected ServletContext servletContext;
 
     public Dictionary<String,String> getInitParams() {
-        // pass configuration for Jersey resource
-        // TODO: make the list of enabled JAX-RS resources and providers
-        // configurable using an OSGi service
         Dictionary<String,String> initParams = new Hashtable<String,String>();
-        initParams.put("javax.ws.rs.Application", JerseyEndpointApplication.class.getName());
-
         // make jersey automatically turn resources into Viewable models and
         // hence lookup matching freemarker templates
         initParams.put("com.sun.jersey.config.feature.ImplicitViewables", "true");
@@ -70,8 +65,13 @@ public class JerseyEndpoint {
     @Activate
     protected void activate(ComponentContext ctx) throws IOException, ServletException, NamespaceException {
 
-        // register the JAX-RS resources as a servlet under configurable alias
-        ServletContainer container = new ServletContainer();
+        // register all the JAX-RS resources into a a JAX-RS application and bind it to a configurable URL
+        // prefix
+        JerseyEndpointApplication app = new JerseyEndpointApplication();
+
+        // TODO contribute classes and singletons to the app here
+
+        ServletContainer container = new ServletContainer(app);
         String alias = (String) ctx.getProperties().get(ALIAS_PROPERTY);
         String staticUrlRoot = (String) ctx.getProperties().get(STATIC_RESOURCES_URL_ROOT_PROPERTY);
         String staticClasspath = (String) ctx.getProperties().get(STATIC_RESOURCES_CLASSPATH_PROPERTY);

Modified: incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpointApplication.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpointApplication.java?rev=1086250&r1=1086249&r2=1086250&view=diff
==============================================================================
--- incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpointApplication.java (original)
+++ incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/JerseyEndpointApplication.java Mon Mar 28 14:22:06 2011
@@ -1,8 +1,6 @@
 package org.apache.stanbol.commons.web;
 
-import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 import javax.ws.rs.core.Application;
@@ -18,20 +16,20 @@ import org.slf4j.LoggerFactory;
  */
 public class JerseyEndpointApplication extends Application {
 
+    @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(JerseyEndpointApplication.class);
-    
-    List<JaxrsResource> jaxrsResources = new ArrayList<JaxrsResource>();
-    
+
+    public final Set<Class<?>> contributedClasses = new HashSet<Class<?>>();
+
+    public final Set<Object> contributedSingletons = new HashSet<Object>();
+
     @Override
     public Set<Class<?>> getClasses() {
         Set<Class<?>> classes = new HashSet<Class<?>>();
-        
-        // resources
-        for (JaxrsResource jr : this.jaxrsResources) {
-            classes.add(jr.getClass());
-        }
 
-        // message body writers
+        classes.addAll(contributedClasses);
+
+        // message body writers, hard-coded for now
         classes.add(GraphWriter.class);
         classes.add(ResultSetWriter.class);
         return classes;
@@ -40,27 +38,19 @@ public class JerseyEndpointApplication e
     @Override
     public Set<Object> getSingletons() {
         Set<Object> singletons = new HashSet<Object>();
-        // view processors
+        singletons.addAll(contributedSingletons);
+
+        // view processors, hard-coded for now
+        // TODO make it possible to pass the template loaders from the OSGi context here:
         singletons.add(new FreemarkerViewProcessor());
         return singletons;
     }
-    
-    
-    public void bindJaxrsResource(JaxrsResource jr) {
-        synchronized (this.jaxrsResources) {
-            this.jaxrsResources.add(jr);
-        }
-        if (log.isInfoEnabled()) {
-            log.info("JaxrsResource {} added to list: {}", jr, this.jaxrsResources);
-        }
+
+    public void contributeClasses(Set<Class<?>> classes) {
+        contributedClasses.addAll(classes);
     }
 
-    public void unbindEnhancementEngine(JaxrsResource jr) {
-        synchronized (this.jaxrsResources) {
-            this.jaxrsResources.remove(jr);
-        }
-        if (log.isInfoEnabled()) {
-            log.info("JaxrsResource {} removed to list: {}", jr, this.jaxrsResources);
-        }
+    public void contributeSingletons(Set<Object> singletons) {
+        contributedSingletons.addAll(singletons);
     }
 }

Added: incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragment.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragment.java?rev=1086250&view=auto
==============================================================================
--- incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragment.java (added)
+++ incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/WebFragment.java Mon Mar 28 14:22:06 2011
@@ -0,0 +1,50 @@
+package org.apache.stanbol.commons.web;
+
+import java.util.Set;
+
+import freemarker.cache.TemplateLoader;
+
+/**
+ * Interface to be implemented by bundles that want to customize the stanbol web interface and REST API by
+ * contributing static resources, JAX-RS resources and Freemarker views.
+ */
+public interface WebFragment {
+
+    /**
+     * Name of the fragment. Should be a lowercase short name without any kind of special character, so as to
+     * be used as a path component in the URL of the static resources.
+     */
+    public String getName();
+
+    /**
+     * Java package name that is the classloading root of the static resources of the fragment to be published
+     * by the OSGi HttpService under /static-url-root/fragment-name/
+     * 
+     * Note: this package should be exported by the bundle.
+     */
+    public String getStaticResourceClassPath();
+
+    /**
+     * Set of JAX-RS resources provided as classes.
+     * 
+     * Note: those classes should be visible: use the Export-Package bundle declaration to export their
+     * packages.
+     */
+    public Set<Class<?>> getJaxrsResourceClasses();
+
+    /**
+     * Set of JAX-RS resources provided as singleton instances.
+     * 
+     * Note: those objects should be visible: use the Export-Package bundle declaration to export their
+     * packages.
+     */
+    public Set<Object> getJaxrsResourceSingletons();
+
+    /**
+     * @return a template load instance that can be used by the FreemarkerViewProcessor for building the HTML
+     *         UI incrementally. If this is an instance of ClassTemplateLoader, the class path visibility
+     *         should be exported using the Export-Package bundle declaration.
+     */
+    public TemplateLoader getTemplateLoader();
+
+}