1 45 package org.openejb.server; 46 47 import org.openejb.loader.SystemInstance; 48 import org.openejb.util.Logger; 49 import org.openejb.util.Messages; 50 import org.openejb.util.FileUtils; 51 import org.openejb.util.ResourceFinder; 52 53 import java.io.File ; 54 import java.io.FileInputStream ; 55 import java.io.FileOutputStream ; 56 import java.io.IOException ; 57 import java.net.InetAddress ; 58 import java.util.HashMap ; 59 import java.util.Properties ; 60 import java.util.Map ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.ArrayList ; 64 65 66 79 public class ServiceManager { 80 81 static Messages messages = new Messages("org.openejb.server.util.resources"); 82 static Logger logger = Logger.getInstance("OpenEJB.server.remote", "org.openejb.server.util.resources"); 83 84 private static ServiceManager manager; 85 86 private static HashMap propsByFile = new HashMap (); 87 private static HashMap fileByProps = new HashMap (); 88 89 private static ServerService[] daemons; 90 91 private boolean stop = false; 92 private final ResourceFinder resourceFinder; 93 94 private ServiceManager() { 95 resourceFinder = new ResourceFinder("META-INF/"); 96 } 97 98 public static ServiceManager getManager() { 99 if (manager == null) { 100 manager = new ServiceManager(); 101 } 102 103 return manager; 104 } 105 106 123 124 public static class ServiceFinder { 126 private final ResourceFinder resourceFinder; 127 private ClassLoader classLoader; 128 129 public ServiceFinder(String basePath) { 130 this(basePath, Thread.currentThread().getContextClassLoader()); 131 } 132 133 public ServiceFinder(String basePath, ClassLoader classLoader) { 134 this.resourceFinder = new ResourceFinder(basePath, classLoader); 135 this.classLoader = classLoader; 136 } 137 138 public Map mapAvailableServices(Class interfase) throws IOException , ClassNotFoundException { 139 Map services = resourceFinder.mapAvailableProperties(ServerService.class.getName()); 140 141 for (Iterator iterator = services.entrySet().iterator(); iterator.hasNext();) { 142 Map.Entry entry = (Map.Entry ) iterator.next(); 143 String name = (String ) entry.getKey(); 144 Properties properties = (Properties ) entry.getValue(); 145 146 String className = properties.getProperty("className"); 147 if (className == null) { 148 className = properties.getProperty("classname"); 149 if (className == null) { 150 className = properties.getProperty("server"); 151 } 152 } 153 154 Class impl = classLoader.loadClass(className); 155 156 if (!interfase.isAssignableFrom(impl)) { 157 services.remove(name); 158 continue; 159 } 160 161 properties.put(interfase, impl); 162 String rawProperties = resourceFinder.findString(interfase.getName() + "/" + name); 163 properties.put(Properties .class, rawProperties); 164 165 } 166 return services; 167 } 168 } 169 170 public void init() throws Exception { 171 try { 172 org.apache.log4j.MDC.put("SERVER", "main"); 173 InetAddress localhost = InetAddress.getLocalHost(); 174 org.apache.log4j.MDC.put("HOST", localhost.getHostName()); 175 } catch (Exception e) { 176 } 177 178 ServiceFinder serviceFinder = new ServiceFinder("META-INF/"); 179 180 Map availableServices = serviceFinder.mapAvailableServices(ServerService.class); 181 List enabledServers = new ArrayList (); 182 183 for (Iterator iterator = availableServices.entrySet().iterator(); iterator.hasNext();) { 184 Map.Entry entry = (Map.Entry ) iterator.next(); 185 String serviceName = (String ) entry.getKey(); 186 Properties serviceProperties = (Properties ) entry.getValue(); 187 188 overrideProperties(serviceName, serviceProperties); 189 serviceProperties.setProperty("name", serviceName); 190 191 if (isEnabled(serviceProperties)) { 192 193 ServerService service = null; 195 196 Class serviceClass = (Class ) serviceProperties.get(ServerService.class); 197 198 try { 199 service = (ServerService) serviceClass.newInstance(); 200 } catch (Throwable t) { 201 String msg1 = messages.format("service.instantiation.err", serviceClass.getName(), t.getClass().getName(), t.getMessage()); 202 throw new ServiceException(msg1, t); 203 } 204 205 service = new ServiceLogger(service); 207 service = new ServiceAccessController(service); 208 service = new ServiceDaemon(service); 209 210 service.init(serviceProperties); 212 enabledServers.add(service); 213 } 214 215 } 216 217 daemons = (ServerService[]) enabledServers.toArray(new ServerService[]{}); 218 } 219 220 private void overrideProperties(String serviceName, Properties serviceProperties) throws IOException { 221 FileUtils base = SystemInstance.get().getBase(); 222 223 File conf = base.getDirectory("conf"); 225 if (conf.exists()) { 226 File serviceConfig = new File (conf, serviceName + ".properties"); 227 if (serviceConfig.exists()){ 228 FileInputStream in = new FileInputStream (serviceConfig); 229 try { 230 serviceProperties.load(in); 231 } finally { 232 in.close(); 233 } 234 } else { 235 FileOutputStream out = new FileOutputStream (serviceConfig); 236 try { 237 String rawPropsContent = (String ) serviceProperties.get(Properties .class); 238 out.write(rawPropsContent.getBytes()); 239 } finally { 240 out.close(); 241 } 242 } 243 } 244 245 String prefix = serviceName + "."; 247 Properties sysProps = System.getProperties(); 248 for (Iterator iterator1 = sysProps.entrySet().iterator(); iterator1.hasNext();) { 249 Map.Entry entry1 = (Map.Entry ) iterator1.next(); 250 String key = (String ) entry1.getKey(); 251 String value = (String ) entry1.getValue(); 252 if (key.startsWith(prefix)){ 253 key = key.replaceFirst(prefix, ""); 254 serviceProperties.setProperty(key, value); 255 } 256 } 257 258 } 259 260 private boolean isEnabled(Properties props) throws ServiceException { 261 String disabled = props.getProperty("disabled", ""); 263 264 if (disabled.equalsIgnoreCase("yes") || disabled.equalsIgnoreCase("true")) { 265 return false; 266 } else { 267 return true; 268 } 269 } 270 271 public synchronized void start() throws ServiceException { 272 boolean display = System.getProperty("openejb.nobanner") == null; 273 274 275 if (display) { 276 System.out.println(" ** Starting Services **"); 277 printRow("NAME", "IP", "PORT"); 278 } 279 280 for (int i = 0; i < daemons.length; i++) { 281 ServerService d = daemons[i]; 282 try { 283 d.start(); 284 if (display) { 285 printRow(d.getName(), d.getIP(), d.getPort() + ""); 286 } 287 } catch (Exception e) { 288 logger.error(d.getName() + " " + d.getIP() + " " + d.getPort() + ": " + e.getMessage()); 289 if (display) { 290 printRow(d.getName(), "----", "FAILED"); 291 } 292 } 293 } 294 if (display) { 295 System.out.println("-------"); 296 System.out.println("Ready!"); 297 } 298 307 try { 308 while (!stop) { 309 this.wait(Long.MAX_VALUE); 311 } 312 } catch (Throwable t) { 313 logger.fatal("Unable to keep the server thread alive. Received exception: " + t.getClass().getName() + " : " + t.getMessage()); 314 } 315 System.out.println("[] exiting vm"); 316 logger.info("Stopping Remote Server"); 317 318 } 319 320 public synchronized void stop() throws ServiceException { 321 System.out.println("[] received stop signal"); 322 stop = true; 323 for (int i = 0; i < daemons.length; i++) { 324 daemons[i].stop(); 325 } 326 notifyAll(); 327 } 328 329 private void printRow(String col1, String col2, String col3) { 330 331 col1 += " "; 333 col1 = col1.substring(0, 20); 334 335 col2 += " "; 337 col2 = col2.substring(0, 15); 338 339 col3 += " "; 341 col3 = col3.substring(0, 6); 342 343 StringBuffer sb = new StringBuffer (50); 344 sb.append(" ").append(col1); 345 sb.append(" ").append(col2); 346 sb.append(" ").append(col3); 347 348 System.out.println(sb.toString()); 349 } 350 } 351 | Popular Tags |