// 1. If only one annotated SipServlet exists, then the SipApplication
// annotation is optional.
// Otherwise a logging error
SipApplication sipApplication = (SipApplication) sbd
.getSipApplication();
String ApplicationName = sipApplication.getAppName();
String mainServletName = sipApplication.getMainservletName();
Map<String, Servlet> annotatedServlets = sipApplication.getAnnotatedServlets();
if (annotatedServlets == null || annotatedServlets.isEmpty()) {
Extension.getInstance().processAnnotations(sipApplication);
}
// Find the number of annotated servlets
int noAnnotatedServlets = annotatedServlets.size();
// Find the number of servlets defined in the DD.
int declarativeDefinedServlets = sipApplication.getServlets().size();
// If multiple annotated servlets exists, then a main servlet has to be
// defined in the SipApplication annotation.
// If a servlet-mapping mechanism is used, then we do not need the
// main-servlet
if (mainServletName == null &&
noAnnotatedServlets > 1 &&
sipApplication.getServletMappings() == null) {
logger.log(Level.SEVERE,
"No main servlet defined in a SAR archive " +
"with multiple annotated servlets");
throw new DeploymentException(
"No main servlet defined in a SAR archive " +
"with multiple annotated servlets");
}
if ( mainServletName != null ) {
ExistsOperand existsOperand = new ExistsOperand();
existsOperand.setVariable("request.method");
Pattern pattern = new Pattern();
pattern.addCondition(existsOperand);
ServletMapping servletMapping = new ServletMapping();
servletMapping.setServletName(mainServletName);
servletMapping.setPattern(pattern);
// applicable if main servlet is annotated
Servlet servlet = annotatedServlets.remove(mainServletName);
if ( servlet != null ) {
sipApplication.addServlet(servlet);
}
sipApplication.addServletMapping(servletMapping);
}
for (String servletName : annotatedServlets.keySet()) {
Servlet servlet = annotatedServlets.get(servletName);
// Check that the servlet belongs to the same application, by bening
// defined in same package
// Check that this servlet name is not defined in sip.xml
// then ignore it.
Map<String, Servlet> servlets = sipApplication.getServlets();
// Servlets defined in sip.xml overrides annotations
if (servlets.get(servletName) != null) {
// Servlet specified in sip.xml
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO,
"Annotated servlet found with same name " +
"as servlet defined in sip.xml name = " + servletName);
logger.log(Level.INFO, "Annotated servlet discarded");
}
continue;
}
// check if the same servlet class has been defined twice using annotations and sip.xml
boolean isDuplicateClass = false;
Iterator servletNameIter = sipApplication.getServletNames();
while( servletNameIter.hasNext()) {
String servletNameinDD = (String) servletNameIter.next();
if(sipApplication.getServlet(servletNameinDD).getServletClass().equals(
servlet.getServletClass())) {
String msg = newLocalStrings.getString(
"enterprise.deployment.backend.sip.same.servlet.class.used",
servlet.getServletName());
logger.log(Level.WARNING, msg);
isDuplicateClass = true;
break;
}
}
if(isDuplicateClass) continue;
/*
* Check that the Servlet belongs to the same Application, That is
* 1. Same Package as the package-info.java package annotation, or
* 2. Same Application Name
*
*/
String servletPackage = servlet.getPackageName();
String anotatedServletApplication = servlet.getApplicationName();
/// XXX: Fix me later
/*
if (!new ApplicationMatcher(sipApplication).match(servletPackage,
anotatedServletApplication)) {
// Not in same application, discharge his servlet
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO,
"Annotated servlet found " + servletName +
" Servlet not defined as part of Sip Application " +
sipApplication.getAppName() + "and discharged");
}
continue;
} else {
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO,
"Deploying " + servletName +
" as belonging to application " +
sipApplication.getAppName());
}
}
*/
ExistsOperand existsOperand = new ExistsOperand();
// If only one annotated servlet exist, this is regarded as main
// servlet
if (servletName.equals(mainServletName) ||
(noAnnotatedServlets == 1)) {
// Create a match all Pattern
existsOperand.setVariable("request.method");
} else {
// Create a match nothing Pattern
// This is not part of Operand.getAttributeValue()
existsOperand.setVariable("NeverMatchMe");
}
sipApplication.addServlet(servlet);
if(sipApplication.getServletMappings(servletName) == null ) {
Pattern pattern = new Pattern();
pattern.addCondition(existsOperand);
ServletMapping servletMapping = new ServletMapping();
servletMapping.setServletName(servletName);
servletMapping.setPattern(pattern);
sipApplication.addServletMapping(servletMapping);
}
}
}