KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > win32 > Service


1 // ========================================================================
2
// $Id: Service.java,v 1.8 2005/08/13 00:01:27 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.win32;
17 import java.io.File JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.mortbay.log.LogFactory;
24 import org.mortbay.http.HttpServer;
25 import org.mortbay.jetty.Server;
26 import org.mortbay.util.LogSupport;
27
28 /* ------------------------------------------------------------ */
29 /** Run Jetty as a Win32 service.
30  *
31  * System.out and System.err output can be controlled with java
32  * properties: SERVICE_OUT and SERVICE_ERR.
33  * The log file can be controlled with the property SERVICE_LOG_FILE
34  * Derived from SCMEventManager.java by Bill Giel/KC Multimedia and Design Group, Inc.,
35  * <h4>Example</h4>
36  * <pre>
37  * jettysvc -c -DSERVICE_OUT="./logs/jettysvc.out" \\
38  * -DSERVICE_ERR="./logs/jettysvc.err" \\
39  * Jetty.xml wrkdir=$JETTY_HOME
40  * </pre>
41  *
42  * @version $Revision: 1.8 $
43  * @author Greg Wilkins (gregw)
44  */

45 public class Service
46 {
47     private static Log log = LogFactory.getLog(Service.class);
48
49     /* ------------------------------------------------------------ */
50     static String JavaDoc serviceLogFile=
51     System.getProperty("SERVICE_LOG_FILE","logs"+File.separator+"yyyy_mm_dd.service.log");
52     
53     /* ------------------------------------------------------------ */
54     public static final int SERVICE_CONTROL_STOP = 1;
55     public static final int SERVICE_CONTROL_PAUSE = 2;
56     public static final int SERVICE_CONTROL_CONTINUE = 3;
57     public static final int SERVICE_CONTROL_INTERROGATE = 4;
58     public static final int SERVICE_CONTROL_SHUTDOWN = 5;
59     public static final int SERVICE_CONTROL_PARAMCHANGE = 6;
60
61     /* ------------------------------------------------------------ */
62     private static Vector JavaDoc _servers;
63     private static Vector JavaDoc _configs;
64     
65     /* ------------------------------------------------------------ */
66     /** Constructor.
67      */

68     private Service()
69     {}
70     
71     /* ------------------------------------------------------------ */
72     public static void dispatchSCMEvent(int eventID)
73     {
74         switch(eventID)
75         {
76           case SERVICE_CONTROL_STOP:
77           case SERVICE_CONTROL_PAUSE:
78               stopAll();
79               break;
80               
81           case SERVICE_CONTROL_CONTINUE :
82               startAll();
83               break;
84               
85           case SERVICE_CONTROL_SHUTDOWN:
86               destroyAll();
87               break;
88               
89           case SERVICE_CONTROL_PARAMCHANGE:
90               stopAll();
91               destroyAll();
92               createAll();
93               startAll();
94               break;
95
96           default:
97               break;
98         }
99     }
100     
101     /* ------------------------------------------------------------ */
102     private static void createAll()
103     {
104         if (_configs!=null)
105         {
106             synchronized(_configs)
107             {
108                 _servers=new Vector JavaDoc();
109                 for(int i=0;i<_configs.size();i++)
110                 {
111                     try
112                     {
113                         Server server = new Server((String JavaDoc)_configs.get(i));
114                         _servers.add(server);
115                     }
116                     catch(Exception JavaDoc e)
117                     {
118                         log.warn(_configs.get(i)+" configuration problem: ",e);
119                     }
120                 }
121             }
122         }
123     }
124     
125     
126     /* ------------------------------------------------------------ */
127     private static void startAll()
128     {
129         try
130         {
131             if (_configs!=null)
132             {
133                 synchronized(_configs)
134                 {
135                     for(int i=0;i<_servers.size();i++)
136                     {
137                         HttpServer server = (HttpServer)_servers.get(i);
138                         if (!server.isStarted())
139                             server.start();
140                     }
141                 }
142             }
143         }
144         catch(Exception JavaDoc e)
145         {
146             log.warn(LogSupport.EXCEPTION,e);
147         }
148     }
149     
150     /* ------------------------------------------------------------ */
151     private static void stopAll()
152     {
153         if (_configs!=null)
154         {
155             synchronized(_configs)
156             {
157                 for(int i=0;i<_servers.size();i++)
158                 {
159                     HttpServer server = (HttpServer)_servers.get(i);
160                     try{server.stop();}catch(InterruptedException JavaDoc e){}
161                 }
162             }
163         }
164     }
165     
166     /* ------------------------------------------------------------ */
167     private static void destroyAll()
168     {
169         stopAll();
170         if (_servers!=null)
171             _servers.clear();
172         _servers=null;
173     }
174
175     /* ------------------------------------------------------------ */
176     public static void stopAndDestroy(String JavaDoc[] arg)
177     {
178         if (arg==null || arg.length==0)
179         {
180             stopAll();
181             destroyAll();
182         }
183         else
184         {
185             log.warn(LogSupport.NOT_IMPLEMENTED);
186         }
187     }
188     
189     /* ------------------------------------------------------------ */
190     public static void main(String JavaDoc[] arg)
191     {
192         String JavaDoc opt;
193         opt = System.getProperty("SERVICE_OUT");
194         if (opt != null)
195         {
196             try
197             {
198                 PrintStream JavaDoc stdout = new PrintStream JavaDoc(new FileOutputStream JavaDoc(opt));
199                 System.setOut(stdout);
200             }
201             catch(Exception JavaDoc e){log.warn(LogSupport.EXCEPTION,e);}
202         }
203         
204         opt = System.getProperty("SERVICE_ERR");
205         if (opt != null)
206         {
207             try
208             {
209                 PrintStream JavaDoc stderr = new PrintStream JavaDoc(new FileOutputStream JavaDoc(opt));
210                 System.setErr(stderr);
211             }
212             catch(Exception JavaDoc e){log.warn(LogSupport.EXCEPTION,e);}
213         }
214         
215         
216         if (arg.length==0)
217             arg=new String JavaDoc[] {"etc/jetty.xml"};
218         
219         try
220         {
221             _configs=new Vector JavaDoc();
222             for (int i=0;i<arg.length;i++)
223                 _configs.add(arg[i]);
224             createAll();
225             startAll();
226         }
227         catch(Exception JavaDoc e)
228         {
229             log.warn(LogSupport.EXCEPTION,e);
230         }
231     }
232 }
233
234
Popular Tags