KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > callflow > AbstractTableAccessObject


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * AbstractTableAccessObject.java
26  *
27  * Created on July 13, 2005, 6:19 PM
28  */

29
30 package com.sun.enterprise.admin.monitor.callflow;
31
32 import java.sql.BatchUpdateException JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import com.sun.enterprise.server.ApplicationServer;
38 import com.sun.enterprise.server.ServerContext;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import com.sun.enterprise.admin.common.constant.AdminConstants;
42 /**
43  *
44  * @author Harpreet Singh
45  */

46 public abstract class AbstractTableAccessObject implements TableAccessObject{
47     
48     private static final Logger JavaDoc logger =
49         Logger.getLogger(AdminConstants.kLoggerName);
50     /*
51      * SQL 99 error code for a table that already exists
52      */

53     private static final String JavaDoc TABLE_EXISTS_SQL_ERROR_CODE = "X0Y32";
54     
55     /*
56      * An equivalent XOPEN SQL State for table already exists should be present
57      * but could not be found.
58      */

59     
60     Connection JavaDoc con = null;
61     /* Holds the table name specific to this server instance.
62      * For e.g. REQUEST_START_TBL on a server instance with name "foo" will be
63      * REQUEST_START_TBL__FOO.
64      */

65     String JavaDoc tableName = null;
66     private static final String JavaDoc DEFAULT_SERVER_NAME = "server";
67     
68     abstract public boolean createTable(Connection JavaDoc connection);
69     abstract public boolean dropTable(Connection JavaDoc connection);
70    
71    boolean createStatmentAndExecuteUpdate(String JavaDoc oldsql,
72            String JavaDoc tableNameWithoutServerInstance){
73        
74         String JavaDoc sql = updateSqlWithTableName (oldsql, tableNameWithoutServerInstance);
75         boolean result = false;
76         Statement JavaDoc stmt = null;
77         try{
78             if (con != null){
79                 stmt = con.createStatement();
80                 stmt.executeUpdate(sql);
81                 result = true;
82             }
83         } catch (java.sql.SQLException JavaDoc se) {
84             // log it
85
logger.log(Level.WARNING, "Error accessing CallFlow tables!", se);
86             result = false;
87         } finally {
88             if(stmt != null){
89                 try{
90                     stmt.close();
91                 }catch(java.sql.SQLException JavaDoc s){
92                     // log it
93
}
94             }
95             stmt = null;
96         }
97         return result;
98    }
99   
100    /**
101     * This method is used to create a database table. If the table already
102     * exists, it logs a message and returns successfully.
103     * As there is no mechanism to actually test if the database exists, it creates
104     * the table and if there is an exception, it assumes it is due to table
105     * being present.
106     */

107    boolean createTable(String JavaDoc oldsql,
108            String JavaDoc tableNameWithoutServerInstance){
109        
110         String JavaDoc sql = updateSqlWithTableName (oldsql, tableNameWithoutServerInstance);
111         boolean result = false;
112         Statement JavaDoc stmt = null;
113         try{
114             if (con != null){
115                 stmt = con.createStatement();
116                 stmt.executeUpdate(sql);
117                 result = true;
118             }
119         } catch (java.sql.SQLException JavaDoc se) {
120             // log it
121
if (se.getSQLState().equalsIgnoreCase (TABLE_EXISTS_SQL_ERROR_CODE)){
122                 logger.log (Level.FINE, "callflow.table_already_exists_error",
123                         tableNameWithoutServerInstance);
124             } else {
125                 logger.log(Level.WARNING, "callflow.table_creation_error",
126                         tableNameWithoutServerInstance);
127                 logger.log(Level.WARNING, "callflow.table_creation_error", se);
128             }
129             result = true;
130         } finally {
131             if(stmt != null){
132                 try{
133                     stmt.close();
134                 }catch(java.sql.SQLException JavaDoc s){
135                     // log it
136
}
137             }
138             stmt = null;
139         }
140         return result;
141    }
142    public String JavaDoc getServerInstanceName () {
143        // get the server name from config
144
String JavaDoc server = DEFAULT_SERVER_NAME;
145        ServerContext sc = ApplicationServer.getServerContext();
146         if (sc != null) {
147            server = sc.getInstanceName ();
148         }
149         return "__" + server;
150    }
151  
152    /**
153     * Adds the server instance name to the table names in the SQL statements
154     * for create/delete and insert. All creates and deletes need to call them
155     * before they submit the query to be executed
156     * @param String complete sql that has the table name without the server instance
157     * name
158     * @param String Name of the table, this table name will be appended by
159     * a "__" and server name
160     */

161    String JavaDoc updateSqlWithTableName (String JavaDoc oldsql, String JavaDoc table) {
162        String JavaDoc newsql = new String JavaDoc(oldsql);
163        newsql = newsql.replaceAll(table, tableName);
164        
165         return newsql;
166    }
167  
168    public boolean delete(PreparedStatement JavaDoc pstmt, String JavaDoc[] requestId) {
169         if (pstmt == null)
170             return false;
171         
172         boolean result = false;
173         try{
174             for (int i = 0 ; i<requestId.length; i++) {
175                 pstmt.setString(1, requestId[i]);
176                 pstmt.addBatch();
177             }
178             int[] updated = pstmt.executeBatch();
179             result = (updated.length == requestId.length)? true : false;
180             if (result == false){
181                 logger.log (Level.WARNING, "callflow.error_delete_row");
182             }
183         } catch(BatchUpdateException JavaDoc bue) {
184             // log it
185
logger.log (Level.WARNING, "callflow.error_delete_row");
186             logger.log(Level.FINE, "Error data into CallFlow tables", bue);
187             result = false;
188         }catch (SQLException JavaDoc se) {
189             // log it
190
logger.log (Level.WARNING, "callflow.error_delete_row");
191             logger.log(Level.FINE, "Error inserting data into CallFlow tables", se);
192             result = false;
193         }
194         return result;
195         
196     }
197 }
198
Popular Tags