001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.lockmanager.impl;
006:
007: import org.apache.commons.lang.builder.HashCodeBuilder;
008:
009: import com.tc.exception.TCRuntimeException;
010: import com.tc.io.TCByteBufferInput;
011: import com.tc.io.TCByteBufferOutput;
012: import com.tc.io.TCSerializable;
013:
014: import java.io.ByteArrayInputStream;
015: import java.io.ByteArrayOutputStream;
016: import java.io.IOException;
017: import java.io.ObjectInputStream;
018: import java.io.ObjectOutputStream;
019: import java.io.Serializable;
020:
021: public class TCStackTraceElement implements TCSerializable,
022: Serializable {
023: private StackTraceElement[] stackTraceElements;
024: private int hashCode;
025:
026: public TCStackTraceElement() {
027: return;
028: }
029:
030: public TCStackTraceElement(StackTraceElement[] stackTraceElements) {
031: this .stackTraceElements = stackTraceElements;
032:
033: computeHashCode();
034: }
035:
036: private void computeHashCode() {
037: HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(5503,
038: 6737);
039: for (int i = 0; i < stackTraceElements.length; i++) {
040: hashCodeBuilder.append(stackTraceElements[i].hashCode());
041: }
042: this .hashCode = hashCodeBuilder.toHashCode();
043: }
044:
045: public Object deserializeFrom(TCByteBufferInput serialInput)
046: throws IOException {
047: int length = serialInput.readInt();
048: stackTraceElements = new StackTraceElement[length];
049: for (int i = 0; i < length; i++) {
050: int numBytes = serialInput.readInt();
051: byte[] bytes = new byte[numBytes];
052: serialInput.read(bytes);
053: ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
054: ObjectInputStream is = new ObjectInputStream(bis);
055: try {
056: stackTraceElements[i] = (StackTraceElement) is
057: .readObject();
058: } catch (ClassNotFoundException e) {
059: throw new TCRuntimeException(e);
060: }
061: }
062: computeHashCode();
063:
064: return this ;
065: }
066:
067: public void serializeTo(TCByteBufferOutput serialOutput) {
068: serialOutput.writeInt(stackTraceElements.length);
069: for (int i = 0; i < stackTraceElements.length; i++) {
070: StackTraceElement se = stackTraceElements[i];
071: try {
072: ByteArrayOutputStream bos = new ByteArrayOutputStream();
073: ObjectOutputStream os = new ObjectOutputStream(bos);
074: os.writeObject(se);
075: byte[] bytes = bos.toByteArray();
076: serialOutput.writeInt(bytes.length);
077: serialOutput.write(bytes);
078: } catch (IOException e) {
079: throw new TCRuntimeException(e);
080: }
081: }
082: }
083:
084: public StackTraceElement[] getStackTraceElements() {
085: return this .stackTraceElements;
086: }
087:
088: public String toString() {
089: StringBuffer sb = new StringBuffer();
090: for (int i = 0; i < stackTraceElements.length; i++) {
091: for (int j = 0; j < i; j++) {
092: sb.append(" ");
093: }
094: sb.append(stackTraceElements[i].toString());
095: sb.append("\n");
096: }
097: return sb.toString();
098: }
099:
100: public boolean equals(Object obj) {
101: if (!(obj instanceof TCStackTraceElement)) {
102: return false;
103: }
104: if (this == obj) {
105: return true;
106: }
107:
108: TCStackTraceElement so = (TCStackTraceElement) obj;
109: if (this .stackTraceElements.length != so.stackTraceElements.length) {
110: return false;
111: }
112: for (int i = 0; i < this .stackTraceElements.length; i++) {
113: if (!this .stackTraceElements[i]
114: .equals(so.stackTraceElements[i])) {
115: return false;
116: }
117: }
118: return true;
119: }
120:
121: public int hashCode() {
122: return hashCode;
123: }
124: }
|