KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > ejbd > EjbDaemon


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: EjbDaemon.java 2476 2006-02-21 08:45:52Z dblevins $
44  */

45 package org.openejb.server.ejbd;
46
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51 import java.io.OutputStream JavaDoc;
52 import java.net.Socket JavaDoc;
53 import java.rmi.RemoteException JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import org.openejb.DeploymentInfo;
57 import org.openejb.ProxyInfo;
58 import org.openejb.client.EJBRequest;
59 import org.openejb.client.RequestMethods;
60 import org.openejb.client.ResponseCodes;
61 import org.openejb.util.Logger;
62 import org.openejb.util.Messages;
63
64 /**
65  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
66  * @since 11/25/2001
67  */

68 public class EjbDaemon implements org.openejb.spi.ApplicationServer, ResponseCodes, RequestMethods {
69
70     Messages _messages = new Messages( "org.openejb.server.util.resources" );
71     Logger logger = Logger.getInstance( "OpenEJB.server.remote", "org.openejb.server.util.resources" );
72
73     Properties JavaDoc props;
74
75     ClientObjectFactory clientObjectFactory;
76     DeploymentIndex deploymentIndex;
77     EjbRequestHandler ejbHandler;
78     JndiRequestHandler jndiHandler;
79     AuthRequestHandler authHandler;
80
81     boolean stop = false;
82
83     static EjbDaemon thiss;
84
85     private EjbDaemon() {
86     }
87
88     public static EjbDaemon getEjbDaemon() {
89         if ( thiss == null ) {
90             thiss = new EjbDaemon();
91         }
92         return thiss;
93     }
94
95     public void init(Properties JavaDoc props) throws Exception JavaDoc{
96         this.props = props;
97         
98         deploymentIndex = new DeploymentIndex();
99
100         clientObjectFactory = new ClientObjectFactory(this);
101         
102         // Request Handlers
103
ejbHandler = new EjbRequestHandler(this);
104         jndiHandler = new JndiRequestHandler(this);
105         authHandler = new AuthRequestHandler(this);
106     }
107
108     public void service(Socket JavaDoc socket) throws IOException JavaDoc{
109         InputStream JavaDoc in = socket.getInputStream();
110         OutputStream JavaDoc out = socket.getOutputStream();
111
112         try {
113             service(in, out);
114         } finally {
115             try {
116                 if (socket != null) socket.close();
117             } catch (Throwable JavaDoc t) {
118                 logger.error("Encountered problem while closing connection with client: " + t.getMessage());
119             }
120         }
121     }
122
123     public void service(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
124         ObjectInputStream JavaDoc ois = null;
125         /**
126          * The ObjectOutputStream used to send outgoing response messages to the client.
127          */

128         ObjectOutputStream JavaDoc oos = null;
129
130
131         try {
132
133             //TODO: Implement multiple request processing
134
//while ( !stop ) {
135

136             // Read the request
137
byte requestType = (byte)in.read();
138
139             //if (requestType == -1) {continue;}
140
if (requestType == -1) {
141                 return;
142             }
143
144             ois = new ObjectInputStream JavaDoc( in );
145             oos = new ObjectOutputStream JavaDoc( out );
146
147             // Process the request
148
switch (requestType) {
149             case EJB_REQUEST: processEjbRequest(ois, oos); break;
150             case JNDI_REQUEST: processJndiRequest(ois, oos);break;
151             case AUTH_REQUEST: processAuthRequest(ois, oos);break;
152             default: logger.error("Unknown request type "+requestType);
153             }
154             try {
155                 if ( oos != null ) {
156                     oos.flush();
157                 }
158             } catch ( Throwable JavaDoc t ) {
159                 logger.error("Encountered problem while communicating with client: "+t.getMessage());
160             }
161             //}
162

163             // Exceptions should not be thrown from these methods
164
// They should handle their own exceptions and clean
165
// things up with the client accordingly.
166
} catch ( SecurityException JavaDoc e ) {
167             logger.error( "Security error: "+ e.getMessage() );
168         } catch ( Throwable JavaDoc e ) {
169             logger.error( "Unexpected error", e );
170             //System.out.println("ERROR: "+clienntIP.getHostAddress()+": " +e.getMessage());
171
} finally {
172             try {
173                 if ( oos != null ) {
174                     oos.flush();
175                 }
176             } catch ( Throwable JavaDoc t ) {
177                 logger.error("Encountered problem while flushing connection with client: " + t.getMessage());
178             }
179         }
180     }
181
182     protected DeploymentInfo getDeployment(EJBRequest req) throws RemoteException JavaDoc {
183         return deploymentIndex.getDeployment(req);
184     }
185
186     public void processEjbRequest (ObjectInputStream JavaDoc in, ObjectOutputStream JavaDoc out) {
187         ejbHandler.processRequest(in,out);
188     }
189
190     public void processJndiRequest(ObjectInputStream JavaDoc in, ObjectOutputStream JavaDoc out) throws Exception JavaDoc{
191         jndiHandler.processRequest(in,out);
192     }
193
194     public void processAuthRequest(ObjectInputStream JavaDoc in, ObjectOutputStream JavaDoc out) {
195         authHandler.processRequest(in,out);
196     }
197
198     //=============================================================
199
// ApplicationServer interface methods
200
//=============================================================
201
public javax.ejb.EJBMetaData JavaDoc getEJBMetaData(ProxyInfo info) {
202         return clientObjectFactory.getEJBMetaData(info);
203     }
204
205     public javax.ejb.Handle JavaDoc getHandle(ProxyInfo info) {
206         return clientObjectFactory.getHandle(info);
207     }
208
209     public javax.ejb.HomeHandle JavaDoc getHomeHandle(ProxyInfo info) {
210         return clientObjectFactory.getHomeHandle(info);
211     }
212
213     public javax.ejb.EJBObject JavaDoc getEJBObject(ProxyInfo info) {
214         return clientObjectFactory.getEJBObject(info);
215     }
216
217     public javax.ejb.EJBHome JavaDoc getEJBHome(ProxyInfo info) {
218         return clientObjectFactory.getEJBHome(info);
219     }
220
221 }
222
223
Popular Tags