if (!(module instanceof WebModule)) {
// not a web module, nothing to do
return;
}
WebModule webModule = (WebModule) module;
WebApp webApp = webModule.getSpecDD();
ServiceReference reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
Set<Class<? extends Application>> applicationClasses = new HashSet<Class<? extends Application>>();
try {
PackageAdmin packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
BundleClassFinder bundleClassFinder = new BundleAssignableClassFinder(packageAdmin, bundle, new Class<?>[] { Application.class }, new ClassDiscoveryFilter() {
@Override
public boolean directoryDiscoveryRequired(String directory) {
return true;
}
@Override
public boolean jarFileDiscoveryRequired(String jarUrl) {
return true;
}
@Override
public boolean packageDiscoveryRequired(String packageName) {
return true;
}
@Override
public boolean rangeDiscoveryRequired(DiscoveryRange discoveryRange) {
return discoveryRange.equals(DiscoveryRange.BUNDLE_CLASSPATH);
}
});
Set<String> classes = bundleClassFinder.find();
for (String clazz : classes) {
applicationClasses.add(bundle.loadClass(clazz).asSubclass(Application.class));
}
} catch (Exception e) {
throw new DeploymentException("Fail to scan javax.ws.rs.core.Application sub classes in application", e);
} finally {
bundle.getBundleContext().ungetService(reference);
}
// JAX-RS specific code here to initialize the runtime and setup the mapping etc.
// there's no Application sub classes found
if (applicationClasses == null || applicationClasses.size() == 0) {
/*
* TODO jaxrs 1.1 spec section 2.3.2 If no Application subclass is present the added servlet MUST be named ...
*/
return;
}
/*
* jaxrs 1.1 spec section 2.3.2 If an Application subclass is present and there is already a servlet defined that has a servlet initialization
* ...
*/
Class<? extends Application> applicationClass;
BundleClassLoader bundleClassLoader = new BundleClassLoader(bundle);
for (Servlet servlet : webApp.getServlet()) {
List<ParamValue> params = servlet.getInitParam();
for (ParamValue parm : params) {
if (!parm.getParamName().trim().equals("javax.ws.rs.Application")) {
continue;
}
for (Class<? extends Application> clazz : applicationClasses) {
if (clazz.getName().equalsIgnoreCase(parm.getParamValue().trim())) {
applicationClass = clazz;
Class<?> servletClass = null;
try {
servletClass = bundleClassLoader.loadClass(servlet.getServletClass());
} catch (ClassNotFoundException e) {
log.warn("failed to load servlet class:" + servlet.getServletClass());
}
if ((servletClass == null) || !servletClass.isAssignableFrom(HttpServlet.class)) {
servlet.setServletClass(REST_SERVLET_NAME);
}
ParamValue paramDeploymentConfig = new ParamValue();
paramDeploymentConfig.setParamName(RestServlet.DEPLOYMENT_CONF_PARAM);
paramDeploymentConfig.setParamValue(GeronimoWinkDeloymentConfiguration.class.getName());
servlet.getInitParam().add(paramDeploymentConfig);
return;
}
}
}
}
/*
* jaxrs 1.1 spec section 2.3.2 If an Application subclass is present ...
*
*
* TODO It is an error for more than one application to be deployed at the same effective servlet mapping
*/
applicationClass = applicationClasses.iterator().next();
Servlet restServletInfo = new Servlet();
restServletInfo.setServletClass(REST_SERVLET_NAME);
restServletInfo.setServletName(REST_SERVLET_NAME);
ParamValue paramApplication = new ParamValue();
paramApplication.setParamName("javax.ws.rs.Application");
paramApplication.setParamValue(applicationClass.getName());
restServletInfo.getInitParam().add(paramApplication);
ParamValue paramDeploymentConfig = new ParamValue();
paramDeploymentConfig.setParamName("deploymentConfiguration");
paramDeploymentConfig.setParamValue(GeronimoWinkDeloymentConfiguration.class.getName());
restServletInfo.getInitParam().add(paramDeploymentConfig);
if (applicationClass.isAnnotationPresent(ApplicationPath.class)) {
ApplicationPath ap = applicationClass.getAnnotation(ApplicationPath.class);
String mapping = ap.value();
if (!mapping.startsWith("/") || !mapping.startsWith("*.")) {
mapping = "/" + mapping;
}
if (!mapping.endsWith("/*")) {
if (mapping.endsWith("/"))
mapping = mapping + "*";
else {
mapping = mapping + "/*";
}
}
ServletMapping restServletMapping = new ServletMapping();
restServletMapping.setServletName(REST_SERVLET_NAME);
restServletMapping.getUrlPattern().add(mapping);
webApp.getServletMapping().add(restServletMapping);
}
webApp.getServlet().add(restServletInfo);
}