Package org.wso2.carbon.dataservices.taskscheduler.internal

Source Code of org.wso2.carbon.dataservices.taskscheduler.internal.TaskSchedulerServiceComponent

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing,
*  software distributed under the License is distributed on an
*  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
*  KIND, either express or implied.  See the License for the
*  specific language governing permissions and limitations
*  under the License.
*
*/
package org.wso2.carbon.dataservices.taskscheduler.internal;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.task.TaskDescriptionRepository;
import org.apache.synapse.task.TaskScheduler;
import org.apache.synapse.task.service.TaskManagementService;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.dataservices.taskscheduler.multitenancy.TaskSchedulerInitializer;
import org.wso2.carbon.dataservices.taskscheduler.services.TaskMetaDataProviderService;
import org.wso2.carbon.dataservices.taskscheduler.services.TaskManagementAdminService;
import org.wso2.carbon.task.JobMetaDataProviderServiceHandler;
import org.wso2.carbon.task.TaskManagementServiceHandler;
import org.wso2.carbon.task.TaskManager;
import org.wso2.carbon.task.services.JobMetaDataProviderService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.ConfigurationContextService;

/**
* This class activates the dataservices.taskscheduler bundle
*
* @scr.component name="dataservices.taskscheduler" immediate="true"
* @scr.reference name="carbon.core.configurationContextService"
* interface="org.wso2.carbon.utils.ConfigurationContextService"
* cardinality="1..1" policy="dynamic" bind="setConfigurationContextService"
* unbind="unsetConfigurationContextService"
*/
public class TaskSchedulerServiceComponent {

    private ConfigurationContext configurationContext;

    private static final Log log = LogFactory.getLog(TaskSchedulerServiceComponent.class);

    private JobMetaDataProviderServiceHandler jobMetaDataProviderServiceHandler =
            new JobMetaDataProviderServiceHandler();

    private TaskManagementServiceHandler taskManagementServiceHandler =
            new TaskManagementServiceHandler();

    /**
     * This method activates the Task Scheduler bundle
     *
     * @param componentContext ComponentContext
     */
    protected void activate(ComponentContext componentContext) {
        try {
            log.debug("Installing Task Scheduler Components");

            BundleContext bundleContext = componentContext.getBundleContext();
            TaskSchedulerBundleActivator bundleActivator=new TaskSchedulerBundleActivator();
            bundleActivator.start(bundleContext);
           
            bundleContext.registerService(JobMetaDataProviderService.class.getName(),
                    new TaskMetaDataProviderService(), null);
            bundleContext.registerService(TaskManagementService.class.getName(),
                    new TaskManagementAdminService(), null);
            bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
                    new TaskSchedulerInitializer(), null);

            /*
            * Adding the services to the jobMetaDataProviderServiceHandler which implement the
            * JobMetaDataProviderService interface, available in the current bundleContext.
            */
            jobMetaDataProviderServiceHandler.addService(bundleContext.getService(
                    bundleContext.getServiceReference(JobMetaDataProviderService.class.getName())));

            /*
            * Adding the services to the taskManagementServiceHandler which implement the
            * TaskManagementService interface, available in the current bundleContext.
            */
            taskManagementServiceHandler.addService(bundleContext.getService(
                    bundleContext.getServiceReference(TaskManagementService.class.getName())));

            configurationContext.setProperty(
                    TaskManager.CARBON_TASK_JOB_METADATA_SERVICE, jobMetaDataProviderServiceHandler);
            configurationContext.setProperty(
                    TaskManager.CARBON_TASK_MANAGEMENT_SERVICE, taskManagementServiceHandler);

            /*
            * In this particular context, the TaskManager class of the Carbon Scheduled Tasks
            * Component is used.
            * Since the Scheduled Tasks Component itself does not initialize a TaskManager instance,
            * this code snippet is used to initialize a TaskManager instance manually.
            */
            TaskManager taskManager = (TaskManager) configurationContext.getProperty(
                    TaskManager.CARBON_TASK_MANAGER);
            if (taskManager == null) {
                taskManager = new TaskManager();
                taskManager.init(jobMetaDataProviderServiceHandler, taskManagementServiceHandler);
                configurationContext.setProperty(
                        TaskManager.CARBON_TASK_MANAGER, taskManager);
            }

            /*
            * In this particular context, the TaskDescriptionRepository class of the
            * Carbon Scheduled Tasks Component is used.
            * Since the Scheduled Tasks Component itself does not initialize a
            * TaskDescriptionRepository instance,this code snippet is used to initialize a
            * the required instance manually.
            */
            TaskDescriptionRepository taskDescriptionRepository = (TaskDescriptionRepository)
                    configurationContext.getProperty(TaskManager.CARBON_TASK_REPOSITORY);
            if (taskDescriptionRepository == null) {
                taskDescriptionRepository = new TaskDescriptionRepository();
                configurationContext.setProperty(
                        TaskManager.CARBON_TASK_REPOSITORY, taskDescriptionRepository);
            }

            /*
            * In this particular context, the TaskScheduler class of the
            * Carbon Scheduled Tasks Component is used.
            * Since the Scheduled Tasks Component itself does not initialize a
            * TaskScheduler instance,this code snippet is used to initialize a
            * the required instance manually.
            */
            TaskScheduler taskScheduler = (TaskScheduler) configurationContext.getProperty(
                    TaskManager.CARBON_TASK_SCHEDULER);
            if (taskScheduler == null) {
                taskScheduler = new TaskScheduler(TaskManager.CARBON_TASK_SCHEDULER);
                taskScheduler.init(null);
                configurationContext.setProperty(
                        TaskManager.CARBON_TASK_SCHEDULER, taskScheduler);
            } else if (taskScheduler.isInitialized()) {
                taskScheduler.init(null);
            }
        } catch (Throwable e) {
            log.error("An error occurred while executing the bundle activator method!", e);
        }
    }

    /**
     * The content of this method is intentionally kept blank as the deactivation gets handled
     * by the underlying layer
     *
     * @param componentContext ComponentContext
     */
    protected void deactivate(ComponentContext componentContext) {

    }

    /**
     * This method is used to bind the ConfigurationContext which is being used in order to access
     * it in this particular class
     *
     * @param configurationContextService configuration context service required to grab the current
     */
    public void setConfigurationContextService(
            ConfigurationContextService configurationContextService) {
        this.configurationContext = configurationContextService.getServerConfigContext();
    }

    /**
     * This method is used to unbind the ConfigurationContext which is being used from the context
     * of this particular class
     *
     * @param configurationContextService configuration context service required to grab the current
     */
    public void unsetConfigurationContextService(
            ConfigurationContextService configurationContextService) {
        this.configurationContext = null;
    }
}
TOP

Related Classes of org.wso2.carbon.dataservices.taskscheduler.internal.TaskSchedulerServiceComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.