📄 cconnection.java
字号:
setName (attributes.substring (attributes.indexOf ("name=") + 5, attributes.indexOf (",AppsHost=")));
setAppsHost (attributes.substring (attributes.indexOf ("AppsHost=") + 9, attributes.indexOf (",AppsPort=")));
int index = attributes.indexOf("AppsPort=");
setAppsPort (attributes.substring (index + 9, attributes.indexOf (",", index)));
index = attributes.indexOf("Profile=");
if (index > 0) // new attribute, may not exist
setConnectionProfile(attributes.substring(index+8, attributes.indexOf (",", index)));
//
setType (attributes.substring (attributes.indexOf ("type=")+5, attributes.indexOf (",DBhost=")));
setDbHost (attributes.substring (attributes.indexOf ("DBhost=") + 7, attributes.indexOf (",DBport=")));
setDbPort (attributes.substring (attributes.indexOf ("DBport=") + 7, attributes.indexOf (",DBname=")));
setDbName (attributes.substring (attributes.indexOf ("DBname=") + 7, attributes.indexOf (",BQ=")));
//
setBequeath (attributes.substring (attributes.indexOf ("BQ=") + 3, attributes.indexOf (",FW=")));
setViaFirewall (attributes.substring (attributes.indexOf ("FW=") + 3, attributes.indexOf (",FWhost=")));
setFwHost (attributes.substring (attributes.indexOf ("FWhost=") + 7, attributes.indexOf (",FWport=")));
setFwPort (attributes.substring (attributes.indexOf ("FWport=") + 7, attributes.indexOf (",UID=")));
//
setDbUid (attributes.substring (attributes.indexOf ("UID=") + 4, attributes.indexOf (",PWD=")));
setDbPwd (attributes.substring (attributes.indexOf ("PWD=") + 4, attributes.indexOf ("]")));
//
}
catch (Exception e)
{
log.severe(attributes + " - " + e.toString ());
}
} // setAttributes
/**
* Equals
* @param o object
* @return true if o equals this
*/
public boolean equals (Object o)
{
if (o instanceof CConnection)
{
CConnection cc = (CConnection)o;
if (cc.getAppsHost().equals (m_apps_host)
&& cc.getAppsPort() == m_apps_port
&& cc.getDbHost().equals (m_db_host)
&& cc.getDbPort() == m_db_port
&& cc.getConnectionProfile().equals(getConnectionProfile())
&& cc.getDbName().equals(m_db_name)
&& cc.getType().equals(m_type)
&& cc.getDbUid().equals(m_db_uid)
&& cc.getDbPwd().equals(m_db_pwd))
return true;
}
return false;
} // equals
/**
* Get Info.
* - Database, Driver, Status Info
* @return info
*/
public String getInfo ()
{
StringBuffer sb = new StringBuffer (m_info[0]);
sb.append (" - ").append (m_info[1])
.append ("\n").append (getDatabase ().toString ())
.append ("\nAppsServerOK=").append (isAppsServerOK (false))
.append (", DatabaseOK=").append (isDatabaseOK ());
return sb.toString ();
} // getInfo
/*************************************************************************
* Hashcode
* @return hashcode of name
*/
public int hashCode ()
{
return m_name.hashCode ();
} // hashCode
/**
* Get Database
* @return database
*/
public CompiereDatabase getDatabase ()
{
// different driver
if (m_db != null && !m_db.getName ().equals (m_type))
m_db = null;
if (m_db == null)
{
try
{
for (int i = 0; i < Database.DB_NAMES.length; i++)
{
if (Database.DB_NAMES[i].equals (m_type))
{
m_db = (CompiereDatabase)Database.DB_CLASSES[i].
newInstance ();
break;
}
}
}
catch (Exception e)
{
log.severe(e.toString ());
}
}
return m_db;
} // getDatabase
/**
* Get Connection String
* @return connection string
*/
public String getConnectionURL ()
{
getDatabase (); // updates m_db
if (m_db != null)
return m_db.getConnectionURL (this);
else
return "";
} // getConnectionURL
/**
* Get Server Connection - do close
* @param autoCommit true if autocommit connection
* @param trxLevel Connection transaction level
* @return Connection
*/
public Connection getServerConnection (boolean autoCommit, int trxLevel)
{
Connection conn = null;
// Server Connection
if (m_ds != null)
{
try
{
conn = m_ds.getConnection ();
conn.setAutoCommit (autoCommit);
conn.setTransactionIsolation (trxLevel);
m_okDB = true;
}
catch (SQLException ex)
{
m_dbException = ex;
log.log(Level.SEVERE, "", ex);
}
}
// Server
return conn;
} // getServerConnection
/**
* Create Connection - no not close.
* Sets m_dbException
* @param autoCommit true if autocommit connection
* @param transactionIsolation Connection transaction level
* @return Connection
*/
public Connection getConnection (boolean autoCommit, int transactionIsolation)
{
Connection conn = null;
m_dbException = null;
m_okDB = false;
//
getDatabase (); // updates m_db
if (m_db == null)
{
m_dbException = new IllegalStateException("No Database Connector");
return null;
}
//
try
{
// if (!Ini.isClient() // Server
// && trxLevel != Connection.TRANSACTION_READ_COMMITTED) // PO_LOB.save()
// {
Exception ee = null;
try
{
conn = m_db.getCachedConnection(this, autoCommit, transactionIsolation);
}
catch (Exception e)
{
ee = e;
}
if (conn == null)
{
Thread.yield();
log.config("retrying - " + ee);
conn = m_db.getCachedConnection(this, autoCommit, transactionIsolation);
}
// System.err.println ("CConnection.getConnection(Cache) - " + getConnectionURL() + ", AutoCommit=" + autoCommit + ", TrxLevel=" + trxLevel);
// }
// else if (isDataSource()) // Client
// {
// conn = m_ds.getConnection();
// System.err.println ("CConnection.getConnection(DataSource) - " + getConnectionURL() + ", AutoCommit=" + autoCommit + ", TrxLevel=" + trxLevel);
// }
// else
// {
// conn = m_db.getDriverConnection (this);
// System.err.println ("CConnection.getConnection(Driver) - " + getConnectionURL() + ", AutoCommit=" + autoCommit + ", TrxLevel=" + trxLevel);
// }
// Verify Connection
if (conn != null)
{
if (conn.getTransactionIsolation() != transactionIsolation)
conn.setTransactionIsolation (transactionIsolation);
if (conn.getAutoCommit() != autoCommit)
conn.setAutoCommit (autoCommit);
m_okDB = true;
}
}
catch (UnsatisfiedLinkError ule)
{
String msg = ule.getLocalizedMessage()
+ " -> Did you set the LD_LIBRARY_PATH ? - " + getConnectionURL();
m_dbException = new Exception(msg);
log.severe(msg);
}
catch (SQLException ex)
{
m_dbException = ex;
if (conn == null)
log.log(Level.SEVERE, getConnectionURL ()
+ ", (1) AutoCommit=" + autoCommit + ",TrxIso=" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")"
+ " - " + ex.getMessage());
else
{
try
{
log.severe(getConnectionURL ()
+ ", (2) AutoCommit=" + conn.getAutoCommit() + "->" + autoCommit
+ ", TrxIso=" + getTransactionIsolationInfo(conn.getTransactionIsolation()) + "->" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")"
+ " - " + ex.getMessage());
}
catch (Exception ee)
{
log.severe(getConnectionURL ()
+ ", (3) AutoCommit=" + autoCommit + ", TrxIso=" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")"
+ " - " + ex.getMessage());
}
}
}
catch (Exception ex)
{
m_dbException = ex;
log.log(Level.SEVERE, getConnectionURL(), ex);
}
// System.err.println ("CConnection.getConnection - " + conn);
return conn;
} // getConnection
/**
* Get Database Exception of last connection attempt
* @return Exception or null
*/
public Exception getDatabaseException ()
{
return m_dbException;
} // getConnectionException
/*************************************************************************/
private InitialContext m_iContext = null;
private Hashtable m_env = null;
/**
* Get Application Server Initial Context
* @param useCache if true, use existing cache
* @return Initial Context or null
*/
public InitialContext getInitialContext (boolean useCache)
{
if (useCache && m_iContext != null)
return m_iContext;
// Set Environment
if (m_env == null || !useCache)
m_env = getInitialEnvironment(getAppsHost(), getAppsPort(), isRMIoverHTTP());
String connect = (String)m_env.get(Context.PROVIDER_URL);
Env.setContext(Env.getCtx(), Context.PROVIDER_URL, connect);
// Get Context
m_iContext = null;
try
{
m_iContext = new InitialContext (m_env);
}
catch (Exception ex)
{
m_okApps = false;
m_appsException = ex;
if (connect == null)
connect = (String)m_env.get(Context.PROVIDER_URL);
log.severe(connect
+ "\n - " + ex.toString ()
+ "\n - " + m_env);
if (CLogMgt.isLevelFinest())
ex.printStackTrace();
}
return m_iContext;
} // getInitialContext
/**
* Get Initial Environment
* @param AppsHost host
* @param AppsPort port
* @param RMIoverHTTP true if tunnel through HTTP
* @return environment
*/
public static Hashtable getInitialEnvironment (String AppsHost, int AppsPort,
boolean RMIoverHTTP)
{
// Set Environment
Hashtable<String,String> env = new Hashtable<String,String>();
String connect = AppsHost;
if (RMIoverHTTP)
{
env.put (Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
if (AppsHost.indexOf("://") == -1)
connect = "http://" + AppsHost + ":" + AppsPort
+ "/invoker/JNDIFactory";
env.put(Context.PROVIDER_URL, connect);
}
else
{
env.put (Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
if (AppsHost.indexOf("://") == -1)
connect = "jnp://" + AppsHost + ":" + AppsPort;
env.put (Context.PROVIDER_URL, connect);
}
env.put (Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
// HTTP - default timeout 0
env.put (org.jnp.interfaces.TimedSocketFactory.JNP_TIMEOUT, "5000"); // timeout in ms
env.put (org.jnp.interfaces.TimedSocketFactory.JNP_SO_TIMEOUT, "5000");
// JNP - default timeout 5 sec
env.put(org.jnp.interfaces.NamingContext.JNP_DISCOVERY_TIMEOUT, "5000");
return env;
} // getInitialEnvironment
/**
* Get Initial Context
* @param env environment
* @return Initial Context
*/
public static InitialContext getInitialContext (Hashtable env)
{
InitialContext iContext = null;
try
{
iContext = new InitialContext (env);
}
catch (Exception ex)
{
log.warning ("URL=" + env.get(Context.PROVIDER_URL)
+ "\n - " + ex.toString ()
+ "\n - " + env);
iContext = null;
if (CLogMgt.isLevelFinest())
ex.printStackTrace();
}
return iContext;
} // getInitialContext
/**
* Query Application Server Status.
* update okApps
* @return true ik OK
*/
private boolean queryAppsServerInfo ()
{
log.finer(getAppsHost());
long start = System.currentTimeMillis();
m_okApps = false;
m_appsException = null;
//
getInitialContext (false);
if (m_iContext == null)
return m_okApps; // false
// Prevent error trace
// CLogMgtLog4J.enable(false);
try
{
StatusHome statusHome = (StatusHome)m_iContext.lookup (StatusHome.JNDI_NAME);
Status status = statusHome.create ();
//
updateInfoFromServer(status);
//
status.remove ();
m_okApps = true;
}
catch (CommunicationException ce) // not a "real" error
{
// m_appsException = ce;
String connect = (String)m_env.get(Context.PROVIDER_URL);
log.warning (connect
+ "\n - " + ce.toString ()
+ "\n - " + m_env);
}
catch (Exception e)
{
m_appsException = e;
String connect = (String)m_env.get(Context.PROVIDER_URL);
log.warning (connect
+ "\n - " + e.toString ()
+ "\n - " + m_env);
}
CLogMgtLog4J.enable(true);
log.fine("Success=" + m_okApps + " - " + (System.currentTimeMillis()-start) + "ms");
return m_okApps;
} // setAppsServerInfo
/**
* Get Last Exception of Apps Server Connection attempt
* @return Exception or null
*/
public Exception getAppsServerException ()
{
return m_appsException;
} // getAppsServerException
/**
* Update Connection Info from Apps Server
* @param svr Apps Server Status
* @throws Exception
*/
private void updateInfoFromServer (Status svr) throws Exception
{
if (svr == null)
throw new IllegalArgumentException ("AppsServer was NULL");
setType (svr.getDbType());
setDbHost (svr.getDbHost());
setDbPort (svr.getDbPort ());
setDbName (svr.getDbName ());
setDbUid (svr.getDbUid ());
setDbPwd (svr.getDbPwd ());
setBequeath (false);
//
setFwHost (svr.getFwHost ());
setFwPort (svr.getFwPort ());
if (getFwHost ().length () == 0)
setViaFirewall (false);
m_version = svr.getDateVersion ();
log.config("Server=" + getDbHost() + ", DB=" + getDbName());
} // update Info
/**
* Convert Statement
* @param origStatement original statement (Oracle notation)
* @return converted Statement
* @throws Exception
*/
public String convertStatement (String origStatement)
throws Exception
{
// make sure we have a good database
if (m_db != null && !m_db.getName ().equals (m_type))
getDatabase ();
if (m_db != null)
return m_db.convertStatement (origStatement);
throw new Exception (
"CConnection.convertStatement - No Converstion Database");
} // convertStatement
/**
* Get Status Info
* @return info
*/
public String getStatus()
{
StringBuffer sb = new StringBuffer (m_apps_host);
sb.append ("{").append (m_db_host)
.append ("-").append (m_db_name)
.append ("-").append (m_db_uid)
.append ("}");
if (m_db != null)
sb.append (m_db.getStatus());
return sb.toString ();
} // getStatus
/**
* Get Transaction Isolation Info
* @param transactionIsolation trx iso
* @return clear test
*/
public static String getTransactionIsolationInfo(int transactionIsolation)
{
if (transactionIsolation == Connection.TRANSACTION_NONE)
return "NONE";
if (transactionIsolation == Connection.TRANSACTION_READ_COMMITTED)
return "READ_COMMITTED";
if (transactionIsolation == Connection.TRANSACTION_READ_UNCOMMITTED)
return "READ_UNCOMMITTED";
if (transactionIsolation == Connection.TRANSACTION_REPEATABLE_READ)
return "REPEATABLE_READ";
if (transactionIsolation == Connection.TRANSACTION_READ_COMMITTED)
return "SERIALIZABLE";
return "<?" + transactionIsolation + "?>";
} // getTransactionIsolationInfo
/**************************************************************************
* Testing
* @param args ignored
*/
public static void main (String[] args)
{
boolean server = true;
if (args.length == 0)
System.out.println("CConnection <server|client>");
else
server = "server".equals(args[0]);
System.out.println("CConnection - " + (server ? "server" : "client"));
//
if (server)
{
Compiere.startup(false);
}
else
Compiere.startup(true);
//
System.out.println ("Connection = ");
// CConnection[name=localhost{dev-dev1-compiere},AppsHost=localhost,AppsPort=1099,type=Oracle,DBhost=dev,DBport=1521,DBname=dev1,BQ=false,FW=false,FWhost=,FWport=1630,UID=compiere,PWD=compiere]
System.out.println (Ini.getProperty (Ini.P_CONNECTION));
CConnection cc = CConnection.get ();
System.out.println (">> " + cc.toStringLong ());
Connection con = cc.getConnection (false,
Connection.TRANSACTION_READ_COMMITTED);
new CConnectionDialog(cc);
} // main
} // CConnection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -