1 23 24 package org.objectweb.fractal.adl; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 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 43 44 public class Launcher { 45 46 private Launcher () { 47 } 48 49 public static void main (final String [] args) throws Exception { 50 String [] pargs = parseArgs(args); 51 Object 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 r = null; 63 try { 64 r = (Runnable )((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 ) { 75 ((Runnable )comp).run(); 76 } 77 } 78 } 79 80 private static Object createComponent (String [] pargs) throws Exception { 81 if (pargs[0].equals("-java")) { 82 Factory f = FactoryFactory.getFactory(FactoryFactory.JAVA_BACKEND); 83 return ((Map )f.newComponent(pargs[1], new HashMap ())).get(pargs[2]); 84 } else { 85 Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND); 86 return f.newComponent(pargs[1], new HashMap ()); 87 } 88 } 89 90 private static String [] parseArgs (final String [] args) { 91 if (args.length < 1 || args.length > 3) { 92 parseError(); 93 } 94 String [] result = new String [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 |