Heng Sin Low 2009-06-01 07:32:30 +00:00
parent a3d3c92dc7
commit aaf1ac619c
2 changed files with 116 additions and 58 deletions

View File

@ -0,0 +1,36 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.util;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.compiere.session.ServerBean;
/**
*
* @author hengsin
*
*/
public class EmbeddedServerProxy implements InvocationHandler {
private ServerBean server = new ServerBean();
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Method m = ServerBean.class.getMethod(method.getName(), method.getParameterTypes());
return m.invoke(server, args);
}
}

View File

@ -17,6 +17,7 @@
package org.compiere.db; package org.compiere.db;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Proxy;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.SQLException; import java.sql.SQLException;
@ -31,6 +32,7 @@ import javax.sql.DataSource;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import org.adempiere.as.ASFactory; import org.adempiere.as.ASFactory;
import org.adempiere.util.EmbeddedServerProxy;
import org.compiere.Adempiere; import org.compiere.Adempiere;
import org.compiere.interfaces.Server; import org.compiere.interfaces.Server;
import org.compiere.interfaces.Status; import org.compiere.interfaces.Status;
@ -50,14 +52,14 @@ import org.compiere.util.ValueNamePair;
public class CConnection implements Serializable, Cloneable public class CConnection implements Serializable, Cloneable
{ {
/** /**
* *
*/ */
private static final long serialVersionUID = -3653060934380586381L; private static final long serialVersionUID = -3653060934380586381L;
/** Connection */ /** Connection */
private static CConnection s_cc = null; private static CConnection s_cc = null;
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger (CConnection.class); private static CLogger log = CLogger.getCLogger (CConnection.class);
/** Connection profiles */ /** Connection profiles */
@Deprecated @Deprecated
public static ValueNamePair[] CONNECTIONProfiles = new ValueNamePair[]{ public static ValueNamePair[] CONNECTIONProfiles = new ValueNamePair[]{
@ -66,9 +68,9 @@ public class CConnection implements Serializable, Cloneable
/** Connection Profile LAN */ /** Connection Profile LAN */
@Deprecated @Deprecated
public static final String PROFILE_LAN = "L"; public static final String PROFILE_LAN = "L";
/** /**
* Connection Profile Terminal Server * Connection Profile Terminal Server
* @deprecated * @deprecated
**/ **/
public static final String PROFILE_TERMINAL = "T"; public static final String PROFILE_TERMINAL = "T";
/** Connection Profile VPM */ /** Connection Profile VPM */
@ -77,14 +79,17 @@ public class CConnection implements Serializable, Cloneable
/** Connection Profile WAN */ /** Connection Profile WAN */
@Deprecated @Deprecated
public static final String PROFILE_WAN = "W"; public static final String PROFILE_WAN = "W";
private final static String COMPONENT_NS = "java:comp/env"; private final static String COMPONENT_NS = "java:comp/env";
/** Prefer component namespace when running at server **/ /** Prefer component namespace when running at server **/
private boolean useComponentNamespace = !Ini.isClient(); private boolean useComponentNamespace = !Ini.isClient();
/** System property flag to embed server bean in process **/
public final static String SERVER_EMBEDDED = "org.adempiere.server.embedded";
/** /**
* Get/Set default client/server Connection * Get/Set default client/server Connection
* @return Connection Descriptor * @return Connection Descriptor
*/ */
public static CConnection get () public static CConnection get ()
@ -93,7 +98,7 @@ public class CConnection implements Serializable, Cloneable
} // get } // get
/** /**
* Get/Set default client/server Connection * Get/Set default client/server Connection
* @param apps_host optional apps host for new connections * @param apps_host optional apps host for new connections
* @return Connection Descriptor * @return Connection Descriptor
*/ */
@ -183,7 +188,7 @@ public class CConnection implements Serializable, Cloneable
return cc; return cc;
} // get } // get
/************************************************************************** /**************************************************************************
* Adempiere Connection * Adempiere Connection
@ -205,7 +210,7 @@ public class CConnection implements Serializable, Cloneable
private String m_apps_host = "MyAppsServer"; private String m_apps_host = "MyAppsServer";
/** Application Port */ /** Application Port */
private int m_apps_port = ASFactory.getApplicationServer().getDefaultNamingServicePort(); private int m_apps_port = ASFactory.getApplicationServer().getDefaultNamingServicePort();
/** Database Type */ /** Database Type */
private String m_type = ""; private String m_type = "";
@ -257,12 +262,12 @@ public class CConnection implements Serializable, Cloneable
private Server m_server = null; private Server m_server = null;
/** DB Info */ /** DB Info */
private String m_dbInfo = null; private String m_dbInfo = null;
/** Had application server been query **/ /** Had application server been query **/
private boolean m_queryAppsServer = false; private boolean m_queryAppsServer = false;
private final static String SECURITY_PRINCIPAL = "org.adempiere.security.principal"; private final static String SECURITY_PRINCIPAL = "org.adempiere.security.principal";
/************************************************************************* /*************************************************************************
* Get Name * Get Name
* @return connection name * @return connection name
@ -289,7 +294,7 @@ public class CConnection implements Serializable, Cloneable
m_name = toString (); m_name = toString ();
} // setName } // setName
/************* /*************
* Get Application Host * Get Application Host
* @return apps host * @return apps host
@ -357,6 +362,9 @@ public class CConnection implements Serializable, Cloneable
*/ */
public boolean isAppsServerOK (boolean tryContactAgain) public boolean isAppsServerOK (boolean tryContactAgain)
{ {
if (isServerEmbedded())
return true;
if (Ini.isClient() && !tryContactAgain && m_queryAppsServer) if (Ini.isClient() && !tryContactAgain && m_queryAppsServer)
return m_okApps; return m_okApps;
@ -365,9 +373,9 @@ public class CConnection implements Serializable, Cloneable
log.warning (getAppsHost() + " ignored"); log.warning (getAppsHost() + " ignored");
return false; return false;
} }
m_queryAppsServer = true; m_queryAppsServer = true;
// Contact it // Contact it
try try
{ {
@ -419,6 +427,13 @@ public class CConnection implements Serializable, Cloneable
//only cache ServerHome for client //only cache ServerHome for client
if (m_server == null || !Ini.isClient()) if (m_server == null || !Ini.isClient())
{ {
if (isServerEmbedded())
{
m_server = (Server)Proxy.newProxyInstance(Server.class.getClassLoader(),
new Class[]{Server.class}, new EmbeddedServerProxy());
return m_server;
}
try try
{ {
Server server = (Server)lookup (Server.JNDI_NAME); Server server = (Server)lookup (Server.JNDI_NAME);
@ -448,7 +463,7 @@ public class CConnection implements Serializable, Cloneable
return m_version; return m_version;
} // getServerVersion } // getServerVersion
/************* /*************
* Get Database Host name * Get Database Host name
* @return db host name * @return db host name
@ -568,7 +583,7 @@ public class CConnection implements Serializable, Cloneable
/** /**
* RMI over HTTP * RMI over HTTP
* *
* Deprecated, always return false * Deprecated, always return false
* @return true if RMI over HTTP (Wan Connection Profile) * @return true if RMI over HTTP (Wan Connection Profile)
* @deprecated * @deprecated
@ -843,7 +858,7 @@ public class CConnection implements Serializable, Cloneable
setBequeath (false); setBequeath (false);
setViaFirewall (false); setViaFirewall (false);
} }
// begin vpj-cd e-evolution 09 ene 2006 // begin vpj-cd e-evolution 09 ene 2006
// PostgreSQL // PostgreSQL
if (isPostgreSQL ()) if (isPostgreSQL ())
@ -938,7 +953,7 @@ public class CConnection implements Serializable, Cloneable
return m_ds != null; return m_ds != null;
} // isDataSource } // isDataSource
/************************************************************************** /**************************************************************************
* Test Database Connection. * Test Database Connection.
* -- Example -- * -- Example --
@ -954,7 +969,7 @@ public class CConnection implements Serializable, Cloneable
{ {
if (!retest && m_ds != null && m_okDB) if (!retest && m_ds != null && m_okDB)
return null; return null;
getDatabase().close(); getDatabase().close();
m_ds = null; m_ds = null;
setDataSource(); setDataSource();
@ -963,7 +978,7 @@ public class CConnection implements Serializable, Cloneable
Connection.TRANSACTION_READ_COMMITTED); Connection.TRANSACTION_READ_COMMITTED);
if (conn != null) if (conn != null)
{ {
try try
{ {
readInfo(conn); readInfo(conn);
conn.close (); conn.close ();
@ -987,9 +1002,9 @@ public class CConnection implements Serializable, Cloneable
if (isDataSource()) if (isDataSource())
m_info[1] += " - via DataSource"; m_info[1] += " - via DataSource";
m_info[1] = m_info[1].replace ('\n', ' '); m_info[1] = m_info[1].replace ('\n', ' ');
log.config(m_info[0] + " - " + m_info[1]); log.config(m_info[0] + " - " + m_info[1]);
} }
/************************************************************************* /*************************************************************************
* Short String representation * Short String representation
* @return appsHost{dbHost-dbName-uid} * @return appsHost{dbHost-dbName-uid}
@ -1069,10 +1084,10 @@ public class CConnection implements Serializable, Cloneable
} }
} }
conn = null; conn = null;
return sb.toString(); return sb.toString();
} // toStringDetail } // toStringDetail
/** /**
* String representation. * String representation.
* Used also for Instanciation * Used also for Instanciation
@ -1171,7 +1186,7 @@ public class CConnection implements Serializable, Cloneable
return sb.toString (); return sb.toString ();
} // getInfo } // getInfo
/************************************************************************* /*************************************************************************
* Hashcode * Hashcode
* @return hashcode of name * @return hashcode of name
@ -1195,14 +1210,14 @@ public class CConnection implements Serializable, Cloneable
{ {
try try
{ {
for (int i = 0; i < Database.DB_NAMES.length; i++) for (int i = 0; i < Database.DB_NAMES.length; i++)
{ {
if (Database.DB_NAMES[i].equals (m_type)) if (Database.DB_NAMES[i].equals (m_type))
{ {
m_db = (AdempiereDatabase)Database.DB_CLASSES[i]. m_db = (AdempiereDatabase)Database.DB_CLASSES[i].
newInstance (); newInstance ();
break; break;
} }
} }
if (m_db != null) // test class loader ability if (m_db != null) // test class loader ability
m_db.getDataSource(this); m_db.getDataSource(this);
@ -1212,10 +1227,10 @@ public class CConnection implements Serializable, Cloneable
System.err.println("Environment Error - Check Adempiere.properties - " + ee); System.err.println("Environment Error - Check Adempiere.properties - " + ee);
if (Ini.isClient()) if (Ini.isClient())
{ {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog
(null, "There is a configuration error:\n" + ee (null, "There is a configuration error:\n" + ee
+ "\nDo you want to reset the saved configuration?", + "\nDo you want to reset the saved configuration?",
"Adempiere Configuration Error", "Adempiere Configuration Error",
JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE)) JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE))
Ini.deletePropertyFile(); Ini.deletePropertyFile();
} }
@ -1296,7 +1311,7 @@ public class CConnection implements Serializable, Cloneable
try try
{ {
// if (!Ini.isClient() // Server // if (!Ini.isClient() // Server
// && trxLevel != Connection.TRANSACTION_READ_COMMITTED) // PO_LOB.save() // && trxLevel != Connection.TRANSACTION_READ_COMMITTED) // PO_LOB.save()
// { // {
Exception ee = null; Exception ee = null;
@ -1320,7 +1335,7 @@ public class CConnection implements Serializable, Cloneable
} }
catch (UnsatisfiedLinkError ule) catch (UnsatisfiedLinkError ule)
{ {
String msg = ule.getLocalizedMessage() String msg = ule.getLocalizedMessage()
+ " -> Did you set the LD_LIBRARY_PATH ? - " + getConnectionURL(); + " -> Did you set the LD_LIBRARY_PATH ? - " + getConnectionURL();
m_dbException = new Exception(msg); m_dbException = new Exception(msg);
log.severe(msg); log.severe(msg);
@ -1345,7 +1360,7 @@ public class CConnection implements Serializable, Cloneable
try try
{ {
log.severe(getConnectionURL () log.severe(getConnectionURL ()
+ ", (2) AutoCommit=" + conn.getAutoCommit() + "->" + autoCommit + ", (2) AutoCommit=" + conn.getAutoCommit() + "->" + autoCommit
+ ", TrxIso=" + getTransactionIsolationInfo(conn.getTransactionIsolation()) + "->" + getTransactionIsolationInfo(transactionIsolation) + ", TrxIso=" + getTransactionIsolationInfo(conn.getTransactionIsolation()) + "->" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")" // + " (" + getDbUid() + "/" + getDbPwd() + ")"
+ " - " + ex.getMessage()); + " - " + ex.getMessage());
@ -1366,7 +1381,7 @@ public class CConnection implements Serializable, Cloneable
//log.log(Level.SEVERE, getConnectionURL(), ex); //log.log(Level.SEVERE, getConnectionURL(), ex);
System.err.println(getConnectionURL() + " - " + ex.getLocalizedMessage()); System.err.println(getConnectionURL() + " - " + ex.getLocalizedMessage());
} }
// System.err.println ("CConnection.getConnection - " + conn); // System.err.println ("CConnection.getConnection - " + conn);
return conn; return conn;
} // getConnection } // getConnection
@ -1399,8 +1414,8 @@ public class CConnection implements Serializable, Cloneable
{ {
SecurityPrincipal sp = (SecurityPrincipal) Env.getCtx().get(SECURITY_PRINCIPAL); SecurityPrincipal sp = (SecurityPrincipal) Env.getCtx().get(SECURITY_PRINCIPAL);
String principal = sp != null ? sp.principal : null; String principal = sp != null ? sp.principal : null;
String credential = sp != null ? sp.credential : null; String credential = sp != null ? sp.credential : null;
m_env = getInitialEnvironment(getAppsHost(), getAppsPort(), false, m_env = getInitialEnvironment(getAppsHost(), getAppsPort(), false,
principal, credential); principal, credential);
} }
String connect = (String)m_env.get(Context.PROVIDER_URL); String connect = (String)m_env.get(Context.PROVIDER_URL);
@ -1455,13 +1470,13 @@ public class CConnection implements Serializable, Cloneable
m_okApps = false; m_okApps = false;
m_queryAppsServer = true; m_queryAppsServer = true;
m_appsException = null; m_appsException = null;
// Carlos Ruiz - globalqss - speed up when jnp://MyAppsServer:1099 is set // Carlos Ruiz - globalqss - speed up when jnp://MyAppsServer:1099 is set
if (getAppsHost().equalsIgnoreCase("MyAppsServer")) { if (getAppsHost().equalsIgnoreCase("MyAppsServer")) {
log.warning (getAppsHost() + " ignored"); log.warning (getAppsHost() + " ignored");
return m_okApps; // false return m_okApps; // false
} }
try try
{ {
Status status = (Status)lookup (Status.JNDI_NAME); Status status = (Status)lookup (Status.JNDI_NAME);
@ -1564,7 +1579,7 @@ public class CConnection implements Serializable, Cloneable
sb.append (m_db.getStatus()); sb.append (m_db.getStatus());
return sb.toString (); return sb.toString ();
} // getStatus } // getStatus
/** /**
* Get Transaction Isolation Info * Get Transaction Isolation Info
* @param transactionIsolation trx iso * @param transactionIsolation trx iso
@ -1584,7 +1599,14 @@ public class CConnection implements Serializable, Cloneable
return "SERIALIZABLE"; return "SERIALIZABLE";
return "<?" + transactionIsolation + "?>"; return "<?" + transactionIsolation + "?>";
} // getTransactionIsolationInfo } // getTransactionIsolationInfo
/**
* @return true if server is embedded in process
*/
public static boolean isServerEmbedded() {
return "true".equalsIgnoreCase(System.getProperty(SERVER_EMBEDDED));
}
public void setAppServerCredential(String principal, String credential) public void setAppServerCredential(String principal, String credential)
{ {
SecurityPrincipal sp = new SecurityPrincipal(); SecurityPrincipal sp = new SecurityPrincipal();
@ -1605,28 +1627,28 @@ public class CConnection implements Serializable, Cloneable
c.m_info = info; c.m_info = info;
return c; return c;
} }
private Object lookup(String jndiName) throws NamingException { private Object lookup(String jndiName) throws NamingException {
InitialContext ctx = getInitialContext(Ini.isClient()); InitialContext ctx = getInitialContext(Ini.isClient());
if (useComponentNamespace) if (useComponentNamespace)
{ {
try try
{ {
return ctx.lookup(COMPONENT_NS + "/" + jndiName); return ctx.lookup(COMPONENT_NS + "/" + jndiName);
} }
catch (Exception e) catch (Exception e)
{ {
log.warning("Component name space not available - " + e.getLocalizedMessage()); log.warning("Component name space not available - " + e.getLocalizedMessage());
//not available //not available
useComponentNamespace = false; useComponentNamespace = false;
} }
} }
//global jndi lookup //global jndi lookup
return ctx.lookup(jndiName); return ctx.lookup(jndiName);
} }
/************************************************************************** /**************************************************************************
* Testing * Testing
* @param args ignored * @param args ignored
@ -1655,5 +1677,5 @@ public class CConnection implements Serializable, Cloneable
Connection con = cc.getConnection (false, Connection con = cc.getConnection (false,
Connection.TRANSACTION_READ_COMMITTED); Connection.TRANSACTION_READ_COMMITTED);
new CConnectionDialog(cc); new CConnectionDialog(cc);
} // main } // main
} // CConnection } // CConnection