001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import java.io.IOException;
019: import java.io.ObjectInput;
020: import java.io.ObjectOutput;
021:
022: public class EJBResponse implements ClusterableResponse {
023:
024: private transient int responseCode = -1;
025: private transient Object result;
026: private transient ServerMetaData server;
027:
028: public EJBResponse() {
029:
030: }
031:
032: public EJBResponse(int code, Object obj) {
033: responseCode = code;
034: result = obj;
035: }
036:
037: public int getResponseCode() {
038: return responseCode;
039: }
040:
041: public Object getResult() {
042: return result;
043: }
044:
045: public void setResponse(int code, Object result) {
046: this .responseCode = code;
047: this .result = result;
048: }
049:
050: public void setServer(ServerMetaData server) {
051: this .server = server;
052: }
053:
054: public ServerMetaData getServer() {
055: return server;
056: }
057:
058: public String toString() {
059: StringBuffer s = null;
060: switch (responseCode) {
061: case ResponseCodes.EJB_APP_EXCEPTION:
062: s = new StringBuffer("EJB_APP_EXCEPTION");
063: break;
064: case ResponseCodes.EJB_ERROR:
065: s = new StringBuffer("EJB_ERROR");
066: break;
067: case ResponseCodes.EJB_OK:
068: s = new StringBuffer("EJB_OK");
069: break;
070: case ResponseCodes.EJB_OK_CREATE:
071: s = new StringBuffer("EJB_OK_CREATE");
072: break;
073: case ResponseCodes.EJB_OK_FOUND:
074: s = new StringBuffer("EJB_OK_FOUND");
075: break;
076: case ResponseCodes.EJB_OK_FOUND_COLLECTION:
077: s = new StringBuffer("EJB_OK_FOUND_COLLECTION");
078: break;
079: case ResponseCodes.EJB_OK_FOUND_ENUMERATION:
080: s = new StringBuffer("EJB_OK_FOUND_ENUMERATION");
081: break;
082: case ResponseCodes.EJB_OK_NOT_FOUND:
083: s = new StringBuffer("EJB_OK_NOT_FOUND");
084: break;
085: case ResponseCodes.EJB_SYS_EXCEPTION:
086: s = new StringBuffer("EJB_SYS_EXCEPTION");
087: break;
088: default:
089: s = new StringBuffer("UNKNOWN_RESPONSE");
090: }
091: s.append(':').append(result);
092:
093: return s.toString();
094: }
095:
096: public void readExternal(ObjectInput in) throws IOException,
097: ClassNotFoundException {
098: byte version = in.readByte(); // future use
099:
100: boolean readServer = in.readBoolean();
101: if (readServer) {
102: server = new ServerMetaData();
103: server.readExternal(in);
104: }
105:
106: responseCode = in.readByte();
107:
108: result = in.readObject();
109: }
110:
111: public void writeExternal(ObjectOutput out) throws IOException {
112: // write out the version of the serialized data for future use
113: out.writeByte(1);
114:
115: if (null != server) {
116: out.writeBoolean(true);
117: server.writeExternal(out);
118: } else {
119: out.writeBoolean(false);
120: }
121:
122: out.writeByte(responseCode);
123:
124: switch (responseCode) {
125: case ResponseCodes.EJB_APP_EXCEPTION:
126: case ResponseCodes.EJB_ERROR:
127: case ResponseCodes.EJB_SYS_EXCEPTION:
128: if (result instanceof Throwable
129: && !(result instanceof ThrowableArtifact)) {
130: Throwable throwable = (Throwable) result;
131: result = new ThrowableArtifact(throwable);
132: }
133: }
134: out.writeObject(result);
135: }
136:
137: }
|