Package org.wso2.carbon.registry.ws.client.internal

Source Code of org.wso2.carbon.registry.ws.client.internal.WSClientServiceComponent

/*
*  Copyright (c) 2005-2009, 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.registry.ws.client.internal;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.derby.impl.sql.compile.HashTableNode;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryProvider;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient;
import org.wso2.carbon.utils.ConfigurationContextService;

import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;

/**
* Service Component for Client to WS API.
*
* @scr.component name="registry.ws.client.component" immediate="true"
* @scr.reference name="config.context.service"
* interface="org.wso2.carbon.utils.ConfigurationContextService"
* cardinality="1..1" policy="dynamic" bind="setConfigurationContextService"
* unbind="unsetConfigurationContextService"
*/
public class WSClientServiceComponent {

    private ConfigurationContext configurationContext;

    private static Log log = LogFactory.getLog(WSClientServiceComponent.class);
    private ServiceRegistration serviceRegistration = null;

    protected void activate(ComponentContext context) {
        RegistryProvider provider = new RegistryProvider() {

            private WSRegistryServiceClient client = null;

            public Registry getRegistry(String registryURL, String username, String password)
                    throws RegistryException {
                if (client != null) {
                    return client;
                }
                if (registryURL != null && username != null && password != null) {
                    if (registryURL.endsWith("/")) {
                        registryURL = registryURL.substring(0, registryURL.length() - 1);
                    }
                    String serverURL = registryURL.substring(0, registryURL.indexOf("/registry"))
                            + "/services/";
                    RegistryUtils.setTrustStoreSystemProperties();
                    client = new WSRegistryServiceClient(serverURL, username, password,
                            configurationContext);
                    return client;
                }

                throw new RegistryException("Unable to create an instance of a WS Registry");
            }
        };

        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("type", "ws");


        serviceRegistration =
                context.getBundleContext().registerService(RegistryProvider.class.getName(),
                        provider, ht);

        if (log.isDebugEnabled()) {
            log.info("Registry WS Client bundle is activated");
        }
    }

    protected void deactivate(ComponentContext context) {
        if (serviceRegistration != null) {
            serviceRegistration.unregister();
            serviceRegistration = null;
        }
        if (log.isDebugEnabled()) {
            log.info("Registry WS Client bundle is deactivated");
        }
    }

    protected void setConfigurationContextService(ConfigurationContextService contextService) {
        configurationContext = contextService.getClientConfigContext();
    }

    protected void unsetConfigurationContextService(ConfigurationContextService contextService) {
        configurationContext = null;
    }

}
TOP

Related Classes of org.wso2.carbon.registry.ws.client.internal.WSClientServiceComponent

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.