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.webadmin.httpd;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.InetAddress;
022: import java.net.Socket;
023: import java.net.URL;
024: import java.util.Properties;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028:
029: import org.apache.openejb.server.ServerService;
030: import org.apache.openejb.server.ServiceException;
031: import org.apache.openejb.util.Logger;
032: import org.apache.openejb.util.LogCategory;
033: import org.apache.openejb.webadmin.HttpHome;
034: import org.apache.openejb.webadmin.HttpObject;
035:
036: /**
037: * This is the main class for the web administration. It takes care of the
038: * processing from the browser, sockets and threading.
039: *
040: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
041: * @author <a href="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
042: * @since 11/25/2001
043: */
044: public class HttpServer implements ServerService {
045:
046: private static final Logger logger = Logger.getInstance(
047: LogCategory.OPENEJB_SERVER, HttpServer.class);
048: private InitialContext jndiContext;
049:
050: public void service(InputStream in, OutputStream out)
051: throws ServiceException, IOException {
052: throw new UnsupportedOperationException("service(in,out)");
053: }
054:
055: public void service(Socket socket) throws ServiceException,
056: IOException {
057: /**
058: * The InputStream used to receive incoming messages from the client.
059: */
060: InputStream in = socket.getInputStream();
061: /**
062: * The OutputStream used to send outgoing response messages to the client.
063: */
064: OutputStream out = socket.getOutputStream();
065:
066: try {
067: processRequest(socket, in, out);
068: } catch (Throwable e) {
069: logger.error("Unexpected error", e);
070: } finally {
071: try {
072: if (out != null) {
073: out.flush();
074: out.close();
075: }
076: if (in != null)
077: in.close();
078: if (socket != null)
079: socket.close();
080: } catch (Throwable t) {
081: logger
082: .error("Encountered problem while closing connection with client: "
083: + t.getMessage());
084: }
085: }
086: }
087:
088: public void start() throws ServiceException {
089: }
090:
091: public void stop() throws ServiceException {
092: }
093:
094: public String getName() {
095: return "webadmin";
096: }
097:
098: public int getPort() {
099: return 0;
100: }
101:
102: public String getIP() {
103: return "";
104: }
105:
106: /** Initalizes this instance and takes care of starting things up
107: * @param props a properties instance for system properties
108: * @throws Exception if an exeption is thrown
109: */
110: public void init(Properties props) throws Exception {
111:
112: //props.putAll(System.getProperties());
113:
114: Properties properties = new Properties();
115: properties
116: .put(Context.INITIAL_CONTEXT_FACTORY,
117: "org.apache.openejb.core.ivm.naming.InitContextFactory");
118: jndiContext = new InitialContext(properties);
119:
120: }
121:
122: /**
123: * takes care of processing requests and creating the webadmin ejb's
124: *
125: * @param in the input stream from the browser
126: * @param out the output stream to the browser
127: */
128: private void processRequest(Socket socket, InputStream in,
129: OutputStream out) {
130:
131: HttpRequestImpl req = new HttpRequestImpl();
132: HttpResponseImpl res = new HttpResponseImpl();
133: InetAddress client = socket.getInetAddress();
134:
135: try {
136: req.readMessage(in);
137: res.setRequest(req);
138: // logger.info(client.getHostName()+": "+req.getRequestLine());
139: } catch (Throwable t) {
140: //TODO: log or something
141: //t.printStackTrace();
142: res = HttpResponseImpl.createError(
143: "Could not read the request.\n"
144: + t.getClass().getName() + ":\n"
145: + t.getMessage(), t);
146: try {
147: logger.error(client.getHostName() + ": "
148: + res.getCode() + " " + req.getRequestLine()
149: + " [" + res.getResponseString() + "]");
150: res.writeMessage(out);
151: } catch (Throwable t2) {
152: //TODO: log or something
153: //t2.printStackTrace();
154: }
155: return;
156: }
157:
158: //System.out.println("[] read");
159: URL uri = null;
160: String file = null;
161:
162: try {
163: uri = req.getURI();
164: file = uri.getFile();
165: int querry = file.indexOf("?");
166: if (querry != -1) {
167: file = file.substring(0, querry);
168: }
169:
170: //System.out.println("[] file="+file);
171:
172: } catch (Throwable t) {
173: //TODO: log or something
174: //t.printStackTrace();
175: res = HttpResponseImpl
176: .createError("Could not determine the module "
177: + file + "\n" + t.getClass().getName()
178: + ":\n" + t.getMessage());
179: try {
180: logger.error(client.getHostName() + ": "
181: + res.getCode() + " " + req.getRequestLine()
182: + " [" + res.getResponseString() + "]");
183: res.writeMessage(out);
184: } catch (Throwable t2) {
185: //TODO: log or something
186: //t2.printStackTrace();
187: }
188: return;
189: }
190:
191: HttpObject httpObject = null;
192:
193: try {
194: httpObject = getHttpObject(file);
195: //System.out.println("[] module="+httpObject);
196: } catch (Throwable t) {
197: //TODO: log or something
198: //t.printStackTrace();
199: res = HttpResponseImpl.createError(
200: "Could not load the module " + file + "\n"
201: + t.getClass().getName() + ":\n"
202: + t.getMessage(), t);
203: //System.out.println("[] res="+res);
204: try {
205: logger.error(client.getHostName() + ": "
206: + res.getCode() + " " + req.getRequestLine()
207: + " [" + res.getResponseString() + "]");
208: res.writeMessage(out);
209: } catch (Throwable t2) {
210: //TODO: log or something
211: //t2.printStackTrace();
212: }
213: return;
214: }
215:
216: try {
217: httpObject.onMessage(req, res);
218: } catch (Throwable t) {
219: //TODO: log or something
220: //t.printStackTrace();
221: res = HttpResponseImpl.createError(
222: "Error occurred while executing the module " + file
223: + "\n" + t.getClass().getName() + ":\n"
224: + t.getMessage(), t);
225: try {
226: logger.error(client.getHostName() + ": "
227: + res.getCode() + " " + req.getRequestLine()
228: + " [" + res.getResponseString() + "]");
229: res.writeMessage(out);
230: } catch (Throwable t2) {
231: //TODO: log or something
232: //t2.printStackTrace();
233: }
234:
235: return;
236: }
237:
238: try {
239: logger.info(client.getHostName() + ": " + res.getCode()
240: + " " + req.getRequestLine() + " ["
241: + res.getResponseString() + "]");
242: res.writeMessage(out);
243: } catch (Throwable t) {
244: //TODO: log or something
245: //t.printStackTrace();
246: return;
247: }
248: }
249:
250: /** gets an ejb object reference for use in <code>processRequest</code>
251: * @param beanName the name of the ejb to look up
252: * @throws IOException if an exception is thrown
253: * @return an object reference of the ejb
254: */
255: private HttpObject getHttpObject(String beanName)
256: throws IOException {
257: Object obj = null;
258:
259: //check for no name, add something here later
260: if (beanName.equals("/")) {
261: try {
262: obj = jndiContext.lookup("Webadmin/Home");
263: } catch (javax.naming.NamingException ne) {
264: throw new IOException(ne.getMessage());
265: }
266: } else {
267: try {
268: obj = jndiContext.lookup(beanName);
269: } catch (javax.naming.NameNotFoundException e) {
270: try {
271: obj = jndiContext.lookup("httpd/DefaultBean");
272: } catch (javax.naming.NamingException ne) {
273: throw new IOException(ne.getMessage());
274: }
275: } catch (javax.naming.NamingException e) {
276: throw new IOException(e.getMessage());
277: }
278: }
279:
280: HttpHome ejbHome = (HttpHome) obj;
281: HttpObject httpObject = null;
282:
283: try {
284: httpObject = ejbHome.create();
285:
286: //
287: obj = org.apache.openejb.util.proxy.ProxyManager
288: .getInvocationHandler(httpObject);
289: org.apache.openejb.core.ivm.BaseEjbProxyHandler handler = null;
290: handler = (org.apache.openejb.core.ivm.BaseEjbProxyHandler) obj;
291: handler.setIntraVmCopyMode(false);
292: } catch (javax.ejb.CreateException cre) {
293: throw new IOException(cre.getMessage());
294: }
295:
296: return httpObject;
297: }
298: }
|