KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > server > uihandler > 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 package org.netbeans.server.uihandler;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NameClassPair JavaDoc;
33 import javax.naming.NamingEnumeration JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import org.openide.util.Exceptions;
36
37 /**
38  *
39  * @author Jindrich Sedek
40  */

41 public final class Utils {
42     public static final String JavaDoc EXCEPTIONS_URL=
43             java.util.ResourceBundle.getBundle("org/netbeans/server/uihandler/Bundle").getString("EXCEPTIONS_URL");
44     
45     public static final String JavaDoc SELECT_MAX_ID = "SELECT max(Id) FROM {0}"; // NOI18N
46
public static final String JavaDoc SELECT_EXIST_ID = "SELECT Id FROM {0} WHERE Name = ''{1}''"; // NOI18N
47
public static final String JavaDoc SELECT_MAX_COMMENT = "SELECT max(Id) FROM Comment WHERE Issue_Id = {0}"; // NOI18N
48

49     public static final String JavaDoc SELECT_HASH = "SELECT * FROM HashCodes WHERE CODE = "; //NOI18N
50
public static final String JavaDoc SELECT_HASH_BY_ISSUE = "SELECT * FROM HashCodes WHERE ISSUEID = "; //NOI18N
51
public static final String JavaDoc INSERT_HASH = "INSERT INTO HashCodes VALUES(?, ?)"; //NOI18N
52

53     public static final String JavaDoc INSERT_NBUSER = "INSERT INTO NbUser VALUES(?, ?)"; // NOI18N
54
public static final String JavaDoc INSERT_STACKTRACE = "INSERT INTO StackTrace VALUES(?, ?, ?, ?)"; // NOI18N
55
public static final String JavaDoc INSERT_ISSUE = "INSERT INTO Issue VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; // NOI18N
56
public static final String JavaDoc INSERT_COMMENT = "INSERT INTO Comment VALUES(?, ?, ?, ?, ?)"; // NOI18N
57
public static final String JavaDoc INSERT_ISSUENBUSER = "INSERT INTO IssueNbUser VALUES(?, ?, ?)"; // NOI18N
58
public static final String JavaDoc INSERT_METHOD = "INSERT INTO Method VALUES(?, ?)"; // NOI18N
59
public static final String JavaDoc INSERT_LINE = "INSERT INTO Line VALUES(?, ?, ?, ?)"; // NOI18N
60

61     private static Connection JavaDoc connection = null;
62     private static final Logger JavaDoc logger = java.util.logging.Logger.getLogger("org.netbeans.server.uihandler"); // NOI18N
63

64     private static String JavaDoc DATABASE_URL = "jdbc:mysql://localhost:3306/exceptions"; // NOI18N
65
private static String JavaDoc USER_NAME = "reporter";
66     private static String JavaDoc PASSWD = "logger_exception";
67     
68     /** Creates a new instance of Utils */
69     public Utils() {
70     }
71     
72     public static synchronized Connection JavaDoc getConnection(){
73         try {
74             assert connection != null:"CONNECTION SHOULD BE NEVER NULL AFTER DEPLOYMENT"; //NOI18N
75
if(connection.isClosed()) setConnection();
76         } catch (Exception JavaDoc ex) {
77             logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex);
78         }
79         return connection;
80     }
81     
82     public static int checkIssue(Throwable JavaDoc thrown){
83         if (thrown == null) return -2;
84         int result = -1;
85         Statement JavaDoc stmt = null;
86         ResultSet JavaDoc resSet = null;
87         try {
88             stmt = getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
89             String JavaDoc query = SELECT_HASH + Integer.toString(countHash(thrown.getStackTrace()));
90             resSet = Utils.executeQuery(stmt, query);
91             if (resSet.next())result = resSet.getInt(2);// I found the same hashCode
92
else result = -1;// nothing found
93
} catch (SQLException JavaDoc except) {
94             logger.log(Level.WARNING, "CHECKING ISSUE", except); //NOI18N
95
} finally {
96             try {
97                 if (resSet != null) {
98                     resSet.close();
99                 }
100             } catch (SQLException JavaDoc e) {
101                 logger.log(Level.INFO, "Closing result set", e); // NOI18N
102
}
103             try {
104                 if (stmt != null) {
105                     stmt.close();
106                 }
107             } catch (SQLException JavaDoc e) {
108                 logger.log(Level.INFO, "Closing statement", e); // NOI18N
109
}
110         }
111         return result;
112     }
113     
114     protected static ResultSet JavaDoc executeQuery(Statement JavaDoc stmt, String JavaDoc query)throws SQLException JavaDoc{
115         logger.log(Level.FINEST, query);
116         return stmt.executeQuery(query);
117     }
118     
119     protected static int executeUpd(Statement JavaDoc stmt, String JavaDoc update)throws SQLException JavaDoc{
120         if (update.length() > 1000) logger.log(Level.FINEST, update.substring(0, 1000)+"\n...\n");
121         else logger.log(Level.FINEST, update);
122         return stmt.executeUpdate(update);
123     }
124     
125     protected static void setConnection() throws SQLException JavaDoc{
126         setConnection(DATABASE_URL, USER_NAME, PASSWD);
127     }
128     
129     protected static void setConnection(String JavaDoc dbURL, String JavaDoc userName, String JavaDoc passwd) throws SQLException JavaDoc{
130         logger.info("SETTING CONNECTION"); // NOI18N
131
connection = DriverManager.getConnection(dbURL, userName, passwd);
132         connection.setAutoCommit(false);
133         connection.setTransactionIsolation(connection.TRANSACTION_READ_UNCOMMITTED);
134     }
135     
136     protected static void closeConnection(){
137         logger.fine("CLOSING CONNECTION"); // NOI18N
138
try {
139             if ((connection != null)&&(!connection.isClosed())){
140                 connection.commit();
141                 connection.close();
142             }
143         } catch (SQLException JavaDoc ex) {
144             logger.log(java.util.logging.Level.WARNING, ex.getMessage(), ex);
145         }
146     }
147     
148     public static int countHash(StackTraceElement JavaDoc[] throwable){
149         String JavaDoc str = throwable[0].getClassName().toString();
150         str = str.concat(throwable[0].getMethodName().toString());
151         for (int i = 1; i < throwable.length; i++) {
152             str = str.concat(throwable[i].getClassName().toString());
153             str = str.concat(throwable[i].getMethodName().toString());
154         }
155         int result = str.hashCode();
156         return result;
157     }
158
159     
160     //
161
// Working with JNDI
162
//
163

