Search in sources :

Example 41 with Activate

use of org.osgi.service.component.annotations.Activate 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 42 with Activate

use of org.osgi.service.component.annotations.Activate in project sling by apache.

the class ResourcePresenter method activate.

@Activate
public void activate(final ResourcePresenterConfiguration configuration, final BundleContext bundleContext) {
    logger.info("activating resource presenter for {}", configuration.path());
    path = configuration.path();
    this.bundleContext = bundleContext;
    try (final ResourceResolver resourceResolver = getServiceResourceResolver()) {
        final Resource resource = resourceResolver.getResource(path);
        if (resource != null) {
            registerResourcePresence();
        }
        registerResourceChangeListener();
    } catch (LoginException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LoginException(org.apache.sling.api.resource.LoginException) Activate(org.osgi.service.component.annotations.Activate)

Example 43 with Activate

use of org.osgi.service.component.annotations.Activate in project sling by apache.

the class MimeTypeServiceImpl method activate.

// ---------- SCR implementation -------------------------------------------
@Activate
protected void activate(final BundleContext context, final Config config) {
    context.addBundleListener(this);
    // register core and default sling mime types
    Bundle bundle = context.getBundle();
    registerMimeType(bundle.getEntry(CORE_MIME_TYPES));
    registerMimeType(bundle.getEntry(MIME_TYPES));
    // register maps of existing bundles
    Bundle[] bundles = context.getBundles();
    for (int i = 0; i < bundles.length; i++) {
        if ((bundles[i].getState() & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING)) != 0 && bundles[i].getBundleId() != bundle.getBundleId()) {
            this.registerMimeType(bundles[i].getEntry(MIME_TYPES));
        }
    }
    // register configuration properties
    if (config.mime_types() != null) {
        for (final String configType : config.mime_types()) {
            registerMimeType(configType);
        }
    }
    try {
        MimeTypeWebConsolePlugin plugin = new MimeTypeWebConsolePlugin(this);
        Dictionary<String, String> props = new Hashtable<>();
        props.put("felix.webconsole.label", MimeTypeWebConsolePlugin.LABEL);
        props.put("felix.webconsole.title", MimeTypeWebConsolePlugin.TITLE);
        props.put("felix.webconsole.category", "Sling");
        props.put("felix.webconsole.css", MimeTypeWebConsolePlugin.CSS_REFS);
        webConsolePluginService = context.registerService("javax.servlet.Servlet", plugin, props);
    } catch (Throwable t) {
    // don't care, we thus don't have the console plugin
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Activate(org.osgi.service.component.annotations.Activate)

Example 44 with Activate

use of org.osgi.service.component.annotations.Activate in project sling by apache.

the class ResourceValidationModelProviderImpl method activate.

@Activate
protected void activate(ComponentContext componentContext) throws LoginException {
    ResourceResolver rr = null;
    try {
        rr = rrf.getServiceResourceResolver(null);
        StringBuilder sb = new StringBuilder("(");
        String[] searchPaths = rr.getSearchPath();
        if (searchPaths.length > 1) {
            sb.append("|");
        }
        for (String searchPath : searchPaths) {
            sb.append("(path=").append(searchPath + "*").append(")");
        }
        sb.append(")");
        Dictionary<String, Object> eventHandlerProperties = new Hashtable<String, Object>();
        eventHandlerProperties.put(EventConstants.EVENT_TOPIC, TOPICS);
        eventHandlerProperties.put(EventConstants.EVENT_FILTER, sb.toString());
        eventHandlerRegistration = componentContext.getBundleContext().registerService(EventHandler.class, this, eventHandlerProperties);
        LOG.debug("Registered event handler for validation models in {}", sb.toString());
    } finally {
        if (rr != null) {
            rr.close();
        }
    }
}
Also used : Hashtable(java.util.Hashtable) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) EventHandler(org.osgi.service.event.EventHandler) Activate(org.osgi.service.component.annotations.Activate)

Example 45 with Activate

use of org.osgi.service.component.annotations.Activate in project sling by apache.

the class ValidationServiceImpl method activate.

@Activate
protected void activate(ValidationServiceConfiguration configuration) {
    this.configuration = configuration;
    ResourceResolver rr = null;
    try {
        rr = rrf.getServiceResourceResolver(null);
        searchPaths = Arrays.asList(rr.getSearchPath());
    } catch (LoginException e) {
        throw new IllegalStateException("Could not get service resource resolver to figure out search paths", e);
    } finally {
        if (rr != null) {
            rr.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

Activate (org.osgi.service.component.annotations.Activate)78 BundleContext (org.osgi.framework.BundleContext)12 OsgiWhiteboard (org.apache.jackrabbit.oak.osgi.OsgiWhiteboard)7 IOException (java.io.IOException)6 Hashtable (java.util.Hashtable)6 File (java.io.File)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)3 Whiteboard (org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard)3 ServiceReference (org.osgi.framework.ServiceReference)3 PromiseFactory (org.osgi.util.promise.PromiseFactory)3 ServerStartupObserver (org.wso2.carbon.core.ServerStartupObserver)3 JsonObject (com.google.gson.JsonObject)2 Dictionary (java.util.Dictionary)2 ObjectName (javax.management.ObjectName)2 WhiteboardExecutor (org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardExecutor)2 LoginException (org.apache.sling.api.resource.LoginException)2 ServiceListener (org.osgi.framework.ServiceListener)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2