001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2005 Emic Networks
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Marc Herbert
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.common.exceptions.driver.protocol;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024:
025: import org.continuent.sequoia.common.stream.DriverBufferedInputStream;
026: import org.continuent.sequoia.common.stream.DriverBufferedOutputStream;
027:
028: /**
029: * This class re-implements StackTraceElement of JDK 1.5, as a brute force
030: * workaround for the forgotten constructor in JDK 1.4.
031: *
032: * @author <a href="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
033: * @version 1.0
034: */
035: public class SerializableStackTraceElement implements Serializable {
036: private String declaringClass;
037: private String methodName;
038: private String fileName;
039: private int lineNumber;
040:
041: // /**
042: // * This is the constructor forgotten in 1.4 (and available in 1.5)
043: // * the only reason why we exist.
044: // */
045: // private SerializableStackTraceElement(String declaringClass, String methodName,
046: // String fileName, int lineNumber)
047: // {
048: // this.declaringClass = declaringClass;
049: // this.methodName = methodName;
050: // this.fileName = fileName;
051: // this.lineNumber = lineNumber;
052: //
053: // }
054:
055: /**
056: * Constructs/converts a standard StackTraceElement (non-serializable in 1.4)
057: * into a SerializableStackTraceElement.
058: *
059: * @param st the element to convert.
060: */
061: public SerializableStackTraceElement(StackTraceElement st) {
062: this .declaringClass = st.getClassName();
063: this .methodName = st.getMethodName();
064: this .fileName = st.getFileName();
065: this .lineNumber = st.getLineNumber();
066: }
067:
068: /**
069: * Deserializes a new <code>SerializableStackTraceElement</code> from the
070: * stream
071: *
072: * @param in the stream to read from
073: * @throws IOException stream error
074: */
075: public SerializableStackTraceElement(DriverBufferedInputStream in)
076: throws IOException {
077: declaringClass = in.readLongUTF();
078: methodName = in.readLongUTF();
079: fileName = in.readLongUTF();
080: lineNumber = in.readInt();
081: }
082:
083: /**
084: * Serializes the object to the given stream.
085: *
086: * @param out the stream to send the object to
087: * @throws IOException stream error
088: */
089: public void sendToStream(DriverBufferedOutputStream out)
090: throws IOException {
091: out.writeLongUTF(declaringClass);
092: out.writeLongUTF(methodName);
093: out.writeLongUTF(fileName);
094: out.writeInt(lineNumber);
095:
096: }
097:
098: /**
099: *
100: * @see StackTraceElement#getLineNumber()
101: */
102: public int getLineNumber() {
103: return lineNumber;
104: }
105:
106: /**
107: *
108: * @see StackTraceElement#getClassName()
109: */
110: public String getClassName() {
111: return declaringClass;
112: }
113:
114: /**
115: *
116: * @see StackTraceElement#getMethodName()
117: */
118: public String getMethodName() {
119: return methodName;
120: }
121:
122: /**
123: *
124: * @see StackTraceElement#isNativeMethod()
125: */
126: public boolean isNativeMethod() {
127: return lineNumber == -2;
128: }
129:
130: /**
131: *
132: * @see StackTraceElement#toString()
133: */
134: public String toString() {
135: return getClassName()
136: + "."
137: + methodName
138: + (isNativeMethod() ? "(Native Method)"
139: : (fileName != null && lineNumber >= 0 ? "("
140: + fileName + ":" + lineNumber + ")"
141: : (fileName != null ? "(" + fileName
142: + ")" : "(Unknown Source)")));
143: }
144:
145: }
|