1 28 29 package org.objectweb.util.launcher.option; 30 31 32 import java.util.Vector ; 33 34 import jregex.Matcher; 35 import jregex.Pattern; 36 37 import org.objectweb.util.cmdline.api.Iterator; 38 import org.objectweb.util.cmdline.lib.DefaultRegExOptionArgument; 39 40 import org.objectweb.util.launcher.CommandJava; 41 import org.objectweb.util.launcher.CommandFactory; 42 import org.objectweb.util.launcher.StringList; 43 44 import org.objectweb.util.launcher.parser.Repository; 45 import org.objectweb.util.launcher.parser.RunDescription; 46 import org.objectweb.util.launcher.parser.ContextDescription; 47 import org.objectweb.util.trace.TraceSystem; 48 49 50 60 public class OptionLoad 61 extends DefaultRegExOptionArgument 62 implements OptionLauncher 63 { 64 65 public final static String shortTag = "-load" ; 66 67 public final static String longTag = "--loadid" ; 68 69 70 public final static String separator = ":" ; 71 72 public final static String list = ","; 73 74 75 protected Vector runs_ ; 76 77 80 public OptionLoad(){ 81 super(new String []{shortTag, longTag}, 82 "<RunID"+separator+ 83 "Option1"+list+"..."+list+"OptionX"+separator+ 84 "Arg1"+list+"..."+list+"ArgX"+separator+ 85 "Prop1"+list+"..."+list+"PropX>", 86 "The identifier of the run to launch", 87 "default"+separator+separator+separator, 88 "([^:]+)"+separator+"([^:]*)"+separator+"([^:]*)"+separator+"(.*)"); 89 runs_ = new Vector (); 90 } 91 92 97 protected class LoadBuilder 98 { 99 100 protected String argument_ ; 101 102 106 public LoadBuilder(String arg) { 107 this.argument_ = arg ; 108 } 109 110 115 protected void completeRun(CommandJava cmd, RunDescription desc) { 116 cmd.setName(desc.getName()); 117 TraceSystem.get("launcher").info("Completing java command " + cmd.getName()); 118 cmd.setClassname(desc.getMainclass()); 119 cmd.setMode(desc.getMode()); 120 try { 121 cmd.getLoader().addURL(desc.getClasses().toStringArray()); 122 } catch (java.net.MalformedURLException ex) { 123 TraceSystem.get("launcher").error("Exception raised: " + ex); 124 } 125 cmd.addArguments(desc.getArguments()); 126 cmd.addProperties(desc.getProperties()); 127 } 128 129 134 protected void completeContext(CommandJava cmd, ContextDescription desc) { 135 try { 136 cmd.getLoader().addURL(desc.getClasses().toStringArray()); 137 } catch (java.net.MalformedURLException ex) { 138 TraceSystem.get("launcher").error("Exception raised: " + ex); 139 } 140 cmd.addArguments(desc.getArguments()); 141 cmd.addProperties(desc.getProperties()); 142 } 143 144 149 protected void completeArgument(CommandJava cmd, String args) { 150 Pattern p = new Pattern(list); 151 String [] arg = p.tokenizer(args).split(); 152 StringList list = new StringList(); 153 for (int i=0 ; i<arg.length ; i++){ 154 if (!arg[i].equals("")) { 155 list.add(arg[i]); 156 } 157 } 158 cmd.addArguments(list); 159 } 160 161 166 protected void completeProperties(CommandJava cmd, String props) { 167 Pattern pattern = new Pattern(list); 168 String [] prop = pattern.tokenizer(props).split(); 169 java.util.Properties list = new java.util.Properties (); 170 for (int i=0 ; i<prop.length ; i++){ 171 if (!prop[i].equals("")) { 172 Pattern pat = new Pattern("="); 173 String [] p = pattern.tokenizer(prop[i]).split(); 174 if (p.length==2) 175 list.put(p[0],p[1]); 176 else if (p.length==1) 177 list.put(p[0],""); 178 } 179 } 180 cmd.addProperties(list); 181 } 182 183 189 public CommandJava complete(Repository repository){ 190 CommandJava cmd = CommandFactory.instance().create(); 191 Matcher match = getPattern().matcher(argument_); 192 match.matches(); 193 194 completeProperties(cmd,match.group(4)); 196 Pattern pattern = new Pattern(list); 197 String [] opt = pattern.tokenizer(match.group(2)).split(); 198 for (int i=0 ; i<opt.length ; i++){ 199 if (!opt[i].equals("")) { 200 ContextDescription desc = repository.getDescription(opt[i]); 201 completeContext(cmd,desc); 202 } 203 } 204 completeRun(cmd,(RunDescription)repository.getDescription(match.group(1))); 205 completeArgument(cmd,match.group(3)); 206 return cmd; 207 } 208 } 209 210 211 217 221 public void consume(Iterator iterator) { 222 setArgument(consumeArgument(iterator)); 223 runs_.addElement(new LoadBuilder(getArgument())); 224 } 225 226 230 public CommandJava[] complete(Repository repository) { 231 java.util.Iterator runs = runs_.iterator(); 232 Vector list = new Vector (); 233 while (runs.hasNext()) 234 list.addElement(((LoadBuilder)runs.next()).complete(repository)); 235 return (CommandJava[])list.toArray(new CommandJava[0]); 236 } 237 238 243 public static String create(String run, String opt, String arg, String prop) { 244 return shortTag+" "+run+separator+opt+separator+arg+separator+prop+" " ; 245 } 246 } 247 | Popular Tags |