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 18:16:58 UTC
svn commit: r1086292 - in
/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web:
JerseyEndpoint.java JerseyEndpointApplication.java
processor/FreemarkerViewProcessor.java
Author: ogrisel
Date: Mon Mar 28 16:16:58 2011
New Revision: 1086292
URL: http://svn.apache.org/viewvc?rev=1086292&view=rev
Log:
STANBOL-120: refactoring the endpoint to use resources contributed by registered WebFragment instances
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
incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/processor/FreemarkerViewProcessor.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=1086292&r1=1086291&r2=1086292&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 16:16:58 2011
@@ -1,19 +1,21 @@
package org.apache.stanbol.commons.web;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
-import org.apache.clerezza.rdf.core.serializedform.Serializer;
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.Property;
import org.apache.felix.scr.annotations.Reference;
-import org.apache.stanbol.commons.web.processor.FreemarkerViewProcessor;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpService;
@@ -23,6 +25,8 @@ import org.slf4j.LoggerFactory;
import com.sun.jersey.spi.container.servlet.ServletContainer;
+import freemarker.cache.ClassTemplateLoader;
+
/**
* Jersey-based RESTful endpoint for the Stanbol Enhancer engines and store.
* <p>
@@ -30,6 +34,7 @@ import com.sun.jersey.spi.container.serv
* resources.
*/
@Component(immediate = true, metatype = true)
+@Reference(name = "webFragment", referenceInterface = WebFragment.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public class JerseyEndpoint {
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -49,11 +54,10 @@ public class JerseyEndpoint {
@Reference
HttpService httpService;
- @Reference
- Serializer serializer;
-
protected ServletContext servletContext;
+ protected final List<WebFragment> webFragments = new ArrayList<WebFragment>();
+
public Dictionary<String,String> getInitParams() {
Dictionary<String,String> initParams = new Hashtable<String,String>();
// make jersey automatically turn resources into Viewable models and
@@ -68,35 +72,43 @@ public class JerseyEndpoint {
// 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);
- String freemakerTemplates = (String) ctx.getProperties().get(FREEMARKER_TEMPLATE_CLASSPATH_PROPERTY);
+ String templateClasspath = (String) ctx.getProperties().get(FREEMARKER_TEMPLATE_CLASSPATH_PROPERTY);
+ // register the base template loader
+ templateClasspath.replaceAll("/$", "");
+ app.contributeTemplateLoader(new ClassTemplateLoader(getClass(), templateClasspath));
+
+ // register the root of static resources
+ httpService.registerResources(staticUrlRoot, staticClasspath, null);
+
+ // incrementally contribute fragment resources
+ for (WebFragment fragment : webFragments) {
+ app.contributeClasses(fragment.getJaxrsResourceClasses());
+ app.contributeSingletons(fragment.getJaxrsResourceSingletons());
+ app.contributeTemplateLoader(fragment.getTemplateLoader());
+ httpService.registerResources(staticUrlRoot + '/' + fragment.getName(),
+ fragment.getStaticResourceClassPath(), null);
+ }
log.info("Registering servlets with HTTP service " + httpService.toString());
+ 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);
- httpService.registerResources(staticUrlRoot, staticClasspath, null);
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
- // forward the main Stanbol Enhancer OSGi components to the servlet context so that
- // they can be looked up by the JAX-RS resources
+ // forward the main Stanbol OSGi runtime context so that JAX-RS resources can lookup arbitrary
+ // services
servletContext = container.getServletContext();
servletContext.setAttribute(BundleContext.class.getName(), ctx.getBundleContext());
- servletContext.setAttribute(Serializer.class.getName(), serializer);
-
servletContext.setAttribute(STATIC_RESOURCES_URL_ROOT_PROPERTY, staticUrlRoot);
- servletContext.setAttribute(FreemarkerViewProcessor.FREEMARKER_TEMPLATE_PATH_INIT_PARAM,
- freemakerTemplates);
-
log.info("Jersey servlet registered at {}", alias);
}
@@ -116,4 +128,13 @@ public class JerseyEndpoint {
this.httpService = null;
}
+ protected void bindWebFragment(WebFragment webFragment) {
+ // TODO: support some ordering for jax-rs resource and template overrides?
+ webFragments.add(webFragment);
+ }
+
+ protected void unbindWebFragment(WebFragment webFragment) {
+ webFragments.remove(webFragment);
+ }
+
}
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=1086292&r1=1086291&r2=1086292&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 16:16:58 2011
@@ -1,6 +1,8 @@
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;
@@ -11,6 +13,9 @@ import org.apache.stanbol.commons.web.wr
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import freemarker.cache.MultiTemplateLoader;
+import freemarker.cache.TemplateLoader;
+
/**
* Define the list of available resources and providers to be used by the Stanbol JAX-RS Endpoint.
*/
@@ -19,9 +24,11 @@ public class JerseyEndpointApplication e
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(JerseyEndpointApplication.class);
- public final Set<Class<?>> contributedClasses = new HashSet<Class<?>>();
+ protected final Set<Class<?>> contributedClasses = new HashSet<Class<?>>();
+
+ protected final Set<Object> contributedSingletons = new HashSet<Object>();
- public final Set<Object> contributedSingletons = new HashSet<Object>();
+ protected List<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>();
@Override
public Set<Class<?>> getClasses() {
@@ -40,9 +47,8 @@ public class JerseyEndpointApplication e
Set<Object> singletons = new HashSet<Object>();
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());
+ MultiTemplateLoader templateLoader = new MultiTemplateLoader((TemplateLoader[]) templateLoaders.toArray());
+ singletons.add(new FreemarkerViewProcessor(templateLoader));
return singletons;
}
@@ -53,4 +59,8 @@ public class JerseyEndpointApplication e
public void contributeSingletons(Set<Object> singletons) {
contributedSingletons.addAll(singletons);
}
+
+ public void contributeTemplateLoader(TemplateLoader templateLoader) {
+ this.templateLoaders.add(templateLoader);
+ }
}
Modified: incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/processor/FreemarkerViewProcessor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/processor/FreemarkerViewProcessor.java?rev=1086292&r1=1086291&r2=1086292&view=diff
==============================================================================
--- incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/processor/FreemarkerViewProcessor.java (original)
+++ incubator/stanbol/branches/http-endpoint-refactoring/commons/web/src/main/java/org/apache/stanbol/commons/web/processor/FreemarkerViewProcessor.java Mon Mar 28 16:16:58 2011
@@ -20,7 +20,7 @@ import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.spi.resource.Singleton;
import com.sun.jersey.spi.template.ViewProcessor;
-import freemarker.cache.ClassTemplateLoader;
+import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
@@ -58,18 +58,19 @@ import freemarker.template.Template;
@Provider
public class FreemarkerViewProcessor implements ViewProcessor<Template> {
- public static final String FREEMARKER_TEMPLATE_PATH_INIT_PARAM = "freemarker.template.path";
-
private final Logger log = LoggerFactory.getLogger(getClass());
protected Configuration freemarkerConfig;
protected String rootPath;
+
+ protected TemplateLoader templateLoader;
@Context
protected ServletContext context;
- public FreemarkerViewProcessor() {
+ public FreemarkerViewProcessor(TemplateLoader templateLoader) {
+ this.templateLoader = templateLoader;
}
/**
@@ -139,20 +140,9 @@ public class FreemarkerViewProcessor imp
protected Configuration getConfig() {
if (freemarkerConfig == null) {
// deferred initialization of the freemarker config to ensure that
- // the injected ServletContext is fully functional and for some
- // reason the #getInitParam access does not work hence using the
- // #getAttribute access after servlet init.
+ // the injected ServletContext is fully functional
Configuration config = new Configuration();
- if (context != null) {
- rootPath = (String) context.getAttribute(FREEMARKER_TEMPLATE_PATH_INIT_PARAM);
- }
- if (rootPath == null || rootPath.trim().length() == 0) {
- log.info("No 'freemarker.template.path' context-param, "
- + "defaulting to '/WEB-INF/templates'");
- rootPath = "/WEB-INF/templates";
- }
- rootPath = rootPath.replaceAll("/$", "");
- config.setTemplateLoader(new ClassTemplateLoader(getClass(), rootPath));
+ config.setTemplateLoader(templateLoader);
// TODO: make the usage of a freemaker properties file an explicit
// parameter declared in the servlet context instead of magic