* @param webModule
* @return
* @throws OpenEJBException
*/
public WebModule deploy(final WebModule webModule) throws OpenEJBException {
final WebApp webApp = webModule.getWebApp();
if (webApp != null && webApp.isMetadataComplete()) {
return webModule;
}
/*
* Classes added to this set will be scanned for annotations
*/
final Set<Class> classes = new HashSet<Class>();
final ClassLoader classLoader = webModule.getClassLoader();
final String webXmlApplication = webApp.contextParamsAsMap().get("javax.ws.rs.Application");
if (webXmlApplication != null) {
webModule.getRestApplications().clear();
webModule.getRestApplications().add(webXmlApplication);
}
final Collection<String> restApp = webModule.getRestApplications();
if (restApp.isEmpty()) {
addRestClassesToScannedClasses(webModule, classes, classLoader);
} else {
for (final String rawClassName : restApp) {
final String application = realClassName(rawClassName);
if (application != null) {
final Class<?> clazz;
try {
clazz = classLoader.loadClass(application);
classes.add(clazz);
} catch (final ClassNotFoundException e) {
throw new OpenEJBException("Unable to load Application class: " + application, e);
}
if (Modifier.isAbstract(clazz.getModifiers())) {
continue;
}
try {
final Application app = Application.class.cast(clazz.newInstance());
try {
if (!app.getClasses().isEmpty()) {
classes.addAll(app.getClasses());
} else {
addRestClassesToScannedClasses(webModule, classes, classLoader);
}
} catch (final RuntimeException npe) {
if (app == null) {
throw npe;
}
// if app depends on cdi no need to do it
}
} catch (final InstantiationException e) {
throw new OpenEJBException("Unable to instantiate Application class: " + application, e);
} catch (final IllegalAccessException e) {
throw new OpenEJBException("Unable to access Application class: " + application, e);
}
}
}
}
/*
* Servlet classes are scanned
*/
for (final Servlet servlet : webApp.getServlet()) {
final String servletName = servlet.getServletName();
if ("javax.ws.rs.core.Application".equals(servletName)) {
servlet.setServletName(ProvidedJAXRSApplication.class.getName());
webModule.getRestApplications().add(ProvidedJAXRSApplication.class.getName());
for (final ServletMapping mapping : webApp.getServletMapping()) {
if (servletName.equals(mapping.getServletName())) {
mapping.setServletName(ProvidedJAXRSApplication.class.getName());
}
}
continue;
}
String servletClass = realClassName(servlet.getServletClass());
if (servletClass == null) { // try with servlet name, @see org.apache.openejb.arquillian.tests.jaxrs.basicapp.BasicApplication
servletClass = realClassName(servletName);
}
if (servletClass != null && servlet.getJspFile() == null) { // jaxrs application doesn't have a jsp file
if (!"org.apache.openejb.server.rest.OpenEJBRestServlet".equals(servletClass)) {
try {
final Class clazz = classLoader.loadClass(servletClass);
classes.add(clazz);
if (servlet.getServletClass() == null) {
servlet.setServletClass(servletClass);
}
} catch (final ClassNotFoundException e) {
if (servlet.getServletClass() != null) {
throw new OpenEJBException("Unable to load servlet class: " + servletClass, e);
} else {
logger.error("servlet " + servletName + " has no servlet-class defined and is not a subclass of Application");
}
}
}
// if the servlet is a rest init servlet don't deploy rest classes automatically
for (final ParamValue param : servlet.getInitParam()) {
if (param.getParamName().equals(Application.class.getName()) || param.getParamName().equals("javax.ws.rs.Application")) {
webModule.getRestApplications().clear();
webModule.getRestApplications().add(param.getParamValue());
break;
}
}
}
}
/*
* Filter classes are scanned
*/
for (final Filter filter : webApp.getFilter()) {
final String filterClass = realClassName(filter.getFilterClass());
if (filterClass != null) {
try {
final Class clazz = classLoader.loadClass(filterClass);
classes.add(clazz);
} catch (final ClassNotFoundException e) {
throw new OpenEJBException("Unable to load servlet filter class: " + filterClass, e);
}
}
}
/*
* Listener classes are scanned
*/
for (final Listener listener : webApp.getListener()) {
final String listenerClass = realClassName(listener.getListenerClass());
if (listenerClass != null) {
try {
final Class clazz = classLoader.loadClass(listenerClass);
classes.add(clazz);