0 Replies Latest reply: Aug 26, 2006 5:01 PM by mdrake-Oracle RSS

    Where can I find the code for the class Base Application ?

    mdrake-Oracle
      Here..
      package com.oracle.st.xmldb.pm.common.baseApp;
      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      import java.io.PrintStream;
      import java.io.PrintWriter;
      import java.io.Reader;
      import java.io.StringWriter;
      import java.io.Writer;
      import java.sql.DriverManager;
      import java.sql.Connection;
      import java.sql.SQLException;
      import java.util.Properties;
      import oracle.jdbc.OracleConnection;
      import oracle.jdbc.pool.OracleOCIConnectionPool;
      import oracle.jdbc.oci.OracleOCIConnection;
      import oracle.xml.parser.v2.DOMParser;
      import oracle.xml.parser.v2.XMLDocument;
      import oracle.xml.parser.v2.XMLElement;
      import org.xml.sax.SAXException;
      import org.w3c.dom.Element;
      import org.w3c.dom.Text;
      import org.w3c.dom.NodeList;
      import java.io.InputStream;
      public class ConnectionProvider extends Object
      {
          public static final boolean DEBUG = true;
      
          protected OracleConnection connection;
      
          protected XMLDocument connectionDefinition;
      
          protected OracleOCIConnectionPool connectionPool;
      
          public static final String CONNECTION = "Connection";
          public static final String DRIVER = "Driver";
          public static final String HOSTNAME = "Hostname";
          public static final String PORT = "Port";
          public static final String SID = "SID";
          public static final String SERVICENAME = "ServiceName";
          public static final String SERVERMODE = "Server";
          public static final String SCHEMA = "Schema";
          public static final String PASSWORD = "Password";
          public static final String POOL = "Pool";
          public static final String THIN_DRIVER = "thin";
          public static final String OCI_DRIVER = "oci8";
      
          public static final String DEFAULT_CONNECTION_DEFINITION = "c:\\temp\\connection.xml";
          public static final String DEFAULT_DRIVER = OCI_DRIVER;
          public static final String DEFAULT_HOSTNAME = "localhost";
          public static final String DEFAULT_PORT = "1521";
          public static final String DEFAULT_SERVERMODE = "DEDICATED";
      
          public static final String TARGET_DIRECTORY = "targetDirectory";
      
          protected PrintStream log;
          
          public ConnectionProvider() {
              
          }
          
          public void initializeConnection()     
          throws SAXException, IOException, SQLException
          {
              this.initializeConnection(System.out);
          }
          
          public void initializeConnection(PrintStream log)
          throws SAXException, IOException, SQLException
          {
            DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() );
            this.log = log;
            loadConnectionSettings();
            this.connection = openConnection();
          }
          
          public ConnectionProvider getConnectionProvider() {
              return this;
          }
          
          public void initalizeConnection(String connectionLocation, PrintStream log)
          throws SAXException, IOException, SQLException
          {
            DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() );
            this.log = log;
            loadConnectionSettings(connectionLocation);
            this.connection = openConnection();
          }
          public void setLogger(PrintStream log)
          {
            this.log = log;
          }
          private void setConnectionSettings(XMLDocument doc)
          {
            this.connectionDefinition = doc;
          }
      
          private void dumpConnectionSettings()
          throws IOException
          {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            this.connectionDefinition.print(pw);
            pw.close();
            sw.close();
          }
          private void setConnectionPool(OracleOCIConnectionPool pool)
          {
            this.connectionPool = pool;
          }
          public OracleConnection getConnection()
          throws SQLException
          {
            if (this.isPooled())
            {
              return (OracleOCIConnection) this.connectionPool.getConnection();
            }
            else
            {
              return this.connection;
            }
         }
          
         public void closeConnection(Connection conn)
         throws Exception
         {
            if (isPooled())
            {
             conn.close();
            }
          } 
          public Connection getConnection(String schema, String passwd)
          throws Exception
          {
            if (isPooled())
            {
              return (OracleOCIConnection) this.getConnection(schema,passwd);
            }
            else
            {
              return this.connection;
            }
          }
          public String getSetting(String nodeName)
          {
            return getSetting(nodeName, null);
          }
       
          public String getSetting(String nodeName, String defaultValue)
          {
            String textValue = null;
            XMLElement root = (XMLElement) this.connectionDefinition.getDocumentElement();
            NodeList children = root.getChildrenByTagName(nodeName);
            if (children.getLength() != 0)
            {
              Element  element = (Element) children.item(0);
              Text text = (Text) element.getFirstChild();
              if (text != null)
              {
                  return text.getData();
              }
            }
            return defaultValue;
          }   
          protected String getDriver()
          {
              return getSetting(DRIVER,DEFAULT_DRIVER);
          }
          protected String getHostname()
          {
              return getSetting(HOSTNAME,DEFAULT_HOSTNAME);
          }
          protected String getPort()
          {
              return getSetting(PORT,DEFAULT_PORT);
          }
          protected String getServerMode()
          {
              return getSetting(SERVERMODE,DEFAULT_SERVERMODE);
          }
          protected String getServiceName()
          {
              return getSetting(SERVICENAME);
          }
          protected String getSID()
          {
              return getSetting(SID);
          }
          protected boolean isPooled()
          {
              String usePool =  getSetting(POOL,Boolean.FALSE.toString());
              return !usePool.equalsIgnoreCase(Boolean.FALSE.toString());
          }
          protected String getSchema()
          {
              return getSetting(SCHEMA);
          }
          protected String getPassword()
          {
              return getSetting(PASSWORD);
          }
          
          public void loadConnectionSettings()
          throws IOException, SAXException
          {
            String filename = System.getProperty( "com.oracle.st.xmldb.pm.ConnectionParameters", this.DEFAULT_CONNECTION_DEFINITION ) ;
            loadConnectionSettings(filename);
          }
          public void loadConnectionSettings(String filename)
          throws IOException, SAXException
          {
            Reader reader = new FileReader(new File(filename));
            DOMParser parser = new DOMParser();
            parser.parse(reader);
            XMLDocument doc = parser.getDocument();
            setConnectionSettings(doc);
            if (DEBUG)
            {
              dumpConnectionSettings();
            }
          }
          protected String getDatabaseURL()
          {
              if( getDriver() != null)
              {
                if( getDriver().equalsIgnoreCase( THIN_DRIVER ) )
                {
                    return "jdbc:oracle:thin:@" + getHostname() + ":" + getPort() + ":" + getSID();
                }
                else
                {
                    return "jdbc:oracle:oci8:@(description=(address=(host=" + getHostname() + ")(protocol=tcp)(port=" + getPort() + "))(connect_data=(service_name=" + getServiceName() + ")(server=" + getServerMode() + ")))";
                }
              }
              else
              {
                return null;
              }
          }   
          private OracleConnection openConnection()
          throws SQLException
          {
              String user = getSchema();
              String password = getPassword();
              String connectionString = user + "/" + password + "@" + getDatabaseURL();
              OracleConnection conn = null;
              if( DEBUG )
              {
                  this.log.println( "ConnectionProvider.establishConnection(): Connecting as " + connectionString );
              }
              try
              {
                  conn = (OracleConnection) DriverManager.getConnection( getDatabaseURL(), user, password );
                  if( DEBUG )
                  {
                      this.log.println( "ConnectionProvider.establishConnection(): Database Connection Established" );
                  }
              }
              catch( SQLException sqle )
              {
                  int err = sqle.getErrorCode();
                  this.log.println( "ConnectionProvider.establishConnection(): Failed to connect using " + connectionString );
                  sqle.printStackTrace(this.log);
                  throw sqle;
              }
              return conn;
          }
          public OracleConnection getNewConnection()
          throws SQLException
          {
            return openConnection();
          }
        
          public OracleOCIConnectionPool createConnectionPool()
          throws Exception
          {
            OracleOCIConnectionPool pool;
            Properties poolConfig  = new Properties( );
            poolConfig.put (OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT, "1");
            poolConfig.put (OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT, "1");
            poolConfig.put (OracleOCIConnectionPool.CONNPOOL_INCREMENT, "0");
            return new OracleOCIConnectionPool(getSchema(),getPassword(), getDatabaseURL(),poolConfig);
          }
         public XMLDocument getConnectionSettings()
         {
           return this.connectionDefinition;
         }
      }
      package com.oracle.st.xmldb.pm.common.baseApp;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.PrintWriter;
      import java.io.Writer;
      import java.sql.Connection;
      import java.sql.SQLException;
      import oracle.sql.CLOB;
      import org.xml.sax.SAXException;
      /**
       * A Class class.
       * <P>
       * @author Mark D. Drake
       * Please complete these missing tags
       * @rref
       * @copyright
       * @concurrency
       * @see
       */
      
      public abstract class BaseApplication extends ConnectionProvider
      {
          public static final boolean DEBUG = true;
      
          public abstract void doSomething( String [] args ) throws Exception;
      
          public CLOB createCLOB( Connection conn, InputStream is )
          throws SQLException, IOException
          {
              CLOB clob = CLOB.createTemporary( conn, false, CLOB.DURATION_SESSION);
              writeToClob(clob,is);
              return clob;
          }
      
          public CLOB writeToClob( CLOB clob, InputStream is)
          throws SQLException, IOException 
          {
              InputStreamReader reader = new InputStreamReader( is );
              Writer writer = clob.setCharacterStream(0);
              
              char [] buffer = new char [ clob.getChunkSize() ];
              for( int charsRead = reader.read( buffer );
              charsRead > - 1;
              charsRead = reader.read( buffer ) )
              {
                  writer.write( buffer, 0, charsRead );
              }
              writer.close();
              return clob;
          }
          /**
           * Please complete the missing tags for main
           * @param
           * @return
           * @throws
           * @pre
           * @post
           */
          public BaseApplication() 
          {
              super();
          }
      }
      This framework uses the following configuration file to provide connection and other types of information
      <?xml version='1.0' encoding='windows-1252'?>
      <Connection>
        <Driver>OCI</Driver>
        <Hostname>localhost</Hostname>
        <Port>1521</Port>
        <ServiceName>ORA10GR2.xp.mark.drake.oracle.com</ServiceName>
        <SID>ORA10GR2</SID>
        <ServerMode>DEDICATED</ServerMode>
        <Schema>SCOTT</Schema>
        <Password>TIGER</Password>
        <TargetTable>PURCHASEORDER</TargetTable>
      </Connection>
      
      C:\xdb\JDeveloper\SimpleExamples>