Skip to content

Commit 81f1a0b

Browse files
author
Kishan Kavala
committed
CLOUDSTACK-4095 : Remove region_id from Transaction. Read from db.properties whenever required
Conflicts: framework/db/src/com/cloud/utils/db/GenericDaoBase.java
1 parent df3b099 commit 81f1a0b

8 files changed

Lines changed: 94 additions & 18 deletions

File tree

engine/schema/src/com/cloud/upgrade/dao/Upgrade307to410.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818
package com.cloud.upgrade.dao;
1919

2020
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
2123
import java.sql.Connection;
2224
import java.sql.PreparedStatement;
2325
import java.sql.SQLException;
26+
import java.util.Properties;
2427

28+
import com.cloud.utils.PropertiesUtil;
29+
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
2530
import org.apache.log4j.Logger;
2631

2732
import com.cloud.utils.db.Transaction;
2833
import com.cloud.utils.exception.CloudRuntimeException;
2934
import com.cloud.utils.script.Script;
35+
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
36+
import org.jasypt.properties.EncryptableProperties;
3037

3138
public class Upgrade307to410 implements DbUpgrade {
3239
final static Logger s_logger = Logger.getLogger(Upgrade307to410.class);
@@ -62,7 +69,28 @@ public void performDataMigration(Connection conn) {
6269
}
6370

6471
private void updateRegionEntries(Connection conn) {
65-
int region_id = Transaction.s_region_id;
72+
File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
73+
final Properties dbProps;
74+
if (EncryptionSecretKeyChecker.useEncryption()) {
75+
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
76+
dbProps = new EncryptableProperties(encryptor);
77+
} else {
78+
dbProps = new Properties();
79+
}
80+
try {
81+
dbProps.load(new FileInputStream(dbPropsFile));
82+
} catch (IOException e) {
83+
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
84+
return;
85+
} catch (NullPointerException e) {
86+
s_logger.fatal("Unable to locate db properties file within classpath or absolute path: db.properties");
87+
return;
88+
}
89+
int region_id = 1;
90+
String regionId = dbProps.getProperty("region.id");
91+
if(regionId != null){
92+
region_id = Integer.parseInt(regionId);
93+
}
6694
PreparedStatement pstmt = null;
6795
try {
6896
//Update regionId in region table

engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@
1717

1818
package com.cloud.upgrade.dao;
1919

20+
import com.cloud.utils.PropertiesUtil;
21+
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
2022
import com.cloud.utils.db.Transaction;
2123
import com.cloud.utils.exception.CloudRuntimeException;
2224
import com.cloud.utils.script.Script;
2325
import org.apache.log4j.Logger;
26+
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
27+
import org.jasypt.properties.EncryptableProperties;
2428

2529
import java.io.File;
30+
import java.io.FileInputStream;
31+
import java.io.IOException;
2632
import java.sql.Connection;
2733
import java.sql.PreparedStatement;
2834
import java.sql.ResultSet;
2935
import java.sql.SQLException;
36+
import java.util.Properties;
3037
import java.util.UUID;
3138

3239
public class Upgrade40to41 implements DbUpgrade {
@@ -74,7 +81,28 @@ public File[] getCleanupScripts() {
7481
}
7582

7683
private void updateRegionEntries(Connection conn) {
77-
int region_id = Transaction.s_region_id;
84+
File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
85+
final Properties dbProps;
86+
if (EncryptionSecretKeyChecker.useEncryption()) {
87+
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
88+
dbProps = new EncryptableProperties(encryptor);
89+
} else {
90+
dbProps = new Properties();
91+
}
92+
try {
93+
dbProps.load(new FileInputStream(dbPropsFile));
94+
} catch (IOException e) {
95+
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
96+
return;
97+
} catch (NullPointerException e) {
98+
s_logger.fatal("Unable to locate db properties file within classpath or absolute path: db.properties");
99+
return;
100+
}
101+
int region_id = 1;
102+
String regionId = dbProps.getProperty("region.id");
103+
if(regionId != null){
104+
region_id = Integer.parseInt(regionId);
105+
}
78106
PreparedStatement pstmt = null;
79107
try {
80108
//Update regionId in region table

engine/schema/src/org/apache/cloudstack/region/dao/RegionDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
public interface RegionDao extends GenericDao<RegionVO, Integer> {
2424

2525
RegionVO findByName(String name);
26-
26+
int getRegionId();
2727
}

engine/schema/src/org/apache/cloudstack/region/dao/RegionDaoImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public RegionVO findByName(String name) {
4545
sc.setParameters("name", name);
4646
return findOneBy(sc);
4747
}
48+
49+
@Override
50+
public int getRegionId(){
51+
return 1;
52+
}
4853
}

framework/db/src/com/cloud/utils/db/GenericDao.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ public interface GenericDao<T, ID extends Serializable> {
265265
*/
266266
Class<T> getEntityBeanType();
267267

268-
public int getRegionId();
269-
270268
/**
271269
* @param sc
272270
* @param filter

framework/db/src/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,11 +1796,6 @@ public SearchCriteria<T> createSearchCriteria() {
17961796
return builder.create();
17971797
}
17981798

1799-
@Override
1800-
public int getRegionId(){
1801-
return Transaction.s_region_id;
1802-
}
1803-
18041799
public Integer getCount(SearchCriteria<T> sc) {
18051800
String clause = sc != null ? sc.getWhereClause() : null;
18061801
if (clause != null && clause.length() == 0) {

framework/db/src/com/cloud/utils/db/Transaction.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public class Transaction {
8383
public static final short AWSAPI_DB = 2;
8484
public static final short SIMULATOR_DB = 3;
8585
public static final short CONNECTED_DB = -1;
86-
public static int s_region_id;
8786

8887
private static AtomicLong s_id = new AtomicLong();
8988
private static final TransactionMBeanImpl s_mbean = new TransactionMBeanImpl();
@@ -1079,12 +1078,6 @@ public static void initDataSource(String propsFileName) {
10791078
System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword"));
10801079
}
10811080

1082-
String regionId = dbProps.getProperty("region.id");
1083-
if(regionId == null){
1084-
s_region_id = 1;
1085-
} else {
1086-
s_region_id = Integer.parseInt(regionId);
1087-
}
10881081
final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
10891082
cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
10901083

server/src/org/apache/cloudstack/region/RegionManagerImpl.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,30 @@
2626
import com.cloud.user.UserAccount;
2727
import com.cloud.user.dao.AccountDao;
2828
import com.cloud.user.dao.UserAccountDao;
29+
import com.cloud.utils.PropertiesUtil;
2930
import com.cloud.utils.component.Manager;
3031
import com.cloud.utils.component.ManagerBase;
32+
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
3133
import org.apache.cloudstack.api.command.admin.account.UpdateAccountCmd;
3234
import org.apache.cloudstack.api.command.admin.domain.UpdateDomainCmd;
3335
import org.apache.cloudstack.api.command.admin.user.DeleteUserCmd;
3436
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
3537
import org.apache.cloudstack.region.dao.RegionDao;
3638
import org.apache.log4j.Logger;
39+
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
40+
import org.jasypt.properties.EncryptableProperties;
3741
import org.springframework.stereotype.Component;
3842

3943
import javax.ejb.Local;
4044
import javax.inject.Inject;
4145
import javax.naming.ConfigurationException;
46+
import java.io.File;
47+
import java.io.FileInputStream;
48+
import java.io.IOException;
4249
import java.util.ArrayList;
4350
import java.util.List;
4451
import java.util.Map;
52+
import java.util.Properties;
4553

4654
@Component
4755
@Local(value = { RegionManager.class })
@@ -63,7 +71,28 @@ public class RegionManagerImpl extends ManagerBase implements RegionManager, Man
6371
@Override
6472
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
6573
_name = name;
66-
_id = _regionDao.getRegionId();
74+
File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
75+
final Properties dbProps;
76+
if (EncryptionSecretKeyChecker.useEncryption()) {
77+
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
78+
dbProps = new EncryptableProperties(encryptor);
79+
} else {
80+
dbProps = new Properties();
81+
}
82+
try {
83+
dbProps.load(new FileInputStream(dbPropsFile));
84+
} catch (IOException e) {
85+
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
86+
return false;
87+
} catch (NullPointerException e) {
88+
s_logger.fatal("Unable to locate db properties file within classpath or absolute path: db.properties");
89+
return false;
90+
}
91+
String regionId = dbProps.getProperty("region.id");
92+
_id = 1;
93+
if(regionId != null){
94+
_id = Integer.parseInt(regionId);
95+
}
6796
return true;
6897
}
6998

0 commit comments

Comments
 (0)