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

📄 tnsparse.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.trinet.util;

/*
 * Java TNS parser API
 *
 * File:          TNSParse.java
 * Purpose:       Parse Oracle's Tnsnames.ora file
 * Author:        Rauf Sarwar
 * Java Ver:      Recommended Java versions are 1.2 and above. Tested and compiled on
 *                Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01).
 *                Check your version from command line.. java -showversion
 * API's:         Requires java.io, java.util, java.lang, java.sql
 * Version:       $Id: TNSParse.java,v 1.1.1.1 2004/12/28 09:39:06 zhou Exp $
 * Bugs:          Bug fixes, suggestions and/or comments should be sent to:
 *                      Rauf.Sarwar@ifsna.com
 * Usage:         Freeware. Modify/Distribute as you wish.
 *
 *
 * Test this API using following. e.g. Get list of Service names.
 *
 * 1) Make sure Java SDK 1.2 or above is installed and CLASSPATH system variable includes
 *    all Java API's.
 *    To check... type javac at command line.
 * 2) Create file TNSParse.java (Filename Case matters) and copy TNSParse code to it
 * 3) Open command line (DOS Prompt)
 * 4) C:\> cd <Directory where TNSParse.java file is>
 * 5) Set CLASSPATH to include current directory
 *       C:\> set CLASSPATH=.;%CLASSPATH%
 * 6) Compile TNSParse.java
 *       C:\> javac TNSParse.java
 * 7) Create file testTnsParse.java (Filename Case matters) in same directory and copy this text to it.
 *    public class testTnsParse {
 *       public void main (String[] args)
 *          throws Exception {
 *
 *          TNSParse t = new TNSParse (args[0]);
 *          String s = new String (t.getAliasList());
 *          t.close();
 *          System.out.println (s);
 *       }
 *    }
 * 8) Compile testTnsParse.java
 *       C:\> javac testTnsParse.java
 * 9) Run testTnsParse.class file passing tnsnames.ora file as parameter
 *       C:\> java testTnsParse C:\<YourOracleHome>\network\admin\tnsnames.ora
 *       OR
 *       C:\> jview testTnsParse C:\<YourOracleHome>\network\admin\tnsnames.ora
 *
 *
 * Date      Name             Revision  History
 * --------  ---------------  --------  -----------------------------------
 * 07152001  Rauf Sarwar      V 1.0     Created
 * 10102001  Rauf Sarwar      V 1.1     Added public getFullConnString, getOciConn,
 *                                      getThinTcpConn, enumerateAddresses, connClose
 * 04052002  Rauf Sarwar      V 1.2     1) Fixed Private tnsRemoveNonDataLines()
 *                                         Exception resolved if closing Parenthesis mismatched
 *                                      2) Fixed private tnsSetAliasString()
 *                                         Skip alias from aliasstr if invalid
 *                                      3) Added better error handling to getOciConn() and getThinTcpConn().
 *                                         Added private buildConnURL() to build string separately.
 *                                      4) Added extractTcpString() to return TCP string as used
 *                                         by Oracle Thin JDBC driver like <HOST>:<PORT>:<SID>
 *
 * ------------------------------------------------------------------------
 *
 *
 * ****** CONTSTRUCTORS ******
 * public TNSParse (String tnsFile)
 * public TNSParse (File tnsFile)
 *
 * ****** PUBLIC METHODS ******
 * public String getAliasList ()
 * public String getFullConnString (String alias)
 * public Connection getOciConn (String alias, String username, String pwd)
 * public Connection getOciConn (String alias, String username, String pwd, boolean autocommit)
 * public Connection getThinTcpConn (String alias, String username, String pwd)
 * public Connection getThinTcpConn (String alias, String username, String pwd, boolean autocommit)
 * public String extractTcpString (String alias)
 * public String enumerateAddresses (String alias)
 * public boolean isConnStrValid (String alias)
 * public void connClose ()
 * public void close ()
 * public String getVersion ()
 *
 * Copied 7/2002 from http://www.orsweb.com/downloads/source/447.html
 */

