1 19 20 21 package org.netbeans.server.uihandler; 22 23 import java.sql.Connection ; 24 import java.sql.DriverManager ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NameClassPair ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 import org.openide.util.Exceptions; 36 37 41 public final class Utils { 42 public static final String EXCEPTIONS_URL= 43 java.util.ResourceBundle.getBundle("org/netbeans/server/uihandler/Bundle").getString("EXCEPTIONS_URL"); 44 45 public static final String SELECT_MAX_ID = "SELECT max(Id) FROM {0}"; public static final String SELECT_EXIST_ID = "SELECT Id FROM {0} WHERE Name = ''{1}''"; public static final String SELECT_MAX_COMMENT = "SELECT max(Id) FROM Comment WHERE Issue_Id = {0}"; 49 public static final String SELECT_HASH = "SELECT * FROM HashCodes WHERE CODE = "; public static final String SELECT_HASH_BY_ISSUE = "SELECT * FROM HashCodes WHERE ISSUEID = "; public static final String INSERT_HASH = "INSERT INTO HashCodes VALUES(?, ?)"; 53 public static final String INSERT_NBUSER = "INSERT INTO NbUser VALUES(?, ?)"; public static final String INSERT_STACKTRACE = "INSERT INTO StackTrace VALUES(?, ?, ?, ?)"; public static final String INSERT_ISSUE = "INSERT INTO Issue VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; public static final String INSERT_COMMENT = "INSERT INTO Comment VALUES(?, ?, ?, ?, ?)"; public static final String INSERT_ISSUENBUSER = "INSERT INTO IssueNbUser VALUES(?, ?, ?)"; public static final String INSERT_METHOD = "INSERT INTO Method VALUES(?, ?)"; public static final String INSERT_LINE = "INSERT INTO Line VALUES(?, ?, ?, ?)"; 61 private static Connection connection = null; 62 private static final Logger logger = java.util.logging.Logger.getLogger("org.netbeans.server.uihandler"); 64 private static String DATABASE_URL = "jdbc:mysql://localhost:3306/exceptions"; private static String USER_NAME = "reporter"; 66 private static String PASSWD = "logger_exception"; 67 68 69 public Utils() { 70 } 71 72 public static synchronized Connection getConnection(){ 73 try { 74 assert connection != null:"CONNECTION SHOULD BE NEVER NULL AFTER DEPLOYMENT"; if(connection.isClosed()) setConnection(); 76 } catch (Exception ex) { 77 logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex); 78 } 79 return connection; 80 } 81 82 public static int checkIssue(Throwable thrown){ 83 if (thrown == null) return -2; 84 int result = -1; 85 Statement stmt = null; 86 ResultSet resSet = null; 87 try { 88 stmt = getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 89 String query = SELECT_HASH + Integer.toString(countHash(thrown.getStackTrace())); 90 resSet = Utils.executeQuery(stmt, query); 91 if (resSet.next())result = resSet.getInt(2); else result = -1; } catch (SQLException except) { 94 logger.log(Level.WARNING, "CHECKING ISSUE", except); } finally { 96 try { 97 if (resSet != null) { 98 resSet.close(); 99 } 100 } catch (SQLException e) { 101 logger.log(Level.INFO, "Closing result set", e); } 103 try { 104 if (stmt != null) { 105 stmt.close(); 106 } 107 } catch (SQLException e) { 108 logger.log(Level.INFO, "Closing statement", e); } 110 } 111 return result; 112 } 113 114 protected static ResultSet executeQuery(Statement stmt, String query)throws SQLException { 115 logger.log(Level.FINEST, query); 116 return stmt.executeQuery(query); 117 } 118 119 protected static int executeUpd(Statement stmt, String update)throws SQLException { 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 { 126 setConnection(DATABASE_URL, USER_NAME, PASSWD); 127 } 128 129 protected static void setConnection(String dbURL, String userName, String passwd) throws SQLException { 130 logger.info("SETTING CONNECTION"); 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"); try { 139 if ((connection != null)&&(!connection.isClosed())){ 140 connection.commit(); 141 connection.close(); 142 } 143 } catch (SQLException ex) { 144 logger.log(java.util.logging.Level.WARNING, ex.getMessage(), ex); 145 } 146 } 147 148 public static int countHash(StackTraceElement [] throwable){ 149 String 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 164 static void printContext(String space, Context c) throws NamingException { 165 if (c == null) { 166 Context initCtx = new InitialContext (); 167 c = (Context ) initCtx.lookup("java:comp/env"); } 169 170 NamingEnumeration <NameClassPair > tips; 171 tips = c.list(""); 172 while (tips.hasMore()) { 173 NameClassPair pair = tips.next(); 174 LogsManager.LOG.warning("binding " + space + pair.getName() + " class: " + pair.getClassName()); 175 176 Object o = c.lookup(pair.getName()); 177 if (pair.getClassName().indexOf("Context") >= 0) { 178 if (o instanceof Context ) { 179 printContext(space + pair.getName() + "/", (Context )o); 180 } 181 } else { 182 LogsManager.LOG.warning(" value " + space + " = " + o); 183 } 184 } 185 186 } 187 188 195 public static <T> T getVariable(String name, Class <T> type) { 196 try { 197 Context initCtx = new InitialContext (); 198 Context c = (Context ) initCtx.lookup("java:comp/env"); c = (Context )c.lookup("analytics"); return type.cast(c.lookup(name)); 201 } catch (NamingException ex) { 202 LogsManager.LOG.log(Level.WARNING, "no env variable for " + name, ex); 203 return null; 204 } catch (ClassCastException 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 |