/*
* 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.adminconsole.core.admin;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axis2.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.adminconsole.core.AdminConsoleUtil;
import org.wso2.carbon.adminconsole.core.connections.ConnectionDAO;
import org.wso2.carbon.adminconsole.core.connections.ConnectionHandler;
import org.wso2.carbon.adminconsole.core.description.DatabaseInfo;
import org.wso2.carbon.adminconsole.core.description.DatabaseInstanceInfo;
import org.wso2.carbon.adminconsole.core.description.DatabaseUserInfo;
import org.wso2.carbon.adminconsole.core.multitenancy.TenantDBData;
import org.wso2.carbon.core.AbstractAdmin;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.sql.SQLException;
public class ConsoleAdmin extends AbstractAdmin {
private TenantDBData tenantData;
private ConnectionDAO dao;
private String tenantDomain;
private static final String ADMIN_CONSOLE_EXTENSION_NS =
"http://www.wso2.org/products/wso2commons/adminconsole";
private static final OMFactory FACTORY = OMAbstractFactory.getOMFactory();
private static final OMNamespace ADMIN_CONSOLE_OM_NAMESPACE = FACTORY.createOMNamespace(
ADMIN_CONSOLE_EXTENSION_NS, "instance");
private static final Log log = LogFactory.getLog(ConsoleAdmin.class);
public ConsoleAdmin() throws AxisFault {
int tenantId = SuperTenantCarbonContext.getCurrentContext(
this.getConfigContext()).getTenantId();
this.tenantDomain = SuperTenantCarbonContext.getCurrentContext(
this.getConfigContext()).getTenantDomain();
try {
this.tenantData = new TenantDBData(tenantId);
this.dao = new ConnectionDAO(tenantId);
} catch (RegistryException e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg, e);
}
}
public String createDatabaseForTenant(String strDatabaseInfo) throws AxisFault {
try {
DatabaseInfo dbInfo = AdminConsoleUtil.createDbInfoObj(
AXIOMUtil.stringToOM(strDatabaseInfo));
if (isValidDatabaseName(dbInfo.getDbName())) {
String jdbcUrl = dao.createDatabase(dbInfo, tenantDomain);
if (jdbcUrl!=null) {
dbInfo.setDbUrl(jdbcUrl);
tenantData.addDatabase(dbInfo);
}
return "Database has been successfully created";
} else {
return "A database with the same name already exists";
}
} catch (RegistryException e) {
String msg = "Unable to create database for tenant";
log.error(msg);
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
String msg = "Unable To Represent DbInfoObj As An OMElement";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (SQLException e) {
String msg = "Cannot Retrieve The Prepared Statement";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (Exception e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg,e);
}
}
public boolean createDatabaseUserForTenant(String strDbUser) throws
AxisFault {
try {
DatabaseUserInfo userInfo = AdminConsoleUtil.createDbUserObj(
AXIOMUtil.stringToOM(strDbUser));
boolean isSuccessful = dao.createDatabaseUser(userInfo);
if (isSuccessful) {
tenantData.addUser(userInfo);
}
return true;
} catch (XMLStreamException e) {
String msg = "Cannot Retrieve DatabaseUserInfo Data";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (RegistryException e) {
String msg = "Cannot Save The User To Registry";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (Exception e) {
String msg = "Error While Adding Database User";
log.error(msg, e);
throw new AxisFault(msg, e);
}
}
public String getDatabaseList() throws AxisFault {
try {
OMElement rootElement = FACTORY.createOMElement(new QName(
ADMIN_CONSOLE_EXTENSION_NS, "adminConsoleExtension", "dbs"));
for (DatabaseInfo db : tenantData.getDatabaseList()) {
OMElement dbElement = ObjSerializer.serializeDbData(ADMIN_CONSOLE_OM_NAMESPACE, db);
rootElement.addChild(dbElement);
}
return rootElement.toString();
} catch (Exception e) {
String msg = "Error while retrieving database list";
log.error(msg);
throw new AxisFault(msg, e);
}
}
public String getUserList(String dbName) throws AxisFault {
try {
OMElement rootElement = FACTORY.createOMElement(new QName(
ADMIN_CONSOLE_EXTENSION_NS, "adminConsoleExtension", "users"));
for (DatabaseUserInfo user : dao.getUsers(dbName)) {
OMElement dbUserElement = ObjSerializer.serializeDbUserData(
ADMIN_CONSOLE_OM_NAMESPACE, user);
rootElement.addChild(dbUserElement);
}
return rootElement.toString();
} catch (Exception e) {
String msg = "Error While Retrieving User list";
log.error(msg);
throw new AxisFault(msg, e);
}
}
public String getDbInstanceList() throws AxisFault {
OMElement root = FACTORY.createOMElement(new QName(
ADMIN_CONSOLE_EXTENSION_NS, "adminConsoleExtension", "instances"));
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving Database Instance Data");
}
for (DatabaseInstanceInfo databaseInstance : tenantData.getDatabaseInstanceList()) {
try {
OMElement instance = ObjSerializer.serializeDbInstanceData(
ADMIN_CONSOLE_OM_NAMESPACE, databaseInstance);
if (databaseInstance != null) {
root.addChild(instance);
}
} catch (Exception e) {
String msg = "Error Loading All Database Instances";
log.error(msg);
throw new AxisFault(msg, e);
}
}
if (log.isDebugEnabled()) {
log.debug("Returning All Database Instance Details");
}
return root.toString();
} catch (RegistryException e) {
String msg = "Error While Retrieving Database Instance List From The Registry";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
String msg="";
log.error(msg,e);
throw new AxisFault(msg,e);
}
}
public String addDatabaseInstance(String strDbInstance) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Adding Database Instance");
}
try {
DatabaseInstanceInfo databaseInstance = AdminConsoleUtil.createDbInstanceObj(
AXIOMUtil.stringToOM(strDbInstance));
if (isValidInstanceName(databaseInstance.getInstanceName())) {
tenantData.addDatabaseInstance(databaseInstance);
return "Database instance has been successfully added";
} else {
return "A database instance with the same name already exists";
}
} catch (RegistryException e) {
String msg = "Error While Adding Database Instance";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
String msg = "Error While De-serializing The Instance OMElement";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (Exception e) {
String msg="";
log.error(msg,e);
throw new AxisFault(msg,e);
}
}
public void dropDatabase(String instanceName, String dbName) throws AxisFault {
try {
dao.dropDatabase(instanceName, dbName);
tenantData.removeDatabase(instanceName, dbName);
} catch (SQLException e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (ClassNotFoundException e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg,e);
} catch (RegistryException e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg ,e);
} catch (Exception e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg, e);
}
}
public String testConnection(String driverClass, String jdbcURL, String username,String password) {
if (driverClass == null || driverClass.length() == 0) {
String message = "Driver class is missing";
log.debug(message);
return message;
}
if (jdbcURL == null || jdbcURL.length() == 0) {
String message = "Driver connection URL is missing";
log.debug(message);
return message;
}
try {
ConnectionHandler.getConnection(jdbcURL, username, password);
String message = "Database connection is successful with driver class " + driverClass
+ " , JDBC url " + jdbcURL + " and username " + username;
log.debug(message);
return message;
} catch (SQLException e) {
String message = "Could not connect to database " + jdbcURL + " with username "
+ username;
log.error(message, e);
return message;
} catch (ClassNotFoundException e) {
String message = "Driver class " + driverClass + " can not be loaded";
log.error(message, e);
return message;
} finally {
ConnectionHandler.closeConnection();
}
}
public String getDatabaseInfo (String dbName) throws AxisFault {
try {
for (DatabaseInfo dbInfo : tenantData.getDatabaseList()) {
if (dbName.equals(dbInfo.getDbName())) {
return ObjSerializer.serializeDbData(
ADMIN_CONSOLE_OM_NAMESPACE, dbInfo).toString();
}
}
return "";
} catch (RegistryException e) {
String msg = "Error while retrieving database list from the registry";
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
throw new AxisFault("", e);
} catch (Exception e) {
throw new AxisFault("", e);
}
}
private boolean isValidDatabaseName (String dbName) throws AxisFault {
try {
for (DatabaseInfo dbInfo : tenantData.getDatabaseList()) {
if (dbName.equals(dbInfo.getDbName())) {
return false;
}
}
return true;
} catch (RegistryException e) {
String msg = "Error while retrieving database list from the registry";
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
throw new AxisFault("", e);
} catch (Exception e) {
throw new AxisFault("", e);
}
}
private boolean isValidInstanceName (String instanceName) throws AxisFault {
try {
for (DatabaseInstanceInfo instanceInfo : tenantData.getDatabaseInstanceList()) {
if (instanceName.equals(instanceInfo.getInstanceName())) {
return false;
}
}
return true;
} catch (RegistryException e) {
String msg = "Error while retrieving database list from the registry";
throw new AxisFault(msg, e);
} catch (XMLStreamException e) {
throw new AxisFault("", e);
} catch (Exception e) {
throw new AxisFault("", e);
}
}
public void removeDatabaseInstance (String instanceName) throws AxisFault {
try {
if (!isValidInstanceName(instanceName)) {
tenantData.removeDbInstance(instanceName);
}
} catch (RegistryException e) {
String msg = "Error while removing database instance";
throw new AxisFault(msg, e);
} catch (Exception e) {
String msg = "Error while removing database instance";
throw new AxisFault(msg, e);
}
}
public String editDatabaseInstanceInfo (String strDbInstance) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Editing Database Instance Info");
}
try {
DatabaseInstanceInfo databaseInstance = AdminConsoleUtil.createDbInstanceObj(
AXIOMUtil.stringToOM(strDbInstance));
if (!isValidInstanceName(databaseInstance.getInstanceName())) {
tenantData.editDatabaseInstanceInfo(databaseInstance);
return "Database instance has been successfully edited";
} else {
return "Database instance does not exist";
}
} catch (XMLStreamException e) {
String msg = "Error While De-serializing The Instance OMElement";
log.error(msg, e);
throw new AxisFault(msg, e);
} catch (Exception e) {
String msg = "";
log.error(msg, e);
throw new AxisFault(msg, e);
}
}
public String getDatabaseUserInfo (String instanceName, String dbName) throws AxisFault {
try {
DatabaseUserInfo userInfo = tenantData.getDatabaseUserInfo(instanceName, dbName);
return ObjSerializer.serializeDbUserData(ADMIN_CONSOLE_OM_NAMESPACE, userInfo).toString();
} catch (RegistryException e) {
throw new AxisFault("", e);
} catch (XMLStreamException e) {
throw new AxisFault("", e);
} catch (Exception e) {
throw new AxisFault("", e);
}
}
public String getDatabaseInstanceInfo (String instanceName) throws AxisFault {
try {
DatabaseInstanceInfo instanceInfo = tenantData.getDatabaseInstanceInfo(instanceName);
return ObjSerializer.serializeDbInstanceData(
ADMIN_CONSOLE_OM_NAMESPACE, instanceInfo).toString();
} catch (RegistryException e) {
throw new AxisFault("", e);
} catch (XMLStreamException e) {
throw new AxisFault("", e);
} catch (Exception e) {
throw new AxisFault("", e);
}
}
}