View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.util.genelv.exceptions;
19  
20  import javax.naming.NamingException;
21  import javax.ejb.CreateException;
22  import javax.ejb.FinderException;
23  import java.sql.*;
24  import java.io.*;
25  import java.rmi.RemoteException;
26  
27  import org.apache.log4j.Category;
28  
29  import com.finalist.util.genelv.db.GenElvConnectionManager;
30  import com.finalist.util.genelv.db.ConnectionManager;
31  
32  /***
33   * Util class for exception handling.
34   */
35  public class ExceptionUtil {
36     static Category log = Category.getInstance(ExceptionUtil.class);
37     static String QUERY_GET_ERROR_CODE = "SELECT errorcode FROM error_codes WHERE exceptiontype=?";
38     static String QUERY_GET_ALL = "SELECT * FROM error_codes";
39     static String QUERY_EXISTS = "SELECT COUNT(*) nr FROM error_codes WHERE exceptiontype=?";
40     static String INSERT_ALL = "INSERT INTO error_codes (id, errorcode, exceptiontype) VALUES (?, ?, ?)";
41  
42  
43     /*** Return a string version of the stack trace if passed throwable is not null. */
44     static public String getStackTrace(Throwable t) {
45        if (t == null)
46           return "::No stack trace available: exception is null";
47        else {
48           StringWriter sw = new StringWriter();
49           PrintWriter pw = new PrintWriter(sw);
50           t.printStackTrace(pw);
51           pw.flush();
52           return sw.toString();
53        }
54     }
55  
56  
57     /*** Determine the error code using the genelv table in the database. */
58     static public String determineErrorCodeDb(Throwable t) throws SQLException {
59  
60        String theErrorCode = "genelv.unspecified";
61        Connection con = GenElvConnectionManager.connect();
62        PreparedStatement prepStat = con.prepareStatement(QUERY_GET_ERROR_CODE);
63        prepStat.setString(1, t.getClass().getName());
64        System.out.println("t.getClass().getName(): " + t.getClass().getName());
65        ResultSet rs = prepStat.executeQuery();
66        if (rs.next()) {
67           theErrorCode = rs.getString("errorcode");
68        }
69        else
70           System.out.println("No records found");
71        return theErrorCode;
72     }
73  
74  
75     /*** Determine the error code on the basis of the type of the exception */
76     static public String determineErrorCodeType(Throwable t) {
77  
78        if (t == null) {
79           return WrappedEJBException.UNSPECIFIED;
80        }
81        if (t instanceof NamingException) {
82           return WrappedEJBException.EJB_JNDI;
83        }
84        else if (t instanceof SQLException) {
85           return WrappedEJBException.JDBC;
86        }
87        else if (t instanceof CreateException) {
88           return WrappedEJBException.EJB_CREATE;
89        }
90        else if (t instanceof FinderException) {
91           return WrappedEJBException.EJB_FINDER;
92        }
93        else if (t instanceof RemoteException) {
94           return WrappedEJBException.REMOTE;
95        }
96        else {
97           return WrappedEJBException.UNSPECIFIED;
98        }
99     }
100 
101 
102    /*** Determines the error key for the given <code>Throwable</code> */
103    static public String determineErrorCode(Throwable t) {
104       String theErrorCode;
105       theErrorCode = determineErrorCodeType(t);
106 // No DB support ...
107       /***
108        try {
109        theErrorCode = determineErrorCodeDb(t);
110        }
111        catch (SQLException ex) {
112        //log.debug("SQLEXception: ", ex);
113        ex.printStackTrace();
114        theErrorCode = determineErrorCodeType(t);
115        }
116        */
117       return theErrorCode;
118    }
119 
120 
121    /*** Export error codes from the database to be connected using <code>com.finalist.util.db.GenElvConnectionManager</code>
122     *  to the database to connected using <code>com.finalist.util.db.ConnectionManager</code>.
123     *  Returns <code>true</code> if all was ok, other <code>false</code>
124     */
125    public static boolean exportErrorCodesToProject() {
126       try {
127          System.out.println("exportErrorCodesToProject()");
128          Connection genelv_con = GenElvConnectionManager.connect();
129          PreparedStatement getPrepStat = genelv_con.prepareStatement(QUERY_GET_ALL);
130          // connect to the other (project) database
131          Connection proj_con = ConnectionManager.connect();
132          PreparedStatement insertPrepStat = proj_con.prepareStatement(INSERT_ALL);
133          // Execute the get query
134          ResultSet errors = getPrepStat.executeQuery();
135          while (errors.next()) {
136             System.out.print("::");
137             insertPrepStat.setInt(1, errors.getInt("id"));
138             insertPrepStat.setString(2, errors.getString("errorcode"));
139             insertPrepStat.setString(3, errors.getString("exceptiontype"));
140             insertPrepStat.executeQuery();
141          }
142       }
143       catch (SQLException e) {
144          //log.error("SQLEXCEPTION DURING EXPORT: ", e);
145          e.printStackTrace();
146          return false;
147       }
148       return true;
149    }
150 
151 
152    /*** Import error codes from the database to be connected using <code>com.finalist.util.db.ConnectionManager</code>
153     *   to the database to connected using <code>com.finalist.util.db.GenElvConnectionManager</code>.
154     * Returns true if all was ok, otherwise false
155     */
156    public static boolean importErrorCodesFromProject() {
157       Connection genelv_con = null, proj_con = null;
158       try {
159          System.out.println("importErrorCodesFromProject()");
160          // FROM PROJECT
161          proj_con = ConnectionManager.connect();
162          PreparedStatement getPrepStat = proj_con.prepareStatement(QUERY_GET_ALL);
163          //
164          ResultSet errors = getPrepStat.executeQuery();
165          // TO GEN ELV DATABASE
166          genelv_con = GenElvConnectionManager.connect();
167          PreparedStatement checkPrepStat = genelv_con.prepareStatement(QUERY_EXISTS);
168          PreparedStatement insertPrepStat = genelv_con.prepareStatement(INSERT_ALL);
169          int i = 0;
170          while (errors.next()) {
171             System.out.println("Stepstone: " + ++i);
172             int id = errors.getInt("id");
173             String errorCode = errors.getString("errorcode");
174             String exceptionType = errors.getString("exceptiontype");
175             checkPrepStat.setString(1, exceptionType);
176             System.out.println("exceptionType: " + exceptionType);
177             ResultSet ers = checkPrepStat.executeQuery();
178             if (ers.next()) {
179                int n = ers.getInt("nr");
180                //String theFoundException = ers.getString("exceptiontype");
181                if (n == 0) {
182                   System.out.println("Adding: " + errorCode + " " + exceptionType);
183                   insertPrepStat.setInt(1, id);
184                   insertPrepStat.setString(2, errorCode);
185                   insertPrepStat.setString(3, exceptionType);
186                   insertPrepStat.executeQuery();
187                }
188                else {
189                   System.out.println("Already there: " + exceptionType + " for " + n); // + " IN " + theFoundException);
190                }
191             }
192          }
193       }
194       catch (SQLException e) {
195          //log.error("SQLEXCEPTION DURING EXPORT: ", e);
196          e.printStackTrace();
197          return false;
198       }
199       finally {
200          try {
201             if (genelv_con != null)
202                genelv_con.close();
203             if (proj_con != null)
204                proj_con.close();
205          }
206          catch (SQLException e) {
207          }
208       }
209       return true;
210    }
211 
212 
213    /*** test main*/
214    public static void main(String[] args) {
215       if (args.length == 1) {
216          if (args[0].equals("export")) {
217             exportErrorCodesToProject();
218          }
219          if (args[0].equals("import")) {
220             importErrorCodesFromProject();
221          }
222          if (args[0].equals("test")) {
223             Exception e = new NullPointerException();
224             String code = determineErrorCode(e);
225             System.out.println("Type: " + e.getClass().getName() + " code: " + code);
226          }
227       }
228       else {
229          System.err.println("USAGE: 1 argument test/import/export");
230       }
231    }
232 }
233