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.InputStream;
020: import java.io.ObjectInput;
021: import java.io.ObjectOutput;
022: import java.io.ObjectOutputStream;
023: import java.io.OutputStream;
024: import java.rmi.RemoteException;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: public class Client {
029: private static final Logger logger = Logger
030: .getLogger("OpenEJB.client");
031:
032: private static final ProtocolMetaData PROTOCOL_VERSION = new ProtocolMetaData(
033: "3.0");
034:
035: private static Client client = new Client();
036:
037: // This lame hook point if only of testing
038: public static void setClient(Client client) {
039: Client.client = client;
040: }
041:
042: public static Response request(Request req, Response res,
043: ServerMetaData server) throws RemoteException {
044: return client.processRequest(req, res, server);
045: }
046:
047: protected Response processRequest(Request req, Response res,
048: ServerMetaData server) throws RemoteException {
049: if (server == null)
050: throw new IllegalArgumentException(
051: "Server instance cannot be null");
052:
053: OutputStream out = null;
054: ObjectOutput objectOut = null;
055: ObjectInput objectIn = null;
056: Connection conn = null;
057:
058: try {
059: /*----------------------------*/
060: /* Get a connection to server */
061: /*----------------------------*/
062: conn = server.connect(req);
063:
064: /*----------------------------------*/
065: /* Get output streams */
066: /*----------------------------------*/
067: try {
068:
069: out = conn.getOuputStream();
070:
071: } catch (IOException e) {
072: throw new RemoteException(
073: "Cannot open output stream to server: ", e);
074:
075: } catch (Throwable e) {
076: throw new RemoteException(
077: "Cannot open output stream to server: ", e);
078: }
079:
080: /*----------------------------------*/
081: /* Write the protocol magic */
082: /*----------------------------------*/
083: try {
084:
085: PROTOCOL_VERSION.writeExternal(out);
086:
087: } catch (Throwable e) {
088: throw new RemoteException(
089: "Cannot write the protocol metadata to the server: ",
090: e);
091: }
092:
093: /*----------------------------------*/
094: /* Write request type */
095: /*----------------------------------*/
096: try {
097:
098: out.write(req.getRequestType());
099:
100: } catch (IOException e) {
101: throw new RemoteException(
102: "Cannot write the request type to the server: ",
103: e);
104:
105: } catch (Throwable e) {
106: throw new RemoteException(
107: "Cannot write the request type to the server: ",
108: e);
109: }
110:
111: /*----------------------------------*/
112: /* Get output streams */
113: /*----------------------------------*/
114: try {
115:
116: objectOut = new ObjectOutputStream(out);
117:
118: } catch (IOException e) {
119: throw new RemoteException(
120: "Cannot open object output stream to server: ",
121: e);
122:
123: } catch (Throwable e) {
124: throw new RemoteException(
125: "Cannot open object output stream to server: ",
126: e);
127: }
128:
129: /*----------------------------------*/
130: /* Write request */
131: /*----------------------------------*/
132: try {
133:
134: req.writeExternal(objectOut);
135: objectOut.flush();
136:
137: } catch (java.io.NotSerializableException e) {
138:
139: throw new IllegalArgumentException(
140: "Object is not serializable: " + e.getMessage());
141:
142: } catch (IOException e) {
143: throw new RemoteException(
144: "Cannot write the request to the server: ", e);
145:
146: } catch (Throwable e) {
147: throw new RemoteException(
148: "Cannot write the request to the server: ", e);
149: }
150:
151: /*----------------------------------*/
152: /* Get input streams */
153: /*----------------------------------*/
154: InputStream in = null;
155: try {
156:
157: in = conn.getInputStream();
158:
159: } catch (IOException e) {
160: throw new RemoteException(
161: "Cannot open input stream to server: ", e);
162: }
163:
164: ProtocolMetaData protocolMetaData = null;
165: try {
166:
167: protocolMetaData = new ProtocolMetaData();
168: protocolMetaData.readExternal(in);
169:
170: } catch (IOException e) {
171: throw new RemoteException(
172: "Cannot deternmine server protocol version: Received "
173: + protocolMetaData.getSpec(), e);
174: }
175:
176: try {
177:
178: objectIn = new EjbObjectInputStream(in);
179:
180: } catch (Throwable e) {
181: throw new RemoteException(
182: "Cannot open object input stream to server ("
183: + protocolMetaData.getSpec() + ") : "
184: + e.getMessage(), e);
185: }
186:
187: /*----------------------------------*/
188: /* Read response */
189: /*----------------------------------*/
190: try {
191:
192: res.readExternal(objectIn);
193: } catch (ClassNotFoundException e) {
194: throw new RemoteException(
195: "Cannot read the response from the server. The class for an object being returned is not located in this system:",
196: e);
197:
198: } catch (IOException e) {
199: throw new RemoteException(
200: "Cannot read the response from the server ("
201: + protocolMetaData.getSpec() + ") : "
202: + e.getMessage(), e);
203:
204: } catch (Throwable e) {
205: throw new RemoteException(
206: "Error reading response from server ("
207: + protocolMetaData.getSpec() + ") : "
208: + e.getMessage(), e);
209: }
210:
211: } catch (RemoteException e) {
212: throw e;
213: } catch (Throwable error) {
214: throw new RemoteException(
215: "Error while communicating with server: ", error);
216:
217: } finally {
218: try {
219: if (conn != null) {
220: conn.close();
221: }
222: } catch (Throwable t) {
223: logger.log(Level.WARNING,
224: "Error closing connection with server: "
225: + t.getMessage(), t);
226: }
227: }
228: return res;
229: }
230:
231: }
|