mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Fixed few resource leaks and added secstoragefirewallcfg command support to simulator
Signed-off-by: Daan Hoogland <daan@onecht.net>
This commit is contained in:
parent
4d6c682f18
commit
b2283d7a28
@ -26,63 +26,50 @@ public class DatabaseAccessObject {
|
|||||||
|
|
||||||
private static Logger s_logger = Logger.getLogger(DatabaseAccessObject.class);
|
private static Logger s_logger = Logger.getLogger(DatabaseAccessObject.class);
|
||||||
|
|
||||||
public void dropKey(Connection conn, String tableName, String key, boolean isForeignKey) {
|
public void dropKey(Connection conn, String tableName, String key, boolean isForeignKey)
|
||||||
PreparedStatement pstmt = null;
|
{
|
||||||
try {
|
String alter_sql_str;
|
||||||
if (isForeignKey) {
|
if (isForeignKey) {
|
||||||
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key);
|
alter_sql_str = "ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key;
|
||||||
} else {
|
} else {
|
||||||
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key);
|
alter_sql_str = "ALTER TABLE " + tableName + " DROP KEY " + key;
|
||||||
}
|
}
|
||||||
|
try(PreparedStatement pstmt = conn.prepareStatement(alter_sql_str);)
|
||||||
|
{
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName);
|
s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
s_logger.warn("Ignored SQL Exception when trying to drop " + (isForeignKey ? "foreign " : "") + "key " + key + " on table " + tableName, e);
|
s_logger.warn("Ignored SQL Exception when trying to drop " + (isForeignKey ? "foreign " : "") + "key " + key + " on table " + tableName, e);
|
||||||
} finally {
|
|
||||||
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping " + (isForeignKey ? "foreign " : "") + "key " + key
|
|
||||||
+ " on table " + tableName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dropPrimaryKey(Connection conn, String tableName) {
|
public void dropPrimaryKey(Connection conn, String tableName) {
|
||||||
PreparedStatement pstmt = null;
|
try(PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");) {
|
||||||
try {
|
|
||||||
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");
|
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
s_logger.debug("Primary key is dropped successfully from the table " + tableName);
|
s_logger.debug("Primary key is dropped successfully from the table " + tableName);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
s_logger.warn("Ignored SQL Exception when trying to drop primary key on table " + tableName, e);
|
s_logger.warn("Ignored SQL Exception when trying to drop primary key on table " + tableName, e);
|
||||||
} finally {
|
|
||||||
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping primary key on table " + tableName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dropColumn(Connection conn, String tableName, String columnName) {
|
public void dropColumn(Connection conn, String tableName, String columnName) {
|
||||||
PreparedStatement pstmt = null;
|
try (PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + columnName);){
|
||||||
try {
|
|
||||||
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + columnName);
|
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
s_logger.debug("Column " + columnName + " is dropped successfully from the table " + tableName);
|
s_logger.debug("Column " + columnName + " is dropped successfully from the table " + tableName);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e);
|
s_logger.warn("Unable to drop column " + columnName + " due to exception", e);
|
||||||
} finally {
|
|
||||||
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement after dropping column " + columnName + " on table " + tableName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean columnExists(Connection conn, String tableName, String columnName) {
|
public boolean columnExists(Connection conn, String tableName, String columnName) {
|
||||||
boolean columnExists = false;
|
boolean columnExists = false;
|
||||||
PreparedStatement pstmt = null;
|
try (PreparedStatement pstmt = conn.prepareStatement("SELECT " + columnName + " FROM " + tableName);){
|
||||||
try {
|
|
||||||
pstmt = conn.prepareStatement("SELECT " + columnName + " FROM " + tableName);
|
|
||||||
pstmt.executeQuery();
|
pstmt.executeQuery();
|
||||||
columnExists = true;
|
columnExists = true;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
s_logger.warn("Field " + columnName + " doesn't exist in " + tableName, e);
|
s_logger.warn("Field " + columnName + " doesn't exist in " + tableName, e);
|
||||||
} finally {
|
|
||||||
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer checking if column " + columnName + " existed on table " + tableName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return columnExists;
|
return columnExists;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -105,8 +105,8 @@ public class EncryptionSecretKeyChanger {
|
|||||||
PropertiesConfiguration backupDBProps = null;
|
PropertiesConfiguration backupDBProps = null;
|
||||||
|
|
||||||
System.out.println("Parsing db.properties file");
|
System.out.println("Parsing db.properties file");
|
||||||
try {
|
try(FileInputStream db_prop_fstream = new FileInputStream(dbPropsFile);) {
|
||||||
dbProps.load(new FileInputStream(dbPropsFile));
|
dbProps.load(db_prop_fstream);
|
||||||
backupDBProps = new PropertiesConfiguration(dbPropsFile);
|
backupDBProps = new PropertiesConfiguration(dbPropsFile);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
System.out.println("db.properties file not found while reading DB secret key" + e.getMessage());
|
System.out.println("db.properties file not found while reading DB secret key" + e.getMessage());
|
||||||
@ -142,11 +142,10 @@ public class EncryptionSecretKeyChanger {
|
|||||||
//db.properties updated successfully
|
//db.properties updated successfully
|
||||||
if (encryptionType.equals("file")) {
|
if (encryptionType.equals("file")) {
|
||||||
//update key file with new MS key
|
//update key file with new MS key
|
||||||
try {
|
try (FileWriter fwriter = new FileWriter(keyFile);
|
||||||
FileWriter fwriter = new FileWriter(keyFile);
|
BufferedWriter bwriter = new BufferedWriter(fwriter);)
|
||||||
BufferedWriter bwriter = new BufferedWriter(fwriter);
|
{
|
||||||
bwriter.write(newMSKey);
|
bwriter.write(newMSKey);
|
||||||
bwriter.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to write new secret to file. Please update the file manually");
|
System.out.println("Failed to write new secret to file. Please update the file manually");
|
||||||
}
|
}
|
||||||
@ -180,11 +179,10 @@ public class EncryptionSecretKeyChanger {
|
|||||||
}
|
}
|
||||||
if (encryptionType.equals("file")) {
|
if (encryptionType.equals("file")) {
|
||||||
//revert secret key in file
|
//revert secret key in file
|
||||||
try {
|
try (FileWriter fwriter = new FileWriter(keyFile);
|
||||||
FileWriter fwriter = new FileWriter(keyFile);
|
BufferedWriter bwriter = new BufferedWriter(fwriter);)
|
||||||
BufferedWriter bwriter = new BufferedWriter(fwriter);
|
{
|
||||||
bwriter.write(oldMSKey);
|
bwriter.write(oldMSKey);
|
||||||
bwriter.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to revert to old secret to file. Please update the file manually");
|
System.out.println("Failed to revert to old secret to file. Please update the file manually");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -131,10 +131,8 @@ public class ScriptRunner {
|
|||||||
} else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) || fullLineDelimiter && trimmedLine.equals(getDelimiter())) {
|
} else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) || fullLineDelimiter && trimmedLine.equals(getDelimiter())) {
|
||||||
command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
|
command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
|
||||||
command.append(" ");
|
command.append(" ");
|
||||||
Statement statement = conn.createStatement();
|
try (Statement statement = conn.createStatement();) {
|
||||||
|
|
||||||
println(command);
|
println(command);
|
||||||
|
|
||||||
boolean hasResults = false;
|
boolean hasResults = false;
|
||||||
if (stopOnError) {
|
if (stopOnError) {
|
||||||
hasResults = statement.execute(command.toString());
|
hasResults = statement.execute(command.toString());
|
||||||
@ -147,12 +145,10 @@ public class ScriptRunner {
|
|||||||
printlnError(e);
|
printlnError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (autoCommit && !conn.getAutoCommit()) {
|
if (autoCommit && !conn.getAutoCommit()) {
|
||||||
conn.commit();
|
conn.commit();
|
||||||
}
|
}
|
||||||
|
try(ResultSet rs = statement.getResultSet();) {
|
||||||
ResultSet rs = statement.getResultSet();
|
|
||||||
if (hasResults && rs != null) {
|
if (hasResults && rs != null) {
|
||||||
ResultSetMetaData md = rs.getMetaData();
|
ResultSetMetaData md = rs.getMetaData();
|
||||||
int cols = md.getColumnCount();
|
int cols = md.getColumnCount();
|
||||||
@ -169,14 +165,10 @@ public class ScriptRunner {
|
|||||||
println("");
|
println("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
command = null;
|
command = null;
|
||||||
try {
|
|
||||||
statement.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Ignore to workaround a bug in Jakarta DBCP
|
|
||||||
}
|
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
int idx = line.indexOf("--");
|
int idx = line.indexOf("--");
|
||||||
if (idx != -1)
|
if (idx != -1)
|
||||||
|
|||||||
@ -111,6 +111,7 @@ import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
|
|||||||
import com.cloud.api.commands.CleanupSimulatorMockCmd;
|
import com.cloud.api.commands.CleanupSimulatorMockCmd;
|
||||||
import com.cloud.api.commands.ConfigureSimulatorCmd;
|
import com.cloud.api.commands.ConfigureSimulatorCmd;
|
||||||
import com.cloud.api.commands.QuerySimulatorMockCmd;
|
import com.cloud.api.commands.QuerySimulatorMockCmd;
|
||||||
|
import com.cloud.agent.api.SecStorageFirewallCfgCommand;
|
||||||
import com.cloud.resource.SimulatorStorageProcessor;
|
import com.cloud.resource.SimulatorStorageProcessor;
|
||||||
import com.cloud.simulator.MockConfigurationVO;
|
import com.cloud.simulator.MockConfigurationVO;
|
||||||
import com.cloud.simulator.MockHost;
|
import com.cloud.simulator.MockHost;
|
||||||
@ -410,9 +411,8 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage
|
|||||||
answer = _mockNetworkMgr.setupPVLAN((PvlanSetupCommand)cmd);
|
answer = _mockNetworkMgr.setupPVLAN((PvlanSetupCommand)cmd);
|
||||||
} else if (cmd instanceof StorageSubSystemCommand) {
|
} else if (cmd instanceof StorageSubSystemCommand) {
|
||||||
answer = this.storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
|
answer = this.storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
|
||||||
} else if (cmd instanceof GetRouterAlertsCommand) {
|
} else if (cmd instanceof GetRouterAlertsCommand || cmd instanceof VpnUsersCfgCommand || cmd instanceof RemoteAccessVpnCfgCommand || cmd instanceof SetMonitorServiceCommand || cmd instanceof AggregationControlCommand ||
|
||||||
answer = new Answer(cmd);
|
cmd instanceof SecStorageFirewallCfgCommand) {
|
||||||
} else if (cmd instanceof VpnUsersCfgCommand || cmd instanceof RemoteAccessVpnCfgCommand || cmd instanceof SetMonitorServiceCommand || cmd instanceof AggregationControlCommand) {
|
|
||||||
answer = new Answer(cmd);
|
answer = new Answer(cmd);
|
||||||
} else {
|
} else {
|
||||||
s_logger.error("Simulator does not implement command of type " + cmd.toString());
|
s_logger.error("Simulator does not implement command of type " + cmd.toString());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user