KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > ServiceDaemon


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

45 package org.openejb.server;
46
47 import java.io.*;
48 import java.net.*;
49 import java.util.*;
50 import org.openejb.*;
51 import org.openejb.util.SafeProperties;
52
53 /**
54  * The Server will call the following methods.
55  *
56  * newInstance() init( port, properties) start() stop()
57  *
58  * All ServerService implementations must have a no argument constructor.
59  *
60  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
61  */

62 public class ServiceDaemon implements ServerService, Runnable JavaDoc {
63
64     ServerService next;
65
66     Properties props;
67     String JavaDoc ip;
68     int port;
69     String JavaDoc name;
70
71     ServerSocket serverSocket;
72
73     /**
74      * We start out in a "stopped" state until someone calls the start method.
75      */

76     boolean stop = true;
77
78     public ServiceDaemon(ServerService next) {
79         this.next = next;
80     }
81
82     /**
83      * Pulls out the access log information
84      *
85      * @param props
86      *
87      * @exception ServiceException
88      */

89     public void init(Properties props) throws Exception JavaDoc {
90         // Do our stuff
91
this.props = props;
92
93         String JavaDoc p = props.getProperty("port");
94         ip = props.getProperty("bind");
95         name = props.getProperty("name");
96
97         port = Integer.parseInt(p);
98         // Then call the next guy
99
next.init(props);
100     }
101
102     public void start() throws ServiceException {
103         synchronized (this) {
104             // Don't bother if we are already started/starting
105
if (!stop)
106                 return;
107
108             stop = false;
109             // Do our stuff
110
try {
111 // serverSocket = new ServerSocket(port, 20, InetAddress.getByName(ip));
112
serverSocket = new ServerSocket(port, 20);
113                 port = serverSocket.getLocalPort();
114                 ip = serverSocket.getInetAddress().getHostAddress();
115
116                 Thread JavaDoc d = new Thread JavaDoc(this);
117                 d.setName("service." + next.getName() + "@" + d.hashCode());
118                 d.setDaemon(true);
119                 d.start();
120             } catch (Exception JavaDoc e) {
121                 throw new ServiceException("Service failed to start.", e);
122                 //e.printStackTrace();
123
}
124
125             // Then call the next guy
126
next.start();
127         }
128     }
129
130     public void stop() throws ServiceException {
131         // Do our stuff
132
synchronized (this) {
133             // Don't bother if we are already stopped/stopping
134
if (stop)
135                 return;
136
137             //System.out.println("[] sending stop signal");
138
stop = true;
139             try {
140                 this.notifyAll();
141             } catch (Throwable JavaDoc t) {
142                 t.printStackTrace();
143                 //logger.error("Unable to notify the server thread to stop.
144
// Received exception: "+t.getClass().getName()+" :
145
// "+t.getMessage());
146
}
147             // Then call the next guy
148
next.stop();
149         }
150     }
151
152     public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
153         throw new UnsupportedOperationException JavaDoc("service(in,out)");
154     }
155
156     public synchronized void service(final Socket socket)
157         throws ServiceException, IOException {
158         Thread JavaDoc d = new Thread JavaDoc(new Runnable JavaDoc() {
159             public void run() {
160                 try {
161                     next.service(socket);
162                 } catch (SecurityException JavaDoc e) {
163                     //logger.error( "Security error: "+ e.getMessage() );
164
} catch (Throwable JavaDoc e) {
165                     //logger.error( "Unexpected error", e );
166

167                 } finally {
168                     try {
169                         if (socket != null)
170                             socket.close();
171                     } catch (Throwable JavaDoc t) {
172                         //logger.error("Encountered problem while closing
173
// connection with client: "+t.getMessage());
174
}
175                 }
176             }
177         });
178         d.setDaemon(true);
179         d.start();
180     }
181
182     /**
183      * Gets the name of the service. Used for display purposes only
184      */

185     public String JavaDoc getName() {
186         return name;
187     }
188
189     /**
190      * Gets the ip number that the daemon is listening on.
191      */

192     public String JavaDoc getIP() {
193         return ip;
194     }
195
196     /**
197      * Gets the port number that the daemon is listening on.
198      */

199     public int getPort() {
200         return port;
201     }
202
203     public void run() {
204
205         Socket socket = null;
206
207         while (!stop) {
208             try {
209                 socket = serverSocket.accept();
210                 socket.setTcpNoDelay(true);
211                 if (!stop) service(socket);
212             } catch (SecurityException JavaDoc e) {
213                 //logger.error( "Security error: "+ e.getMessage() );
214
} catch (Throwable JavaDoc e) {
215                 //logger.error( "Unexpected error", e );
216

217             }
218         }
219     }
220 }
221
Popular Tags