Author: aparfonov
Date: 2009-10-19 18:48:23 -0400 (Mon, 19 Oct 2009)
New Revision: 304
Modified:
ws/branches/2.2.x/rest/core/pom.xml
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
Log:
EXOJCR-185 : Add annotation scanning feature
Modified: ws/branches/2.2.x/rest/core/pom.xml
===================================================================
--- ws/branches/2.2.x/rest/core/pom.xml 2009-10-19 15:34:14 UTC (rev 303)
+++ ws/branches/2.2.x/rest/core/pom.xml 2009-10-19 22:48:23 UTC (rev 304)
@@ -75,6 +75,10 @@
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</dependency>
+ <dependency>
+ <groupId>net.sf.scannotation</groupId>
+ <artifactId>scannotation</artifactId>
+ </dependency>
</dependencies>
<build>
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java 2009-10-19
15:34:14 UTC (rev 303)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java 2009-10-19
22:48:23 UTC (rev 304)
@@ -18,17 +18,31 @@
*/
package org.exoplatform.services.rest.servlet;
+import org.exoplatform.services.log.ExoLogger;
+
+import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.DependencySupplier;
+import org.exoplatform.services.rest.Filter;
import org.exoplatform.services.rest.RequestHandler;
import org.exoplatform.services.rest.ResourceBinder;
import org.exoplatform.services.rest.impl.ApplicationDeployer;
import org.exoplatform.services.rest.impl.ProviderBinder;
import org.exoplatform.services.rest.impl.ResourceBinderImpl;
import org.exoplatform.services.rest.impl.RequestHandlerImpl;
+import org.scannotation.AnnotationDB;
+import org.scannotation.WarUrlFinder;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.util.Map;
+import java.util.Set;
+
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
+import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.Provider;
/**
* Initialize required components of JAX-RS framework and deploy single JAX-RS
application.
@@ -39,6 +53,8 @@
public class RestInitializedListener implements ServletContextListener
{
+ private static final Log LOG = ExoLogger.getLogger(RestInitializedListener.class);
+
/**
* {@inheritDoc}
*/
@@ -51,14 +67,25 @@
*/
public void contextInitialized(ServletContextEvent event)
{
+ boolean scan =
Boolean.parseBoolean(event.getServletContext().getInitParameter("ws.rs.scan.components"));
String dependencyInjectorFQN =
event.getServletContext().getInitParameter(DependencySupplier.class.getName());
- DependencySupplier dependencySupplier = null;
- if (dependencyInjectorFQN != null)
+
+ ResourceBinder resources = new ResourceBinderImpl();
+ ApplicationDeployer deployer = new ApplicationDeployer(resources,
ProviderBinder.getInstance());
+
+ String applicationFQN =
event.getServletContext().getInitParameter("javax.ws.rs.Application");
+ if (applicationFQN != null)
{
+ if (scan)
+ {
+ String msg = "Scan of rest components is disabled cause to specified
'javax.ws.rs.Application'.";
+ LOG.warn(msg);
+ }
try
{
- Class<?> cl =
Thread.currentThread().getContextClassLoader().loadClass(dependencyInjectorFQN.trim());
- dependencySupplier = (DependencySupplier)cl.newInstance();
+ Class<?> cl =
Thread.currentThread().getContextClassLoader().loadClass(applicationFQN.trim());
+ Application application = (Application)cl.newInstance();
+ deployer.deploy(application);
}
catch (ClassNotFoundException cnfe)
{
@@ -73,18 +100,59 @@
throw new RuntimeException(iae);
}
}
+ else if (scan)
+ {
+ URL classes = WarUrlFinder.findWebInfClassesPath(event);
+ URL[] libs = WarUrlFinder.findWebInfLibClasspaths(event);
+ AnnotationDB annotationDB = new AnnotationDB();
+ // Disable processing of API, implementation and JAX-RS packages
+ annotationDB.setIgnoredPackages(new
String[]{"org.exoplatform.services.rest", "javax.ws.rs"});
+ try
+ {
+ if (classes != null)
+ annotationDB.scanArchives(classes);
+ annotationDB.scanArchives(libs);
- ResourceBinder resources = new ResourceBinderImpl();
- ApplicationDeployer deployer = new ApplicationDeployer(resources,
ProviderBinder.getInstance());
+ Map<String, Set<String>> results =
annotationDB.getAnnotationIndex();
+ for (String annotation : new String[]{Path.class.getName(),
Provider.class.getName(),
+ Filter.class.getName()})
+ {
+ if (results.get(annotation) != null)
+ {
+ for (String fqn : results.get(annotation))
+ {
+ try
+ {
+ Class<?> cl =
Thread.currentThread().getContextClassLoader().loadClass(fqn);
+ if (cl.isInterface() || Modifier.isAbstract(cl.getModifiers()))
+ {
+ LOG.info("Skip abstract class or interface " +
fqn);
+ continue;
+ }
+ deployer.deploy(cl);
+ LOG.info("Deployed component: " + fqn);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
- String applicationFQN =
event.getServletContext().getInitParameter("javax.ws.rs.Application");
- if (applicationFQN != null)
+ DependencySupplier dependencySupplier = null;
+ if (dependencyInjectorFQN != null)
{
try
{
- Class<?> cl =
Thread.currentThread().getContextClassLoader().loadClass(applicationFQN.trim());
- Application application = (Application)cl.newInstance();
- deployer.deploy(application);
+ Class<?> cl =
Thread.currentThread().getContextClassLoader().loadClass(dependencyInjectorFQN.trim());
+ dependencySupplier = (DependencySupplier)cl.newInstance();
}
catch (ClassNotFoundException cnfe)
{