* @param webModule
* @return
* @throws OpenEJBException
*/
public WebModule deploy(WebModule webModule) throws OpenEJBException {
WebApp webApp = webModule.getWebApp();
if (webApp != null && webApp.isMetadataComplete()) return webModule;
/*
* Classes added to this set will be scanned for annotations
*/
Set<Class> classes = new HashSet<Class>();
ClassLoader classLoader = webModule.getClassLoader();
final String webXmlApplication = webApp.contextParamsAsMap().get("javax.ws.rs.Application");
if (webXmlApplication != null) {
webModule.getRestApplications().clear();
webModule.getRestApplications().add(webXmlApplication);
}
Collection<String> restApp = webModule.getRestApplications();
if (restApp.isEmpty()) {
for (String rawClassName : webModule.getRestClasses()) {
final String className = realClassName(rawClassName);
if (className != null) {
Class<?> clazz;
try {
clazz = classLoader.loadClass(className);
classes.add(clazz);
} catch (ClassNotFoundException e) {
throw new OpenEJBException("Unable to load REST class: " + className, e);
}
}
}
} else {
for (String rawClassName : restApp) {
final String application = realClassName(rawClassName);
if (application != null) {
Class<?> clazz;
try {
clazz = classLoader.loadClass(application);
classes.add(clazz);
} catch (ClassNotFoundException e) {
throw new OpenEJBException("Unable to load Application class: " + application, e);
}
try {
Application app = Application.class.cast(clazz.newInstance());
classes.addAll(app.getClasses());
} catch (InstantiationException e) {
throw new OpenEJBException("Unable to instantiate Application class: " + application, e);
} catch (IllegalAccessException e) {
throw new OpenEJBException("Unable to access Application class: " + application, e);
}
}
}
}
/*
* Servlet classes are scanned
*/
for (Servlet servlet : webApp.getServlet()) {
String servletClass = realClassName(servlet.getServletClass());
if (servletClass == null) { // try with servlet name, @see org.apache.openejb.arquillian.tests.jaxrs.basicapp.BasicApplication
servletClass = realClassName(servlet.getServletName());
}
if (servletClass != null) {
if (!"org.apache.openejb.server.rest.OpenEJBRestServlet".equals(servletClass)) {
try {
Class clazz = classLoader.loadClass(servletClass);
classes.add(clazz);
if (servlet.getServletClass() == null) {
servlet.setServletClass(servletClass);
}
} catch (ClassNotFoundException e) {
if (servlet.getServletClass() != null) {
throw new OpenEJBException("Unable to load servlet class: " + servletClass, e);
} else {
logger.error("servlet " + servlet.getServletName() + " 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 (ParamValue param : servlet.getInitParam()) {
if (param.getParamName().equals(Application.class.getName())) {
webModule.getRestApplications().clear();
webModule.getRestApplications().add(param.getParamValue());
break;
}
}
}
}
/*
* Filter classes are scanned
*/
for (Filter filter : webApp.getFilter()) {
String filterClass = realClassName(filter.getFilterClass());
if (filterClass != null) {
try {
Class clazz = classLoader.loadClass(filterClass);
classes.add(clazz);
} catch (ClassNotFoundException e) {
throw new OpenEJBException("Unable to load servlet filter class: " + filterClass, e);
}
}
}
/*
* Listener classes are scanned
*/
for (Listener listener : webApp.getListener()) {
String listenerClass = realClassName(listener.getListenerClass());
if (listenerClass != null) {
try {
Class clazz = classLoader.loadClass(listenerClass);
classes.add(clazz);