KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Utils.java
22  *
23  * Created on November 20, 2006, 7:24 PM
24  *
25  */

26
27 package org.netbeans.modules.exceptions;
28
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.DriverManager JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Statement JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.LinkedList JavaDoc;
42 import java.util.logging.Formatter JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.LogRecord JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46 import javax.servlet.ServletContextEvent JavaDoc;
47
48 /**
49  *
50  * @author jindra
51  */

52 public class Utils implements javax.servlet.ServletContextListener JavaDoc{
53     public static final String JavaDoc PARAM_SHOW_LINE = "LINE"; // NOI18N
54
public static final String JavaDoc PARAM_SHOW_COMMENT = "COMMENT"; // NOI18N
55
public static final String JavaDoc PARAM_SHOW_ISSUENBUSER = "ISSUENBUSER"; // NOI18N
56
public static final String JavaDoc PARAM_SHOW_ISSUE = "ISSUE"; // NOI18N
57
public static final String JavaDoc PARAM_SHOW_STACKTRACE = "STACKTRACE"; // NOI18N
58
public static final String JavaDoc PARAM_SHOW_METHOD = "METHOD"; // NOI18N
59
public static final String JavaDoc PARAM_SHOW_NBFILES = "NBFILES"; // NOI18N
60
public static final String JavaDoc PARAM_SHOW_NBUSER = "NBUSER"; // NOI18N
61
public static final String JavaDoc PARAM_SHOW_LOGGER = "LOGGER"; // NOI18N
62

63     public static final String JavaDoc SELECT_ISSUE = "select * from ISSUE where Id = "; // NOI18N
64
public static final String JavaDoc SELECT_LOGGER = "select * from LOGGER where Id = "; // NOI18N
65
public static final String JavaDoc SELECT_USER = "select reportDate, Name from IssueNbUser, NbUser where NbUser_Id = id and Issue_id = "; // NOI18N
66
public static final String JavaDoc SELECT_STACKTRACE = "select Message, Class from StackTrace where id = "; // NOI18N
67
public static final String JavaDoc SELECT_LINES = "select M.Name, F.Name, LineNumber from Line as L, Method as M, NbFiles as F where Method_Id = M.Id and F.Id = Files_Id and L.StackTrace_id = "; // NOI18N
68
public static final String JavaDoc SELECT_COMMENT = "select Name,Comment from NbUser as A, Comment as B where NbUser_id = A.Id and Issue_id = "; // NOI18N
69
public static final String JavaDoc SELECT_MAX_ID = "SELECT max(Id) FROM {0}"; // NOI18N
70
public static final String JavaDoc SELECT_EXIST_ID = "SELECT Id FROM {0} WHERE Name = ''{1}''"; // NOI18N
71
public static final String JavaDoc SELECT_COMPONENTS = "SELECT DISTINCT COMPONENT FROM COMPONENTS"; // NOI18N
72
public static final String JavaDoc SELECT_SUBCOMPONENTS = "SELECT SUBCOMPONENT FROM COMPONENTS WHERE COMPONENT = "; // NOI18N
73

74     public static final String JavaDoc SELECT_HASH = "SELECT * FROM HashCodes WHERE CODE = "; //NOI18N
75
public static final String JavaDoc SELECT_HASH_BY_ISSUE = "SELECT * FROM HashCodes WHERE ISSUEID = "; //NOI18N
76
public static final String JavaDoc INSERT_HASH = "INSERT INTO HashCodes VALUES(?, ?)"; //NOI18N
77

78     public static final String JavaDoc INSERT_NBUSER = "INSERT INTO NbUser VALUES(?, ?)"; // NOI18N
79
public static final String JavaDoc INSERT_STACKTRACE = "INSERT INTO StackTrace VALUES(?, ?, ?, ?)"; // NOI18N
80
public static final String JavaDoc INSERT_ISSUE = "INSERT INTO Issue VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // NOI18N
81
public static final String JavaDoc INSERT_COMMENT = "INSERT INTO Comment VALUES(?, ?, ?, ?)"; // NOI18N
82
public static final String JavaDoc INSERT_ISSUENBUSER = "INSERT INTO IssueNbUser VALUES(?, ?, ?)"; // NOI18N
83
public static final String JavaDoc INSERT_METHOD = "INSERT INTO Method VALUES(?, ?)"; // NOI18N
84
public static final String JavaDoc INSERT_NBFILES = "INSERT INTO NbFiles VALUES(?, ?)"; // NOI18N
85
public static final String JavaDoc INSERT_LINE = "INSERT INTO Line VALUES(?, ?, ?, ?, ?)"; // NOI18N
86
public static final String JavaDoc INSERT_LOGGER = "INSERT INTO Logger VALUES(?, ?)"; // NOI18N
87
public static final String JavaDoc INSERT_COMPONENTS ="INSERT INTO Components VALUES(?, ?)"; // NOI18N
88

89     private static Connection JavaDoc connection;
90     private static Logger JavaDoc logger = java.util.logging.Logger.getLogger("org.netbeans.modules.exceptions"); // NOI18N
91
private static java.util.HashMap JavaDoc<java.lang.String JavaDoc,java.lang.String JavaDoc[]> components = null;
92     
93     private static String JavaDoc DATABASE_URL = "jdbc:derby://localhost:1527/exceptions;create=false"; // NOI18N
94
private static String JavaDoc ISSUEZILLA_URL = "jdbc:mysql://qa-amd64-linux:3306/issuezilla"; // NOI18N
95

96     /** Creates a new instance of Utils */
97     public Utils() {
98     }
99     
100     
101     public static Connection JavaDoc getConnection(){
102         try {
103             if(connection.isClosed()) connection = setConnection(connection);
104         } catch (Exception JavaDoc ex) {
105             logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex);
106         }
107         return connection;
108     }
109     
110     public static String JavaDoc getHTMLLog(ResultSet JavaDoc result) throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc{
111         Formatter JavaDoc logFormater = new HTMLFormatter();
112         String JavaDoc loggerText = "";
113         InputStream JavaDoc stream = result.getBinaryStream(2);
114         ObjectInputStream JavaDoc objectIstream = new ObjectInputStream JavaDoc(stream);
115         Object JavaDoc o = objectIstream.readObject();
116         if (o instanceof LinkedList JavaDoc){
117             Iterator JavaDoc it = ((LinkedList JavaDoc) o).iterator();
118             while (it.hasNext()){
119                 Object JavaDoc oElement = it.next();
120                 if (oElement instanceof LogRecord JavaDoc){
121                     loggerText = loggerText.concat(logFormater.format((LogRecord JavaDoc)oElement)+"</br>");
122                 }
123             }
124             
125         }
126         return loggerText;
127     }
128     
129     public static HashMap JavaDoc<String JavaDoc, String JavaDoc[]> getComponents() throws SQLException JavaDoc{
130         if (components == null){
131             components = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
132             Statement JavaDoc stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
133             ResultSet JavaDoc result = stmt.executeQuery(Utils.SELECT_COMPONENTS);
134             Statement JavaDoc stmtSub = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
135             ResultSet JavaDoc resultSub;
136             String JavaDoc componentName;
137             String JavaDoc subComponentName;
138             ArrayList JavaDoc<String JavaDoc> subComponents = new ArrayList JavaDoc<String JavaDoc>();
139             while (result.next()){
140                 subComponents.clear();
141                 componentName = result.getString(1);
142                 String JavaDoc query = SELECT_SUBCOMPONENTS+"'"+componentName+"'";
143                 logger.log(Level.FINEST, query);
144                 resultSub = stmtSub.executeQuery(query);
145                 while (resultSub.next()){
146                     subComponentName = resultSub.getString(1);
147                     subComponents.add(subComponentName);
148                 }
149                 components.put(componentName, subComponents.toArray(new String JavaDoc[1]));
150             }
151             stmt.close();
152             stmtSub.close();
153         }
154         return components;
155     }
156     
157     protected static ResultSet JavaDoc executeQuery(Statement JavaDoc stmt, String JavaDoc query)throws SQLException JavaDoc{
158         logger.log(Level.FINEST, query);
159         return stmt.executeQuery(query);
160     }
161     
162     protected static int executeUpd(Statement JavaDoc stmt, String JavaDoc update)throws SQLException JavaDoc{
163         if (update.length() > 1000) logger.log(Level.FINEST, update.substring(0, 1000)+"\n...\n");
164         else logger.log(Level.FINEST, update);
165         return stmt.executeUpdate(update);
166     }
167     
168     private static void addLogger(){
169         logger.addHandler(new ExceptionsLoggingHandler());// ON DEPLOY!!
170
logger.setLevel(Level.FINEST);
171     }
172     
173     private static Connection JavaDoc setConnection(Connection JavaDoc conn) throws SQLException JavaDoc{
174         String JavaDoc userName = "guest"; // NOI18N
175
conn = DriverManager.getConnection(DATABASE_URL, userName, userName);
176         conn.setAutoCommit(true);
177         conn.setTransactionIsolation(conn.TRANSACTION_READ_UNCOMMITTED);
178         return conn;
179     }
180     
181     private static void updateComponents() throws SQLException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc{
182         Class.forName("com.mysql.jdbc.Driver").newInstance(); // NOI18N
183
Connection JavaDoc issueZillaCon;
184         String JavaDoc userName = "guest"; // NOI18N
185
issueZillaCon = DriverManager.getConnection(ISSUEZILLA_URL, userName, userName);
186         Statement JavaDoc isszstmt = issueZillaCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
187         Statement JavaDoc stmt = getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
188         stmt.executeUpdate("DELETE FROM COMPONENTS");/* remove old informations in components table */ // NOI18N
189
stmt.close();
190         PreparedStatement JavaDoc pStmt = getConnection().prepareStatement(INSERT_COMPONENTS);
191         ResultSet JavaDoc isszresult = isszstmt.executeQuery("SELECT DISTINCT COMPONENT, SUBCOMPONENT FROM ISSUE"); // NOI18N
192
while (isszresult.next()) {// insert new informations into components table
193
String JavaDoc component = isszresult.getString(1);
194             String JavaDoc subComponent = isszresult.getString(2);
195             pStmt.setString(1, component);
196             pStmt.setString(2, subComponent);
197             pStmt.execute();
198         }
199         isszstmt.close();
200         pStmt.close();
201         issueZillaCon.close();
202         connection.commit();
203         logger.log(Level.INFO, "COMPONENTS TABLE WAS UPDATED"); // NOI18N
204
}
205     
206     //---------------------- SERVLET CONTEXT LISTENER -----------------------//
207

208     public void contextInitialized(ServletContextEvent JavaDoc arg0) {
209         try {
210             logger.setLevel(Level.FINEST);
211             Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); // NOI18N
212
connection = setConnection(connection);
213             updateComponents();
214             addLogger();
215             getComponents();
216         } catch (Exception JavaDoc ex) {
217             logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex);
218             throw new Error JavaDoc("Impossible to deploy servlet", ex); // NOI18N
219
};
220     }
221     
222     public void contextDestroyed(ServletContextEvent JavaDoc arg0) {
223         try {
224             if (connection != null) connection.close();
225         } catch (SQLException JavaDoc ex) {
226             logger.log(java.util.logging.Level.SEVERE,ex.getMessage(), ex);
227         }
228     }
229     
230 }
231
Popular Tags