KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > Client


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 "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP 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  * THE OPENEJB GROUP 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 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Client.java 2475 2006-02-21 00:18:55Z dblevins $
44  */

45 package org.openejb.client;
46
47 import java.io.IOException JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52 import java.io.OutputStream JavaDoc;
53 import java.rmi.RemoteException JavaDoc;
54
55 /**
56  *
57  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
58  * @since 11/25/2001
59  */

60 public class Client {
61
62     public static Response request(Request req, Response res, ServerMetaData server) throws RemoteException JavaDoc {
63         if ( server == null ) throw new IllegalArgumentException JavaDoc("Client request error: ServerMetaData cannot be null");
64
65         OutputStream JavaDoc out = null;
66         ObjectOutput JavaDoc objectOut = null;
67         ObjectInput JavaDoc objectIn = null;
68         Connection conn = null;
69
70         try{
71             /*----------------------------*/
72             /* Get a connection to server */
73             /*----------------------------*/
74             try{
75                 conn = ConnectionManager.getConnection( server );
76             } catch (IOException JavaDoc e){
77                 throw new RemoteException JavaDoc("Cannot access server: "+server.getHost()+":"+server.getPort()+" Exception: ", e );
78             } catch (Throwable JavaDoc e){
79                 throw new RemoteException JavaDoc("Cannot access server: "+server.getHost()+":"+server.getPort()+" due to an unkown exception in the OpenEJB client: ", e );
80             }
81
82             /*----------------------------------*/
83             /* Get output streams */
84             /*----------------------------------*/
85             try{
86
87                 out = conn.getOuputStream();
88
89             } catch (IOException JavaDoc e){
90                 throw new RemoteException JavaDoc("Cannot open output stream to server: " , e );
91
92             } catch (Throwable JavaDoc e){
93                 throw new RemoteException JavaDoc("Cannot open output stream to server: " , e );
94             }
95
96             /*----------------------------------*/
97             /* Write request type */
98             /*----------------------------------*/
99             try{
100
101                 out.write( req.getRequestType() );
102
103             } catch (IOException JavaDoc e){
104                 throw new RemoteException JavaDoc("Cannot write the request type to the server: " , e );
105
106             } catch (Throwable JavaDoc e){
107                 throw new RemoteException JavaDoc("Cannot write the request type to the server: " , e );
108             }
109
110             /*----------------------------------*/
111             /* Get output streams */
112             /*----------------------------------*/
113             try{
114
115                 objectOut = new ObjectOutputStream JavaDoc( out );
116
117             } catch (IOException JavaDoc e){
118                 throw new RemoteException JavaDoc("Cannot open object output stream to server: " , e );
119
120             } catch (Throwable JavaDoc e){
121                 throw new RemoteException JavaDoc("Cannot open object output stream to server: " , e );
122             }
123
124
125             /*----------------------------------*/
126             /* Write request */
127             /*----------------------------------*/
128             try{
129
130                 // Write the request data.
131
req.writeExternal( objectOut );
132                 objectOut.flush();
133
134             } catch (java.io.NotSerializableException JavaDoc e){
135                 //TODO:3: This doesn't seem to work in the OpenEJB test suite
136
// run some other test to see if the exception reaches the client.
137
throw new IllegalArgumentException JavaDoc("Object is not serializable: "+ e.getMessage());
138
139             } catch (IOException JavaDoc e){
140                 throw new RemoteException JavaDoc("Cannot write the request to the server: " , e );
141
142             } catch (Throwable JavaDoc e){
143                 throw new RemoteException JavaDoc("Cannot write the request to the server: " , e );
144             }
145
146             /*----------------------------------*/
147             /* Get input streams */
148             /*----------------------------------*/
149             try{
150
151                 objectIn = new ObjectInputStream JavaDoc(conn.getInputStream());
152             } catch (IOException JavaDoc e){
153                 throw new RemoteException JavaDoc("Cannot open object input stream to server: " , e );
154
155             } catch (Throwable JavaDoc e){
156                 throw new RemoteException JavaDoc("Cannot open object input stream to server: " , e );
157             }
158
159             /*----------------------------------*/
160             /* Read response */
161             /*----------------------------------*/
162             try{
163                 // Read the response from the server
164
res.readExternal( objectIn );
165             } catch (ClassNotFoundException JavaDoc e){
166                 throw new RemoteException JavaDoc("Cannot read the response from the server. The class for an object being returned is not located in this system:" , e );
167
168             } catch (IOException JavaDoc e){
169                 throw new RemoteException JavaDoc("Cannot read the response from the server." , e );
170
171             } catch (Throwable JavaDoc e){
172                 throw new RemoteException JavaDoc("Error reading response from server: " , e );
173             }
174
175         } catch ( Throwable JavaDoc error ) {
176             throw new RemoteException JavaDoc("Error while communicating with server: " , error );
177
178         } finally {
179             try {
180                 conn.close();
181             } catch (Throwable JavaDoc t){
182                 //TODO:2: Log this
183
System.out.println("Error closing connection with server: "+t.getMessage() );
184             }
185         }
186         return res;
187     }
188
189 }
190
191
192
Popular Tags