1 22 package org.objectweb.petals.system.launch; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.rmi.UnmarshalException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 import javax.management.MBeanServerConnection ; 33 import javax.management.ObjectName ; 34 35 import org.objectweb.fractal.adl.Factory; 36 import org.objectweb.fractal.adl.FactoryFactory; 37 import org.objectweb.fractal.api.Component; 38 import org.objectweb.fractal.api.NoSuchInterfaceException; 39 import org.objectweb.fractal.api.control.ContentController; 40 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 41 import org.objectweb.fractal.api.control.LifeCycleController; 42 import org.objectweb.fractal.util.Fractal; 43 import org.objectweb.util.monolog.Monolog; 44 45 import org.objectweb.petals.PetalsException; 46 import org.objectweb.petals.jbi.management.service.ManagementException; 47 import org.objectweb.petals.jbi.management.systemstate.SystemState; 48 import org.objectweb.petals.jbi.transport.TransportException; 49 import org.objectweb.petals.jbi.transport.Transporter; 50 import org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean; 51 import org.objectweb.petals.util.JNDIUtil; 52 import org.objectweb.petals.util.SystemUtil; 53 54 60 public class Launcher { 61 62 65 protected Component petalsComposite; 66 67 70 public static void main(String [] args) { 71 try { 72 if (args.length > 0 && "stop".equals(args[0])) { 73 Launcher launcher = new Launcher(); 74 launcher.closeServer(false); 75 } else if (args.length > 0 && "shutdown".equals(args[0])) { 76 Launcher launcher = new Launcher(); 77 launcher.closeServer(true); 78 } else if (args.length > 0 && "JNDIBrowser".equals(args[0])) { 79 System.out.println(JNDIUtil.browseJNDI(args)); 80 } else { 81 Launcher launcher = new Launcher(); 82 launcher.run(args); 83 } 84 } catch (Throwable e) { 85 e.printStackTrace(System.err); 86 Component kernelComp = new Launcher().getPetalsComposite(); 87 if (kernelComp != null) { 88 try { 89 LifeCycleController lcc = Fractal 90 .getLifeCycleController(kernelComp); 91 if (lcc.getFcState().equals(LifeCycleController.STARTED)) { 92 lcc.stopFc(); 93 System.out.println("Petals is " + lcc.getFcState()); 94 } 95 } catch (IllegalLifeCycleException e1) { 96 System.err.println("Problem while stopping Petals"); 97 } catch (NoSuchInterfaceException e2) { 98 System.err.println("Problem while stopping Petals"); 99 } 100 } 101 System.err.println("Petals is stopped !"); 102 System.exit(-1); 103 } 104 } 105 106 public Component getPetalsComposite() { 107 return petalsComposite; 108 } 109 110 115 protected void initializeFractal() throws Exception { 116 System.setProperty("fractal.provider", 118 "org.objectweb.fractal.julia.Julia"); 119 System.setProperty("julia.loader", 120 "org.objectweb.fractal.julia.loader.DynamicLoader"); 121 System.setProperty("julia.config", "julia.cfg"); 122 123 Map <String , Object > context = new HashMap <String , Object >(); 124 Factory factory = FactoryFactory.getFactory( 126 FactoryFactory.FRACTAL_BACKEND, context); 127 String compositeName = "Petals"; 131 petalsComposite = (Component) factory.newComponent(compositeName, 132 context); 133 134 } 135 136 140 protected void initializeMonolog() throws IOException { 141 142 new File (SystemUtil 144 .getPetalsInstallDirectory() 145 + File.separator + "logs").mkdirs(); 146 147 URL url1 = this.getClass().getResource("/loggers.properties"); 149 Properties p1 = new Properties (); 150 p1.load(url1.openStream()); 151 String petalsLF = p1.getProperty("handler.petalsLF.output"); 152 if (petalsLF != null) { 153 p1.setProperty("handler.petalsLF.output", new File (SystemUtil 154 .getPetalsInstallDirectory() 155 + File.separator + "logs" + File.separator + petalsLF) 156 .getAbsolutePath()); 157 } 158 String joramLF = p1.getProperty("handler.joramLF.output"); 159 if (joramLF != null) { 160 p1.setProperty("handler.joramLF.output", new File (SystemUtil 161 .getPetalsInstallDirectory() 162 + File.separator + "logs" + File.separator + joramLF) 163 .getAbsolutePath()); 164 } 165 Monolog.initialize(); 166 Monolog.loadMonologConfiguration(p1); 167 } 168 169 protected void startPetals() throws NoSuchInterfaceException, 170 IllegalLifeCycleException { 171 String componentName = "jndi-server"; 172 ContentController contentController = Fractal 173 .getContentController(petalsComposite); 174 Component jndiServer = FractalHelper.getComponentByName( 175 contentController, componentName); 176 177 LifeCycleController jndiServerLifeCycleController = Fractal 178 .getLifeCycleController(jndiServer); 179 jndiServerLifeCycleController.startFc(); 180 181 LifeCycleController lcc = Fractal 183 .getLifeCycleController(petalsComposite); 184 lcc.startFc(); 185 } 186 187 protected void run(String [] args) throws Exception { 188 189 System.setProperty( 191 "com.sun.xml.namespace.QName.useCompatibleSerialVersionUID", "1.0"); 192 193 try { 194 initializeMonolog(); 195 } catch (IOException e) { 196 throw new PetalsException( 197 "Monolog can't be correctly initialized !"); 198 } 199 200 Monolog.initialize().getLogger("root0.Petals.launcher"); 201 202 System.setProperty("javax.management.builder.initial", 203 "mx4j.server.MX4JMBeanServerBuilder"); 204 205 initializeFractal(); 207 208 startPetals(); 210 211 MBeanHelper.registerMBeans(petalsComposite); 213 214 ContentController contentController = Fractal 215 .getContentController(petalsComposite); 216 217 recoverSystem(contentController); 219 220 startListening(contentController); 222 223 LifeCycleController lcc = Fractal 225 .getLifeCycleController(petalsComposite); 226 227 Runtime.getRuntime().addShutdownHook( 228 new SystemExitHook(lcc, contentController)); 229 230 System.out.println("##### Petals container correctly started #####"); 231 232 if (args != null && args.length > 0 234 && "-console".equalsIgnoreCase(args[0])) { 235 commandLineMode(contentController); 236 } 237 } 238 239 246 protected void closeServer(boolean shutdown) { 247 try { 248 249 System.out.println("Petals is "+(shutdown?"shutdowning...":"stopping...")); 250 251 String action = (shutdown? "shutdownContainer":"stopContainer"); 252 253 MBeanServerConnection connection = MBeanHelper.retrieveJMXServer(); 255 256 ObjectName result = MBeanHelper.retrieveServiceMBean("PetalsAdmin", 258 connection); 259 260 if (result != null) { 262 connection.invoke(result, action, new Object [0], 263 new String [0]); 264 265 } 266 } catch (UnmarshalException e) { 267 } catch (Exception e) { 269 System.err.println("Petals can not be accessed."); 270 e.printStackTrace(System.err); 271 } 272 System.out.println("Petals has been "+(shutdown?"shutdown.":"stopped.")); 273 } 274 275 276 284 protected void recoverSystem(ContentController contentContoller) 285 throws NoSuchInterfaceException, ManagementException { 286 Component jbi = FractalHelper.getComponentByName(contentContoller, 287 "jbi"); 288 ContentController contentControllerForJBI = Fractal 289 .getContentController(jbi); 290 Component systemState = FractalHelper.getComponentByName( 291 contentControllerForJBI, "systemstate-impl"); 292 SystemState systemStateService = (SystemState) systemState 293 .getFcInterface("service"); 294 systemStateService.recoverAllEntities(); 295 } 296 297 306 protected void startListening(ContentController contentContoller) 307 throws NoSuchInterfaceException, ManagementException, 308 TransportException { 309 Component jbi = FractalHelper.getComponentByName(contentContoller, 310 "jbi"); 311 ContentController contentControllerForJBI = Fractal 312 .getContentController(jbi); 313 Component systemState = FractalHelper.getComponentByName( 314 contentControllerForJBI, "transporter-impl"); 315 Transporter transporter = (Transporter) systemState 316 .getFcInterface("service"); 317 transporter.startListening(); 318 } 319 320 328 protected void commandLineMode(ContentController contentController) 329 throws NoSuchInterfaceException { 330 Component systemState = FractalHelper.getComponentByName( 331 contentController, "petalsadmin-service"); 332 PetalsAdminServiceMBean petalsService = (PetalsAdminServiceMBean) systemState 333 .getFcInterface("service"); 334 335 CommandReader console = new CommandReader(petalsService); 336 337 console.read(); 338 } 339 340 } 341 | Popular Tags |