1 16 package org.mortbay.jetty.win32; 17 import java.io.File ; 18 import java.io.FileOutputStream ; 19 import java.io.PrintStream ; 20 import java.util.Vector ; 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 45 public class Service 46 { 47 private static Log log = LogFactory.getLog(Service.class); 48 49 50 static String 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 _servers; 63 private static Vector _configs; 64 65 66 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 (); 109 for(int i=0;i<_configs.size();i++) 110 { 111 try 112 { 113 Server server = new Server((String )_configs.get(i)); 114 _servers.add(server); 115 } 116 catch(Exception 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 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 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 [] 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 [] arg) 191 { 192 String opt; 193 opt = System.getProperty("SERVICE_OUT"); 194 if (opt != null) 195 { 196 try 197 { 198 PrintStream stdout = new PrintStream (new FileOutputStream (opt)); 199 System.setOut(stdout); 200 } 201 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 202 } 203 204 opt = System.getProperty("SERVICE_ERR"); 205 if (opt != null) 206 { 207 try 208 { 209 PrintStream stderr = new PrintStream (new FileOutputStream (opt)); 210 System.setErr(stderr); 211 } 212 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 213 } 214 215 216 if (arg.length==0) 217 arg=new String [] {"etc/jetty.xml"}; 218 219 try 220 { 221 _configs=new Vector (); 222 for (int i=0;i<arg.length;i++) 223 _configs.add(arg[i]); 224 createAll(); 225 startAll(); 226 } 227 catch(Exception e) 228 { 229 log.warn(LogSupport.EXCEPTION,e); 230 } 231 } 232 } 233 234 | Popular Tags |