Search in sources :

Example 96 with Activate

use of org.apache.felix.scr.annotations.Activate in project sling by apache.

the class OakDiscoveryService method activate.

/**
     * Activate this service
     */
@Activate
protected void activate(final BundleContext bundleContext) {
    logger.debug("OakDiscoveryService activating...");
    if (settingsService == null) {
        throw new IllegalStateException("settingsService not found");
    }
    if (oakViewChecker == null) {
        throw new IllegalStateException("heartbeatHandler not found");
    }
    slingId = settingsService.getSlingId();
    ClusterSyncService consistencyService;
    if (config.getSyncTokenEnabled()) {
        //TODO: ConsistencyHistory is implemented a little bit hacky ..
        ClusterSyncHistory consistencyHistory = new ClusterSyncHistory();
        oakBacklogClusterSyncService.setConsistencyHistory(consistencyHistory);
        syncTokenService.setConsistencyHistory(consistencyHistory);
        consistencyService = new ClusterSyncServiceChain(oakBacklogClusterSyncService, syncTokenService);
    } else {
        consistencyService = oakBacklogClusterSyncService;
    }
    viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, consistencyService);
    if (config.getMinEventDelay() > 0) {
        viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
    }
    final String isolatedClusterId = UUID.randomUUID().toString();
    {
        // create a pre-voting/isolated topologyView which would be used
        // until the first voting has finished.
        // this way for the single-instance case the clusterId can
        // remain the same between a getTopology() that is invoked before
        // the first TOPOLOGY_INIT and afterwards
        DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
        Map<String, String> emptyProperties = new HashMap<String, String>();
        DefaultInstanceDescription isolatedInstance = new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
        Collection<InstanceDescription> col = new ArrayList<InstanceDescription>();
        col.add(isolatedInstance);
        final DefaultTopologyView topology = new DefaultTopologyView();
        topology.addInstances(col);
        topology.setNotCurrent();
        setOldView(topology);
    }
    setOldView((DefaultTopologyView) getTopology());
    getOldView().setNotCurrent();
    // make sure the first heartbeat is issued as soon as possible - which
    // is right after this service starts. since the two (discoveryservice
    // and heartbeatHandler need to know each other, the discoveryservice
    // is passed on to the heartbeatHandler in this initialize call).
    oakViewChecker.initialize(this);
    viewStateManagerLock.lock();
    try {
        viewStateManager.handleActivated();
        doUpdateProperties();
        DefaultTopologyView newView = (DefaultTopologyView) getTopology();
        if (newView.isCurrent()) {
            viewStateManager.handleNewView(newView);
        } else {
            // SLING-3750: just issue a log.info about the delaying
            logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
        }
        activated = true;
        setOldView(newView);
        // bind them to the viewstatemanager too
        for (TopologyEventListener listener : pendingListeners) {
            viewStateManager.bind(listener);
        }
        pendingListeners.clear();
        viewStateManager.bind(changePropagationListener);
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
    URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
    if (topologyConnectorURLs != null) {
        for (int i = 0; i < topologyConnectorURLs.length; i++) {
            final URL aURL = topologyConnectorURLs[i];
            if (aURL != null) {
                try {
                    logger.info("activate: registering outgoing topology connector to " + aURL);
                    connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
                } catch (final Exception e) {
                    logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
                }
            }
        }
    }
    logger.debug("OakDiscoveryService activated.");
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) URL(java.net.URL) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) ClusterSyncHistory(org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncHistory) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) ClusterSyncServiceChain(org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncServiceChain) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) OakBacklogClusterSyncService(org.apache.sling.discovery.commons.providers.spi.base.OakBacklogClusterSyncService) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Collection(java.util.Collection) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashMap(java.util.HashMap) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Activate(org.apache.felix.scr.annotations.Activate)

Example 97 with Activate

use of org.apache.felix.scr.annotations.Activate in project sling by apache.

the class Init method activate.

