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

Source Code of org.wso2.carbon.cloud.csg.agent.ui.CSGAgentAdminClient

/*
* 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.ui;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.cloud.csg.agent.stub.CSGAgentAdminServiceCSGException;
import org.wso2.carbon.cloud.csg.agent.stub.CSGAgentAdminServiceStub;
import org.wso2.carbon.cloud.csg.agent.stub.types.carbon.CSGServerBean;

import java.rmi.RemoteException;
import java.util.Locale;
import java.util.ResourceBundle;

/**
* This class <code>CSGAgentAdminClient</code> provides the admin client for CSGAgent.
*/
public class CSGAgentAdminClient {

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

    private static final String BUNDLE = "org.wso2.carbon.cloud.csg.agent.ui.i18n.Resources";

    private ResourceBundle bundle;

    public CSGAgentAdminServiceStub stub;

    public CSGAgentAdminClient() throws AxisFault{
        stub = new CSGAgentAdminServiceStub();
        bundle = ResourceBundle.getBundle(BUNDLE, new Locale("EN"));
    }

    public CSGAgentAdminClient(String cookie,
                               String backendServerURL,
                               ConfigurationContext configCtx,
                               Locale locale) throws AxisFault {

        String serviceURL = backendServerURL + "CSGAgentAdminService";
        bundle = ResourceBundle.getBundle(BUNDLE, locale);
        stub = new CSGAgentAdminServiceStub(configCtx, serviceURL);
        Options options = stub._getServiceClient().getOptions();
        options.setManageSession(true);
        options.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
    }

    /**
     * Publish the service
     *
     * @param serviceName name of the service to publish
     * @param serverName name of the server from which to unpublished
     * @throws AxisFault in case of an error
     */
    public void publish(String serviceName, String serverName) throws AxisFault {
        try {
            stub.publishService(serviceName, serverName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.publish.service");
            handleException(msg, e);
        }
    }

    /**
     * Un publish the service
     * @param serviceName name of the service to unpublished
     * @param serverName name of the server from which to unpublished
     * @throws AxisFault in case of an error
     */
    public void unPublish(String serviceName, String serverName) throws AxisFault {
        try {
            stub.unPublishService(serviceName, serverName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.unpublish.service");
            handleException(msg, e);
        }
    }

    /**
     * Add a CSG server bean
     * @param csgServer the csg server bean with server information
     * @throws AxisFault throws in case of an error
     */
    public void addCSGServer(CSGServerBean csgServer) throws AxisFault {
        try {
            stub.addCSGServer(csgServer);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.add.the.csg.server");
            handleException(msg, e);
        }
    }

    /**
     * Get the available list of csg servers
     * @return the csg server list
     * @throws AxisFault throws in case of an error
     */
    public CSGServerBean[] getCSGServerList() throws AxisFault{
        try {
            return stub.getCSGServerList();
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.retrieve.csg.servers");
            handleException(msg, e);
        }
        return null;
    }

    /**
     * Remove this csg server instance
     * @param serverName the unique csg server name to delete
     * @throws AxisFault throws in case of an error
     */
    public void removeCSGServer(String serverName) throws AxisFault{
        try {
            stub.removeCSGServer(serverName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.remove.csg.server");
            handleException(msg, e);
        }
    }

    /**
     * Get the CSG server bean given the server name
     * @param serverName the csg server name
     * @return the csg server instance given by this name
     * @throws AxisFault throws in case of an error
     */
    public CSGServerBean getCSGServer(String serverName) throws AxisFault {
        try {
            return stub.getCSGServer(serverName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.remove.csg.server");
            handleException(msg, e);
        }
        return null;
    }

    /**
     * Update an already existing csg server
     * @param bean the new csg server information
     * @throws AxisFault throws in case of an error
     */
    public void updateCSGServer(CSGServerBean bean) throws AxisFault{
        try {
            stub.updateCSGServer(bean);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.update.csg.server");
            handleException(msg, e);
        }
    }

    /**
     * Is the given service already published
     * @param serviceName service
     * @return true if service is published false otherwise
     * @throws AxisFault throws in case of an error
     */
    public boolean isservicePublished(String serviceName) throws AxisFault{
        try {
            return stub.isservicePublished(serviceName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.get.published.flag");
            handleException(msg, e);
        }
        return false;
    }

    /**
     * Get the list of servers that given service is published
     * @param serviceName the service
     * @return the list of servers
     * @throws AxisFault throws in case of an error
     */
    public String[] getPublishedServerList(String serviceName)throws AxisFault{
        try {
            return stub.getPublishedServerList(serviceName);
        } catch (RemoteException e) {
            String msg = bundle.getString("connect.error");
            handleException(msg, e);
        } catch (CSGAgentAdminServiceCSGException e) {
            String msg = bundle.getString("cannot.get.published.server.list");
            handleException(msg, e);
        }
        return null;
    }

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

Related Classes of org.wso2.carbon.cloud.csg.agent.ui.CSGAgentAdminClient

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.