⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cconnection.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		{
			System.err.println ("CConnection.setBequeath " + e.toString ());
		}
	}

	/**
	 *  Get Database Type
	 *  @return database type
	 */
	public String getType ()
	{
		return m_type;
	}

	/**
	 *  Set Database Type and default settings.
	 *  Checked against installed databases
	 *  @param type database Type, e.g. Database.DB_ORACLE
	 */
	public void setType (String type)
	{
		for (int i = 0; i < Database.DB_NAMES.length; i++)
		{
			if (Database.DB_NAMES[i].equals (type))
			{
				m_type = type;
				m_okDB = false;
				break;
			}
		}
		//  PostgreSQL defaults
		if (isPostgreSQL ())
		{
			setBequeath (false);
			setViaFirewall (false);
			if (getDbPort () == DB_Oracle.DEFAULT_PORT)
				setDbPort (DB_PostgreSQL.DEFAULT_PORT);
		}
		//  Oracle Defaults
		else if (isOracle ())
		{
			if (getDbPort () == DB_PostgreSQL.DEFAULT_PORT)
				setDbPort (DB_Oracle.DEFAULT_PORT);
			setFwPort (DB_Oracle.DEFAULT_CM_PORT);
		}
	} //  setType

	/**
	 *  Supports BLOB
	 *  @return true if BLOB is supported
	 */
	public boolean supportsBLOB ()
	{
		return m_db.supportsBLOB ();
	} //  supportsBLOB


	/**
	 *  Is Oracle DB
	 *  @return true if Oracle
	 */
	public boolean isOracle ()
	{
		return Database.DB_ORACLE.equals (m_type);
	} //  isOracle

	/**
	 *  Is PostgreSQL DB
	 *  @return true if PostgreSQL
	 */
	public boolean isPostgreSQL ()
	{
		return Database.DB_POSTGRESQL.equals (m_type);
	} //  isPostgreSQL

	/**
	 *  Is Database Connection OK
	 *  @return true if database connection is OK
	 */
	public boolean isDatabaseOK ()
	{
		return m_okDB;
	} //  isDatabaseOK

	/**
	 *  Test Database Connection.
	 *  -- Example --
	 *  Database: PostgreSQL - 7.1.3
	 *  Driver:   PostgreSQL Native Driver - PostgreSQL 7.2 JDBC2
	 *  -- Example --
	 *  Database: Oracle - Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production With the Partitioning option JServer Release 8.1.7.0.0 - Production
	 *  Driver:   Oracle JDBC driver - 9.0.1.1.0
	 *  @return Exception or null
	 */
	public Exception testDatabase ()
	{
		//  the actual test
		Connection conn = getConnection (true,
						  Connection.TRANSACTION_READ_COMMITTED);
		if (conn != null)
		{
			try
			{
				DatabaseMetaData dbmd = conn.getMetaData ();
				m_info[0] = "Database=" + dbmd.getDatabaseProductName ()
							+ " - " + dbmd.getDatabaseProductVersion ();
				m_info[0] = m_info[0].replace ('\n', ' ');
				m_info[1] = "Driver  =" + dbmd.getDriverName ()
							+ " - " + dbmd.getDriverVersion ();
				m_info[1] = m_info[1].replace ('\n', ' ');
				System.out.println (m_info[0]);
				System.out.println (m_info[1]);
				conn.close ();
			}
			catch (Exception e)
			{
				System.err.println ("CConnectiom.testDatabase - " + e);
			}
		}
		return getDatabaseException (); //  from opening
	} //  testDatabase

	/*************************************************************************/

	/**
	 *  Short String representation
	 *  @return appsHost{dbHost-sid-uid}
	 */
	public String toString ()
	{
		StringBuffer sb = new StringBuffer (m_apps_host);
		sb.append ("{").append (m_db_host)
		  .append ("-").append (m_db_name)
		  .append ("-").append (m_db_uid)
		  .append ("}");
		return sb.toString ();
	} //  toString

	/**
	 *  String representation.
	 *  Used also for Instanciation
	 *  @see setAttributes(String) setAttributes
	 *  @return string representation
	 */
	public String toStringLong ()
	{
		StringBuffer sb = new StringBuffer ("CConnection[");
		sb.append ("name=").append (m_name)
		  .append (",AppsHost=").append (m_apps_host)
		  .append (",AppsPort=").append (m_apps_port)
		  .append (",RMIoverHTTP=").append (m_RMIoverHTTP)
		  .append (",type=").append (m_type)
		  .append (",DBhost=").append (m_db_host)
		  .append (",DBport=").append (m_db_port)
		  .append (",DBname=").append (m_db_name)
		  .append (",BQ=").append (m_bequeath)
		  .append (",FW=").append (m_firewall)
		  .append (",FWhost=").append (m_fw_host)
		  .append (",FWport=").append (m_fw_port)
		  .append (",UID=").append (m_db_uid)
		  .append (",PWD=").append (m_db_pwd)
		  ;
		sb.append ("]");
		return sb.toString ();
	} //  toStringLong

	/**
	 *  Set Attributes from String (pases toStringLong())
	 *  @param attributes attributes
	 */
	private void setAttributes (String attributes)
	{
		try
		{
			setName (attributes.substring (
			  attributes.indexOf ("name=") + 5,
			  attributes.indexOf (",AppsHost=")));
			setAppsHost (attributes.substring (
			  attributes.indexOf ("AppsHost=") + 9,
			  attributes.indexOf (",AppsPort=")));
			setAppsPort (attributes.substring (
			  attributes.indexOf ("AppsPort=") + 9,
			  attributes.indexOf (",RMIoverHTTP=")));
            setRMIoverHTTP (attributes.substring (
              attributes.indexOf ("RMIoverHTTP=") + 12,
              attributes.indexOf (",type=")));
            
			//
			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)
		{
			System.err.println ("CConnection.setAttributes - " + 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.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 ("\n").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)
			{
				System.err.println ("CConnection.getDatabase " + 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 "";
	} //  getConnectionString

	/**
	 *  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;
				s_log.error ("createServerConnection", ex);
			}
		}

		//	Server
		return conn;
	} //	getServerConnection


	/**
	 *  Get Connection - no not close
	 *  @param autoCommit true if autocommit connection
	 *  @param trxLevel Connection transaction level
	 *  @return Connection
	 */
	public Connection getConnection (boolean autoCommit, int trxLevel)
	{
		Connection conn = null;
		getDatabase (); //  updates m_db
		if (m_db == null)
			return null;
		//
		m_dbException = null;
		m_okDB = false;
		try
		{
			DriverManager.registerDriver (m_db.getDriver ());
			DriverManager.setLoginTimeout (Database.CONNECTION_TIMEOUT);
			conn = DriverManager.getConnection (getConnectionURL (), getDbUid (),
				   getDbPwd ());
			conn.setAutoCommit (autoCommit);
			conn.setTransactionIsolation (trxLevel);
			m_okDB = true;
		}
		catch (UnsatisfiedLinkError ule)
		{
			System.err.println ("CConection.getConnection - "
			  + getConnectionURL ()
			  + " - Did you set the LD_LIBRARY_PATH ? - " + ule.toString ());
		}
		catch (Exception ex)
		{
			m_dbException = ex;
			System.err.println ("CConection.getConnection - "
			  + getConnectionURL ()
			  + " - " + ex.toString ());
			//	e.printStackTrace();
		}
		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;
	private boolean m_RMIoverHTTP = false;

	/**
	 *  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 = new Hashtable ();
            if(isRMIoverHTTP()){
                m_env.put (Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.HttpNamingContextFactory");
                if(getAppsHost().indexOf("://")==-1){  //if there is only host specified get default location
                    m_env.put (Context.PROVIDER_URL, "http://"+getAppsHost()+":"+getAppsPort()+"/invoker/JNDIFactory");                    
                }else{     //there is full url - allow to specify all things - port is ignored
                    m_env.put (Context.PROVIDER_URL, getAppsHost());                    
                }
            }else{
				m_env.put (Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                m_env.put (Context.PROVIDER_URL, getAppsHost ());                
            }
			m_env.put (Context.URL_PKG_PREFIXES,
			  "org.jboss.naming:org.jnp.interfaces");
		}
        

		//	Get Context
		m_iContext = null;
		try
		{
			m_iContext = new InitialContext (m_env);
		}
		catch (Exception e)
		{
			m_okApps = false;
			m_appsException = e;
			System.err.println ("CConection.getInitialContext - " + getAppsHost ()
			  + " - " + e.toString ());
		}
		return m_iContext;
	}

	//	getInitialContext

	/**
	 *  Set Application Server Status.
	 *  update okApps
	 *  @return true ik OK
	 */
	private boolean setAppsServerInfo ()
	{
		m_okApps = false;
		m_appsException = null;
		//
		getInitialContext (false);
		if (m_iContext == null)
			return m_okApps;

		//	Prevent error trace
		Logger root = Logger.getRootLogger ();
		Level level = root.getLevel ();
		root.setLevel (Level.OFF);
		try
		{
			StatusHome statusHome = (StatusHome)m_iContext.lookup (StatusHome.
									JNDI_NAME);
			Status status = statusHome.create ();
			//
			updateInfo (status);
			//
			status.remove ();
			m_okApps = true;
		}
		catch (CommunicationException ce)
		{
			//	m_appsException = ce;
			System.out.println ("CConection.setAppsServerInfo - " + getAppsHost ()
			  + " - " + ce.toString ());
		}
		catch (Exception e)
		{
			m_appsException = e;
			System.err.println ("CConection.setAppsServerInfo - " + getAppsHost ()
			  + " - " + e.toString ());
		}
		root.setLevel (level);
		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 updateInfo (Status svr)
	  throws Exception
	{
		if (svr == null)
			throw new IllegalArgumentException ("AppsServer was NULL");

		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 ();

	} //  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


	/**
	 *     * 	Testing
	 *     * 	@param args ignored
	 */
	public static void main (String[] args)
	{
		Ini.setClient (false);
		Ini.loadProperties (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);

	}

	public boolean isRMIoverHTTP ()
	{
		return m_RMIoverHTTP;
	}

	public void setRMIoverHTTP (boolean RMIoverHTTP)
	{
		this.m_RMIoverHTTP = RMIoverHTTP;
	} //	main

	public void setRMIoverHTTP (String RMIoverHTTP)
	{
		try
		{
			setRMIoverHTTP (Boolean.valueOf (RMIoverHTTP).booleanValue ());
		}
		catch (Exception e)
		{
			System.err.println ("CConnection.setRMIoverHTTP " + e.toString ());
		}
	}

} //  CConnection

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -