1 19 20 26 27 package org.netbeans.modules.exceptions; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.ObjectInputStream ; 33 import java.io.ObjectOutputStream ; 34 import java.sql.PreparedStatement ; 35 import java.sql.ResultSet ; 36 import java.sql.SQLException ; 37 import java.sql.Statement ; 38 import java.text.MessageFormat ; 39 import java.util.LinkedList ; 40 import java.util.logging.LogRecord ; 41 import org.netbeans.modules.exceptions.data.Record; 42 import static org.netbeans.modules.exceptions.Utils.*; 43 47 public class DbInsertion implements Runnable { 48 private static final int MAXUsrName = 30; 49 private static final int MAXMethodName = 100; 50 private static final int MAXMessage = 100; 51 private static final int MAXClass = 100; 52 private static final int MAXSummary = 200; 53 private static final int MAXVM = 50; 54 private static final int MAXProductVersion = 50; 55 private static final int MAXOperatingSystem = 50; 56 private static final int MAXComment = 1000; 57 private static final int MAXComponent = 50; 58 private static final int MAXSubComponent = 50; 59 private static final int MAXAssignTo = 50; 60 private static int UserMax = 0; 61 private static int MethodMax = 0; 62 private static int IssueMax = 0; 63 private static int FilesMax = 0; 64 private static int LoggerMax = 0; 65 66 byte[] array; 67 68 public DbInsertion(byte[] _array) { 69 array = _array; 70 } 71 72 public void run() { 73 try { 74 ObjectInputStream str = new ObjectInputStream (new ByteArrayInputStream (array)); 75 Record rec = (Record ) str.readObject(); 77 insertIntoDB(rec); 79 } catch (IOException ex) { 80 java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, 81 ex.getMessage(), ex); 82 } catch (ClassNotFoundException ex) { 83 java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, 84 ex.getMessage(), ex); 85 }; 86 } 87 88 89 private static void insertIntoDB(Record rec){ 90 try { 91 rec = check(rec); 92 PreparedStatement pStmt; 93 pStmt = Utils.getConnection().prepareStatement(INSERT_NBUSER); 94 Integer issueId = getIssueNext(); 95 Integer userId = getExistUserId(rec.getUserName()); 96 Integer commentNext = new Integer (1); Integer loggerId = getLoggerNext(); 98 if (userId == 0){ userId = getUserNext(); 100 pStmt.setInt(1, userId); 101 pStmt.setString(2, rec.getUserName()); 102 pStmt.execute(); 103 pStmt.close(); 104 } 105 LinkedList <LogRecord > logger = rec.getLogger(); 107 ByteArrayOutputStream bStream = new ByteArrayOutputStream (); 108 ObjectOutputStream stream = new ObjectOutputStream (bStream); 109 stream.writeObject(logger); 110 stream.flush(); 111 byte[] array = bStream.toByteArray(); 112 113 pStmt = getConnection().prepareStatement(INSERT_LOGGER); 114 pStmt.setInt(1, loggerId); 115 pStmt.setBinaryStream(2, new ByteArrayInputStream (array), array.length); 116 pStmt.execute(); 117 pStmt.close(); 118 pStmt = getConnection().prepareStatement(INSERT_ISSUE); 120 pStmt.setInt(1, issueId); 121 pStmt.setString(2, rec.getUserData()[3]); 122 pStmt.setInt(3, loggerId); 123 pStmt.setString(4, rec.getVm()); 124 pStmt.setString(5, rec.getVersion()); 125 pStmt.setString(6, rec.getUserData()[0]); 126 pStmt.setString(7, rec.getUserData()[1]); 127 pStmt.setString(8, rec.getUserData()[2]); 128 pStmt.setString(9, rec.getOs()); 129 pStmt.setString(10, rec.getSeverity().toString()); 130 pStmt.execute(); 131 pStmt.close(); 132 pStmt = getConnection().prepareStatement(INSERT_STACKTRACE); 134 pStmt.setInt(1, issueId); 135 pStmt.setString(2, rec.getMessage()); 136 pStmt.setString(3, rec.getClassName()); 137 pStmt.setNull(4, java.sql.Types.INTEGER); 138 pStmt.execute(); 139 pStmt.close(); 140 pStmt = getConnection().prepareStatement(INSERT_COMMENT); 142 pStmt.setInt(1, commentNext); 143 pStmt.setInt(2, issueId); 144 pStmt.setInt(3, userId); 145 pStmt.setString(4, rec.getUserData()[4]); 146 pStmt.execute(); 147 pStmt.close(); 148 pStmt = getConnection().prepareStatement(INSERT_ISSUENBUSER); 150 pStmt.setInt(1, issueId); 151 pStmt.setInt(2, userId); 152 java.util.Calendar now = new java.util.GregorianCalendar (); 153 now.setTime(new java.util.Date ()); 154 pStmt.setTimestamp(3, new java.sql.Timestamp (now.getTimeInMillis())); 155 pStmt.execute(); 156 pStmt.close(); 157 pStmt = getConnection().prepareStatement(INSERT_HASH); 159 pStmt.setInt(1, new CheckIssue().countHash(rec.getThrowable())); 160 pStmt.setInt(2, issueId); 161 pStmt.execute(); 162 pStmt.close(); 163 pStmt = getConnection().prepareStatement(INSERT_METHOD); 165 PreparedStatement pStmtFiles = getConnection().prepareStatement(INSERT_NBFILES); 166 PreparedStatement pStmtLines = getConnection().prepareStatement(INSERT_LINE); 167 pStmtLines.setInt(1, issueId); 168 Integer methodId, fileId, lineNumber; 169 for (int i = 0; i < rec.getThrowable().length; i++) { 170 StackTraceElement element = rec.getThrowable()[i]; 171 String methodName = element.getClassName().concat(".").concat(element.getMethodName()); 172 String fileName = element.getFileName(); 173 if (methodName.length()> MAXMethodName) methodName = methodName.substring(methodName.length()-MAXMethodName); 174 methodId = getExistingMethodId(methodName); 175 if (methodId==0){ methodId=getMethodNext(); 177 pStmt.setInt(1, methodId); 178 pStmt.setString(2, methodName); 179 pStmt.execute(); 180 } 181 fileId = getExistingFileId(fileName); 182 if (fileId==0){ fileId=getFilesNext(); 184 pStmtFiles.setInt(1, fileId); 185 pStmtFiles.setString(2, fileName); 186 pStmtFiles.execute(); 187 } 188 if (element.getLineNumber()>=0) lineNumber = element.getLineNumber(); 189 else lineNumber = 0; 190 pStmtLines.setInt(2, methodId); 191 pStmtLines.setInt(3, fileId); 192 pStmtLines.setInt(4, lineNumber); 193 pStmtLines.setInt(5, i); 194 pStmtLines.execute(); 195 } 196 pStmt.close(); 197 pStmtFiles.close(); 198 pStmtLines.close(); 199 getConnection().commit(); 200 java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.INFO, "TRANSACTION COMMITED"); 201 } catch (SQLException ex) { 202 java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, ex.getMessage(), ex); 203 } catch (IOException ex) { 204 java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, ex.getMessage(), ex); 205 }; 206 207 } 208 209 210 private static Record check(Record rec){ 211 if (rec.getUserName().length() > MAXUsrName) rec.setUserName(rec.getUserName().substring(0, MAXUsrName)); 212 if (rec.getMessage().length() > MAXMessage) rec.setMessage(rec.getMessage().substring(0, MAXMessage)); 213 if (rec.getClassName().length() > MAXClass) rec.setClassName(rec.getClassName().substring(0, MAXClass)); 214 if (rec.getVm().length() > MAXVM) rec.setVm(rec.getVm().substring(0, MAXVM)); 215 if (rec.getVersion().length() > MAXProductVersion) rec.setVersion(rec.getVersion().substring(0, MAXProductVersion)); 216 if (rec.getOs().length() > MAXOperatingSystem) rec.setOs(rec.getOs().substring(0, MAXOperatingSystem)); 217 String [] userData = rec.getUserData(); 218 if (userData[0].length()> MAXComponent)userData[0] = userData[0].substring(0, MAXComponent); 219 if (userData[1].length()> MAXSubComponent)userData[1] = userData[1].substring(0, MAXSubComponent); 220 if (userData[2].length()> MAXAssignTo)userData[2] = userData[2].substring(0, MAXAssignTo); 221 if (userData[3].length()> MAXSummary)userData[3] = userData[3].substring(0, MAXSummary); 222 if (userData[4].length()> MAXComment)userData[4] = userData[4].substring(0, MAXComment); 223 return rec; 224 } 225 226 private static int getExistingMethodId(String methodName) throws SQLException { 227 return getExistId("Method", methodName); 228 } 229 230 private static int getExistingFileId(String fileName) throws SQLException { 231 return getExistId("NbFiles", fileName); 232 } 233 234 private static int getExistUserId(String userName) throws SQLException { 235 return getExistId("NbUser", userName); 236 } 237 238 private static int getExistId(String tableName, String subjectName) throws SQLException { 239 Statement stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 240 ResultSet result = executeQuery(stmt, MessageFormat.format(SELECT_EXIST_ID, tableName, subjectName)); 241 int value = 0; 242 if (result.next()) value = result.getInt(1); 243 stmt.close(); 244 return value; 245 } 246 247 private static Integer getCommentNext(Integer issueID) throws SQLException { 248 Statement stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 249 ResultSet result = executeQuery(stmt, "SELECT max(Id) FROM Comment WHERE Id = '" + issueID.toString()+"'"); 250 result.next(); 251 int value = result.getInt(1); 252 stmt.close(); 253 return value+1; 254 } 255 256 private static synchronized Integer getLoggerNext() throws SQLException { 257 if (LoggerMax == 0) LoggerMax = getMax("LOGGER"); 258 return ++LoggerMax; 259 } 260 261 262 private static synchronized Integer getFilesNext() throws SQLException { 263 if (FilesMax == 0) FilesMax = getMax("NbFiles"); 264 return ++FilesMax; 265 } 266 267 private static synchronized Integer getUserNext() throws SQLException { 268 if (UserMax == 0) UserMax = getMax("NbUser"); 269 return ++UserMax; 270 } 271 272 private static synchronized Integer getMethodNext() throws SQLException { 273 if (MethodMax == 0) MethodMax = getMax("Method"); 274 return ++MethodMax; 275 } 276 277 private static synchronized Integer getIssueNext() throws SQLException { 278 if (IssueMax == 0) IssueMax = getMax("ISSUE"); 279 return ++IssueMax; 280 } 281 282 private static int getMax(String tableName) throws SQLException { 283 Statement stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 284 ResultSet result = executeQuery(stmt, MessageFormat.format(SELECT_MAX_ID, tableName)); 285 result.next(); 286 int value = result.getInt(1); 287 stmt.close(); 288 return value; 289 } 290 291 public static void main(String [] args) throws SQLException { 292 Record rec = new Record (); 293 String [] userData = new String [rec.ITEMS_COUNT]; 294 rec.setClassName("java.lang.NPE"); 295 rec.setVersion("version"); 296 rec.setUserName("jindra"); 297 rec.setOs("OS"); 298 Throwable tr = new NullPointerException ("PROBLEM"); 299 rec.setThrowable(tr.getStackTrace()); 300 rec.setVm("TIGER"); 301 rec.setUserData(userData); 302 } 303 304 305 } 306 | Popular Tags |