Package org.wso2.carbon.deployment.synchronizer.util

Source Code of org.wso2.carbon.deployment.synchronizer.util.Utils

/*
*  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.deployment.synchronizer.util;

import org.apache.axis2.util.JavaUtils;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.deployment.synchronizer.DeploymentSynchronizerConstants;
import org.wso2.carbon.deployment.synchronizer.DeploymentSynchronizerException;
import org.wso2.carbon.event.core.subscription.Subscription;
import org.wso2.carbon.event.ws.internal.builders.exceptions.InvalidMessageException;
import org.wso2.carbon.event.ws.internal.builders.utils.BuilderUtils;
import org.wso2.carbon.registry.common.eventing.RegistryEvent;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.eventing.RegistryEventingConstants;
import org.wso2.carbon.registry.eventing.services.EventingService;

public class Utils {

    /**
     * Load the deployment synchronizer configuration from the global ServerConfiguration
     * of Carbon.
     *
     * @return a DeploymentSynchronizerConfiguration instance
     */
    public static DeploymentSynchronizerConfiguration getDeploymentSyncConfiguration() {
        DeploymentSynchronizerConfiguration config = new DeploymentSynchronizerConfiguration();
        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
        String value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.ENABLED);
        config.setEnabled(value != null && JavaUtils.isTrueExplicitly(value));

        value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.AUTO_CHECKOUT_MODE);
        config.setAutoCheckout(value != null && JavaUtils.isTrueExplicitly(value));

        value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.AUTO_COMMIT_MODE);
        config.setAutoCommit(value != null && JavaUtils.isTrueExplicitly(value));

        value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.USE_EVENTING);
        config.setUseEventing(value != null && JavaUtils.isTrueExplicitly(value));

        value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.AUTO_SYNC_PERIOD);
        if (value != null) {
            config.setPeriod(Long.parseLong(value));
        } else {
            config.setPeriod(DeploymentSynchronizerConstants.DEFAULT_AUTO_SYNC_PERIOD);
        }
        return config;
    }

    public static String getAbsoluteRegistryPath(String registryPath) {
        String registryType = getRegistryType();

        if (!registryPath.startsWith("/")) {
            registryPath = "/" + registryPath;
        }

        if (DeploymentSynchronizerConstants.REGISTRY_TYPE_LOCAL_REPOSITORY.equals(registryType)) {
            return RegistryConstants.LOCAL_REPOSITORY_BASE_PATH + registryPath;
        } else if (DeploymentSynchronizerConstants.REGISTRY_TYPE_GOVERNANCE.equals(registryType)) {
            return RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + registryPath;
        } else {
            return RegistryConstants.CONFIG_REGISTRY_BASE_PATH + registryPath;
        }
    }

    public static String subscribeForRegistryEvents(UserRegistry registry, String absolutePath,
                                        String endpoint) throws DeploymentSynchronizerException {

        EventingService eventingService = ServiceReferenceHolder.getEventingService();
        if (eventingService == null) {
            throw new IllegalStateException("Registry eventing service unavailable");
        }

        String topic = RegistryEventingConstants.TOPIC_PREFIX + absolutePath +
                RegistryEvent.TOPIC_SEPARATOR + "#";

        SuperTenantCarbonContext.startTenantFlow();
        try {
            Subscription subscription =
                    BuilderUtils.createSubscription(endpoint,
                            "http://wso2.org/registry/eventing/dialect/topicFilter",
                            topic);
            subscription.setEventDispatcherName(RegistryEventingConstants.TOPIC_PREFIX);
            subscription.setTenantId(registry.getCallerTenantId());
            subscription.setOwner(registry.getUserName());

            SuperTenantCarbonContext currentContext = SuperTenantCarbonContext.getCurrentContext();
            currentContext.setTenantId(registry.getCallerTenantId(), true);
            currentContext.setUserRealm(registry.getUserRealm());
            currentContext.setUsername(registry.getUserName());

            return eventingService.subscribe(subscription);
        } catch (InvalidMessageException e) {
            throw new DeploymentSynchronizerException("Error while subscribing for registry " +
                    "events on collection: " + absolutePath, e);
        } finally {
            SuperTenantCarbonContext.endTenantFlow();
        }
    }

    public static boolean unsubscribeForRegistryEvents(String subscriptionId, int tenantId) {
        EventingService eventingService = ServiceReferenceHolder.getEventingService();
        if (eventingService == null) {
            throw new IllegalStateException("Registry eventing service unavailable");
        }

        SuperTenantCarbonContext.startTenantFlow();
        try {
            SuperTenantCarbonContext currentContext = SuperTenantCarbonContext.getCurrentContext();
            currentContext.setTenantId(tenantId, true);
            return eventingService.unsubscribe(subscriptionId);
        } finally {
            SuperTenantCarbonContext.endTenantFlow();
        }
    }

    /**
     * Retrieves the registry space which should be used by the deployment synchronizer for storing
     * resources. This is a global setting which comes through the ServerConfiguration of Carbon.
     * By default the configuration registry space of the tenant will be returned by this method.
     *
     * @param tenantId Tenant ID
     * @return a registry space owned by the tenant
     * @throws org.wso2.carbon.registry.core.exceptions.RegistryException if the registry cannot be accessed
     */
    public static UserRegistry getRegistry(int tenantId) throws RegistryException {
        String registryType = getRegistryType();

        RegistryService registryService = ServiceReferenceHolder.getRegistryService();
        if (DeploymentSynchronizerConstants.REGISTRY_TYPE_LOCAL_REPOSITORY.equals(registryType)) {
            return registryService.getLocalRepository(tenantId);
        } else if (DeploymentSynchronizerConstants.REGISTRY_TYPE_GOVERNANCE.equals(registryType)) {
            return registryService.getGovernanceSystemRegistry(tenantId);
        } else {
            return registryService.getConfigSystemRegistry(tenantId);
        }
    }

    private static String getRegistryType() {
        String registryType = DeploymentSynchronizerConstants.DEFAULT_REGISTRY_TYPE;
        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
        String value = serverConfig.getFirstProperty(DeploymentSynchronizerConstants.REGISTRY_TYPE);
        if (value != null) {
            registryType = value;
        }
        return registryType;
    }
}
TOP

Related Classes of org.wso2.carbon.deployment.synchronizer.util.Utils

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.