- Changes to Configuring hibernate to connect to multiple databases: cloudbridge and cloudstack

- This avoids making multiple API calls just to list ServiceOfferings
This commit is contained in:
prachi 2012-04-26 15:08:36 -07:00
parent 35d3c279db
commit 74a152df8d
16 changed files with 566 additions and 354 deletions

8
awsapi/.classpath Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/deps"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,28 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.pool_size">20</property>
<property name="hibernate.connection.autocommit">false</property>
<!-- transactiion isolation level : 1 - read uncommitted, 2 - read committed, 4 - repeatable read, 8 - Serializable -->
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.order_updates">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<!-- to debug hibernate generated SQL, open following configuration property -->
<!--
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
-->
<!-- Mapping files -->
<mapping resource="com/cloud/stack/models/CloudStackConfiguration.hbm.xml"/>
<mapping resource="com/cloud/stack/models/CloudStackServiceOffering.hbm.xml"/>
</session-factory>
</hibernate-configuration>

View File

@ -29,38 +29,44 @@ import com.cloud.bridge.util.QueryHelper;
public class EntityDao<T> {
private Class<?> clazz;
public EntityDao(Class<?> clazz) {
private boolean isCloudStackSession = false;
public EntityDao(Class<?> clazz){
this(clazz, false);
}
public EntityDao(Class<?> clazz, boolean isCloudStackSession) {
this.clazz = clazz;
this.isCloudStackSession = isCloudStackSession;
// Note : beginTransaction can be called multiple times
PersistContext.beginTransaction();
PersistContext.beginTransaction(isCloudStackSession);
}
@SuppressWarnings("unchecked")
public T get(Serializable id) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
return (T)session.get(clazz, id);
}
public T save(T entity) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
session.saveOrUpdate(entity);
return entity;
}
public T update(T entity) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
session.saveOrUpdate(entity);
return entity;
}
public void delete(T entity) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
session.delete(entity);
}
public T queryEntity(String hql, Object[] params) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
Query query = session.createQuery(hql);
query.setMaxResults(1);
QueryHelper.bindParameters(query, params);
@ -68,7 +74,7 @@ public class EntityDao<T> {
}
public List<T> queryEntities(String hql, Object[] params) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
Query query = session.createQuery(hql);
QueryHelper.bindParameters(query, params);
@ -76,7 +82,7 @@ public class EntityDao<T> {
}
public List<T> queryEntities(String hql, int offset, int limit, Object[] params) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
Query query = session.createQuery(hql);
QueryHelper.bindParameters(query, params);
query.setFirstResult(offset);
@ -85,7 +91,7 @@ public class EntityDao<T> {
}
public int executeUpdate(String hql, Object[] params) {
Session session = PersistContext.getSession();
Session session = PersistContext.getSession(isCloudStackSession);
Query query = session.createQuery(hql);
QueryHelper.bindParameters(query, params);

View File

@ -28,6 +28,7 @@ import org.hibernate.Session;
import org.hibernate.Transaction;
import com.cloud.bridge.util.CloudSessionFactory;
import com.cloud.bridge.util.CloudStackSessionFactory;
import com.cloud.bridge.util.Tuple;
/**
@ -48,78 +49,144 @@ public class PersistContext {
protected final static Logger logger = Logger.getLogger(PersistContext.class);
private static final CloudSessionFactory sessionFactory;
private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();
private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();
private static final ThreadLocal<Map<String, Object>> threadStore = new ThreadLocal<Map<String, Object>>();
private static final CloudStackSessionFactory cloudStackSessionFactory;
private static final ThreadLocal<Session> threadCloudStackSession = new ThreadLocal<Session>();
private static final ThreadLocal<Transaction> threadCloudStackTransaction = new ThreadLocal<Transaction>();
static {
try {
sessionFactory = CloudSessionFactory.getInstance();
cloudStackSessionFactory = CloudStackSessionFactory.getInstance();
} catch(HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
}
}
public static Session getSession(boolean cloudStackSession) {
Session s = null;
try {
if(cloudStackSession){
s = threadCloudStackSession.get();
if(s == null) {
s = cloudStackSessionFactory.openSession();
threadCloudStackSession.set(s);
}
}else{
s = threadSession.get();
if(s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
}
} catch(HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
}
return s;
}
public static Session getSession() {
Session s = threadSession.get();
try {
if(s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
} catch(HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
}
return s;
return getSession(false);
}
public static void closeSession() {
closeSession(false);
}
public static void closeSession(boolean cloudStackSession) {
try {
if(cloudStackSession){
Session s = (Session) threadCloudStackSession.get();
threadCloudStackSession.set(null);
if (s != null && s.isOpen())
s.close();
}else{
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen())
s.close();
}
}catch(HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
}
}
public static void beginTransaction(boolean cloudStackTxn) {
Transaction tx = null;
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen())
s.close();
if(cloudStackTxn){
tx = threadCloudStackTransaction.get();
}else{
tx = threadTransaction.get();
}
if (tx == null) {
tx = getSession(cloudStackTxn).beginTransaction();
if(cloudStackTxn){
threadCloudStackTransaction.set(tx);
}else{
threadTransaction.set(tx);
}
}
} catch(HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
}
}
public static void beginTransaction() {
Transaction tx = threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
beginTransaction(false);
}
public static void commitTransaction(boolean cloudStackTxn) {
Transaction tx = null;
if(cloudStackTxn){
tx = threadCloudStackTransaction.get();
}else{
tx = threadTransaction.get();
}
try {
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ){
tx.commit();
}
} catch(HibernateException e) {
if(cloudStackTxn){
threadCloudStackTransaction.set(null);
}else{
threadTransaction.set(null);
}
} catch (HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
rollbackTransaction(cloudStackTxn);
throw new PersistException(e);
}
}
public static void commitTransaction() {
Transaction tx = threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() )
tx.commit();
threadTransaction.set(null);
} catch (HibernateException e) {
logger.error("Exception " + e.getMessage(), e);
rollbackTransaction();
throw new PersistException(e);
}
commitTransaction(false);
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
public static void rollbackTransaction(boolean cloudStackTxn) {
Transaction tx = null;
if(cloudStackTxn){
tx = (Transaction)threadCloudStackTransaction.get();
threadCloudStackTransaction.set(null);
}else{
tx = (Transaction)threadTransaction.get();
threadTransaction.set(null);
}
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
tx.rollback();
}
@ -127,10 +194,14 @@ public class PersistContext {
logger.error("Exception " + e.getMessage(), e);
throw new PersistException(e);
} finally {
closeSession();
closeSession(cloudStackTxn);
}
}
public static void rollbackTransaction() {
rollbackTransaction(false);
}
public static void flush() {
commitTransaction();
beginTransaction();

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2011 Citrix Systems, Inc. All rights reserved.
*
* 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 com.cloud.bridge.persist.dao;
import org.apache.log4j.Logger;
import com.cloud.bridge.persist.EntityDao;
import com.cloud.stack.models.CloudStackConfiguration;
public class CloudStackConfigurationDao extends EntityDao<CloudStackConfiguration> {
public static final Logger logger = Logger.getLogger(CloudStackConfigurationDao.class);
public CloudStackConfigurationDao() {
super(CloudStackConfiguration.class, true);
}
public String getConfigValue( String configName ){
CloudStackConfiguration config = queryEntity("from CloudStackConfiguration where name=?", new Object[] {configName});
if(config != null){
return config.getValue();
}
return null;
}
}

View File

@ -1,80 +0,0 @@
/*
* Copyright (C) 2011 Citrix Systems, Inc. All rights reserved.
*
* 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 com.cloud.bridge.persist.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.cloud.bridge.util.ConfigurationHelper;
public class CloudStackDao extends BaseDao {
public static final Logger logger = Logger.getLogger(CloudStackDao.class);
private Connection conn = null;
public CloudStackDao() {
}
public String getConfigValue( String configName ){
String value = null;
try {
openConnection();
PreparedStatement statement = conn.prepareStatement ( "SELECT value FROM `cloud`.`configuration` where name = ?" );
statement.setString( 1, configName );
statement.executeQuery();
ResultSet rs = statement.getResultSet ();
if (rs.next()) {
value = rs.getString(1);
}
}catch (Exception e) {
logger.warn("Failed to access CloudStack DB, got error: ", e);
} finally {
try{
closeConnection();
}catch(SQLException e){
}
}
return value;
}
private void openConnection()
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
if (null == conn) {
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
conn = DriverManager.getConnection( "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + cloud_dbName, dbUser, dbPassword );
}
}
private void closeConnection() throws SQLException {
if (null != conn) conn.close();
conn = null;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2011 Citrix Systems, Inc. All rights reserved.
*
* 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 com.cloud.bridge.persist.dao;
import org.apache.log4j.Logger;
import com.cloud.bridge.persist.EntityDao;
import com.cloud.stack.models.CloudStackConfiguration;
import com.cloud.stack.models.CloudStackServiceOffering;
public class CloudStackSvcOfferingDao extends EntityDao<CloudStackServiceOffering> {
public static final Logger logger = Logger.getLogger(CloudStackSvcOfferingDao.class);
public CloudStackSvcOfferingDao() {
super(CloudStackServiceOffering.class, true);
}
public CloudStackServiceOffering getSvcOfferingByName( String name ){
return queryEntity("from CloudStackServiceOffering where name=?", new Object[] {name});
}
public CloudStackServiceOffering getSvcOfferingById( String id ){
return queryEntity("from CloudStackServiceOffering where id=?", new Object[] {id});
}
}

View File

@ -15,165 +15,51 @@
*/
package com.cloud.bridge.persist.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import com.cloud.bridge.model.UserCredentials;
import com.cloud.bridge.persist.EntityDao;
import com.cloud.bridge.service.exception.NoSuchObjectException;
public class UserCredentialsDao extends BaseDao{
public class UserCredentialsDao extends EntityDao<UserCredentials>{
public static final Logger logger = Logger.getLogger(UserCredentialsDao.class);
private Connection conn = null;
public UserCredentialsDao() {
super(UserCredentials.class);
}
public void setUserKeys( String cloudAccessKey, String cloudSecretKey )
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
public void setUserKeys( String cloudAccessKey, String cloudSecretKey ){
UserCredentials user = getByAccessKey( cloudAccessKey );
PreparedStatement statement = null;
openConnection();
try {
if ( null == user ) {
// -> do an insert since the user does not exist yet
statement = conn.prepareStatement ( "INSERT INTO usercredentials (AccessKey, SecretKey) VALUES(?,?)" );
statement.setString( 1, cloudAccessKey );
statement.setString( 2, cloudSecretKey );
}
else {
// -> do an update since the user exists
statement = conn.prepareStatement ( "UPDATE usercredentials SET SecretKey=? WHERE AccessKey=?" );
statement.setString( 1, cloudSecretKey );
statement.setString( 2, cloudAccessKey );
}
int count = statement.executeUpdate();
statement.close();
} finally {
closeConnection();
}
if ( null == user ) {
// -> do an insert since the user does not exist yet
user = new UserCredentials();
user.setAccessKey(cloudAccessKey);
user.setSecretKey(cloudSecretKey);
save(user);
}
else {
// -> do an update since the user exists
user.setAccessKey(cloudAccessKey);
user.setSecretKey(cloudSecretKey);
update(user);
}
}
public void setCertificateId( String cloudAccessKey, String certId )
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
public void setCertificateId( String cloudAccessKey, String certId ){
UserCredentials user = getByAccessKey( cloudAccessKey );
PreparedStatement statement = null;
if (null == user) throw new NoSuchObjectException( "Cloud API Access Key [" + cloudAccessKey + "] is unknown" );
openConnection();
try {
statement = conn.prepareStatement ( "UPDATE usercredentials SET CertUniqueId=? WHERE AccessKey=?" );
statement.setString( 1, certId );
statement.setString( 2, cloudAccessKey );
int count = statement.executeUpdate();
statement.close();
} finally {
closeConnection();
}
user.setCertUniqueId(certId);
update(user);
}
public UserCredentials getByAccessKey( String cloudAccessKey )
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
openConnection();
UserCredentials user = null;
try {
PreparedStatement statement = conn.prepareStatement ( "SELECT SecretKey, CertUniqueId FROM usercredentials WHERE AccessKey=?" );
statement.setString( 1, cloudAccessKey );
statement.executeQuery();
ResultSet rs = statement.getResultSet ();
if (rs.next()) {
user = new UserCredentials();
user.setAccessKey( cloudAccessKey );
user.setSecretKey( rs.getString( "SecretKey" ));
user.setCertUniqueId( rs.getString( "CertUniqueId" ));
}
} finally {
closeConnection();
}
return user;
}
public UserCredentials getByCertUniqueId( String certId )
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
openConnection();
UserCredentials user = null;
try {
PreparedStatement statement = conn.prepareStatement ( "SELECT AccessKey, SecretKey FROM usercredentials WHERE CertUniqueId=?" );
statement.setString( 1, certId );
statement.executeQuery();
ResultSet rs = statement.getResultSet ();
if (rs.next()) {
user = new UserCredentials();
user.setAccessKey( rs.getString( "AccessKey" ));
user.setSecretKey( rs.getString( "SecretKey" ));
user.setCertUniqueId( certId );
}
} finally {
closeConnection();
}
return user;
public UserCredentials getByAccessKey( String cloudAccessKey ){
return queryEntity("FROM UserCredentials WHERE AccessKey=?", new Object[] {cloudAccessKey});
}
private void openConnection()
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
if (null == conn) {
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
conn = DriverManager.getConnection( "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + awsapi_dbName, dbUser, dbPassword );
}
}
private void closeConnection() throws SQLException {
if (null != conn) conn.close();
conn = null;
}
public static void preCheckTableExistence() {
UserCredentialsDao dao = new UserCredentialsDao();
try {
dao.checkTableExistence();
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkTableExistence() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
openConnection();
try {
PreparedStatement statement = conn.prepareStatement ( "SELECT * FROM usercredentials " );
statement.executeQuery();
ResultSet rs = statement.getResultSet ();
if (rs.next()) {
return;
}
return;
} catch(Exception e) {
Statement statement = conn.createStatement();
statement.execute( "create table usercredentials(id integer auto_increment primary key, AccessKey varchar(1000), SecretKey varchar(1000), CertUniqueId varchar(1000))" );
statement.close();
}
finally{
closeConnection();
}
}
public UserCredentials getByCertUniqueId( String certId ){
return queryEntity("FROM UserCredentials WHERE CertUniqueId=?", new Object[] {certId});
}
}

View File

@ -9,7 +9,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cloud.bridge.persist.dao.CloudStackDao;
import com.cloud.bridge.persist.dao.CloudStackConfigurationDao;
import com.cloud.bridge.persist.dao.UserCredentialsDao;
import com.cloud.bridge.util.ConfigurationHelper;
@ -28,10 +28,8 @@ public class EC2MainServlet extends HttpServlet{
*/
public void init( ServletConfig config ) throws ServletException {
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
UserCredentialsDao.preCheckTableExistence();
// check if API is enabled
CloudStackDao csDao = new CloudStackDao();
CloudStackConfigurationDao csDao = new CloudStackConfigurationDao();
String value = csDao.getConfigValue(ENABLE_EC2_API);
if(value != null){
isEC2APIEnabled = Boolean.valueOf(value);

View File

@ -33,6 +33,7 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.cloud.bridge.persist.dao.CloudStackSvcOfferingDao;
import com.cloud.bridge.persist.dao.OfferingDao;
import com.cloud.bridge.service.UserContext;
import com.cloud.bridge.service.exception.EC2ServiceException;
@ -1560,30 +1561,19 @@ public class EC2Engine {
/**
*
* Convert from the Amazon instanceType strings to Cloud serviceOfferingId
*
*/
private CloudStackServiceOffering getCSServiceOfferingId(String instanceType) throws Exception{
private CloudStackServiceOffering getCSServiceOfferingId(String instanceType){
try {
if (null == instanceType) instanceType = "m1.small";
List<CloudStackServiceOffering> svcOfferings = getApi().listServiceOfferings(null, null, null, null, null,
null, null);
if(svcOfferings == null || svcOfferings.isEmpty()){
logger.debug("No ServiceOffering found to be defined by name: "+instanceType );
return null;
}
CloudStackSvcOfferingDao dao = new CloudStackSvcOfferingDao();
return dao.getSvcOfferingByName(instanceType);
for(CloudStackServiceOffering offering : svcOfferings){
if(instanceType.equalsIgnoreCase(offering.getName())){
return offering;
}
}
return null;
} catch(Exception e) {
logger.error( "listServiceOfferings - ", e);
logger.error( "Error while retrieving ServiceOffering information by name - ", e);
throw new EC2ServiceException(ServerError.InternalError, e.getMessage());
}
}
@ -1596,22 +1586,18 @@ public class EC2Engine {
* @return A valid value for the Amazon defined instanceType
* @throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException
*/
private String serviceOfferingIdToInstanceType( String serviceOfferingId )
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
private String serviceOfferingIdToInstanceType( String serviceOfferingId ){
try{
List<CloudStackServiceOffering> svcOfferings = getApi().listServiceOfferings(null, serviceOfferingId, null, null, null,
null, null);
if(svcOfferings == null || svcOfferings.isEmpty()){
logger.warn( "No instanceType match for serverOfferingId: [" + serviceOfferingId + "]" );
CloudStackSvcOfferingDao dao = new CloudStackSvcOfferingDao();
CloudStackServiceOffering offering = dao.getSvcOfferingById(serviceOfferingId);
if(offering == null){
logger.warn( "No instanceType match for serviceOfferingId: [" + serviceOfferingId + "]" );
return "m1.small";
}
else return svcOfferings.get(0).getName();
return offering.getName();
}
catch(Exception e) {
logger.error( "serviceOfferingIdToInstanceType - ", e);
logger.error( "sError while retrieving ServiceOffering information by id - ", e);
throw new EC2ServiceException(ServerError.InternalError, e.getMessage());
}
}

View File

@ -0,0 +1,109 @@
/*
* Copyright (C) 2011 Citrix Systems, Inc. All rights reserved.
*
* 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 com.cloud.bridge.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;
import org.apache.log4j.Logger;
/**
* @author Kelven Yang
*/
public class CloudStackSessionFactory {
private static CloudStackSessionFactory instance;
public static final Logger logger = Logger.getLogger(CloudStackSessionFactory.class);
private SessionFactory factory;
private CloudStackSessionFactory() {
Configuration cfg = new Configuration();
File file = ConfigurationHelper.findConfigurationFile("CloudStack.cfg.xml");
File propertiesFile = ConfigurationHelper.findConfigurationFile("db.properties");
Properties dbProp = null;
String dbName = null;
String dbHost = null;
String dbUser = null;
String dbPassword = null;
String dbPort = null;
if (null != propertiesFile) {
if(EncryptionSecretKeyCheckerUtil.useEncryption()){
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyCheckerUtil.getEncryptor();
dbProp = new EncryptableProperties(encryptor);
} else {
dbProp = new Properties();
}
try {
dbProp.load( new FileInputStream( propertiesFile ));
} catch (FileNotFoundException e) {
logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e);
} catch (IOException e) {
logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e);
}
}
//
// we are packaging hibernate mapping files along with the class files,
// make sure class loader use the same class path when initializing hibernate mapping.
// This is important when we are deploying and testing at different environment (Tomcat/JUnit test runner)
//
if(file != null && dbProp != null){
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
cfg.configure(file);
dbHost = dbProp.getProperty( "db.cloud.host" );
dbName = dbProp.getProperty( "db.cloud.name" );
dbUser = dbProp.getProperty( "db.cloud.username" );
dbPassword = dbProp.getProperty( "db.cloud.password" );
dbPort = dbProp.getProperty( "db.cloud.port" );
cfg.setProperty("hibernate.connection.url", "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName);
cfg.setProperty("hibernate.connection.username", dbUser);
cfg.setProperty("hibernate.connection.password", dbPassword);
factory = cfg.buildSessionFactory();
}else{
logger.warn("Unable to open load db configuration");
throw new RuntimeException("nable to open load db configuration");
}
}
public synchronized static CloudStackSessionFactory getInstance() {
if(instance == null) {
instance = new CloudStackSessionFactory();
}
return instance;
}
public Session openSession() {
return factory.openSession();
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cloud.stack.models.CloudStackConfiguration" table="configuration" lazy="true">
<id name="name" type="string" column="name" >
</id>
<property name="category">
<column name="category" />
</property>
<property name="value">
<column name="value" />
</property>
<property name="description">
<column name="description" />
</property>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,56 @@
package com.cloud.stack.models;
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
public class CloudStackConfiguration implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName(ApiConstants.CATEGORY)
private String category;
@SerializedName(ApiConstants.NAME)
private String name;
@SerializedName(ApiConstants.VALUE)
private String value;
@SerializedName(ApiConstants.DESCRIPTION)
private String description;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cloud.stack.models.CloudStackServiceOffering" table="disk_offering" lazy="true">
<id name="id" type="string" column="uuid" >
</id>
<property name="name">
<column name="name" />
</property>
<property name="domainId">
<column name="domain_id" />
</property>
</class>
</hibernate-mapping>

View File

@ -49,7 +49,7 @@ public class CloudStackServiceOffering {
private Long memory;
@SerializedName(ApiConstants.NAME)
private String name;
@SerializedName(ApiConstants.OFFER_HA)
@SerializedName(ApiConstants.OFFER_HA)
private Boolean offerHa;
@SerializedName(ApiConstants.STORAGE_TYPE)
private String storageType;
@ -72,6 +72,10 @@ public class CloudStackServiceOffering {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @return the cpuNumber
*/
@ -121,7 +125,11 @@ public class CloudStackServiceOffering {
return domainId;
}
/**
public void setDomainId(String domainId) {
this.domainId = domainId;
}
/**
* @return the hostTags
*/
public String getHostTags() {
@ -155,7 +163,11 @@ public class CloudStackServiceOffering {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @return the offerHa
*/

115
deps/.classpath vendored
View File

@ -1,50 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="XenServerJava"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="cloud-axis.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-backport-util-concurrent-3.0.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-bcprov-jdk16-1.45.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-cglib.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-collections-3.2.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-discovery.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-logging-1.1.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-ehcache.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-email.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-httpcore-4.0.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jasypt-1.8.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jsch-0.1.42.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jstl-1.2.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-libvirt-0.4.5.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-log4j.jar" sourcepath="/home/dev/thirdparty/apache-log4j-1.2.16/src/main/java"/>
<classpathentry exported="true" kind="lib" path="cloud-mysql-connector-java-5.1.7-bin.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-servlet-api.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-trilead-ssh2-build213.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-ws-commons-util-1.0.2.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-wsdl4j.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xenserver-5.6.100-1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xmlrpc-client-3.1.3.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xmlrpc-common-3.1.3.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xstream-1.3.1.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-6.1.26.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-util-6.1.26.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-log4j-extras.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-iControl.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-manageontap.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-apputils.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-lib-jaxrpc.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim25.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jnetpcap.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-junit.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-codec-1.5.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-dbcp-1.4.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-pool-1.5.6.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-google-gson-1.7.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-httpclient-3.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-netscaler.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-netscaler-sdx.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-configuration-1.8.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-lang-2.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="XenServerJava"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="cloud-axis.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-backport-util-concurrent-3.0.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-bcprov-jdk16-1.45.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-cglib.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-collections-3.2.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-discovery.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-logging-1.1.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-ehcache.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-email.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-httpcore-4.0.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jasypt-1.8.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jsch-0.1.42.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jstl-1.2.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-libvirt-0.4.5.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-log4j.jar" sourcepath="/home/dev/thirdparty/apache-log4j-1.2.16/src/main/java"/>
<classpathentry exported="true" kind="lib" path="cloud-mysql-connector-java-5.1.7-bin.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-servlet-api.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-trilead-ssh2-build213.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-ws-commons-util-1.0.2.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xenserver-5.6.100-1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xmlrpc-client-3.1.3.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xmlrpc-common-3.1.3.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-xstream-1.3.1.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-6.1.26.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-util-6.1.26.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-log4j-extras.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-iControl.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-manageontap.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-apputils.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-lib-jaxrpc.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim25.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-jnetpcap.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-junit.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-codec-1.5.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-dbcp-1.4.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-pool-1.5.6.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-google-gson-1.7.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-httpclient-3.1.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-netscaler.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-netscaler-sdx.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-configuration-1.8.jar"/>
<classpathentry exported="true" kind="lib" path="cloud-commons-lang-2.6.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axiom-api-1.2.8.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axiom-impl-1.2.8.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-adb-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-ant-plugin-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-jaxbri-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-jaxws-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-jibx-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-json-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-kernel-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-transport-http-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/axis2-transport-local-1.5.1.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/hibernate3.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/neethi-2.0.4.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/rampart-lib/wss4j-1.5.8.jar"/>
<classpathentry exported="true" kind="lib" path="awsapi-lib/json_simple-1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>