KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
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: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.adl;
26
27 import java.util.HashMap JavaDoc;
28
29 import org.objectweb.fractal.adl.Factory;
30 import org.objectweb.fractal.api.Component;
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.LifeCycleController;
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: <br/>
38  * <code> Launcher [-factory=&lt;factory def&gt;] [-backend=&lt;backend def&gt;] &lt;definition&gt; [ &lt;itf&gt; ]</code>
39  * <br/>Where &lt;definition&gt; is the name of the component to be instantiated
40  * and started,and &lt;itf&gt; is the name of its Runnable interface, if it has
41  * one.
42  */

43 public final class Launcher
44 {
45
46   private Launcher()
47   {
48   }
49
50   /**
51    * Entry point
52    *
53    * @param args command line arguments
54    * @throws Exception if error.
55    */

56   public static void main(final String JavaDoc[] args) throws Exception JavaDoc
57   {
58     String JavaDoc[] pargs = parseArgs(args);
59     Object JavaDoc comp = createComponent(pargs);
60
61     if (comp instanceof Component)
62     {
63       LifeCycleController lc = null;
64       try
65       {
66         lc = Fractal.getLifeCycleController((Component) comp);
67       }
68       catch (NoSuchInterfaceException ignored)
69       {
70       }
71       if (lc != null)
72       {
73         lc.startFc();
74       }
75       Runnable JavaDoc r = null;
76       try
77       {
78         r = (Runnable JavaDoc) ((Component) comp).getFcInterface(pargs[2]);
79       }
80       catch (NoSuchInterfaceException ignored)
81       {
82       }
83       if (r != null)
84       {
85         r.run();
86       }
87     }
88     else
89     {
90       if (comp instanceof LifeCycleController)
91       {
92         ((LifeCycleController) comp).startFc();
93       }
94       if (comp instanceof Runnable JavaDoc)
95       {
96         ((Runnable JavaDoc) comp).run();
97       }
98     }
99   }
100
101   private static Object JavaDoc createComponent(String JavaDoc[] pargs) throws Exception JavaDoc
102   {
103     Factory f = org.objectweb.fractal.adl.FactoryFactory.getFactory(pargs[0],
104         pargs[1], new HashMap JavaDoc());
105     return f.newComponent(pargs[2], new HashMap JavaDoc());
106   }
107
108   private static String JavaDoc[] parseArgs(final String JavaDoc[] args)
109   {
110     if (args.length < 1 || args.length > 4)
111     {
112       parseError();
113     }
114     String JavaDoc[] result = new String JavaDoc[4];
115     int i = 0;
116     if (args[i].startsWith("-factory="))
117     {
118       result[0] = args[i].substring(9);
119       i++;
120     }
121     else
122     {
123       result[0] = FactoryFactory.FACTORY_NAME;
124     }
125
126     if (args.length == i)
127     {
128       parseError();
129     }
130     if (args[i].startsWith("-backend="))
131     {
132       result[1] = args[i].substring(9);
133       i++;
134     }
135     else
136     {
137       result[1] = FactoryFactory.FRACTAL_BACKEND;
138     }
139
140     if (args.length == i)
141     {
142       parseError();
143     }
144     result[2] = args[i];
145
146     i++;
147     if (args.length == i)
148     {
149       result[3] = "run";
150     }
151     else
152     {
153       result[3] = args[i];
154     }
155     if (args.length > i + 1)
156     {
157       parseError();
158     }
159
160     return result;
161   }
162
163   private static void parseError()
164   {
165     System.out.print("Usage: Launcher [-factory=<factory def>|-backend=");
166     System.out.println("<backend def>] <definition> [ <itf> ]");
167     System.out.print("where <definition> is the name of the component to be ");
168     System.out.print("instantiated and started,\n <itf> is the name of ");
169     System.out.print("its Runnable interface, if it has one,\n ");
170     System.out.print("<factory def> is the name of the factory implementation");
171     System.out.print(" to be used (if not specified use Dream implementation)");
172     System.out.print(",\n <backend def> is the name of the backend ");
173     System.out.print("implementation to be used (if not specified use Fractal");
174     System.out.print(" implementation for Dream factory)");
175     System.exit(1);
176   }
177
178 }
Popular Tags