LOGGER.error("bindServlet: Cannot register servlet {} without a servlet name", reference);
return false;
}
// check for Sling properties in the service registration
ServletResourceProvider provider = servletResourceProviderFactory.create(reference);
if (provider == null) {
// this is expected if the servlet is not destined for Sling
return false;
}
// only now try to access the servlet service, this may still fail
Servlet servlet = null;
try {
servlet = (Servlet) context.locateService(REF_SERVLET, reference);
} catch (Throwable t) {
LOGGER.warn("bindServlet: Failed getting the service for reference " + reference, t);
}
if (servlet == null) {
LOGGER.error("bindServlet: Servlet service not available from reference {}", reference);
return false;
}
// assign the servlet to the provider
provider.setServlet(servlet);
// initialize now
try {
servlet.init(new SlingServletConfig(servletContext, reference, name));
LOGGER.debug("bindServlet: Servlet {} added", name);
} catch (ServletException ce) {
LOGGER.error("bindServlet: Component " + name + " failed to initialize", ce);
return false;
} catch (Throwable t) {
LOGGER.error("bindServlet: Unexpected problem initializing component " + name, t);
return false;
}
final Dictionary<String, Object> params = new Hashtable<String, Object>();
params.put(ResourceProvider.ROOTS, provider.getServletPaths());
params.put(Constants.SERVICE_DESCRIPTION, "ServletResourceProvider for Servlets at "
+ Arrays.asList(provider.getServletPaths()));
final ServiceRegistration reg = context.getBundleContext()
.registerService(ResourceProvider.SERVICE_NAME, provider, params);
LOGGER.info("Registered {}", provider.toString());
synchronized (this.servletsByReference) {
servletsByReference.put(reference, new ServletReg(servlet, reg));
}
return true;