import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;

public class TNSParse {

   /*
    * File Version
    */
   private final String version = "$Id: TNSParse.java,v 1.1.1.1 2004/12/28 09:39:06 zhou Exp $";
   /*
    * java.sql Connection interface
    */
   private Connection conn = null;
   /*
    * Tnsnames.ora File object
    */
   private File tnsnamesora = null;
   /*
    * Holds Tnsnames.ora file contents
    */
   private String filestr = "";
   /*
    * Holds delimited Alias/Service name information
    */
   private String aliasstr = "";
   /*
    * Standard End-Of-Line Carriage Return\Line feed
    */
   private final String eol = "\r\n";
   /*
    * Space identifier
    */
   private final String spacerep = " ";
   /*
    * null identifier
    */
   private final String nullrep = "";
   /*
    * Comment identifier
    */
   private final char commchar = '#';
   /*
    * (char)5 identifier
    */
   private final String strchar5 = (char)5 +"";


   /**************************
    ****** CONSTRUCTORS ******
    **************************/

   /*
    * public TNSParse (String tnsFile)
    *    @param  tnsFile: fully qualified Tnsnames.ora filename
    *
    *    Constructs TNSParse object used to parse Tnsnames.ora file
    */
   public TNSParse (String tnsFile)
      throws Exception {

      this (new File (tnsFile));
   }

   /*
    * public TNSParse (File tnsFile)
    *    @param  tnsFile: File object
    *
    *    Constructs TNSParse object used to parse Tnsnames.ora file
    */
   public TNSParse (File tnsFile)
      throws Exception {

      tnsnamesora = tnsFile;
      tnsReadAndParseFile ();
   }


   /**************************
    ***** PUBLIC METHODS *****
    **************************/

   /*
    * public String getAliasList ()
    *    @param
    *    @return   String:  End-Of-Line delimited list of Alias/Service names
    *
    *    Return End-Of-Line delimited list of Alias/Service names
    */
   public String getAliasList ()
      throws Exception {

      return aliasstr;
   }

   /*
    * public String getFullConnString (String alias)
    *    @param    alias: Alias/Service name
    *    @return   String: Full connect string
    *
    *    Return full connect string. Full connect string begins with Alias/Service
    *    name and ends with last ")"
    */
   public String getFullConnString (String alias)
      throws Exception {

      checkAliasExist (alias);
      return getConnectString (alias);
   }

   /*
    * public Connection getOciConn (String alias, String username, String pwd)
    *    @param    alias: Alias/Service name
    *              username: Oracle database username
    *              pwd: Oracle password
    *    @return   Connection: SQL Connection interface
    *
    *    Builds and returns JDBC native OCI Connection.
    *    AutoCommit is disabled by default.
    *
    *    When done, Connection MUST be closed by calling connClose()
    *
    *    throws: Exception if no TCP protocol is defined
    *            SQLException if database error occurs
    */
   public Connection getOciConn (String alias, String username, String pwd)
      throws Exception {

      return getOciConn (alias, username, pwd, false);
   }

   /*
    * public Connection getOciConn (String alias, String username, String pwd, boolean autocommit)
    *    @param    alias: Alias/Service name
    *              username: Oracle database username
    *              pwd: Oracle password
    *              autocommit: true to enable AutoCommit, false to disable AutoCommit
    *    @return   Connection: SQL Connection interface
    *
    *    Builds and returns JDBC native OCI Connection.
    *
    *    When done, Connection MUST be closed by calling connClose()
    *
    *    throws: Exception if Connection string is invalid. (If parenthesis mismatch).
    *            SQLException if database error occurs
    */
   public Connection getOciConn (String alias, String username, String pwd, boolean autocommit)
      throws Exception {

      checkAliasExist (alias);
      if (!isConnStrValid (alias))
         throwException ("Service name '" + alias.toUpperCase() + "' is invalid. Check parenthesis", false);
      String strtmp = new String (makeSqlConnection (alias, "oci", username, pwd, autocommit));
      if (!strtmp.equalsIgnoreCase("SUCCESS"))
         throwException (strtmp, false);
      return conn;
   }

