KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > Launcher


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
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 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.fractal.api.Component;
30 import org.objectweb.fractal.api.NoSuchInterfaceException;
31 import org.objectweb.fractal.api.control.LifeCycleController;
32
33 import org.objectweb.fractal.util.Fractal;
34
35 /**
36  * A class to launch a component from its definition from the command line.
37  * Usage:
38  * Launcher [-java|-fractal] <definition> [ <itf> ]
39  * where <definition> is the name of the component to be instantiated
40  * and started,and <itf> is the name of its Runnable interface, if it
41  * has one.
42  */

43
44 public class Launcher {
45
46   private Launcher () {
47   }
48
49   public static void main (final String JavaDoc[] args) throws Exception JavaDoc {
50     String JavaDoc[] pargs = parseArgs(args);
51     Object JavaDoc comp = createComponent(pargs);
52
53     if (comp instanceof Component) {
54       LifeCycleController lc = null;
55       try {
56         lc = Fractal.getLifeCycleController((Component)comp);
57       } catch (NoSuchInterfaceException ignored) {
58       }
59       if (lc != null) {
60         lc.startFc();
61       }
62       Runnable JavaDoc r = null;
63       try {
64         r = (Runnable JavaDoc)((Component)comp).getFcInterface(pargs[2]);
65       } catch (NoSuchInterfaceException ignored) {
66       }
67       if (r != null) {
68         r.run();
69       }
70     } else {
71       if (comp instanceof LifeCycleController) {
72         ((LifeCycleController)comp).startFc();
73       }
74       if (comp instanceof Runnable JavaDoc) {
75         ((Runnable JavaDoc)comp).run();
76       }
77     }
78   }
79
80   private static Object JavaDoc createComponent (String JavaDoc[] pargs) throws Exception JavaDoc {
81     if (pargs[0].equals("-java")) {
82       Factory f = FactoryFactory.getFactory(FactoryFactory.JAVA_BACKEND);
83       return ((Map JavaDoc)f.newComponent(pargs[1], new HashMap JavaDoc())).get(pargs[2]);
84     } else {
85       Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
86       return f.newComponent(pargs[1], new HashMap JavaDoc());
87     }
88   }
89   
90   private static String JavaDoc[] parseArgs (final String JavaDoc[] args) {
91     if (args.length < 1 || args.length > 3) {
92       parseError();
93     }
94     String JavaDoc[] result = new String JavaDoc[3];
95     if (args[0].equals("-java") || args[0].equals("-fractal")) {
96       if (args.length < 2) {
97         parseError();
98       }
99       result[0] = args[0];
100       result[1] = args[1];
101       result[2] = args.length == 3 ? args[2] : "run";
102     } else {
103       result[0] = "-java";
104       result[1] = args[0];
105       result[2] = args.length >= 2 ? args[1] : "run";
106     }
107     return result;
108   }
109   
110   private static void parseError () {
111     System.out.println("Usage: Launcher [-java|-fractal] <definition> [ <itf> ]");
112     System.out.print("where <definition> is the name of the component to be ");
113     System.out.print("instantiated and started,\nand <itf> is the name of ");
114     System.out.println("its Runnable interface, if it has one");
115     System.exit(1);
116   }
117 }
118
Popular Tags