use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider in project sling by apache.
the class SlingServletResolver method createServlet.
private boolean createServlet(final Servlet servlet, final ServiceReference<Servlet> reference) {
// check for a name, this is required
final String name = getName(reference);
// check for Sling properties in the service registration
final ServletResourceProvider provider = servletResourceProviderFactory.create(reference, servlet);
if (provider == null) {
// this is expected if the servlet is not destined for Sling
return false;
}
// initialize now
try {
servlet.init(new SlingServletConfig(servletContext, reference, name));
LOGGER.debug("bindServlet: Servlet {} initialized", name);
} catch (ServletException ce) {
LOGGER.error("bindServlet: Servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference) + " failed to initialize", ce);
return false;
} catch (Throwable t) {
LOGGER.error("bindServlet: Unexpected problem initializing servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference), t);
return false;
}
final List<ServiceRegistration<ResourceProvider<Object>>> regs = new ArrayList<>();
for (final String root : provider.getServletPaths()) {
@SuppressWarnings("unchecked") final ServiceRegistration<ResourceProvider<Object>> reg = (ServiceRegistration<ResourceProvider<Object>>) context.registerService(ResourceProvider.class.getName(), provider, createServiceProperties(reference, provider, root));
regs.add(reg);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered {}", provider.toString());
}
synchronized (this.servletsByReference) {
servletsByReference.put(reference, new ServletReg(servlet, regs));
}
return true;
}
use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider in project sling by apache.
the class SlingServletResolverTest method testCreateServiceRegistrationProperties.
@Test
public void testCreateServiceRegistrationProperties() throws Throwable {
MockServiceReference msr = new MockServiceReference(null);
msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, "sample");
msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "GET");
Field srpf = SlingServletResolver.class.getDeclaredField("servletResourceProviderFactory");
srpf.setAccessible(true);
ServletResourceProviderFactory factory = (ServletResourceProviderFactory) srpf.get(servletResolver);
ServletResourceProvider servlet = factory.create(msr, null);
Method createServiceProperties = SlingServletResolver.class.getDeclaredMethod("createServiceProperties", ServiceReference.class, ServletResourceProvider.class, String.class);
createServiceProperties.setAccessible(true);
// no ranking
assertNull(msr.getProperty(Constants.SERVICE_RANKING));
@SuppressWarnings("unchecked") final Dictionary<String, Object> p1 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
assertNull(p1.get(Constants.SERVICE_RANKING));
// illegal type of ranking
Object nonIntValue = "Some Non Integer Value";
msr.setProperty(Constants.SERVICE_RANKING, nonIntValue);
assertEquals(nonIntValue, msr.getProperty(Constants.SERVICE_RANKING));
@SuppressWarnings("unchecked") final Dictionary<String, Object> p2 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
assertNull(p2.get(Constants.SERVICE_RANKING));
// illegal type of ranking
Object intValue = Integer.valueOf(123);
msr.setProperty(Constants.SERVICE_RANKING, intValue);
assertEquals(intValue, msr.getProperty(Constants.SERVICE_RANKING));
@SuppressWarnings("unchecked") final Dictionary<String, Object> p3 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
assertEquals(intValue, p3.get(Constants.SERVICE_RANKING));
}
Aggregations