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;
017:
018: import java.io.*;
019: import java.net.*;
020: import java.util.*;
021:
022: /**
023: * @version $Rev: 598826 $ $Date: 2007-11-27 16:16:39 -0800 $
024: */
025: public class ServiceDaemon implements ServerService, Runnable {
026:
027: ServerService next;
028:
029: Properties props;
030: String ip;
031: int port;
032:
033: ServerSocket serverSocket;
034:
035: boolean stop = true;
036:
037: public ServiceDaemon(ServerService next) {
038: this .next = next;
039: }
040:
041: public ServiceDaemon(ServerService next, int port, String ip) {
042: this .port = port;
043: this .ip = ip;
044: this .next = next;
045: }
046:
047: public void init(Properties props) throws Exception {
048:
049: this .props = props;
050:
051: String p = props.getProperty("port");
052: ip = props.getProperty("bind");
053:
054: port = Integer.parseInt(p);
055:
056: next.init(props);
057: }
058:
059: public void start() throws ServiceException {
060: synchronized (this ) {
061:
062: if (!stop)
063: return;
064:
065: stop = false;
066:
067: try {
068: InetAddress address = InetAddress.getByName(ip);
069: serverSocket = new ServerSocket(port, 20, address);
070: // serverSocket = new ServerSocket(port, 20);
071: port = serverSocket.getLocalPort();
072: ip = serverSocket.getInetAddress().getHostAddress();
073:
074: Thread d = new Thread(this );
075: d.setName("service." + next.getName() + "@"
076: + d.hashCode());
077: d.setDaemon(true);
078: d.start();
079: } catch (Exception e) {
080: throw new ServiceException("Service failed to start.",
081: e);
082:
083: }
084:
085: next.start();
086: }
087: }
088:
089: public void stop() throws ServiceException {
090:
091: synchronized (this ) {
092:
093: if (stop)
094: return;
095:
096: stop = true;
097: try {
098: this .notifyAll();
099: } catch (Throwable t) {
100: t.printStackTrace();
101:
102: // Received exception: "+t.getClass().getName()+" :
103: // "+t.getMessage());
104: }
105:
106: next.stop();
107: }
108: }
109:
110: public void service(InputStream in, OutputStream out)
111: throws ServiceException, IOException {
112: throw new UnsupportedOperationException("service(in,out)");
113: }
114:
115: public synchronized void service(final Socket socket)
116: throws ServiceException, IOException {
117: Thread d = new Thread(new Runnable() {
118: public void run() {
119: try {
120: next.service(socket);
121: } catch (SecurityException e) {
122:
123: } catch (Throwable e) {
124:
125: } finally {
126: try {
127: if (socket != null)
128: socket.close();
129: } catch (Throwable t) {
130:
131: // connection with client: "+t.getMessage());
132: }
133: }
134: }
135: });
136: d.setDaemon(true);
137: d.start();
138: }
139:
140: public String getName() {
141: return next.getName();
142: }
143:
144: public String getIP() {
145: return ip;
146: }
147:
148: public int getPort() {
149: return port;
150: }
151:
152: public void run() {
153:
154: Socket socket = null;
155:
156: while (!stop) {
157: try {
158: socket = serverSocket.accept();
159: socket.setTcpNoDelay(true);
160: if (!stop)
161: service(socket);
162: } catch (SecurityException e) {
163:
164: } catch (Throwable e) {
165: e.printStackTrace();
166: }
167: }
168: }
169: }
|