KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ServiceDaemon


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 /* @version $Id: ServiceDaemon.java 155409 2005-02-26 12:57:06Z dirkv $ */
18
19 import java.io.*;
20 import java.net.*;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import org.apache.commons.daemon.Daemon;
25 import org.apache.commons.daemon.DaemonController;
26 import org.apache.commons.daemon.DaemonContext;
27
28 import org.apache.commons.collections.ExtendedProperties;
29 import java.io.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 public class ServiceDaemon implements Daemon {
33
34     private ExtendedProperties prop = null;
35     private Process JavaDoc proc[] = null;
36     private ServiceDaemonReadThread readout[] = null;
37     private ServiceDaemonReadThread readerr[] = null;
38
39     public ServiceDaemon() {
40         super();
41         System.err.println("ServiceDaemon: instance "+this.hashCode()+
42                            " created");
43     }
44
45     protected void finalize() {
46         System.err.println("ServiceDaemon: instance "+this.hashCode()+
47                            " garbage collected");
48     }
49
50     /**
51      * init and destroy were added in jakarta-tomcat-daemon.
52      */

53     public void init(DaemonContext context)
54     throws Exception JavaDoc {
55         /* Set the err */
56         System.setErr(new PrintStream(new FileOutputStream(new File("ServiceDaemon.err")),true));
57         System.err.println("ServiceDaemon: instance "+this.hashCode()+
58                            " init");
59
60         /* read the properties file */
61         prop = new ExtendedProperties("startfile");
62
63         /* create an array to store the processes */
64     int i=0;
65         for (Iterator JavaDoc e = prop.getKeys(); e.hasNext() ;) {
66             e.next();
67             i++;
68         }
69         System.err.println("ServiceDaemon: init for " + i + " processes");
70         proc = new Process JavaDoc[i];
71         readout = new ServiceDaemonReadThread[i];
72         readerr = new ServiceDaemonReadThread[i];
73         for (i=0;i<proc.length;i++) {
74             proc[i] = null;
75             readout[i] = null;
76             readerr[i] = null;
77         }
78
79         System.err.println("ServiceDaemon: init done ");
80
81     }
82
83     public void start() {
84         /* Dump a message */
85         System.err.println("ServiceDaemon: starting");
86
87         /* Start */
88     int i=0;
89         for (Iterator JavaDoc e = prop.getKeys(); e.hasNext() ;) {
90            String JavaDoc name = (String JavaDoc) e.next();
91            System.err.println("ServiceDaemon: starting: " + name + " : " + prop.getString(name));
92            try {
93                proc[i] = Runtime.getRuntime().exec(prop.getString(name));
94            } catch(Exception JavaDoc ex) {
95                System.err.println("Exception: " + ex);
96            }
97            /* Start threads to read from Error and Out streams */
98            readerr[i] =
99                new ServiceDaemonReadThread(proc[i].getErrorStream());
100            readout[i] =
101                new ServiceDaemonReadThread(proc[i].getInputStream());
102            readerr[i].start();
103            readout[i].start();
104            i++;
105         }
106     }
107
108     public void stop()
109     throws IOException JavaDoc, InterruptedException JavaDoc {
110         /* Dump a message */
111         System.err.println("ServiceDaemon: stopping");
112
113         for (int i=0;i<proc.length;i++) {
114             if ( proc[i]==null)
115                continue;
116             proc[i].destroy();
117             try {
118                 proc[i].waitFor();
119             } catch(InterruptedException JavaDoc ex) {
120                 System.err.println("ServiceDaemon: exception while stopping:" +
121                                     ex);
122             }
123         }
124
125         System.err.println("ServiceDaemon: stopped");
126     }
127
128     public void destroy() {
129         System.err.println("ServiceDaemon: instance "+this.hashCode()+
130                            " destroy");
131     }
132
133 }
134
Popular Tags