IDEMPIERE-472 Allow postgresql connection via SSL / Peer review

This commit is contained in:
Carlos Ruiz 2012-10-26 13:46:55 -05:00
parent e99ca7f076
commit e2c0220862
9 changed files with 79 additions and 59 deletions

View File

@ -51,7 +51,7 @@ public class BaseActivator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
//Load SSL
loadSSLDBProperties(Adempiere.getAdempiereHome());
loadInitProperties(Adempiere.getAdempiereHome());
bundleContext = context;
context.registerService(CommandProvider.class.getName(), new StackTraceCommand(), null);
}
@ -59,19 +59,19 @@ public class BaseActivator implements BundleActivator {
/*
* Load idempiereInit.properties into the system.
*/
private void loadSSLDBProperties(String filePath) {
private void loadInitProperties(String filePath) {
Properties s_prop = new Properties();
Properties props = new Properties();
String fileName = filePath+ File.separator+"idempiereInit.properties";
File env = new File (fileName);
boolean loadOk = true;
if (env.exists())
if (env.exists() && env.isFile() && env.canRead())
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(env);
s_prop.load(fis);
props.load(fis);
}catch (Exception e){
loadOk = false;
System.err.println("Exception while loading idempiereInit.properties: "+ e.toString());
@ -85,10 +85,10 @@ public class BaseActivator implements BundleActivator {
if (loadOk) {
String key = null;
String value =null;
Enumeration<?> enumeration = s_prop.propertyNames();
Enumeration<?> enumeration = props.propertyNames();
while (enumeration.hasMoreElements()) {
key = (String) enumeration.nextElement();
value = s_prop.getProperty(key);
value = props.getProperty(key);
System.setProperty(key,value) ;
//System.out.println("Loaded Key: "+ key + "- Value :"+value);
}

View File

@ -1392,13 +1392,13 @@ public class ConfigurationData
/**
* @param ADEMPIERE_DB_EXISTS
*/
public void setDatabaseExists(String pass)
public void setDatabaseExists(String dbExists)
{
if (p_panel != null)
p_panel.okdbExists.setSelected("Yes".equalsIgnoreCase(pass));
p_panel.okdbExists.setSelected("Y".equalsIgnoreCase(dbExists));
else
updateProperty(ADEMPIERE_DB_EXISTS, pass);
updateProperty(ADEMPIERE_DB_EXISTS, dbExists);
}
/**
@ -1406,9 +1406,14 @@ public class ConfigurationData
*/
public boolean getDatabaseExists()
{
Object dbExists = p_properties.get(ADEMPIERE_DB_EXISTS);
if (dbExists == null)
dbExists = "N";
else
dbExists = dbExists.toString();
return p_panel != null
? p_panel.okdbExists.isSelected()
: "Yes".equalsIgnoreCase(p_properties.get(ADEMPIERE_DB_EXISTS).toString());
: "Y".equalsIgnoreCase( (String) dbExists);
}
/**

View File

@ -130,7 +130,7 @@ public class ConfigurationPanel extends JPanel implements ActionListener, IDBCon
private JLabel lDatabaseType = new JLabel();
JComboBox fDatabaseType = new JComboBox(ConfigurationData.DBTYPE);
//
JLabel lDatabaseServer = new JLabel("Base de Datos Existe");
JLabel lDatabaseServer = new JLabel();
JLabel ldbExists = new JLabel();
JTextField fDatabaseServer = new JTextField(FIELDLENGTH);
private JLabel lDatabaseName = new JLabel();
@ -526,7 +526,7 @@ public class ConfigurationPanel extends JPanel implements ActionListener, IDBCon
m_success = false;
m_errorString = null;
try
{
{
test();
}
catch (Exception ex)

View File

@ -60,6 +60,7 @@ public class SetupRes extends ListResourceBundle
{ "MailServer", "Mail Server" },
{ "AdminEMailInfo", "iDempiere Administrator EMail" },
{ "AdminEMail", "Admin EMail" },
{ "DbExists", "DB Already Exists" },
{ "DatabaseServerInfo", "Database Server Name" },
{ "DatabaseServer", "Database Server" },
{ "JavaHomeInfo", "Java Home Folder" },
@ -72,7 +73,6 @@ public class SetupRes extends ListResourceBundle
{ "MailPassword", "Mail Password" },
{ "KeyStorePassword", "KeyStore Password" },
{ "KeyStorePasswordInfo", "Password for SSL Key Store" },
{ "DbExists" , "DB Already Exists" },
//
{ "JavaType", "Java VM"},
{ "JavaTypeInfo", "Java VM Vendor"},

View File

@ -61,6 +61,7 @@ public class SetupRes_es extends ListResourceBundle
{ "MailServer", "Servidor Correo" },
{ "AdminEMailInfo", "Email Administrador iDempiere" },
{ "AdminEMail", "Email Admin" },
{ "DbExists", "Base de Datos Existe" }, // ?? OJO!!!
{ "DatabaseServerInfo", "Nombre Servidor Base de Datos" },
{ "DatabaseServer", "Servidor Base de Datos" },
{ "JavaHomeInfo", "Carpeta Java Home" },
@ -73,7 +74,6 @@ public class SetupRes_es extends ListResourceBundle
{ "MailPassword", "Contrase\u00f1a Correo" },
{ "KeyStorePassword", "Contrase\u00f1a Key Store" },
{ "KeyStorePasswordInfo", "Contrase\u00f1a para SSL Key Store" },
{ "DbExists" , "Base de Datos Existe" },
//
{ "JavaType", "Java VM"},
{ "JavaTypeInfo", "Proveedor Java VM"},

View File

@ -425,16 +425,19 @@ public class ConfigurationConsole {
}
private void dbExists(BufferedReader reader, PrintWriter writer) throws IOException {
String dbExists = data.getDatabaseExists() ? "Y" : "N";
writer.println("DB Already Exists?(Y/N) [N]: ");
writer.println("DB Already Exists?(Y/N) [" + dbExists + "]: ");
String yesNo = reader.readLine();
if ((yesNo == null || yesNo.trim().length() == 0) || "n".equalsIgnoreCase(yesNo))
if (yesNo == null || yesNo.trim().length() == 0)
yesNo = dbExists;
if ("n".equalsIgnoreCase(yesNo))
{
data.setDatabaseExists("No");
data.setDatabaseExists("N");
}
else
{
data.setDatabaseExists("Yes");
data.setDatabaseExists("Y");
}
}

View File

@ -324,7 +324,7 @@ public class ConfigOracle implements IDatabaseConfig
return error;
log.info("OK: Database Port = " + databasePort);
data.setProperty(ConfigurationData.ADEMPIERE_DB_PORT, String.valueOf(databasePort));
boolean isDBExists = data.getDatabaseExists();
// JDBC Database Info
String databaseName = data.getDatabaseName(); // Service Name
@ -334,8 +334,13 @@ public class ConfigOracle implements IDatabaseConfig
if (monitor != null)
monitor.update(new DBConfigStatus(DBConfigStatus.DATABASE_SYSTEM_PASSWORD, "ErrorJDBC",
pass, true, error));
if (!pass && !isDBExists)
return error;
if (!pass) {
if (isDBExists) {
log.warning(error);
} else {
return error;
}
}
//
// URL (derived) jdbc:oracle:thin:@//prod1:1521/prod1
String url = "jdbc:oracle:thin:@//" + databaseServer.getHostName()
@ -372,20 +377,23 @@ public class ConfigOracle implements IDatabaseConfig
if (monitor != null)
monitor.update(new DBConfigStatus(DBConfigStatus.DATABASE_USER, "ErrorJDBC",
pass, true, error));
if (pass){
if (pass)
{
log.info("OK: Database User = " + databaseUser);
if (m_con != null)
data.setProperty(ConfigurationData.ADEMPIERE_WEBSTORES, data.getWebStores(m_con));
}else{
if (isDBExists){
}
else
{
if (isDBExists) {
return error;
}else {
} else {
log.warning(error);
}
}
data.setProperty(ConfigurationData.ADEMPIERE_DB_USER, databaseUser);
data.setProperty(ConfigurationData.ADEMPIERE_DB_PASSWORD, databasePassword);
data.setProperty(ConfigurationData.ADEMPIERE_DB_EXISTS,(isDBExists==true ?"Yes":"No"));
data.setProperty(ConfigurationData.ADEMPIERE_DB_EXISTS, (isDBExists ? "Y" : "N"));
String ospath;
if (System.getProperty("os.name").startsWith("Windows"))
ospath = "windows";

View File

@ -122,6 +122,7 @@ public class ConfigPostgreSQL implements IDatabaseConfig
// JDBC Database Info
String databaseName = data.getDatabaseName(); // Service Name
String systemPassword = data.getDatabaseSystemPassword();
// URL (derived)
String urlSystem = p_db.getConnectionURL(databaseServer.getHostName(), databasePort,
p_db.getSystemDatabase(databaseName), p_db.getSystemUser());
@ -131,9 +132,13 @@ public class ConfigPostgreSQL implements IDatabaseConfig
if (monitor != null)
monitor.update(new DBConfigStatus(DBConfigStatus.DATABASE_SYSTEM_PASSWORD, "ErrorJDBC",
pass, true, error));
if (!pass && !isDBExists)
return error;
if (!pass) {
if (isDBExists) {
log.warning(error);
} else {
return error;
}
}
log.info("OK: System Connection = " + urlSystem);
data.setProperty(ConfigurationData.ADEMPIERE_DB_SYSTEM, systemPassword);
@ -141,13 +146,6 @@ public class ConfigPostgreSQL implements IDatabaseConfig
// Database User Info
String databaseUser = data.getDatabaseUser(); // UID
String databasePassword = data.getDatabasePassword(); // PWD
pass = databasePassword != null && databasePassword.length() > 0;
error = "Invalid Database User Password";
if (monitor != null)
monitor.update(new DBConfigStatus(DBConfigStatus.DATABASE_USER, "ErrorJDBC",
pass, true, error));
if (!pass)
return error;
//
String url= p_db.getConnectionURL(databaseServer.getHostName(), databasePort,
databaseName, databaseUser);
@ -157,20 +155,24 @@ public class ConfigPostgreSQL implements IDatabaseConfig
if (monitor != null)
monitor.update(new DBConfigStatus(DBConfigStatus.DATABASE_USER, "ErrorJDBC",
pass, true, error));
if (pass){
if (pass)
{
log.info("OK: Database User = " + databaseUser);
}else{
if (isDBExists){
}
else
{
if (isDBExists) {
return error;
}else {
} else {
log.warning(error);
}
}
data.setProperty(ConfigurationData.ADEMPIERE_DB_URL, url);
data.setProperty(ConfigurationData.ADEMPIERE_DB_URL, url);
data.setProperty(ConfigurationData.ADEMPIERE_DB_NAME, databaseName);
data.setProperty(ConfigurationData.ADEMPIERE_DB_USER, databaseUser);
data.setProperty(ConfigurationData.ADEMPIERE_DB_PASSWORD, databasePassword);
data.setProperty(ConfigurationData.ADEMPIERE_DB_EXISTS,(isDBExists==true ?"Yes":"No"));
data.setProperty(ConfigurationData.ADEMPIERE_DB_EXISTS, (isDBExists ? "Y" : "N"));
return null;
} // test

View File

@ -47,6 +47,7 @@ import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Ini;
import org.compiere.util.Trx;
import org.compiere.util.Util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@ -175,17 +176,18 @@ public class DB_PostgreSQL implements AdempiereDatabase
public String getConnectionURL (CConnection connection)
{
// jdbc:postgresql://hostname:portnumber/databasename?encoding=UNICODE
String urlParameters = System.getProperty("org.idempiere.postgresql.URLParameters") ;
StringBuffer sb = new StringBuffer("jdbc:postgresql:");
sb.append("//").append(connection.getDbHost())
StringBuilder sb = new StringBuilder("jdbc:postgresql://")
.append(connection.getDbHost())
.append(":").append(connection.getDbPort())
.append("/").append(connection.getDbName())
.append("?encoding=UNICODE");
if (urlParameters != null)
sb.append(urlParameters);
m_connection = sb.toString();
String urlParameters = System.getProperty("org.idempiere.postgresql.URLParameters");
if (!Util.isEmpty(urlParameters)) {
sb.append("&").append(urlParameters);
}
m_connection = sb.toString();
return m_connection;
} // getConnectionString
@ -200,16 +202,16 @@ public class DB_PostgreSQL implements AdempiereDatabase
public String getConnectionURL (String dbHost, int dbPort, String dbName,
String userName)
{
//String ULR = "jdbc:postgresql://"+ dbHost + ":" + dbPort + "/" + dbName;
StringBuilder sb = new StringBuilder("jdbc:postgresql://")
.append(dbHost)
.append(":").append(dbPort)
.append("/").append(dbName);
String urlParameters = System.getProperty("org.idempiere.postgresql.URLParameters") ;
StringBuffer sb = new StringBuffer("jdbc:postgresql:");
sb.append("//").append(dbHost)
.append(":").append(dbPort)
.append("/").append(dbName);
if (urlParameters != null)
if (!Util.isEmpty(urlParameters)) {
sb.append("?").append(urlParameters);
}
return sb.toString();
} // getConnectionURL