public void merge(Class<?>[] classes, WebAppType webApp, MergeContext mergeContext) throws DeploymentException {
for (Class<?> cls : classes) {
if (!HttpServlet.class.isAssignableFrom(cls)) {
throw new DeploymentException("The class " + cls.getName() + " with WebServlet annotation must extend javax.servlet.HttpServlet");
}
WebServlet webServlet = cls.getAnnotation(WebServlet.class);
boolean valueAttributeConfigured = webServlet.value().length > 0;
boolean urlPatternsAttributeConfigured = webServlet.urlPatterns().length > 0;
if (valueAttributeConfigured && urlPatternsAttributeConfigured) {
throw new DeploymentException("value and urlPatterns must not be configured on the same WebServlet annotation in the class " + cls.getName());
}
if (!valueAttributeConfigured && !urlPatternsAttributeConfigured) {
throw new DeploymentException("At least one of value and urlPatterns attribute should be configured on the WebServlet annotation in the class " + cls.getName());
}
String servletName = webServlet.name().length() == 0 ? cls.getName() : webServlet.name();
String[] urlPatterns = valueAttributeConfigured ? webServlet.value() : webServlet.urlPatterns();
if (ServletMergeHandler.isServletConfigured(servletName, mergeContext)) {
ServletType targetServlet = ServletMergeHandler.getServlet(servletName, mergeContext);
//merge init-params, we only merge those init-param that are not explicitly configured in the web.xml or web-fragment.xml
for (WebInitParam webInitParam : webServlet.initParams()) {
String paramName = webInitParam.name();
if (ServletInitParamMergeHandler.isServletInitParamConfigured(servletName, paramName, mergeContext)) {
continue;
}
ParamValueType newParamValue = targetServlet.addNewInitParam();
newParamValue.addNewDescription().setStringValue(webInitParam.description());
newParamValue.addNewParamName().setStringValue(webInitParam.name());
newParamValue.addNewParamValue().setStringValue(webInitParam.value());
ServletInitParamMergeHandler.addServletInitParam(servletName, newParamValue, ElementSource.ANNOTATION, mergeContext.getCurrentJarUrl(), mergeContext);
}
} else {
//Add a new Servlet
//create servlet element
ServletType newServlet = webApp.addNewServlet();
if (!webServlet.displayName().isEmpty()) {
newServlet.addNewDisplayName().setStringValue(webServlet.displayName());
}
newServlet.addNewServletClass().setStringValue(cls.getName());
newServlet.addNewServletName().setStringValue(servletName);
newServlet.addNewAsyncSupported().setBooleanValue(webServlet.asyncSupported());
if (!webServlet.description().isEmpty()) {
newServlet.addNewDescription().setStringValue(webServlet.description());
}
if (webServlet.loadOnStartup() != -1) {
newServlet.setLoadOnStartup(webServlet.loadOnStartup());
}
for (WebInitParam webInitParam : webServlet.initParams()) {
ParamValueType paramValue = newServlet.addNewInitParam();
paramValue.addNewDescription().setStringValue(webInitParam.description());
paramValue.addNewParamName().setStringValue(webInitParam.name());
paramValue.addNewParamValue().setStringValue(webInitParam.value());
}
if (!webServlet.smallIcon().isEmpty() || !webServlet.largeIcon().isEmpty()) {
IconType iconType = newServlet.addNewIcon();
if (!webServlet.smallIcon().isEmpty()) {
iconType.addNewSmallIcon().setStringValue(webServlet.smallIcon());
}
if (!webServlet.largeIcon().isEmpty()) {
iconType.addNewLargeIcon().setStringValue(webServlet.largeIcon());
}
}
//TODO Figure out how to handle MultipartConfig annotation
MultipartConfig multipartConfig = cls.getAnnotation(MultipartConfig.class);
if (multipartConfig != null) {