1 19 20 26 27 package org.netbeans.modules.exceptions; 28 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.ObjectInputStream ; 32 import java.sql.Connection ; 33 import java.sql.DriverManager ; 34 import java.sql.PreparedStatement ; 35 import java.sql.ResultSet ; 36 import java.sql.SQLException ; 37 import java.sql.Statement ; 38 import java.util.ArrayList ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.LinkedList ; 42 import java.util.logging.Formatter ; 43 import java.util.logging.Level ; 44 import java.util.logging.LogRecord ; 45 import java.util.logging.Logger ; 46 import javax.servlet.ServletContextEvent ; 47 48 52 public class Utils implements javax.servlet.ServletContextListener { 53 public static final String PARAM_SHOW_LINE = "LINE"; public static final String PARAM_SHOW_COMMENT = "COMMENT"; public static final String PARAM_SHOW_ISSUENBUSER = "ISSUENBUSER"; public static final String PARAM_SHOW_ISSUE = "ISSUE"; public static final String PARAM_SHOW_STACKTRACE = "STACKTRACE"; public static final String PARAM_SHOW_METHOD = "METHOD"; public static final String PARAM_SHOW_NBFILES = "NBFILES"; public static final String PARAM_SHOW_NBUSER = "NBUSER"; public static final String PARAM_SHOW_LOGGER = "LOGGER"; 63 public static final String SELECT_ISSUE = "select * from ISSUE where Id = "; public static final String SELECT_LOGGER = "select * from LOGGER where Id = "; public static final String SELECT_USER = "select reportDate, Name from IssueNbUser, NbUser where NbUser_Id = id and Issue_id = "; public static final String SELECT_STACKTRACE = "select Message, Class from StackTrace where id = "; public static final String 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 = "; public static final String SELECT_COMMENT = "select Name,Comment from NbUser as A, Comment as B where NbUser_id = A.Id and Issue_id = "; 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_COMPONENTS = "SELECT DISTINCT COMPONENT FROM COMPONENTS"; public static final String SELECT_SUBCOMPONENTS = "SELECT SUBCOMPONENT FROM COMPONENTS WHERE COMPONENT = "; 74 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(?, ?)"; 78 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_NBFILES = "INSERT INTO NbFiles VALUES(?, ?)"; public static final String INSERT_LINE = "INSERT INTO Line VALUES(?, ?, ?, ?, ?)"; public static final String INSERT_LOGGER = "INSERT INTO Logger VALUES(?, ?)"; public static final String INSERT_COMPONENTS ="INSERT INTO Components VALUES(?, ?)"; 89 private static Connection connection; 90 private static Logger logger = java.util.logging.Logger.getLogger("org.netbeans.modules.exceptions"); private static java.util.HashMap <java.lang.String ,java.lang.String []> components = null; 92 93 private static String DATABASE_URL = "jdbc:derby://localhost:1527/exceptions;create=false"; private static String ISSUEZILLA_URL = "jdbc:mysql://qa-amd64-linux:3306/issuezilla"; 96 97 public Utils() { 98 } 99 100 101 public static Connection getConnection(){ 102 try { 103 if(connection.isClosed()) connection = setConnection(connection); 104 } catch (Exception ex) { 105 logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex); 106 } 107 return connection; 108 } 109 110 public static String getHTMLLog(ResultSet result) throws SQLException , IOException , ClassNotFoundException { 111 Formatter logFormater = new HTMLFormatter(); 112 String loggerText = ""; 113 InputStream stream = result.getBinaryStream(2); 114 ObjectInputStream objectIstream = new ObjectInputStream (stream); 115 Object o = objectIstream.readObject(); 116 if (o instanceof LinkedList ){ 117 Iterator it = ((LinkedList ) o).iterator(); 118 while (it.hasNext()){ 119 Object oElement = it.next(); 120 if (oElement instanceof LogRecord ){ 121 loggerText = loggerText.concat(logFormater.format((LogRecord )oElement)+"</br>"); 122 } 123 } 124 125 } 126 return loggerText; 127 } 128 129 public static HashMap <String , String []> getComponents() throws SQLException { 130 if (components == null){ 131 components = new HashMap <String , String []>(); 132 Statement stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 133 ResultSet result = stmt.executeQuery(Utils.SELECT_COMPONENTS); 134 Statement stmtSub = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 135 ResultSet resultSub; 136 String componentName; 137 String subComponentName; 138 ArrayList <String > subComponents = new ArrayList <String >(); 139 while (result.next()){ 140 subComponents.clear(); 141 componentName = result.getString(1); 142 String 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 [1])); 150 } 151 stmt.close(); 152 stmtSub.close(); 153 } 154 return components; 155 } 156 157 protected static ResultSet executeQuery(Statement stmt, String query)throws SQLException { 158 logger.log(Level.FINEST, query); 159 return stmt.executeQuery(query); 160 } 161 162 protected static int executeUpd(Statement stmt, String update)throws SQLException { 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()); logger.setLevel(Level.FINEST); 171 } 172 173 private static Connection setConnection(Connection conn) throws SQLException { 174 String userName = "guest"; 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 , ClassNotFoundException , InstantiationException , IllegalAccessException { 182 Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection issueZillaCon; 184 String userName = "guest"; issueZillaCon = DriverManager.getConnection(ISSUEZILLA_URL, userName, userName); 186 Statement isszstmt = issueZillaCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 187 Statement stmt = getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 188 stmt.executeUpdate("DELETE FROM COMPONENTS"); stmt.close(); 190 PreparedStatement pStmt = getConnection().prepareStatement(INSERT_COMPONENTS); 191 ResultSet isszresult = isszstmt.executeQuery("SELECT DISTINCT COMPONENT, SUBCOMPONENT FROM ISSUE"); while (isszresult.next()) { String component = isszresult.getString(1); 194 String 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"); } 205 206 208 public void contextInitialized(ServletContextEvent arg0) { 209 try { 210 logger.setLevel(Level.FINEST); 211 Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); connection = setConnection(connection); 213 updateComponents(); 214 addLogger(); 215 getComponents(); 216 } catch (Exception ex) { 217 logger.log(java.util.logging.Level.SEVERE, ex.getMessage(), ex); 218 throw new Error ("Impossible to deploy servlet", ex); }; 220 } 221 222 public void contextDestroyed(ServletContextEvent arg0) { 223 try { 224 if (connection != null) connection.close(); 225 } catch (SQLException ex) { 226 logger.log(java.util.logging.Level.SEVERE,ex.getMessage(), ex); 227 } 228 } 229 230 } 231 | Popular Tags |