📄 cconnection.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.db;
import java.io.*;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.compiere.util.*;
import org.compiere.interfaces.*;
/**
* Compiere Connection Descriptor
*
* @author Jorg Janke
* @author Marek Mosiewicz<marek.mosiewicz@jotel.com.pl> - support for RMI over HTTP
* @version $Id: CConnection.java,v 1.24 2003/05/04 18:33:32 marekmosiewicz Exp $
*/
public class CConnection
implements Serializable
{
/** Connection */
private static CConnection s_cc = null;
/** Logger */
private static Logger s_log = Logger.getLogger (CConnection.class);
/**
* * Get/Set default client/server Connection from Ini
* * @return Connection Descriptor
*/
public static CConnection get ()
{
if (s_cc == null)
{
String attributes = Ini.getProperty (Ini.P_CONNECTION);
if (attributes == null || attributes.length () == 0)
{
CConnectionDialog ccd = new CConnectionDialog (new CConnection ());
s_cc = ccd.getConnection ();
// set also in ALogin and Ctrl
Ini.setProperty (Ini.P_CONNECTION, s_cc.toStringLong ());
Ini.saveProperties (Ini.isClient ());
}
else
{
s_cc = new CConnection ();
s_cc.setAttributes (attributes);
}
} // client
// System.out.println("CConnection.get" + s_cc.toString());
return s_cc;
} // getConnection
/**
* Get specific connection
* @param type database Type, e.g. Database.DB_ORACLE
* @param db_host db host
* @param db_port db port
* @param db_name db name
* @return connection
*/
public static CConnection get (String type, String db_host, int db_port,
String db_name)
{
return get (type, db_host, db_port, db_name, null, null);
} // get
/**
* Get specific client connection
* @param type database Type, e.g. Database.DB_ORACLE
* @param db_host db host
* @param db_port db port
* @param db_name db name
* @param db_uid db user id
* @param db_pwd db user password
* @return connection
*/
public static CConnection get (String type, String db_host, int db_port,
String db_name, String db_uid, String db_pwd)
{
CConnection cc = new CConnection ();
cc.setAppsHost (db_host); // set Apps=DB
cc.setType (type);
cc.setDbHost (db_host);
cc.setDbPort (db_port);
cc.setDbName (db_name);
//
if (db_uid != null)
cc.setDbUid (db_uid);
if (db_pwd != null)
cc.setDbPwd (db_pwd);
return cc;
} // get
/*************************************************************************/
/**
* * Set Data Source
* * @param context context to get DataSource, e.g. java:OracleDS
* @param dataSourceName if null use OracleDS
*/
public void setDataSource (Context context, String dataSourceName)
{
String name = null;
try
{
if (dataSourceName == null || dataSourceName.length () == 0)
name = "java:OracleDS";
else
name = "java:" + dataSourceName;
m_ds = (DataSource)context.lookup (name);
if (m_ds == null)
s_log.error ("setDataSource " + name + " - not found");
}
catch (Exception ex)
{
s_log.error ("setDataSource: " + name, ex);
}
} // setDataSource
/**
* * Set Server Connection
* * @param ds DataSource
*/
public void setDataSource (DataSource ds)
{
m_ds = ds;
} // setDataSource
/**
* * Get Server Connection
* * @return DataSource
*/
public DataSource getDataSource ()
{
return m_ds;
} // getDataSource
/**
* * Has Server Connection
* * @return true if DataSource exists
*/
public boolean isDataSource ()
{
return m_ds != null;
} // isDataSource
/*************************************************************************/
/**
* Compiere Connection
*/
protected CConnection ()
{
// System.out.println("CConnection");
} // CConnection
/** Name of Connection */
private String m_name = "Standard";
/** Application Host */
private String m_apps_host = "dev";
/** Application Port */
private int m_apps_port = 1099;
/** Database Type */
private String m_type = Database.DB_ORACLE;
/** Database Host */
private String m_db_host = "dev";
/** Database Port */
private int m_db_port = DB_Oracle.DEFAULT_PORT;
/** Database name */
private String m_db_name = "dev1";
/** In Memory connection */
private boolean m_bequeath = false;
/** Connection uses Firewall */
private boolean m_firewall = false;
/** Firewall host */
private String m_fw_host = "";
/** Firewall port */
private int m_fw_port = DB_Oracle.DEFAULT_CM_PORT;
/** DB User name */
private String m_db_uid = "compiere";
/** DB User password */
private String m_db_pwd = "compiere";
/** Database */
private CompiereDatabase m_db = null;
/** ConnectionException */
private Exception m_dbException = null;
private Exception m_appsException = null;
/** Database Connection */
private boolean m_okDB = false;
/** Apps Server Connection */
private boolean m_okApps = false;
/** Info */
private String[] m_info = new String[2];
/** Server Version */
private String m_version = null;
/** DataSource */
private DataSource m_ds = null;
/*************************************************************************/
/**
* Get Name
* @return connection name
*/
public String getName ()
{
return m_name;
}
/**
* Set Name
* @param name connection name
*/
public void setName (String name)
{
m_name = name;
} // setName
/**
* Set Name
*/
protected void setName ()
{
m_name = toString ();
} // setName
/*************/
/**
* Get Application Host
* @return apps host
*/
public String getAppsHost ()
{
return m_apps_host;
}
/**
* Set Application Host
* @param apps_host apps host
*/
public void setAppsHost (String apps_host)
{
m_apps_host = apps_host;
m_name = toString ();
m_okApps = false;
}
/**
* Get Apps Port
* @return port
*/
public int getAppsPort ()
{
return m_apps_port;
}
/**
* Set Apps Port
* @param apps_port apps port
*/
public void setAppsPort (int apps_port)
{
m_apps_port = apps_port;
m_okApps = false;
}
/**
* Set Apps Port
* @param apps_portString appd port as String
*/
public void setAppsPort (String apps_portString)
{
try
{
setAppsPort (Integer.parseInt (apps_portString));
}
catch (Exception e)
{
System.err.println ("CConnection.setAppsPort " + e.toString ());
}
} // setAppsPort
/**
* Is Application Server OK
* @param tryContact try to contact
* @return true if Apps Server exists
*/
public boolean isAppsServerOK (boolean tryContact)
{
if (!tryContact)
return m_okApps;
// Get Context
if (m_iContext == null)
{
getInitialContext (false);
if (!m_okApps)
return false;
}
// Contact it
try
{
StatusHome statusHome = (StatusHome)m_iContext.lookup (StatusHome.
JNDI_NAME);
Status status = statusHome.create ();
m_version = status.getDateVersion ();
status.remove ();
m_okApps = true;
}
catch (Exception ce)
{
m_okApps = false;
}
catch (Throwable t)
{
m_okApps = false;
}
return m_okApps;
} // isAppsOK
/**
* Test ApplicationServer
* @return Exception or null
*/
public Exception testAppsServer ()
{
if (setAppsServerInfo ())
testDatabase ();
return getAppsServerException ();
} // testAppsServer
/**
* Get Apps Server Version
* @return db host name
*/
public String getServerVersion ()
{
return m_version;
}
// getVersion
/*************/
/**
* Get Database Host name
* @return db host name
*/
public String getDbHost ()
{
return m_db_host;
}
/**
* Set Database host name
* @param db_host db host
*/
public void setDbHost (String db_host)
{
m_db_host = db_host;
m_name = toString ();
m_okDB = false;
}
/**
* Get Database Name (SID)
* @return db name
*/
public String getDbName ()
{
return m_db_name;
}
/**
* Set Database Name (SID)
* @param db_name db name
*/
public void setDbName (String db_name)
{
m_db_name = db_name;
m_name = toString ();
m_okDB = false;
}
/**
* Get DB Port
* @return port
*/
public int getDbPort ()
{
return m_db_port;
}
/**
* Set DB Port
* @param db_port db port
*/
public void setDbPort (int db_port)
{
m_db_port = db_port;
m_okDB = false;
}
/**
* Set DB Port
* @param db_portString db port as String
*/
public void setDbPort (String db_portString)
{
try
{
setDbPort (Integer.parseInt (db_portString));
}
catch (Exception e)
{
System.err.println ("CConnection.setDbPort " + e.toString ());
}
} // setDbPort
/**
* Get Database Password
* @return db password
*/
public String getDbPwd ()
{
return m_db_pwd;
}
/**
* Set DB password
* @param db_pwd db user password
*/
public void setDbPwd (String db_pwd)
{
m_db_pwd = db_pwd;
m_okDB = false;
}
/**
* Get Database User
* @return db user
*/
public String getDbUid ()
{
return m_db_uid;
}
/**
* Set Database User
* @param db_uid db user id
*/
public void setDbUid (String db_uid)
{
m_db_uid = db_uid;
m_name = toString ();
m_okDB = false;
}
/**
* Is DB via Firewall
* @return true if via firewall
*/
public boolean isViaFirewall ()
{
return m_firewall;
}
public void setViaFirewall (boolean viaFirewall)
{
m_firewall = viaFirewall;
m_okDB = false;
}
public void setViaFirewall (String viaFirewallString)
{
try
{
setViaFirewall (Boolean.valueOf (viaFirewallString).booleanValue ());
}
catch (Exception e)
{
System.err.println ("CConnection.setViaFirewall " + e.toString ());
}
}
public String getFwHost ()
{
return m_fw_host;
}
public void setFwHost (String fw_host)
{
m_fw_host = fw_host;
m_okDB = false;
}
/**
* Get Firewall port
* @return firewall port
*/
public int getFwPort ()
{
return m_fw_port;
}
/**
* Set Firewall port
* @param fw_port firewall port
*/
public void setFwPort (int fw_port)
{
m_fw_port = fw_port;
m_okDB = false;
}
/**
* Set Firewall port
* @param fw_portString firewall port as String
*/
public void setFwPort (String fw_portString)
{
try
{
setFwPort (Integer.parseInt (fw_portString));
}
catch (Exception e)
{
System.err.println ("CConnection.setFwPort " + e.toString ());
}
}
/**
* Is it a bequeath connection
* @return true if bequeath connection
*/
public boolean isBequeath ()
{
return m_bequeath;
}
/**
* Set Bequeath
* @param bequeath bequeath connection
*/
public void setBequeath (boolean bequeath)
{
m_bequeath = bequeath;
m_okDB = false;
}
/**
* Set Bequeath
* @param bequeathString bequeath connection as String (true/false)
*/
public void setBequeath (String bequeathString)
{
try
{
setBequeath (Boolean.valueOf (bequeathString).booleanValue ());
}
catch (Exception e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -