Search in sources :

Example 1 with ServletResourceProviderFactory

use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory in project sling by apache.

the class SlingServletResolver method activate.

// ---------- SCR Integration ----------------------------------------------
/**
     * Activate this component.
     */
@Activate
protected void activate(final BundleContext context, final Config config) throws LoginException {
    final Collection<PendingServlet> refs;
    synchronized (this.pendingServlets) {
        refs = new ArrayList<>(pendingServlets);
        pendingServlets.clear();
        this.sharedScriptResolver = resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "scripts"));
        this.searchPaths = this.sharedScriptResolver.getSearchPath();
        servletResourceProviderFactory = new ServletResourceProviderFactory(config.servletresolver_servletRoot(), this.searchPaths);
        // register servlets immediately from now on
        this.context = context;
    }
    createAllServlets(refs);
    // execution paths
    this.executionPaths = config.servletresolver_paths();
    if (this.executionPaths != null) {
        // we simply set the array to null
        if (this.executionPaths.length == 0) {
            this.executionPaths = null;
        } else {
            boolean hasRoot = false;
            for (int i = 0; i < this.executionPaths.length; i++) {
                final String path = this.executionPaths[i];
                if (path == null || path.length() == 0 || path.equals("/")) {
                    hasRoot = true;
                    break;
                }
            }
            if (hasRoot) {
                this.executionPaths = null;
            }
        }
    }
    this.defaultExtensions = config.servletresolver_defaultExtensions();
    // create cache - if a cache size is configured
    this.cacheSize = config.servletresolver_cacheSize();
    if (this.cacheSize > 5) {
        this.cache = new ConcurrentHashMap<>(cacheSize);
        this.logCacheSizeWarning = true;
    } else {
        this.cacheSize = 0;
    }
    // setup default servlet
    this.getDefaultServlet();
    // and finally register as event listener if we need to flush the cache
    if (this.cache != null) {
        final Dictionary<String, Object> props = new Hashtable<>();
        props.put("event.topics", new String[] { "javax/script/ScriptEngineFactory/*", "org/apache/sling/api/adapter/AdapterFactory/*", "org/apache/sling/scripting/core/BindingsValuesProvider/*" });
        props.put(ResourceChangeListener.PATHS, "/");
        props.put("service.description", "Apache Sling Servlet Resolver and Error Handler");
        props.put("service.vendor", "The Apache Software Foundation");
        this.eventHandlerReg = context.registerService(new String[] { ResourceChangeListener.class.getName(), EventHandler.class.getName() }, this, props);
    }
    this.plugin = new ServletResolverWebConsolePlugin(context);
    if (this.cacheSize > 0) {
        try {
            Dictionary<String, String> mbeanProps = new Hashtable<>();
            mbeanProps.put("jmx.objectname", "org.apache.sling:type=servletResolver,service=SlingServletResolverCache");
            ServletResolverCacheMBeanImpl mbean = new ServletResolverCacheMBeanImpl();
            mbeanRegistration = context.registerService(SlingServletResolverCacheMBean.class, mbean, mbeanProps);
        } catch (Throwable t) {
            LOGGER.debug("Unable to register mbean");
        }
    }
}
Also used : Hashtable(java.util.Hashtable) ServletResourceProviderFactory(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory) SlingServletResolverCacheMBean(org.apache.sling.servlets.resolver.jmx.SlingServletResolverCacheMBean) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with ServletResourceProviderFactory

use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory 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));
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) Field(java.lang.reflect.Field) Dictionary(java.util.Dictionary) ServletResourceProviderFactory(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ServletResourceProviderFactory (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 MockServiceReference (org.apache.sling.commons.testing.osgi.MockServiceReference)1 ServletResourceProvider (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider)1 SlingServletResolverCacheMBean (org.apache.sling.servlets.resolver.jmx.SlingServletResolverCacheMBean)1 Test (org.junit.Test)1 Activate (org.osgi.service.component.annotations.Activate)1