1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: Response.java,v 1.1 2004/11/26 01:51:03 tanderson Exp $
44 */
45 package org.exolab.jms.net.connector;
46
47 import java.io.IOException;
48 import java.io.ObjectInput;
49 import java.io.ObjectOutput;
50 import java.io.Serializable;
51 import java.lang.reflect.Method;
52
53 import org.exolab.jms.net.util.SerializationHelper;
54
55
56 /***
57 * A <code>Response</code> wraps the result of a remote method invocation
58 *
59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60 * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:03 $
61 * @see Request
62 * @see Connection
63 */
64 public class Response implements Serializable {
65
66 /***
67 * The invocation result. May be <code>null</code>
68 */
69 private Object _object;
70
71 /***
72 * An exception generated by the invocation. May be <code>null</code>
73 */
74 private Throwable _exception;
75
76 /***
77 * The invoked method
78 */
79 private transient Method _method;
80
81 /***
82 * Construct a new <code>Response</code> for a successful invocation
83 *
84 * @param object the result of the invocation
85 * @param method the invoked method
86 */
87 public Response(Object object, Method method) {
88 _object = object;
89 _method = method;
90 }
91
92 /***
93 * Construct a new <code>Response</code> for a failed invocation
94 *
95 * @param exception the exception generated by the invocation
96 */
97 public Response(Throwable exception) {
98 _exception = exception;
99 }
100
101 /***
102 * Returns the result of the invocation
103 *
104 * @return the result of the invocation
105 */
106 public Object getObject() {
107 return _object;
108 }
109
110 /***
111 * Returns the exception generated by the invocation
112 *
113 * @return the exception, or <code>null</code> if no exception was thrown
114 */
115 public Throwable getException() {
116 return _exception;
117 }
118
119 /***
120 * Determines if the response is a successful return
121 *
122 * @return <code>true</code> if the response is a successful return
123 */
124 public boolean isReturn() {
125 return (_exception == null);
126 }
127
128 /***
129 * Determines if the response is an exception
130 *
131 * @return <code>true</code> if the response is an exception
132 */
133 public boolean isException() {
134 return !isReturn();
135 }
136
137 /***
138 * Writes this to a stream
139 *
140 * @param out the stream to write to
141 * @throws IOException if an I/O error occurs
142 */
143 public void write(ObjectOutput out) throws IOException {
144 boolean normal = isReturn();
145 out.writeBoolean(normal);
146 if (normal) {
147 Class type = _method.getReturnType();
148 if (type != void.class) {
149 SerializationHelper.write(type, _object, out);
150 }
151 } else {
152 out.writeObject(_exception);
153 }
154 }
155
156 /***
157 * Read a response from a stream
158 *
159 * @param in the stream to read from
160 * @param method the method that the response relates to
161 * @return the read response
162 * @throws ClassNotFoundException if the response contains a class which
163 * can't be found
164 * @throws IOException if an I/O error occurs
165 */
166 public static Response read(ObjectInput in, Method method)
167 throws ClassNotFoundException, IOException {
168 Response response = null;
169 boolean returnResponse = in.readBoolean();
170 if (returnResponse) {
171 Class type = method.getReturnType();
172 Object object = null;
173 if (type != void.class) {
174 object = SerializationHelper.read(type, in);
175 }
176 response = new Response(object, method);
177 } else {
178 Object object = in.readObject();
179 response = new Response((Throwable) object);
180 }
181 return response;
182 }
183
184 }