@Activate
protected void activate() {
    try {
        ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
        Session session = resourceResolver.adaptTo(Session.class);
        JackrabbitSession jackrabbitSession = (JackrabbitSession) session;
        UserManager userManager = jackrabbitSession.getUserManager();
        if (userManager.getAuthorizable("testUser") == null) {
            userManager.createUser("testUser", "password");
        }
        final Principal testUserPrincipal = new Principal() {

            public String getName() {
                return "testUser";
            }
        };
        session.save();
    } catch (Exception e) {
        //To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
}
Also used : UserManager(org.apache.jackrabbit.api.security.user.UserManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Principal(java.security.Principal) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Activate(org.apache.felix.scr.annotations.Activate)

Example 98 with Activate

use of org.apache.felix.scr.annotations.Activate in project sling by apache.

the class DiscoveryServiceImpl method activate.

/**
     * Activate this service
     */
@Activate
protected void activate(final BundleContext bundleContext) {
    logger.debug("DiscoveryServiceImpl activating...");
    if (settingsService == null) {
        throw new IllegalStateException("settingsService not found");
    }
    if (heartbeatHandler == null) {
        throw new IllegalStateException("heartbeatHandler not found");
    }
    slingId = settingsService.getSlingId();
    final ClusterSyncService clusterSyncService;
    if (!config.useSyncTokenService()) {
        logger.info("activate: useSyncTokenService is configured to false. Using pass-through cluster-sync-service.");
        clusterSyncService = PASS_THROUGH_CLUSTER_SYNC_SERVICE;
    } else if (syncTokenService == null) {
        logger.warn("activate: useSyncTokenService is configured to true but there's no SyncTokenService! Using pass-through cluster-sync-service instead.");
        clusterSyncService = syncTokenService;
    } else {
        logger.info("activate: useSyncTokenService is configured to true, using the available SyncTokenService: " + syncTokenService);
        clusterSyncService = syncTokenService;
    }
    viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, clusterSyncService);
    if (config.getMinEventDelay() > 0) {
        viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
    }
    final String isolatedClusterId = UUID.randomUUID().toString();
    {
        // create a pre-voting/isolated topologyView which would be used
        // until the first voting has finished.
        // this way for the single-instance case the clusterId can
        // remain the same between a getTopology() that is invoked before
        // the first TOPOLOGY_INIT and afterwards
        DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
        Map<String, String> emptyProperties = new HashMap<String, String>();
        DefaultInstanceDescription isolatedInstance = new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
        Collection<InstanceDescription> col = new ArrayList<InstanceDescription>();
        col.add(isolatedInstance);
        final DefaultTopologyView topology = new DefaultTopologyView();
        topology.addInstances(col);
        topology.setNotCurrent();
        setOldView(topology);
    }
    setOldView((DefaultTopologyView) getTopology());
    getOldView().setNotCurrent();
    // make sure the first heartbeat is issued as soon as possible - which
    // is right after this service starts. since the two (discoveryservice
    // and heartbeatHandler need to know each other, the discoveryservice
    // is passed on to the heartbeatHandler in this initialize call).
    heartbeatHandler.initialize(this, isolatedClusterId);
    viewStateManagerLock.lock();
    try {
        viewStateManager.handleActivated();
        doUpdateProperties();
        DefaultTopologyView newView = (DefaultTopologyView) getTopology();
        if (newView.isCurrent()) {
            viewStateManager.handleNewView(newView);
        } else {
            // SLING-3750: just issue a log.info about the delaying
            logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
        }
        activated = true;
        setOldView(newView);
        // bind them to the viewstatemanager too
        for (TopologyEventListener listener : pendingListeners) {
            viewStateManager.bind(listener);
        }
        pendingListeners.clear();
        viewStateManager.bind(changePropagationListener);
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
    URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
    if (topologyConnectorURLs != null) {
        for (int i = 0; i < topologyConnectorURLs.length; i++) {
            final URL aURL = topologyConnectorURLs[i];
            if (aURL != null) {
                try {
                    logger.info("activate: registering outgoing topology connector to " + aURL);
                    connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
                } catch (final Exception e) {
                    logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
                }
            }
        }
    }
    registerMBean(bundleContext);
    logger.debug("DiscoveryServiceImpl activated.");
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) URL(java.net.URL) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Collection(java.util.Collection) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashMap(java.util.HashMap) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Activate(org.apache.felix.scr.annotations.Activate)

Example 99 with Activate

use of org.apache.felix.scr.annotations.Activate in project sling by apache.

the class ModelAdapterFactory method activate.

@Activate
protected void activate(final ComponentContext ctx) {
    Dictionary<?, ?> props = ctx.getProperties();
    final int maxRecursionDepth = PropertiesUtil.toInteger(props.get(PROP_MAX_RECURSION_DEPTH), DEFAULT_MAX_RECURSION_DEPTH);
    this.invocationCountThreadLocal = new ThreadLocal<ThreadInvocationCounter>() {

        @Override
        protected ThreadInvocationCounter initialValue() {
            return new ThreadInvocationCounter(maxRecursionDepth);
        }
    };
    this.adapterCache = Collections.synchronizedMap(new WeakHashMap<Object, Map<Class, Object>>());
    BundleContext bundleContext = ctx.getBundleContext();
    this.queue = new ReferenceQueue<Object>();
    this.disposalCallbacks = new ConcurrentHashMap<java.lang.ref.Reference<Object>, DisposalCallbackRegistryImpl>();
    Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
    properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
    properties.put(Constants.SERVICE_DESCRIPTION, "Sling Models OSGi Service Disposal Job");
    properties.put("scheduler.name", "Sling Models OSGi Service Disposal Job");
    properties.put("scheduler.concurrent", false);
    properties.put("scheduler.period", PropertiesUtil.toLong(props.get(PROP_CLEANUP_JOB_PERIOD), DEFAULT_CLEANUP_JOB_PERIOD));
    this.jobRegistration = bundleContext.registerService(Runnable.class.getName(), this, properties);
    this.listener = new ModelPackageBundleListener(ctx.getBundleContext(), this, this.adapterImplementations, bindingsValuesProvidersByContext);
    Hashtable<Object, Object> printerProps = new Hashtable<Object, Object>();
    printerProps.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
    printerProps.put(Constants.SERVICE_DESCRIPTION, "Sling Models Configuration Printer");
    printerProps.put("felix.webconsole.label", "slingmodels");
    printerProps.put("felix.webconsole.title", "Sling Models");
    printerProps.put("felix.webconsole.configprinter.modes", "always");
    this.configPrinterRegistration = bundleContext.registerService(Object.class.getName(), new ModelConfigurationPrinter(this, bundleContext, adapterImplementations), printerProps);
}
Also used : Reference(org.apache.felix.scr.annotations.Reference) PhantomReference(java.lang.ref.PhantomReference) Hashtable(java.util.Hashtable) ModelClass(org.apache.sling.models.impl.model.ModelClass) WeakHashMap(java.util.WeakHashMap) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 100 with Activate

use of org.apache.felix.scr.annotations.Activate in project sling by apache.

the class MongoDBNoSqlResourceProviderFactory method activate.

@Activate
private void activate(ComponentContext componentContext, Map<String, Object> config) {
    String connectionString = PropertiesUtil.toString(config.get(CONNECTION_STRING_PROPERTY), CONNECTION_STRING_DEFAULT);
    String database = PropertiesUtil.toString(config.get(DATABASE_PROPERTY), DATABASE_DEFAULT);
    String collection = PropertiesUtil.toString(config.get(COLLECTION_PROPERTY), COLLECTION_DEFAULT);
    mongoClient = new MongoClient(connectionString);
    NoSqlAdapter mongodbAdapter = new MongoDBNoSqlAdapter(mongoClient, database, collection);
    // enable call logging and metrics for {@link MongoDBNoSqlAdapter}
    noSqlAdapter = new MetricsNoSqlAdapterWrapper(mongodbAdapter, LoggerFactory.getLogger(MongoDBNoSqlAdapter.class));
}
Also used : NoSqlAdapter(org.apache.sling.nosql.generic.adapter.NoSqlAdapter) MongoClient(com.mongodb.MongoClient) MetricsNoSqlAdapterWrapper(org.apache.sling.nosql.generic.adapter.MetricsNoSqlAdapterWrapper) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

Activate (org.apache.felix.scr.annotations.Activate)153 ConfigurationException (org.osgi.service.cm.ConfigurationException)31 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 BundleContext (org.osgi.framework.BundleContext)19 File (java.io.File)15 OsgiWhiteboard (org.apache.jackrabbit.oak.osgi.OsgiWhiteboard)12 URL (java.net.URL)11 Hashtable (java.util.Hashtable)11 ServiceReference (org.osgi.framework.ServiceReference)11 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)9 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 Map (java.util.Map)6 Session (javax.jcr.Session)5 StandardMBean (javax.management.StandardMBean)5 Whiteboard (org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard)5 Filter (org.osgi.framework.Filter)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4