   /*
    * public Connection getThinTcpConn (String alias, String username, String pwd)
    *    @param    alias: Alias/Service name
    *              username: Oracle database username
    *              pwd: Oracle password
    *    @return   Connection: SQL Connection interface
    *
    *    Builds and returns JDBC thin Connection. Java uses TCP stack to connect,
    *    therefore connection string must specify TCP protocol.
    *    If multiple ADDRESSES defined, returns first available TCP connection.
    *    AutoCommit is disabled by default.
    *
    *    When done, Connection MUST be closed by calling connClose()
    *
    *    throws: Exception if no TCP protocol is defined
    *            SQLException if database error occurs
    */
   public Connection getThinTcpConn (String alias, String username, String pwd)
      throws Exception {

      return getThinTcpConn (alias, username, pwd, false);
   }

   /*
    * public Connection getThinTcpConn (String alias, String username, String pwd, boolean autocommit)
    *    @param    alias: Alias/Service name
    *              username: Oracle database username
    *              pwd: Oracle password
    *              autocommit: true to enable AutoCommit, false to disable AutoCommit
    *    @return   Connection: SQL Connection interface  jdbc:oracle:thin:@<HOST>:<PORT>:<SID>
    *
    *    Builds and returns JDBC thin Connection. Java uses TCP stack to connect,
    *    therefore connection string must specify TCP protocol.
    *    If multiple ADDRESSES defined, returns first available TCP connection.
    *
    *    When done, Connection MUST be closed by calling connClose()
    *
    *    throws: Exception if no TCP protocol is defined
    *            SQLException if database error occurs
    */
   public Connection getThinTcpConn (String alias, String username, String pwd, boolean autocommit)
      throws Exception {

      checkAliasExist (alias);
      String connstr = new String (getConnectString (alias));
      if (!isTcpAvailable (connstr))
         throwException ("Service name '" + alias.toUpperCase() + "' does not use TCP protocol", false);
      String strthin = new String (buildThinString (getFirstTcpString (connstr)));
      String strtmp = new String (makeSqlConnection (strthin, "thin", username, pwd, autocommit));
      if (!strtmp.equalsIgnoreCase("SUCCESS"))
         throwException (strtmp, false);
      return conn;
   }

   /*
    * public String extractTcpString (String alias)
    *    @param    alias: Alias/Service name
    *    @return   String: Fully concatenated Thin TCP string like <HOST>:<PORT>:<SID> OR null if does not exis
t
    *
    *    Extracts a TCP string if available, builds the string as used by Oracle thin jdbc driver
    */
   public String extractTcpString (String alias)
      throws Exception {

      checkAliasExist (alias);
      String connstr = new String (getConnectString (alias));
      if (!isTcpAvailable (connstr))
         return null;
      return buildThinString (getFirstTcpString (connstr));
   }

   /*
    * public String enumerateAddresses (String alias)
    *    @param    alias: Alias/Service name
    *    @return   String: End-Of-Line delimited list of all addresses including CONNECT_DATA
    *              (ADDRESS=(.....))(CONNECT_DATA=(...))
    *
    *    Parses a connection string and returns all defined ADDRESSES
    */
   public String enumerateAddresses (String alias)
      throws Exception {

      checkAliasExist (alias);
      String connstr = new String (getConnectString (alias));
      return enumerateAddressList (connstr);
   }

   /*
    * public boolean isConnStrValid (String alias)
    *    @param    alias: Alias/Service name
    *    @return   boolean: true if valid, false otherwise
    *
    *    Only checks if all parenthesis "(" and ")" are matched
    */
   public boolean isConnStrValid (String alias)
      throws Exception {

      checkAliasExist (alias);
      String strtmp = new String (getConnectString (alias));

⌨️ 快捷键说明

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