1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.openejb.server.ejbd;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.OutputStream;
24 import java.net.Socket;
25 import java.rmi.RemoteException;
26 import java.util.Properties;
27
28 import org.apache.openejb.DeploymentInfo;
29 import org.apache.openejb.ProxyInfo;
30 import org.apache.openejb.loader.SystemInstance;
31 import org.apache.openejb.spi.ContainerSystem;
32 import org.apache.openejb.client.EJBRequest;
33 import org.apache.openejb.client.RequestMethodConstants;
34 import org.apache.openejb.client.EjbObjectInputStream;
35 import org.apache.openejb.client.ProtocolMetaData;
36 import org.apache.openejb.util.LogCategory;
37 import org.apache.openejb.util.Logger;
38 import org.apache.openejb.util.Messages;
39
40 public class EjbDaemon implements org.apache.openejb.spi.ApplicationServer {
41
42 private static final ProtocolMetaData PROTOCOL_VERSION = new ProtocolMetaData("3.0");
43
44 private static final Messages _messages = new Messages("org.apache.openejb.server.util.resources");
45 static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_SERVER_REMOTE, "org.apache.openejb.server.util.resources");
46
47 private ClientObjectFactory clientObjectFactory;
48 // DeploymentIndex deploymentIndex;
49 private EjbRequestHandler ejbHandler;
50 private JndiRequestHandler jndiHandler;
51 private AuthRequestHandler authHandler;
52
53 boolean stop = false;
54
55 static EjbDaemon thiss;
56 private ContainerSystem containerSystem;
57
58 private EjbDaemon() {
59 }
60
61 public static EjbDaemon getEjbDaemon() {
62 if (thiss == null) {
63 thiss = new EjbDaemon();
64 }
65 return thiss;
66 }
67
68 public void init(Properties props) throws Exception {
69 containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
70 // deploymentIndex = new DeploymentIndex(containerSystem.deployments());
71
72 clientObjectFactory = new ClientObjectFactory(this, props);
73
74 ejbHandler = new EjbRequestHandler(this);
75 jndiHandler = new JndiRequestHandler(this);
76 authHandler = new AuthRequestHandler(this);
77 }
78
79 public void service(Socket socket) throws IOException {
80 InputStream in = socket.getInputStream();
81 OutputStream out = socket.getOutputStream();
82
83 try {
84 service(in, out);
85 } finally {
86 try {
87 if (socket != null) socket.close();
88 } catch (Throwable t) {
89 logger.error("Encountered problem while closing connection with client: " + t.getMessage());
90 }
91 }
92 }
93
94 public void service(InputStream in, OutputStream out) throws IOException {
95 ProtocolMetaData protocolMetaData = new ProtocolMetaData();
96 String requestTypeName = null;
97
98 ObjectInputStream ois = null;
99 ObjectOutputStream oos = null;
100
101 try {
102
103 protocolMetaData.readExternal(in);
104
105 PROTOCOL_VERSION.writeExternal(out);
106
107 byte requestType = (byte) in.read();
108
109 if (requestType == -1) {
110 return;
111 }
112
113 ois = new EjbObjectInputStream(in);
114 oos = new ObjectOutputStream(out);
115
116 // Exceptions should not be thrown from these methods
117 // They should handle their own exceptions and clean
118 // things up with the client accordingly.
119 switch (requestType) {
120 case RequestMethodConstants.EJB_REQUEST:
121 requestTypeName = "EJB_REQUEST";
122 processEjbRequest(ois, oos);
123 break;
124 case RequestMethodConstants.JNDI_REQUEST:
125 requestTypeName = "JNDI_REQUEST";
126 processJndiRequest(ois, oos);
127 break;
128 case RequestMethodConstants.AUTH_REQUEST:
129 requestTypeName = "AUTH_REQUEST";
130 processAuthRequest(ois, oos);
131 break;
132 default:
133 requestTypeName = requestType+" (UNKNOWN)";
134 logger.error("\"" + requestTypeName + " " + protocolMetaData.getSpec() + "\" FAIL \"Unknown request type " + requestType);
135
136 }
137 } catch (SecurityException e) {
138 logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \"Security error - "+e.getMessage()+"\"",e);
139 } catch (Throwable e) {
140 logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \"Unexpected error - "+e.getMessage()+"\"",e);
141 } finally {
142 try {
143 if (oos != null) {
144 oos.flush();
145 oos.close();
146 } else if (out != null) {
147 out.flush();
148 out.close();
149 }
150 } catch (Throwable t) {
151 logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \""+t.getMessage()+"\"");
152 }
153 }
154 }
155
156 protected DeploymentInfo getDeployment(EJBRequest req) throws RemoteException {
157 String deploymentId = req.getDeploymentId();
158 DeploymentInfo deploymentInfo = containerSystem.getDeploymentInfo(deploymentId);
159 if (deploymentInfo == null) throw new RemoteException("No deployment: "+deploymentId);
160 return deploymentInfo;
161 }
162
163 public void processEjbRequest(ObjectInputStream in, ObjectOutputStream out) {
164 ejbHandler.processRequest(in, out);
165 }
166
167 public void processJndiRequest(ObjectInputStream in, ObjectOutputStream out) throws Exception {
168 jndiHandler.processRequest(in, out);
169 }
170
171 public void processAuthRequest(ObjectInputStream in, ObjectOutputStream out) {
172 authHandler.processRequest(in, out);
173 }
174
175 public javax.ejb.EJBMetaData getEJBMetaData(ProxyInfo info) {
176 return clientObjectFactory.getEJBMetaData(info);
177 }
178
179 public javax.ejb.Handle getHandle(ProxyInfo info) {
180 return clientObjectFactory.getHandle(info);
181 }
182
183 public javax.ejb.HomeHandle getHomeHandle(ProxyInfo info) {
184 return clientObjectFactory.getHomeHandle(info);
185 }
186
187 public javax.ejb.EJBObject getEJBObject(ProxyInfo info) {
188 return clientObjectFactory.getEJBObject(info);
189 }
190
191 public Object getBusinessObject(ProxyInfo info) {
192 return clientObjectFactory.getBusinessObject(info);
193 }
194
195 public javax.ejb.EJBHome getEJBHome(ProxyInfo info) {
196 return clientObjectFactory.getEJBHome(info);
197 }
198
199 }
200