KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > system > launch > Launcher


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: Launcher.java 461 2006-05-24 12:12:45Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.system.launch;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.rmi.UnmarshalException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.ObjectName JavaDoc;
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 /**
55  * This class is used to start the Petals platform
56  *
57  * @author alouis, ddesjardins, Rafael Marins
58  * @version $Rev: 461 $ $Date: 2006-05-24 14:12:45 +0200 (mer., 24 mai 2006) $
59  */

60 public class Launcher {
61
62     /**
63      * Petals composite
64      */

65     protected Component petalsComposite;
66
67     /**
68      * @param args
69      */

70     public static void main(String JavaDoc[] 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 JavaDoc 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     /**
111      * Initialize Fractal
112      *
113      * @throws Exception
114      */

115     protected void initializeFractal() throws Exception JavaDoc {
116         // check required fractal properties
117
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 JavaDoc<String JavaDoc, Object JavaDoc> context = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
124         // Create Fractal ADL factories
125
Factory factory = FactoryFactory.getFactory(
126             FactoryFactory.FRACTAL_BACKEND, context);
127         // System.setSecurityManager(new SecurityManager());
128
// Registry.createRegistry(Registry.DEFAULT_PORT, context);
129
// Create composite from ADL
130
String JavaDoc compositeName = "Petals";
131         petalsComposite = (Component) factory.newComponent(compositeName,
132             context);
133
134     }
135
136     /**
137      *
138      * @throws IOException
139      */

140     protected void initializeMonolog() throws IOException JavaDoc {
141         
142         // create logs directory is not present
143
new File JavaDoc(SystemUtil
144             .getPetalsInstallDirectory()
145             + File.separator + "logs").mkdirs();
146         
147         // Configure Monolog
148
URL JavaDoc url1 = this.getClass().getResource("/loggers.properties");
149         Properties JavaDoc p1 = new Properties JavaDoc();
150         p1.load(url1.openStream());
151         String JavaDoc petalsLF = p1.getProperty("handler.petalsLF.output");
152         if (petalsLF != null) {
153             p1.setProperty("handler.petalsLF.output", new File JavaDoc(SystemUtil
154                 .getPetalsInstallDirectory()
155                 + File.separator + "logs" + File.separator + petalsLF)
156                 .getAbsolutePath());
157         }
158         String JavaDoc joramLF = p1.getProperty("handler.joramLF.output");
159         if (joramLF != null) {
160             p1.setProperty("handler.joramLF.output", new File JavaDoc(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 JavaDoc 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         // Start the composite
182
LifeCycleController lcc = Fractal
183             .getLifeCycleController(petalsComposite);
184         lcc.startFc();
185     }
186
187     protected void run(String JavaDoc[] args) throws Exception JavaDoc {
188
189         // set the compatible SUID for the Qname
190
System.setProperty(
191             "com.sun.xml.namespace.QName.useCompatibleSerialVersionUID", "1.0");
192
193         try {
194             initializeMonolog();
195         } catch (IOException JavaDoc 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         // Initialize Fractal
206
initializeFractal();
207
208         // Start Petals
209
startPetals();
210
211         // expose JBI MBean objectin the JMX Server
212
MBeanHelper.registerMBeans(petalsComposite);
213
214         ContentController contentController = Fractal
215             .getContentController(petalsComposite);
216
217         // recover all entities
218
recoverSystem(contentController);
219
220         // tell the Transporter to start Listening
221
startListening(contentController);
222
223         // Set up the shutdown hook
224
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         // show the commandLine mode if asked
233
if (args != null && args.length > 0
234             && "-console".equalsIgnoreCase(args[0])) {
235             commandLineMode(contentController);
236         }
237     }
238
239     /**
240      * Stop or Shutdown the Petals server.
241      * We connect to the JMX server of the server that is running
242      * with the server.properties entries.
243      *
244      * @param shutdown the server if true, stop the server if false
245      */

246     protected void closeServer(boolean shutdown) {
247         try {
248
249             System.out.println("Petals is "+(shutdown?"shutdowning...":"stopping..."));
250             
251             String JavaDoc action = (shutdown? "shutdownContainer":"stopContainer");
252             
253             // get the jmxServer of this container
254
MBeanServerConnection JavaDoc connection = MBeanHelper.retrieveJMXServer();
255
256             // search the PetalsAdmin mbean
257
ObjectName JavaDoc result = MBeanHelper.retrieveServiceMBean("PetalsAdmin",
258                 connection);
259
260             // call Stop
261
if (result != null) {
262                 connection.invoke(result, action, new Object JavaDoc[0],
263                     new String JavaDoc[0]);
264
265             }
266         } catch (UnmarshalException JavaDoc e) {
267             // nothing to do, the JVM of the jmx server is stopped
268
} catch (Exception JavaDoc 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     /**
277      * Recover the system state. All state elements are recovered (components,
278      * share libraries, service assemblies)
279      *
280      * @param contentContoller
281      * @throws NoSuchInterfaceException
282      * @throws ManagementException
283      */

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     /**
298      * Start listening for incoming messages. This is only activated after all
299      * the components has been restarted.
300      *
301      * @param contentContoller
302      * @throws NoSuchInterfaceException
303      * @throws ManagementException
304      * @throws TransportException
305      */

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     /**
321      * Show a command line to interact with Petals TODO we pass the PetalsAdmin
322      * object to the CommandReader to perform some actions. Find a better way.
323      *
324      * @param contentController
325      * used to retreive PetalsAdmin
326      * @throws NoSuchInterfaceException
327      */

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