Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

TAbstractConnectionManager.java

00001 /*
00002 *  Name:      TAbstractConnectionManager.java
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Class for handling connections in JAVA.
00005 *  Date:      $Date: 2003/10/06 13:21:36 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 2002-2003  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00027 package org.uesqlc;
00028 
00029 import java.sql.Connection;
00030 import java.sql.ResultSet;
00031 import java.sql.SQLException;
00032 import java.sql.Statement;
00033 import java.util.Enumeration;
00034 import java.util.Hashtable;
00035 import java.util.Vector;
00036 import java.util.logging.Level;
00037 import java.util.logging.Logger;
00038 
00039 
00041 public abstract class TAbstractConnectionManager
00042 {
00043 
00045   protected Hashtable   tConnectionDataHashtable;
00046 
00048   protected TConnectionData   tCurrentConnectionData;
00049 
00051   protected Vector   tInputEmbeddedVariableVector;
00052 
00054   protected Vector   tOutputEmbeddedVariableVector;
00055 
00057   protected String   yCurrentConnection;
00058 
00060   public Logger   tLogger;
00061 
00062 
00063   //
00064   //  C O N S T R U C T O R S
00065   //
00066 
00071   public TAbstractConnectionManager()
00072   {
00073 
00074     tConnectionDataHashtable      = new Hashtable();
00075     tCurrentConnectionData        = null;
00076     tInputEmbeddedVariableVector  = new Vector();
00077     tOutputEmbeddedVariableVector = new Vector();
00078     yCurrentConnection            = null;
00079     tLogger                       = Logger.getLogger ("org.uesqlc");
00080     if ( tLogger.getLevel() == null )
00081     {
00082       //
00083       //  Deactivates logging if it has not been set externally.
00084       //
00085       tLogger.setLevel (Level.OFF);
00086     }
00087     initialize();
00088 
00089   }  // TAbstractConnectionManager()
00090 
00092   public void terminate() throws SQLException
00093   {
00094     finish();
00095   }
00096 
00103   public void bindConnection (final String kyCONNECTION_NAME, TConnectionData tCONNECTION_DATA)
00104     throws SQLException
00105   {
00106     tConnectionDataHashtable.put (kyCONNECTION_NAME, tCONNECTION_DATA);
00107     tCONNECTION_DATA.getConnection().setAutoCommit (false);
00108   }
00109 
00116   public void bindCursor (final String kyCURSOR_NAME, ResultSet tRESULT_SET)
00117   {
00118     tCurrentConnectionData.bind (kyCURSOR_NAME, tRESULT_SET);
00119   }
00120 
00125   public void commitCurrent()
00126   {
00127 
00128     try
00129     {
00130       tCurrentConnectionData.getConnection().commit();
00131     }
00132     catch (final SQLException ktEXCEPTION)
00133     {
00134       //
00135       //  Condition:           invalid transaction termination
00136       //  Class subcondition:  no subclass
00137       //
00138       TState.SQLCODE    = -1;
00139       TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00140       TState.SQLSTATE   = TState.invalid_transaction_termination._kyNO_SUBCLASS;
00141     }
00142 
00143   }  // commitCurrent()
00144 
00146   public void connect (final String kyCONNECTION_NAME)
00147   {
00148     if ( tCurrentConnectionData != null )
00149     {
00150       connect ( kyCONNECTION_NAME                    ,
00151                 tCurrentConnectionData.yHostName     ,
00152                 tCurrentConnectionData.ySchemaName   ,
00153                 tCurrentConnectionData.yUserName     ,
00154                 tCurrentConnectionData.yUserPassword );
00155     }
00156     else
00157     {
00158       //
00159       //  Condition:           connection exception
00160       //  Class subcondition:  SQL-client unable to establish SQL-connection
00161       //
00162       TState.SQLCODE    = -1;
00163       TState.SQLSTATE   = TState.connection_exception._kySQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION;
00164       TState.SQLMESSAGE = TState._getDescription();
00165     }
00166 
00167   }  // connect()
00168 
00174   public void connect (final String kyCONNECTION_NAME, final String kySCHEMA_NAME)
00175   {
00176 
00177     if ( tCurrentConnectionData != null )
00178     {
00179       connect ( kyCONNECTION_NAME                    ,
00180                 tCurrentConnectionData.yHostName     ,
00181                 kySCHEMA_NAME                        ,
00182                 tCurrentConnectionData.yUserName     ,
00183                 tCurrentConnectionData.yUserPassword );
00184     }
00185     else
00186     {
00187       //
00188       //  Condition:           connection exception
00189       //  Class subcondition:  SQL-client unable to establish SQL-connection
00190       //
00191       TState.SQLCODE    = -1;
00192       TState.SQLSTATE   = TState.connection_exception._kySQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION;
00193       TState.SQLMESSAGE = TState._getDescription();
00194     }
00195 
00196   }  // connect()
00197 
00205   public void connect ( final String kyCONNECTION_NAME ,
00206                         final String kyHOST_NAME       ,
00207                         final String kySCHEMA_NAME     ,
00208                         final String kyUSER_LOGIN      )
00209   {
00210 
00211     int      iBarIndex;
00212     String   yUserName;
00213     String   yUserPassword = "";
00214 
00215     iBarIndex = kyUSER_LOGIN.indexOf ('/');
00216     if ( iBarIndex == -1 )
00217     {
00218       yUserName = kyUSER_LOGIN.replaceAll ("'", "");
00219     }
00220     else
00221     {
00222       yUserName     = kyUSER_LOGIN.substring (0, iBarIndex).replaceAll ("'", "");
00223       yUserPassword = kyUSER_LOGIN.substring (iBarIndex + 1).replaceAll ("'", "");
00224     }
00225     connect ( kyCONNECTION_NAME ,
00226               kyHOST_NAME       ,
00227               kySCHEMA_NAME     ,
00228               yUserName         ,
00229               yUserPassword     );
00230 
00231   }  // connect()
00232 
00241   public abstract void connect ( final String kyCONNECTION_NAME ,
00242                                  final String kyHOST_NAME       ,
00243                                  final String kySCHEMA_NAME     ,
00244                                  final String kyUSER_NAME       ,
00245                                  final String kyUSER_PASSWORD   );
00246 
00252   public void rollbackCurrent()
00253   {
00254 
00255     try
00256     {
00257       tCurrentConnectionData.getConnection().rollback();
00258     }
00259     catch (final SQLException ktEXCEPTION)
00260     {
00261       //
00262       //  Condition:           invalid transaction termination
00263       //  Class subcondition:  no subclass
00264       //
00265       TState.SQLCODE    = -1;
00266       TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00267       TState.SQLSTATE   = TState.invalid_transaction_termination._kyNO_SUBCLASS;
00268     }
00269 
00270   }  // rollbackCurrent()
00271 
00273   public void unbindAllConnections()
00274   {
00275     Enumeration   tElementEnumeration = tConnectionDataHashtable.keys();
00276 
00277     while ( tElementEnumeration.hasMoreElements() )
00278     {
00279       unbindConnection ((String) tElementEnumeration.nextElement());
00280     }
00281   }
00282 
00289   public void unbindConnection (final String kyCONNECTION_NAME)
00290   {
00291 
00292     TConnectionData   tConnectionData = (TConnectionData) tConnectionDataHashtable.get (kyCONNECTION_NAME);
00293     Connection        tConnection     = tConnectionData.getConnection();
00294 
00295     tConnectionDataHashtable.remove (kyCONNECTION_NAME);
00296     try
00297     {
00298       tConnection.commit();
00299       tConnection.close();
00300     }
00301     catch (final SQLException ktEXCEPTION)
00302     {
00303       //
00304       //  Condition:           connection exception
00305       //  Class subcondition:  connection failure
00306       //
00307       TState.SQLCODE    = -1;
00308       TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00309       TState.SQLSTATE   = TState.connection_exception._kyCONNECTION_FAILURE;
00310     }
00311     if ( kyCONNECTION_NAME == yCurrentConnection )
00312     {
00313       yCurrentConnection = null;
00314     }
00315 
00316   }  // unbindConnection()
00317 
00319   public void unbindCurrentConnection()
00320   {
00321     if ( isConnected() )
00322     {
00323       unbindConnection (yCurrentConnection);
00324     }
00325   }
00326 
00331   public void unbindCursor (final String kyCURSOR_NAME)
00332   {
00333     tCurrentConnectionData.unbind (kyCURSOR_NAME);
00334   }
00335 
00340   public void clearQueues()
00341   {
00342     tInputEmbeddedVariableVector.clear();
00343     tOutputEmbeddedVariableVector.clear();
00344   }
00345 
00352   public ResultSet execute (final String kySTATEMENT)
00353   {
00354 
00355     String      yInstantiatedStatement;
00356     Statement   tStatement = null;
00357     ResultSet   tResultSet = null;
00358 
00359     resetStatus();
00360     try
00361     {
00362       yInstantiatedStatement = instantiate (kySTATEMENT);
00363       tStatement             = createStatement();
00364 // <DEBUG>
00365       tLogger.info (yInstantiatedStatement);
00366 // </DEBUG>
00367       if ( tStatement.execute (yInstantiatedStatement) )
00368       {
00369         tResultSet = tStatement.getResultSet();
00370       }
00371     }
00372     catch (final SQLException ktEXCEPTION)
00373     {
00374       //
00375       //  Condition:           transaction rollback
00376       //  Class subcondition:  statement completion unknown
00377       //
00378       TState.SQLCODE    = -1;
00379       TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00380       TState.SQLSTATE   = TState.transaction_rollback._kySTATEMENT_COMPLETION_UNKNOWN;
00381       tResultSet        = null;
00382       try
00383       {
00384         rollbackCurrent();
00385         tStatement.close();
00386       }
00387       catch (final SQLException ktINNER_EXCEPTION) {}
00388     }
00389     if ( TState.SQLCODE == 0 )
00390     {
00391       if ( tResultSet == null )
00392       {
00393         // 
00394         //  Condition:  no data
00395         // 
00396         TState.SQLCODE    = 101;
00397         TState.SQLSTATE   = TState.no_data._kyNO_SUBCLASS;
00398         TState.SQLMESSAGE = TState._getDescription();
00399         try
00400         {
00401           tStatement.close();
00402         }
00403         catch (final SQLException ktEXCEPTION) {}
00404       }
00405     }
00406     return tResultSet;
00407 
00408   }  // execute()
00409 
00416   public void executeClean (final String kySTATEMENT)
00417   {
00418 
00419     String      yInstantiatedStatement;
00420     Statement   tStatement = null;
00421 
00422     resetStatus();
00423     try
00424     {
00425       yInstantiatedStatement = instantiate (kySTATEMENT);
00426       tStatement             = createStatement();
00427 // <DEBUG>
00428       tLogger.info (yInstantiatedStatement);
00429 // </DEBUG>
00430       tStatement.execute (yInstantiatedStatement);
00431       tStatement.close();
00432     }
00433     catch (final SQLException ktEXCEPTION)
00434     {
00435       try
00436       {
00437         tStatement.close();
00438       }
00439       catch (final SQLException ktINNER_EXCEPTION) {}
00440     }
00441 
00442   }  // executeClean()
00443 
00451   public ResultSet executeSingleRow (final String kySTATEMENT)
00452   {
00453 
00454     String      yInstantiatedStatement;
00455     Statement   tStatement = null;
00456     ResultSet   tResultSet = null;
00457 
00458     resetStatus();
00459     try
00460     {
00461       yInstantiatedStatement = instantiate (kySTATEMENT);
00462       tStatement             = createStatement();
00463 // <DEBUG>
00464       tLogger.info (yInstantiatedStatement);
00465 // </DEBUG>
00466       tResultSet = tStatement.executeQuery (yInstantiatedStatement);
00467     }
00468     catch (final SQLException ktEXCEPTION)
00469     {
00470       //
00471       //  Condition:           transaction rollback
00472       //  Class subcondition:  statement completion unknown
00473       //
00474       TState.SQLCODE    = -1;
00475       TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00476       TState.SQLSTATE   = TState.transaction_rollback._kySTATEMENT_COMPLETION_UNKNOWN;
00477       tResultSet        = null;
00478       try
00479       {
00480         rollbackCurrent();
00481         tStatement.close();
00482       }
00483       catch (final SQLException ktINNER_EXCEPTION) {}
00484     }
00485     if ( TState.SQLCODE == 0 )
00486     {
00487       try
00488       {
00489         if ( !tResultSet.next() )
00490         {
00491           // 
00492           //  Condition:  no data
00493           // 
00494           TState.SQLCODE    = 101;
00495           TState.SQLSTATE   = TState.no_data._kyNO_SUBCLASS;
00496           TState.SQLMESSAGE = TState._getDescription();
00497           tResultSet        = null;
00498         }
00499         else
00500         {
00501           if ( !tResultSet.next() )
00502           {
00503             //
00504             //  There is only one row, so everything is all right.
00505             //
00506             tResultSet.beforeFirst();
00507           }
00508           else
00509           {
00510             // 
00511             //  Condition:  cardinality violation
00512             // 
00513             TState.SQLCODE    = -1;
00514             TState.SQLSTATE   = TState.cardinality_violation._kyNO_SUBCLASS;
00515             TState.SQLMESSAGE = TState._getDescription();
00516             tResultSet        = null;
00517             try
00518             {
00519               rollbackCurrent();
00520               tStatement.close();
00521             }
00522             catch (final SQLException ktINNER_EXCEPTION) {}
00523           }
00524         }
00525       }
00526       catch (final SQLException ktEXCEPTION)
00527       {
00528         //
00529         //  Condition:           invalid cursor state
00530         //  Class subcondition:  no subclass
00531         //
00532         TState.SQLCODE    = -1;
00533         TState.SQLMESSAGE = ktEXCEPTION.getMessage();
00534         TState.SQLSTATE   = TState.invalid_cursor_state._kyNO_SUBCLASS;
00535         tResultSet        = null;
00536         try
00537         {
00538           rollbackCurrent();
00539           tStatement.close();
00540         }
00541         catch (final SQLException ktINNER_EXCEPTION) {}
00542       }
00543     }
00544     return tResultSet;
00545 
00546   }  // executeSingleRow()
00547 
00549   public void finish() throws SQLException
00550   {
00551     unbindAllConnections();
00552     clearQueues();
00553     tConnectionDataHashtable.clear();
00554     yCurrentConnection = null;
00555   }
00556 
00564   public IType getFromOutputQueue (final int kiINDEX)
00565   {
00566     return (IType) tOutputEmbeddedVariableVector.get (kiINDEX);
00567   }
00568 
00570   public void initialize()
00571   {
00572     connect ("DEFAULT");
00573     if ( !isSqlerrorCondition() )
00574     {
00575       setCurrentConnection ("DEFAULT");
00576     }
00577   }
00578 
00585   public String instantiate (final String kySTATEMENT)
00586   {
00587 
00588     int            iPatternIndex;
00589     IType          tType;
00590     StringBuffer   yStatementInstance = new StringBuffer (kySTATEMENT);
00591 
00592     while ( !tInputEmbeddedVariableVector.isEmpty() )
00593     {
00594       iPatternIndex = yStatementInstance.indexOf ("$z");
00595       if ( -1 == iPatternIndex )
00596       {
00597         break;
00598       }
00599       else
00600       {
00601         tType = (IType) tInputEmbeddedVariableVector.firstElement();
00602         yStatementInstance.replace (iPatternIndex, iPatternIndex + 2, tType.get());
00603         tInputEmbeddedVariableVector.remove (0);
00604       }
00605     }
00606     return yStatementInstance.toString();
00607 
00608   }  // instantiate()
00609 
00615   public IType popFromOutputQueue()
00616   {
00617     return (IType) tOutputEmbeddedVariableVector.remove (tOutputEmbeddedVariableVector.size() - 1);
00618   }
00619 
00625   public void pushOnInputQueue (final IType ktVALUE)
00626   {
00627     tInputEmbeddedVariableVector.add (ktVALUE);
00628   }
00629 
00635   public void pushOnOutputQueue (IType tVARIABLE)
00636   {
00637     tOutputEmbeddedVariableVector.add (tVARIABLE);
00638   }
00639 
00641   public void resetStatus()
00642   {
00643     //
00644     //  Condition:  successful completion
00645     //
00646     TState.SQLCODE    = 0;
00647     TState.SQLSTATE   = TState.successful_completion._kyNO_SUBCLASS;
00648     TState.SQLMESSAGE = TState._getDescription();
00649   }
00650 
00655   public void setCurrentConnection (final String kyCONNECTION_NAME)
00656   {
00657 
00658     if ( tConnectionDataHashtable.containsKey (kyCONNECTION_NAME) )
00659     {
00660       yCurrentConnection     = kyCONNECTION_NAME;
00661       tCurrentConnectionData = (TConnectionData) tConnectionDataHashtable.get (kyCONNECTION_NAME);
00662 // <DEBUG>
00663       tLogger.info ("SET CONNECTION '" + kyCONNECTION_NAME + "'");
00664 // </DEBUG>
00665       resetStatus();
00666     }
00667     else
00668     {
00669       //
00670       //  Condition:           connection exception
00671       //  Class subcondition:  connection does not exist
00672       //
00673       TState.SQLCODE    = -1;
00674       TState.SQLSTATE   = TState.connection_exception._kyCONNECTION_DOES_NOT_EXIST;
00675       TState.SQLMESSAGE = TState._getDescription();
00676     }
00677       
00678   }  // setCurrentConnection()
00679 
00680 
00681   //
00682   //  S E L E C T O R S
00683   //
00684 
00689   protected Statement createStatement() throws SQLException
00690   {
00691     return tCurrentConnectionData.getConnection().createStatement();
00692   }
00693 
00698   public final String currentConnectionName()
00699   {
00700     return yCurrentConnection;
00701   }
00702 
00708   public ResultSet getCursor (final String kyCURSOR_NAME)
00709   {
00710     return tCurrentConnectionData.getCursor (kyCURSOR_NAME);
00711   }
00712 
00717   public boolean isConnected()
00718   {
00719     return ( yCurrentConnection != null );
00720   }
00721 
00727   public boolean isConnected (final String kyCONNECTION_NAME)
00728   {
00729     return tConnectionDataHashtable.containsKey (kyCONNECTION_NAME);
00730   }
00731 
00736   public boolean isNotFoundCondition()
00737   {
00738     return ( TState.SQLSTATE == TState.no_data._kyNO_SUBCLASS );
00739   }
00740 
00745   public boolean isSqlerrorCondition()
00746   {
00747     return ( TState.SQLCODE < 0 );
00748   }
00749 
00750 }  // class TAbstractConnectionManager

Generated on Mon Oct 13 02:40:10 2003 for UESQLC by doxygen1.2.18