Package org.wso2.carbon.cloud.csg.agent.service

Source Code of org.wso2.carbon.cloud.csg.agent.service.CSGAgentAdminService

/*
* Copyright 2009-2010 WSO2, Inc. (http://wso2.com)
*
* Licensed 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.cloud.csg.agent.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.cloud.csg.agent.CSGAgentConfigLayer;
import org.wso2.carbon.cloud.csg.agent.CSGAgentUtils;
import org.wso2.carbon.cloud.csg.agent.CSGServicePublisher;
import org.wso2.carbon.cloud.csg.agent.client.AuthenticationClient;
import org.wso2.carbon.cloud.csg.common.CSGConstant;
import org.wso2.carbon.cloud.csg.common.CSGException;
import org.wso2.carbon.cloud.csg.common.CSGServerBean;
import org.wso2.carbon.core.AbstractAdmin;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

/**
* The class <code>CSGAgentAdminService</code> provides the admin service to manipulate the
* CSGAgent remotely
*/
public class CSGAgentAdminService extends AbstractAdmin {

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


    /**
     * Deploy the proxy service
     *
     * @param serviceName the service to deploy
     * @param serverName  the serverName to publish
     * @throws CSGException throws in case of an error
     */
    public void publishService(String serviceName, String serverName) throws CSGException {
        CSGServicePublisher servicePublisher = new CSGAgentConfigLayer().getCSGServicePublisher();
        if (servicePublisher.publish(serviceName, serverName)) {
            log.info("The service '" + serviceName + "', published sucessfully");
        } else {
            handleException("Cloud not publish the service");
        }
    }

    /**
     * Un-deploy the proxy service
     *
     * @param serviceName the service to un-deploy
     * @param serverName  the server name to publish
     * @throws CSGException throws in case of an error
     */
    public void unPublishService(String serviceName, String serverName) throws CSGException {
        CSGServicePublisher servicePublisher = new CSGAgentConfigLayer().getCSGServicePublisher();
        if (servicePublisher.unPublish(serviceName, serverName)) {
            log.info("The service '" + serviceName + "', un-published sucessfully");
        } else {
            handleException("Cloud not un-publish the service");
        }
    }

    public void addCSGServer(CSGServerBean csgServer) throws CSGException {
        try {

            String userName = csgServer.getUserName();
            String passWord = csgServer.getPassWord();
            String hostName = csgServer.getHost();
            String domainName = csgServer.getDomainName();

            // authenticate using provided credentials and if logged in persist them
            String authServerUrl = "https://" + hostName + ":" + csgServer.getPort() +
                    "/services/AuthenticationAdmin";
            AuthenticationClient authClient = new AuthenticationClient();
            authClient.getLoggedAuthAdminStub(
                    authServerUrl, userName, passWord, hostName, domainName);

            org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
            CSGAgentUtils.persistServer(registry, csgServer);
        } catch (Exception e) {
            handleException("Cloud not add CSG server :" + csgServer.getHost(), e);
        }
    }

    public CSGServerBean getCSGServer(String csgServerName) throws CSGException {
        org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
        String resourcePath = CSGConstant.REGISTRY_SERVER_RESOURCE_PATH + "/" + csgServerName;
        try {
            if (registry.resourceExists(resourcePath)) {
                Resource resource = registry.get(resourcePath);
                return CSGAgentUtils.getCSGServerBean(resource);
            }
        } catch (RegistryException e) {
            handleException("Cloud not read the registry resource '" + resourcePath + "'", e);
        }
        return null;
    }

    public CSGServerBean[] getCSGServerList() throws CSGException {
        try {
            org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
            if (registry.resourceExists(CSGConstant.REGISTRY_SERVER_RESOURCE_PATH)) {
                Resource resource = registry.get(CSGConstant.REGISTRY_SERVER_RESOURCE_PATH);
                if (resource instanceof Collection) {
                    Collection collection = (Collection) resource;
                    int size = collection.getChildCount();
                    CSGServerBean[] beanInfo = new CSGServerBean[size];
                    String[] child = collection.getChildren();
                    for (int i = 0; child.length > i; i++) {
                        String s = child[i]; // returns the set of path
                        Resource childResource = registry.get(s);
                        beanInfo[i] = CSGAgentUtils.getCSGServerBean(childResource);
                    }
                    return beanInfo;
                }
            }
        } catch (RegistryException e) {
            handleException("Cloud not retrieve the CSG server list", e);
        }
        return null;
    }

    public void updateCSGServer(CSGServerBean csgServer) throws CSGException {
        org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
        String resource = CSGConstant.REGISTRY_SERVER_RESOURCE_PATH + "/" + csgServer.getName();
        try {
            if (registry.resourceExists(resource)) {
                // delete the resource and add it again
                registry.delete(resource);
                CSGAgentUtils.persistServer(registry, csgServer);
            }
        } catch (RegistryException e) {
            handleException("Cloud not read the registry resource '" + resource + "'", e);
        }
    }

    public void removeCSGServer(String csgServerName) throws CSGException {
        try {
            org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
            String resource = CSGConstant.REGISTRY_SERVER_RESOURCE_PATH + "/" + csgServerName;
            if (registry.resourceExists(resource)) {
                registry.delete(resource);
            } else {
                log.error("The resource '" + resource + "' does not exist!");
            }
        } catch (Exception e) {
            handleException("Cloud not remove the CSG server: " + csgServerName, e);
        }
    }

    public boolean isservicePublished(String serviceName) throws CSGException {
        try {
            org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
            String resourcePath = CSGConstant.REGISTRY_FLAG_RESOURCE_PATH + "/" + serviceName + ".flag";
            if(registry.resourceExists(resourcePath)){
                Resource resource = registry.get(resourcePath);
                String content = new String((byte []) resource.getContent());
                if(CSGConstant.CSG_PUBLISHED_FLAG.equals(content)){
                    return true;
                }
            }
        } catch (Exception e) {
            handleException("Cloud not retrieve the service publish flag for service '" + serviceName + "'", e);
        }
        return false;
    }

    public String[] getPublishedServerList(String serviceName) throws CSGException{
        try {
            org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
            String serverResourcePath = CSGConstant.REGISTRY_FLAG_RESOURCE_PATH + "/" + serviceName + ".server";
            if(registry.resourceExists(serverResourcePath)){
                Resource serverResource = registry.get(serverResourcePath);
                String content = new String((byte []) serverResource.getContent());
                return content.split(",");
            }
        } catch (RegistryException e) {
            handleException("Cloud not retrieve the published server list", e);
        }
        return null;
    }

    private void handleException(String msg) throws CSGException {
        log.error(msg);
        throw new CSGException(msg);
    }

    private void handleException(String msg, Throwable t) throws CSGException {
        log.error(msg, t);
        throw new CSGException(msg, t);
    }
}
TOP

Related Classes of org.wso2.carbon.cloud.csg.agent.service.CSGAgentAdminService

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.