1 24 25 package org.objectweb.dream.adl; 26 27 import java.util.HashMap ; 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 43 public final class Launcher 44 { 45 46 private Launcher() 47 { 48 } 49 50 56 public static void main(final String [] args) throws Exception 57 { 58 String [] pargs = parseArgs(args); 59 Object 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 r = null; 76 try 77 { 78 r = (Runnable ) ((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 ) 95 { 96 ((Runnable ) comp).run(); 97 } 98 } 99 } 100 101 private static Object createComponent(String [] pargs) throws Exception 102 { 103 Factory f = org.objectweb.fractal.adl.FactoryFactory.getFactory(pargs[0], 104 pargs[1], new HashMap ()); 105 return f.newComponent(pargs[2], new HashMap ()); 106 } 107 108 private static String [] parseArgs(final String [] args) 109 { 110 if (args.length < 1 || args.length > 4) 111 { 112 parseError(); 113 } 114 String [] result = new String [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 |