KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AloneService


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: AloneService.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
25 import org.apache.commons.collections.ExtendedProperties;
26 import java.io.IOException JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /*
30  * That is like the ServiceDaemon but it does used the interface.
31  */

32 public class AloneService {
33
34     private ExtendedProperties prop = null;
35     private Process JavaDoc proc[] = null;
36     private ServiceDaemonReadThread readout[] = null;
37     private ServiceDaemonReadThread readerr[] = null;
38
39     protected void finalize() {
40         System.err.println("ServiceDaemon: instance "+this.hashCode()+
41                            " garbage collected");
42     }
43
44     /**
45      * init and destroy were added in jakarta-tomcat-daemon.
46      */

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