Java tutorial
/* * Copyright (c) 2013, 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.ss.integration.common.clients; import org.apache.axis2.AxisFault; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.integration.common.admin.client.utils.AuthenticateStubUtil; import org.wso2.carbon.rssmanager.core.dto.xsd.*; import org.wso2.carbon.rssmanager.ui.stub.RSSAdminStub; import java.rmi.RemoteException; public class RSSManagerClient { private RSSAdminStub stub; private static final Log log = LogFactory.getLog(RSSManagerClient.class); private String serviceName = "RSSAdmin"; public RSSManagerClient(String backEndUrl, String sessionCookie) throws AxisFault { String endPoint = backEndUrl + serviceName; stub = new RSSAdminStub(endPoint); AuthenticateStubUtil.authenticateStub(sessionCookie, stub); } public RSSAdminStub getStub() { return stub; } public void dropDatabasePrivilegesTemplate(String environmentName, String templateName) throws AxisFault { try { stub.removeDatabasePrivilegeTemplate(environmentName, templateName); } catch (Exception e) { handleException("Fail to drop database template" + " '" + templateName + "' : " + e.getMessage(), e); } } public void editDatabasePrivilegesTemplate(String environmentName, DatabasePrivilegeTemplateInfo template) throws AxisFault { try { stub.updateDatabasePrivilegeTemplate(environmentName, template); } catch (Exception e) { handleException("Fail to edit database template" + " '" + template.getName() + "' : " + e.getMessage(), e); } } public void createDatabasePrivilegesTemplate(String environmentName, DatabasePrivilegeTemplateInfo template) throws AxisFault { try { stub.addDatabasePrivilegeTemplate(environmentName, template); } catch (Exception e) { handleException("Fail to create database privilege template" + " '" + template.getName() + "' : " + e.getMessage(), e); } } public DatabasePrivilegeTemplateInfo[] getDatabasePrivilegesTemplates(String environmentName) throws AxisFault { DatabasePrivilegeTemplateInfo[] templates = new DatabasePrivilegeTemplateInfo[0]; try { templates = stub.getDatabasePrivilegeTemplates(environmentName); if (templates == null) { return new DatabasePrivilegeTemplateInfo[0]; } } catch (Exception e) { handleException("Fail to get database privilege template list" + " : " + e.getMessage(), e); } return templates; } public void editUserPrivileges(String environmentName, DatabasePrivilegeSetInfo privileges, DatabaseUserInfo user, String databaseName) throws AxisFault { try { stub.updateDatabaseUserPrivileges(environmentName, privileges, user, databaseName); } catch (Exception e) { handleException( "Fail to edit database user privileges" + " : '" + user.getName() + "' : " + e.getMessage(), e); } } public void createDatabase(String environmentName, DatabaseInfo database) throws AxisFault { try { if (stub == null) { throw new AxisFault("stub is null"); } stub.addDatabase(environmentName, database); } catch (Exception e) { handleException("Fail to create database" + " '" + database.getName() + "' : " + e.getMessage(), e); } } public DatabaseInfo[] getDatabaseList(String environmentName) throws AxisFault { DatabaseInfo[] databases = new DatabaseInfo[0]; try { databases = stub.getDatabases(environmentName); if (databases == null) { return new DatabaseInfo[0]; } } catch (Exception e) { handleException("Fail to retrieve database list" + " : " + e.getMessage(), e); } return databases; } public DatabaseInfo getDatabase(String environmentName, String rssInstanceName, String databaseName, String type) throws AxisFault { DatabaseInfo database = null; try { database = stub.getDatabase(environmentName, rssInstanceName, databaseName, type); } catch (Exception e) { handleException("Fail to retrieve database instance data" + " : " + e.getMessage(), e); } return database; } public void dropDatabase(String environmentName, String rssInstanceName, String databaseName, String type) throws AxisFault { try { stub.removeDatabase(environmentName, rssInstanceName, databaseName, type); } catch (Exception e) { handleException("Fail to drop database" + " : " + e.getMessage(), e); } } public RSSInstanceInfo[] getRSSInstanceList(String environmentName) throws AxisFault { RSSInstanceInfo[] rssInstances = new RSSInstanceInfo[0]; try { rssInstances = stub.getRSSInstances(environmentName); if (rssInstances == null) { return new RSSInstanceInfo[0]; } } catch (Exception e) { handleException("Fail to retrieve RSS instance list" + " : " + e.getMessage(), e); } return rssInstances; } public void createRSSInstance(String environmentName, RSSInstanceInfo rssInstance) throws AxisFault { try { stub.addRSSInstance(environmentName, rssInstance); } catch (Exception e) { handleException("Fail to add RSS server instance" + " :" + rssInstance.getRssInstanceName() + " : " + e.getMessage(), e); } } public void testConnection(String driverClass, String jdbcUrl, String username, String password) throws AxisFault { try { stub.testConnection(driverClass, jdbcUrl, username, password); } catch (Exception e) { handleException("Error occurred while connecting to '" + jdbcUrl + "' with the username '" + username + "' " + "and the driver class '" + driverClass + "' : " + e.getMessage(), e); } } public void editRSSInstance(String environmentName, RSSInstanceInfo rssInstance) throws AxisFault { try { stub.updateRSSInstance(environmentName, rssInstance); } catch (Exception e) { handleException( "Fail to edit RSS Instance" + " :" + rssInstance.getRssInstanceName() + " : " + e.getMessage(), e); } } public DatabaseUserInfo getDatabaseUser(String environmentName, String rssInstance, String username, String type) throws AxisFault { DatabaseUserInfo user = null; try { user = stub.getDatabaseUser(environmentName, rssInstance, username, type); } catch (Exception e) { handleException("Fail to retrieve database user data" + " : " + e.getMessage(), e); } return user; } public void dropDatabaseUser(String environmentName, String rssInstance, String username, String type) throws AxisFault { try { stub.removeDatabaseUser(environmentName, rssInstance, username, type); } catch (Exception e) { handleException("Fail to drop database user" + " : " + e.getMessage(), e); } } public void createCarbonDataSource(String environmentName, String dataSourceName, UserDatabaseEntryInfo entry) throws AxisFault { try { stub.addCarbonDataSource(environmentName, dataSourceName, entry); } catch (Exception e) { handleException("Fail to create datasource" + " : " + e.getMessage(), e); } } public void createDatabaseUser(String environmentName, DatabaseUserInfo user) throws AxisFault { try { stub.addDatabaseUser(environmentName, user); } catch (RemoteException e) { handleException("Fail to create database user" + " : " + e.getMessage(), e); } catch (Exception e) { handleException("Fail to create database user" + " : " + e.getMessage(), e); } } public DatabasePrivilegeTemplateInfo getDatabasePrivilegesTemplate(String environmentName, String templateName) throws AxisFault { DatabasePrivilegeTemplateInfo tempalte = null; try { tempalte = stub.getDatabasePrivilegeTemplate(environmentName, templateName); } catch (Exception e) { handleException("Fail to retrieve database privilege template info" + " : " + e.getMessage(), e); } return tempalte; } private void handleException(String msg, Exception e) throws AxisFault { log.error(msg, e); throw new AxisFault(msg, e); } public RSSInstanceInfo getRSSInstance(String environmentName, String rssInstanceName, String type) throws AxisFault { RSSInstanceInfo rssIns = null; try { rssIns = stub.getRSSInstance(environmentName, rssInstanceName, type); } catch (Exception e) { handleException("Fail to retrieve database server instance properties" + " : " + e.getMessage(), e); } return rssIns; } public DatabaseUserInfo[] getDatabaseUsers(String environmentName) throws AxisFault { DatabaseUserInfo[] users = new DatabaseUserInfo[0]; try { users = stub.getDatabaseUsers(environmentName); } catch (Exception e) { handleException("Fail to get database users" + ": " + e.getMessage(), e); } return users; } public void dropRSSInstance(String environmentName, String instanceName, String type) throws AxisFault { try { stub.removeRSSInstance(environmentName, instanceName, type); } catch (Exception e) { handleException("Fail to drop RSS instance" + " '" + instanceName + "' : " + e.getMessage(), e); } } public int getSystemRSSInstanceCount(String environmentName) throws AxisFault { int count = 0; try { // TODO /* count = stub.getSystemRSSInstanceCount(environmentName); */ } catch (Exception e) { handleException("Fail to get system RSS instance count" + " : " + e.getMessage(), e); } return count; } public void attachUserToDatabase(String environmentName, String rssInstance, String databaseName, String username, String templateName, String type) throws AxisFault { try { stub.attachUser(environmentName, type, templateName, username, databaseName, rssInstance); } catch (Exception e) { String msg = "Fail to attach database user" + " '" + databaseName + "' : " + e.getMessage(); handleException(msg, e); } } public void detachUserFromDatabase(String environmentName, String rssInstance, String databaseName, String username, String type) throws AxisFault { try { stub.detachUser(environmentName, databaseName, type, username, rssInstance); } catch (Exception e) { String msg = "Fail to de-attach user" + " '" + databaseName + "' : " + e.getMessage(); handleException(msg, e); } } public DatabaseUserInfo[] getUsersAttachedToDatabase(String environmentName, String rssInstanceName, String databaseName, String type) throws AxisFault { DatabaseUserInfo[] users = new DatabaseUserInfo[0]; try { users = stub.getAttachedUsers(environmentName, rssInstanceName, databaseName, type); } catch (Exception e) { String msg = "Fail to retrieve users attache to the database" + " '" + databaseName + "' : " + e.getMessage(); handleException(msg, e); } return users; } public DatabaseUserInfo[] getAvailableUsersToAttachToDatabase(String environmentName, String rssInstanceName, String databaseName, String type) throws AxisFault { DatabaseUserInfo[] users = new DatabaseUserInfo[0]; try { users = stub.getAvailableUsers(environmentName, rssInstanceName, databaseName, type); } catch (Exception e) { String msg = "Fail to retrieve database users attach to the database" + " '" + databaseName + "' : " + e.getMessage(); handleException(msg, e); } return users; } public DatabasePrivilegeSetInfo getUserDatabasePermissions(String environmentName, String rssInstanceName, String databaseName, String username, String type) throws AxisFault { DatabasePrivilegeSetInfo privileges = null; try { privileges = stub.getUserDatabasePrivileges(environmentName, rssInstanceName, databaseName, username, type); } catch (Exception e) { String msg = "Failed to retrieve database permissions granted to the user" + " '" + username + "' on the database '" + databaseName + "' : " + e.getMessage(); handleException(msg, e); } return privileges; } public String[] getRSSEnvironmentNames() throws AxisFault { String[] environments = new String[0]; try { environments = stub.getEnvironments(); if (environments == null) { return new String[0]; } } catch (Exception e) { String msg = "failed to retrieve rss environments list" + " : " + e.getMessage(); handleException(msg, e); } return environments; } public void createSnapshot(String environmentName, String databaseName, String type) throws AxisFault { try { stub.createSnapshot(environmentName, databaseName, type); } catch (Exception e) { String msg = "Failed to create snapshot" + " : " + e.getMessage(); handleException(msg, e); } } }