164     static void printContext(String JavaDoc space, Context JavaDoc c) throws NamingException JavaDoc {
165         if (c == null) {
166             Context JavaDoc initCtx = new InitialContext JavaDoc();
167             c = (Context JavaDoc) initCtx.lookup("java:comp/env"); // NOI18N
168
}
169         
170         NamingEnumeration JavaDoc<NameClassPair JavaDoc> tips;
171         tips = c.list("");
172         while (tips.hasMore()) {
173             NameClassPair JavaDoc pair = tips.next();
174             LogsManager.LOG.warning("binding " + space + pair.getName() + " class: " + pair.getClassName());
175             
176             Object JavaDoc o = c.lookup(pair.getName());
177             if (pair.getClassName().indexOf("Context") >= 0) {
178                 if (o instanceof Context JavaDoc) {
179                     printContext(space + pair.getName() + "/", (Context JavaDoc)o);
180                 }
181             } else {
182                 LogsManager.LOG.warning(" value " + space + " = " + o);
183             }
184         }
185         
186     }
187     
188     /**
189      * Reads value for a variable defined in the "env-entry" section of web.xml
190      *
191      * @param name name of the variabl
192      * @param type the expected type of the variable
193      * @return value for the variable or null if there is no, or there is not the right type
194      */

195     public static <T> T getVariable(String JavaDoc name, Class JavaDoc<T> type) {
196         try {
197             Context JavaDoc initCtx = new InitialContext JavaDoc();
198             Context JavaDoc c = (Context JavaDoc) initCtx.lookup("java:comp/env"); // NOI18N
199
c = (Context JavaDoc)c.lookup("analytics"); // NOI18N
200
return type.cast(c.lookup(name));
201         } catch (NamingException JavaDoc ex) {
202             LogsManager.LOG.log(Level.WARNING, "no env variable for " + name, ex);
203             return null;
204         } catch (ClassCastException JavaDoc ex) {
205             LogsManager.LOG.log(Level.WARNING, "wrong env variable for " + name + " we need " + type.getName(), ex);
206             return null;
207         }
208     }
209 }
210
Popular Tags