1 25 package com.mysql.jdbc.profiler; 26 27 import java.util.Date ; 28 29 import com.mysql.jdbc.Util; 30 31 34 public class ProfilerEvent { 35 36 39 public static final byte TYPE_WARN = 0; 40 41 44 public static final byte TYPE_OBJECT_CREATION = 1; 45 46 49 public static final byte TYPE_PREPARE = 2; 50 51 54 public static final byte TYPE_QUERY = 3; 55 56 59 public static final byte TYPE_EXECUTE = 4; 60 61 64 public static final byte TYPE_FETCH = 5; 65 66 69 protected byte eventType; 70 71 74 protected int connectionId; 75 76 79 protected int statementId; 80 81 84 protected int resultSetId; 85 86 89 protected long eventCreationTime; 90 91 94 protected int eventDurationMillis; 95 96 100 protected int hostNameIndex; 101 102 105 protected String hostName; 106 107 111 protected int catalogIndex; 112 113 116 protected String catalog; 117 118 122 protected int eventCreationPointIndex; 123 124 127 protected Throwable eventCreationPoint; 128 129 133 protected String eventCreationPointDesc; 134 135 138 protected String message; 139 140 166 public ProfilerEvent(byte eventType, String hostName, String catalog, 167 int connectionId, int statementId, int resultSetId, 168 long eventCreationTime, int eventDurationMillis, 169 String eventCreationPointDesc, Throwable eventCreationPoint, 170 String message) { 171 this.eventType = eventType; 172 this.connectionId = connectionId; 173 this.statementId = statementId; 174 this.resultSetId = resultSetId; 175 this.eventCreationTime = eventCreationTime; 176 this.eventDurationMillis = eventDurationMillis; 177 this.eventCreationPoint = eventCreationPoint; 178 this.eventCreationPointDesc = eventCreationPointDesc; 179 this.message = message; 180 } 181 182 187 public String getEventCreationPointAsString() { 188 if (this.eventCreationPointDesc == null) { 189 this.eventCreationPointDesc = Util 190 .stackTraceToString(this.eventCreationPoint); 191 } 192 193 return this.eventCreationPointDesc; 194 } 195 196 201 public String toString() { 202 StringBuffer buf = new StringBuffer (32); 203 204 switch (this.eventType) { 205 case TYPE_EXECUTE: 206 buf.append("EXECUTE"); 207 break; 208 209 case TYPE_FETCH: 210 buf.append("FETCH"); 211 break; 212 213 case TYPE_OBJECT_CREATION: 214 buf.append("CONSTRUCT"); 215 break; 216 217 case TYPE_PREPARE: 218 buf.append("PREPARE"); 219 break; 220 221 case TYPE_QUERY: 222 buf.append("QUERY"); 223 break; 224 225 case TYPE_WARN: 226 buf.append("WARN"); 227 break; 228 default: 229 buf.append("UNKNOWN"); 230 } 231 232 buf.append(" created: "); 233 buf.append(new Date (this.eventCreationTime)); 234 buf.append(" duration: "); 235 buf.append(this.eventDurationMillis); 236 buf.append(" connection: "); 237 buf.append(this.connectionId); 238 buf.append(" statement: "); 239 buf.append(this.statementId); 240 buf.append(" resultset: "); 241 buf.append(this.resultSetId); 242 243 if (this.message != null) { 244 buf.append(" message: "); 245 buf.append(this.message); 246 247 } 248 249 if (this.eventCreationPointDesc != null) { 250 buf.append("\n\nEvent Created at:\n"); 251 buf.append(this.eventCreationPointDesc); 252 } 253 254 return buf.toString(); 255 } 256 257 266 public static ProfilerEvent unpack(byte[] buf) throws Exception { 267 int pos = 0; 268 269 byte eventType = buf[pos++]; 270 int connectionId = readInt(buf, pos); 271 pos += 4; 272 int statementId = readInt(buf, pos); 273 pos += 4; 274 int resultSetId = readInt(buf, pos); 275 pos += 4; 276 long eventCreationTime = readLong(buf, pos); 277 pos += 8; 278 int eventDurationMillis = readInt(buf, pos); 279 pos += 4; 280 int eventCreationPointIndex = readInt(buf, pos); 281 pos += 4; 282 byte[] eventCreationAsBytes = readBytes(buf, pos); 283 pos += 4; 284 285 if (eventCreationAsBytes != null) { 286 pos += eventCreationAsBytes.length; 287 } 288 289 byte[] message = readBytes(buf, pos); 290 pos += 4; 291 292 if (message != null) { 293 pos += message.length; 294 } 295 296 return new ProfilerEvent(eventType, "", "", connectionId, statementId, 297 resultSetId, eventCreationTime, eventDurationMillis, 298 new String (eventCreationAsBytes, "ISO8859_1"), null, 299 new String (message, "ISO8859_1")); 300 } 301 302 309 public byte[] pack() throws Exception { 310 311 int len = 1 + 4 + 4 + 4 + 8 + 4 + 4; 312 313 byte[] eventCreationAsBytes = null; 314 315 getEventCreationPointAsString(); 316 317 if (this.eventCreationPointDesc != null) { 318 eventCreationAsBytes = this.eventCreationPointDesc 319 .getBytes("ISO8859_1"); 320 len += (4 + eventCreationAsBytes.length); 321 } else { 322 len += 4; 323 } 324 325 byte[] messageAsBytes = null; 326 327 if (messageAsBytes != null) { 328 messageAsBytes = this.message.getBytes("ISO8859_1"); 329 len += (4 + messageAsBytes.length); 330 } else { 331 len += 4; 332 } 333 334 byte[] buf = new byte[len]; 335 336 int pos = 0; 337 338 buf[pos++] = this.eventType; 339 pos = writeInt(this.connectionId, buf, pos); 340 pos = writeInt(this.statementId, buf, pos); 341 pos = writeInt(this.resultSetId, buf, pos); 342 pos = writeLong(this.eventCreationTime, buf, pos); 343 pos = writeInt(this.eventDurationMillis, buf, pos); 344 pos = writeInt(this.eventCreationPointIndex, buf, pos); 345 346 if (eventCreationAsBytes != null) { 347 pos = writeBytes(eventCreationAsBytes, buf, pos); 348 } else { 349 pos = writeInt(0, buf, pos); 350 } 351 352 if (messageAsBytes != null) { 353 pos = writeBytes(messageAsBytes, buf, pos); 354 } else { 355 pos = writeInt(0, buf, pos); 356 } 357 358 return buf; 359 } 360 361 private static int writeInt(int i, byte[] buf, int pos) { 362 363 buf[pos++] = (byte) (i & 0xff); 364 buf[pos++] = (byte) (i >>> 8); 365 buf[pos++] = (byte) (i >>> 16); 366 buf[pos++] = (byte) (i >>> 24); 367 368 return pos; 369 } 370 371 private static int writeLong(long l, byte[] buf, int pos) { 372 buf[pos++] = (byte) (l & 0xff); 373 buf[pos++] = (byte) (l >>> 8); 374 buf[pos++] = (byte) (l >>> 16); 375 buf[pos++] = (byte) (l >>> 24); 376 buf[pos++] = (byte) (l >>> 32); 377 buf[pos++] = (byte) (l >>> 40); 378 buf[pos++] = (byte) (l >>> 48); 379 buf[pos++] = (byte) (l >>> 56); 380 381 return pos; 382 } 383 384 private static int writeBytes(byte[] msg, byte[] buf, int pos) { 385 pos = writeInt(msg.length, buf, pos); 386 387 System.arraycopy(msg, 0, buf, pos, msg.length); 388 389 return pos + msg.length; 390 } 391 392 private static int readInt(byte[] buf, int pos) { 393 return (buf[pos++] & 0xff) | ((buf[pos++] & 0xff) << 8) 394 | ((buf[pos++] & 0xff) << 16) | ((buf[pos++] & 0xff) << 24); 395 396 } 397 398 private static long readLong(byte[] buf, int pos) { 399 return (long) (buf[pos++] & 0xff) | ((long) (buf[pos++] & 0xff) << 8) 400 | ((long) (buf[pos++] & 0xff) << 16) 401 | ((long) (buf[pos++] & 0xff) << 24) 402 | ((long) (buf[pos++] & 0xff) << 32) 403 | ((long) (buf[pos++] & 0xff) << 40) 404 | ((long) (buf[pos++] & 0xff) << 48) 405 | ((long) (buf[pos++] & 0xff) << 56); 406 } 407 408 private static byte[] readBytes(byte[] buf, int pos) { 409 int length = readInt(buf, pos); 410 411 pos += 4; 412 413 byte[] msg = new byte[length]; 414 System.arraycopy(buf, pos, msg, 0, length); 415 416 return msg; 417 } 418 419 424 public String getCatalog() { 425 return this.catalog; 426 } 427 428 433 public int getConnectionId() { 434 return this.connectionId; 435 } 436 437 443 public Throwable getEventCreationPoint() { 444 return this.eventCreationPoint; 445 } 446 447 453 public long getEventCreationTime() { 454 return this.eventCreationTime; 455 } 456 457 462 public int getEventDurationMillis() { 463 return this.eventDurationMillis; 464 } 465 466 471 public byte getEventType() { 472 return this.eventType; 473 } 474 475 480 public int getResultSetId() { 481 return this.resultSetId; 482 } 483 484 489 public int getStatementId() { 490 return this.statementId; 491 } 492 493 498 public String getMessage() { 499 return this.message; 500 } 501 } 502 | Popular Tags |