Search in sources :

Example 1 with SlingRepositoryInitializer

use of org.apache.sling.jcr.api.SlingRepositoryInitializer in project sling by apache.

the class RepositoryInitializersTest method registerInitializer.

private void registerInitializer(String id, int serviceRanking) {
    final SlingRepositoryInitializer init = new TestInitializer(id);
    final Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, new Integer(serviceRanking));
    context.bundleContext().registerService(SlingRepositoryInitializer.class.getName(), init, props);
}
Also used : SlingRepositoryInitializer(org.apache.sling.jcr.api.SlingRepositoryInitializer) Hashtable(java.util.Hashtable)

Example 2 with SlingRepositoryInitializer

use of org.apache.sling.jcr.api.SlingRepositoryInitializer in project sling by apache.

the class AbstractSlingRepositoryManager method start.

/**
     * This method actually starts the backing repository instannce and
     * registeres the repository service.
     * <p>
     * Multiple subsequent calls to this method without calling {@link #stop()}
     * first have no effect.
     *
     * @param bundleContext The {@code BundleContext} to register the repository
     *            service (and optionally more services required to operate the
     *            repository)
     * @param config The configuration to apply to this instance.
     */
protected final void start(final BundleContext bundleContext, final Config config) {
    // already setup ?
    if (this.bundleContext != null) {
        log.debug("start: Repository already started and registered");
        return;
    }
    this.bundleContext = bundleContext;
    this.defaultWorkspace = config.defaultWorkspace;
    this.disableLoginAdministrative = config.disableLoginAdministrative;
    this.repoInitializerTracker = new ServiceTracker<SlingRepositoryInitializer, SlingRepositoryInitializerInfo>(bundleContext, SlingRepositoryInitializer.class, new ServiceTrackerCustomizer<SlingRepositoryInitializer, SlingRepositoryInitializerInfo>() {

        @Override
        public SlingRepositoryInitializerInfo addingService(final ServiceReference<SlingRepositoryInitializer> reference) {
            final SlingRepositoryInitializer service = bundleContext.getService(reference);
            if (service != null) {
                final SlingRepositoryInitializerInfo info = new SlingRepositoryInitializerInfo(service, reference);
                synchronized (repoInitLock) {
                    if (masterSlingRepository != null) {
                        log.debug("Executing {}", info.initializer);
                        try {
                            info.initializer.processRepository(masterSlingRepository);
                        } catch (final Exception e) {
                            log.error("Exception in a SlingRepositoryInitializer: " + info.initializer, e);
                        }
                    }
                }
                return info;
            }
            return null;
        }

        @Override
        public void modifiedService(final ServiceReference<SlingRepositoryInitializer> reference, final SlingRepositoryInitializerInfo service) {
        // nothing to do
        }

        @Override
        public void removedService(final ServiceReference<SlingRepositoryInitializer> reference, final SlingRepositoryInitializerInfo service) {
            bundleContext.ungetService(reference);
        }
    });
    this.repoInitializerTracker.open();
    // If allowLoginAdministrativeForBundle is overridden we assume we don't need
    // a LoginAdminWhitelist service - that's the case if the derived class
    // implements its own strategy and the LoginAdminWhitelist interface is
    // not exported by this bundle anyway, so cannot be implemented differently.
    boolean enableWhitelist = !isAllowLoginAdministrativeForBundleOverridden();
    final CountDownLatch waitForWhitelist = new CountDownLatch(enableWhitelist ? 1 : 0);
    if (enableWhitelist) {
        whitelistTracker = new ServiceTracker<LoginAdminWhitelist, LoginAdminWhitelist>(bundleContext, LoginAdminWhitelist.class, null) {

            @Override
            public LoginAdminWhitelist addingService(final ServiceReference<LoginAdminWhitelist> reference) {
                try {
                    return super.addingService(reference);
                } finally {
                    waitForWhitelist.countDown();
                }
            }
        };
        whitelistTracker.open();
    }
    // start repository asynchronously to allow LoginAdminWhitelist to become available
    // NOTE: making this conditional allows tests to register a mock whitelist before
    // activating the RepositoryManager, so they don't need to deal with async startup
    new Thread("Apache Sling Repository Startup Thread") {

        @Override
        public void run() {
            try {
                waitForWhitelist.await();
                initializeAndRegisterRepositoryService();
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted while waiting for LoginAdminWhitelist", e);
            }
        }
    }.start();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) LoginAdminWhitelist(org.apache.sling.jcr.base.internal.LoginAdminWhitelist) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceReference(org.osgi.framework.ServiceReference) SlingRepositoryInitializer(org.apache.sling.jcr.api.SlingRepositoryInitializer)

Aggregations

SlingRepositoryInitializer (org.apache.sling.jcr.api.SlingRepositoryInitializer)2 Hashtable (java.util.Hashtable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LoginAdminWhitelist (org.apache.sling.jcr.base.internal.LoginAdminWhitelist)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1