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.server.ejbd;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.OutputStream;
023: import java.net.Socket;
024: import java.rmi.RemoteException;
025: import java.util.Properties;
026:
027: import org.apache.openejb.DeploymentInfo;
028: import org.apache.openejb.ProxyInfo;
029: import org.apache.openejb.loader.SystemInstance;
030: import org.apache.openejb.spi.ContainerSystem;
031: import org.apache.openejb.client.EJBRequest;
032: import org.apache.openejb.client.RequestMethodConstants;
033: import org.apache.openejb.client.EjbObjectInputStream;
034: import org.apache.openejb.client.ProtocolMetaData;
035: import org.apache.openejb.util.LogCategory;
036: import org.apache.openejb.util.Logger;
037: import org.apache.openejb.util.Messages;
038:
039: public class EjbDaemon implements
040: org.apache.openejb.spi.ApplicationServer {
041:
042: private static final ProtocolMetaData PROTOCOL_VERSION = new ProtocolMetaData(
043: "2.0");
044:
045: private static final Messages _messages = new Messages(
046: "org.apache.openejb.server.util.resources");
047: static final Logger logger = Logger.getInstance(
048: LogCategory.OPENEJB_SERVER_REMOTE,
049: "org.apache.openejb.server.util.resources");
050:
051: private ClientObjectFactory clientObjectFactory;
052: // DeploymentIndex deploymentIndex;
053: private EjbRequestHandler ejbHandler;
054: private JndiRequestHandler jndiHandler;
055: private AuthRequestHandler authHandler;
056:
057: boolean stop = false;
058:
059: static EjbDaemon this s;
060: private ContainerSystem containerSystem;
061:
062: private EjbDaemon() {
063: }
064:
065: public static EjbDaemon getEjbDaemon() {
066: if (this s == null) {
067: this s = new EjbDaemon();
068: }
069: return this s;
070: }
071:
072: public void init(Properties props) throws Exception {
073: containerSystem = SystemInstance.get().getComponent(
074: ContainerSystem.class);
075: // deploymentIndex = new DeploymentIndex(containerSystem.deployments());
076:
077: clientObjectFactory = new ClientObjectFactory(this , props);
078:
079: ejbHandler = new EjbRequestHandler(this );
080: jndiHandler = new JndiRequestHandler(this );
081: authHandler = new AuthRequestHandler(this );
082: }
083:
084: public void service(Socket socket) throws IOException {
085: InputStream in = socket.getInputStream();
086: OutputStream out = socket.getOutputStream();
087:
088: try {
089: service(in, out);
090: } finally {
091: try {
092: if (socket != null)
093: socket.close();
094: } catch (Throwable t) {
095: logger
096: .error("Encountered problem while closing connection with client: "
097: + t.getMessage());
098: }
099: }
100: }
101:
102: public void service(InputStream in, OutputStream out)
103: throws IOException {
104: ProtocolMetaData protocolMetaData = new ProtocolMetaData();
105: String requestTypeName = null;
106:
107: ObjectInputStream ois = null;
108: ObjectOutputStream oos = null;
109:
110: try {
111:
112: protocolMetaData.readExternal(in);
113:
114: PROTOCOL_VERSION.writeExternal(out);
115:
116: byte requestType = (byte) in.read();
117:
118: if (requestType == -1) {
119: return;
120: }
121:
122: ois = new EjbObjectInputStream(in);
123: oos = new ObjectOutputStream(out);
124:
125: // Exceptions should not be thrown from these methods
126: // They should handle their own exceptions and clean
127: // things up with the client accordingly.
128: switch (requestType) {
129: case RequestMethodConstants.EJB_REQUEST:
130: requestTypeName = "EJB_REQUEST";
131: processEjbRequest(ois, oos);
132: break;
133: case RequestMethodConstants.JNDI_REQUEST:
134: requestTypeName = "JNDI_REQUEST";
135: processJndiRequest(ois, oos);
136: break;
137: case RequestMethodConstants.AUTH_REQUEST:
138: requestTypeName = "AUTH_REQUEST";
139: processAuthRequest(ois, oos);
140: break;
141: default:
142: requestTypeName = requestType + " (UNKNOWN)";
143: logger.error("\"" + requestTypeName + " "
144: + protocolMetaData.getSpec()
145: + "\" FAIL \"Unknown request type "
146: + requestType);
147:
148: }
149: } catch (SecurityException e) {
150: logger.error("\"" + requestTypeName + " "
151: + protocolMetaData.getSpec()
152: + "\" FAIL \"Security error - " + e.getMessage()
153: + "\"", e);
154: } catch (Throwable e) {
155: logger.error("\"" + requestTypeName + " "
156: + protocolMetaData.getSpec()
157: + "\" FAIL \"Unexpected error - " + e.getMessage()
158: + "\"", e);
159: } finally {
160: try {
161: if (oos != null) {
162: oos.flush();
163: oos.close();
164: } else if (out != null) {
165: out.flush();
166: out.close();
167: }
168: } catch (Throwable t) {
169: logger.error("\"" + requestTypeName + " "
170: + protocolMetaData.getSpec() + "\" FAIL \""
171: + t.getMessage() + "\"");
172: }
173: }
174: }
175:
176: protected DeploymentInfo getDeployment(EJBRequest req)
177: throws RemoteException {
178: String deploymentId = req.getDeploymentId();
179: DeploymentInfo deploymentInfo = containerSystem
180: .getDeploymentInfo(deploymentId);
181: if (deploymentInfo == null)
182: throw new RemoteException("No deployment: " + deploymentId);
183: return deploymentInfo;
184: }
185:
186: public void processEjbRequest(ObjectInputStream in,
187: ObjectOutputStream out) {
188: ejbHandler.processRequest(in, out);
189: }
190:
191: public void processJndiRequest(ObjectInputStream in,
192: ObjectOutputStream out) throws Exception {
193: jndiHandler.processRequest(in, out);
194: }
195:
196: public void processAuthRequest(ObjectInputStream in,
197: ObjectOutputStream out) {
198: authHandler.processRequest(in, out);
199: }
200:
201: public javax.ejb.EJBMetaData getEJBMetaData(ProxyInfo info) {
202: return clientObjectFactory.getEJBMetaData(info);
203: }
204:
205: public javax.ejb.Handle getHandle(ProxyInfo info) {
206: return clientObjectFactory.getHandle(info);
207: }
208:
209: public javax.ejb.HomeHandle getHomeHandle(ProxyInfo info) {
210: return clientObjectFactory.getHomeHandle(info);
211: }
212:
213: public javax.ejb.EJBObject getEJBObject(ProxyInfo info) {
214: return clientObjectFactory.getEJBObject(info);
215: }
216:
217: public Object getBusinessObject(ProxyInfo info) {
218: return clientObjectFactory.getBusinessObject(info);
219: }
220:
221: public javax.ejb.EJBHome getEJBHome(ProxyInfo info) {
222: return clientObjectFactory.getEJBHome(info);
223: }
224:
225: }
|