1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.common.SwingUtil; 29 import org.enhydra.tool.common.PathHandle; 30 import org.enhydra.tool.common.ResUtil; 31 32 import java.io.File ; 34 import java.util.ResourceBundle ; 35 import java.util.StringTokenizer ; 36 import java.util.ArrayList ; 37 38 public class Main { 40 static public boolean debug = true; 41 42 static ResourceBundle res = 44 ResourceBundle.getBundle("org.enhydra.tool.archive.Res"); 46 private static final String PARAM_HELP = "-help"; private static final String PARAM_EJB_ARCHIVE = "-ejbArchive"; private static final String PARAM_EAR_ARCHIVE = "-earArchive"; private static final String PARAM_JAR_ARCHIVE = "-jarArchive"; private static final String PARAM_WEB_ARCHIVE = "-webArchive"; private static final String PARAM_CLASSPATH = "-classpath"; private static final String PARAM_FILES = "-files"; private static final String PARAM_NO_ALT = "-noAlt"; private static final String PARAM_LIB = "-lib"; 57 public Main() { 58 System.out.println("Kelp Archive Wizard " + 59 ToolBoxInfo.getToolBoxVersion()); 60 System.out.println(ToolBoxInfo.getCopyright()); 61 } 62 63 public static void main(String [] args) { 64 Main main = new Main(); 65 66 main.init(args); 67 System.exit(0); 68 } 69 70 private void init(String [] args) { 71 ArchiveTool packager = null; 72 File newFile = null; 73 JarPlan plan = null; 74 75 if (args.length < 1) { 76 String newArchive = null; 77 78 SwingUtil.setLookAndFeelToSystem(); 79 newArchive = ArchiveTool.runWizard(); 80 if (newArchive == null) { 81 System.out.println("No archive created."); 82 } else { 83 System.out.println("Created archive: " + newArchive); 84 } 85 } else if (args[0].equalsIgnoreCase(PARAM_HELP)) { 86 showUsage(); 87 } else { 88 try { 89 plan = parsePlanArgs(args); 90 plan.setClassFiltering(true); 91 packager = new ArchiveTool(); 92 newFile = packager.buildArchive(plan); 93 System.out.println(ResUtil.format(res.getString("Created_file_0_"), 94 newFile)); 95 } catch (ArgumentException e) { 96 System.out.println(ResUtil.format(res.getString("Error_0_"), 97 e.getMessage())); 98 showUsage(); 99 } catch (ArchiveException e) { 100 System.out.println(ResUtil.format(res.getString("Error_0_"), 101 e.getMessage())); 102 } 103 } 104 } 105 106 private JarPlan parsePlanArgs(String [] args) throws ArgumentException { 107 JarPlan plan = null; 108 109 if (args.length < 1) { 110 throw new ArgumentException(res.getString("Too_few_arguments")); 111 } else if (args[0].equalsIgnoreCase(PARAM_EJB_ARCHIVE)) { 112 plan = parseEjbArgs(args); 113 } else if (args[0].equalsIgnoreCase(PARAM_EAR_ARCHIVE)) { 114 plan = parseEarArgs(args); 115 } else if (args[0].equalsIgnoreCase(PARAM_JAR_ARCHIVE)) { 116 plan = parseJarArgs(args); 117 } else if (args[0].equalsIgnoreCase(PARAM_WEB_ARCHIVE)) { 118 plan = parseWarArgs(args); 119 } else { 120 throw new ArgumentException(ResUtil.format(res.getString("First_argument_must"), 121 PARAM_WEB_ARCHIVE, 122 PARAM_JAR_ARCHIVE)); 123 } 124 return plan; 125 } 126 127 private EjbPlan parseEjbArgs(String [] args) throws ArgumentException { 128 EjbPlan ejbPlan = null; 129 int cursor = 1; 130 131 if (args.length < 5) { 132 throw new ArgumentException(res.getString("Too_few_arguments")); 133 } else if (args.length >= 8) { 134 throw new ArgumentException(ResUtil.format(res.getString("Too_many_arguments"), 135 PARAM_EJB_ARCHIVE)); 136 } else if (!args[1].equals(PARAM_CLASSPATH)) { 137 throw new ArgumentException(ResUtil.format(res.getString("Missing_parameter_"), 138 PARAM_CLASSPATH)); 139 } else { 140 try { 141 Descriptor[] desc = new Descriptor[1]; 142 143 desc[0] = new Descriptor(true, Descriptor.EJB); 144 ejbPlan = new EjbPlan(); 145 cursor = readClasses(ejbPlan, args); 146 desc[0].setPath(args[cursor++]); 147 desc[0].validate(); 148 ejbPlan.setDescriptors(desc); 149 ejbPlan.setArchivePath(args[cursor]); 150 } catch (ArchiveException e) { 151 throw new ArgumentException(e.getMessage()); 152 } 153 } 154 return ejbPlan; 155 } 156 157 private EarPlan parseEarArgs(String [] args) throws ArgumentException { 158 EarPlan earPlan = null; 159 StringTokenizer tokenizer = null; 160 PathHandle ph = null; 161 int cursor = 1; 162 163 if (args.length < 4) { 164 throw new ArgumentException(res.getString("Too_few_arguments")); 165 } else if (args.length > 8) { 166 throw new ArgumentException(ResUtil.format(res.getString("Too_many_arguments"), 167 PARAM_EAR_ARCHIVE)); 168 } else { 169 try { 170 earPlan = new EarPlan(); 171 { 172 String [] files = new String [0]; 173 174 tokenizer = new StringTokenizer (args[cursor++], ":"); 175 while (tokenizer.hasMoreTokens()) { 176 ph = PathHandle.createPathHandle(tokenizer.nextToken()); 177 if (ph.hasExtension("war")) { 178 earPlan.addWebApplication(ph.getPath()); 179 } else { 180 earPlan.addEnterpriseModule(ph.getPath()); 181 } 182 } 183 } 184 cursor = readLibFiles(earPlan, args, cursor); 185 if (args[cursor].equalsIgnoreCase(PARAM_NO_ALT)) { 186 earPlan.setAlt(false); 187 cursor++; 188 } 189 earPlan.setAppName(args[cursor++]); 190 earPlan.setArchivePath(args[cursor]); 191 } catch (ArchiveException e) { 192 throw new ArgumentException(e.getMessage()); 193 } 194 } 195 return earPlan; 196 } 197 198 private JarPlan parseJarArgs(String [] args) throws ArgumentException { 199 JarPlan jarPlan = null; 200 int cursor = 1; 201 202 if (args.length < 4) { 203 throw new ArgumentException(res.getString("Too_few_arguments")); 204 } else if (args.length >= 7) { 205 throw new ArgumentException(ResUtil.format(res.getString("Too_many_arguments"), 206 PARAM_JAR_ARCHIVE)); 207 } else if (!args[1].equals(PARAM_CLASSPATH)) { 208 throw new ArgumentException(ResUtil.format(res.getString("Second_argument_must"), 209 PARAM_CLASSPATH)); 210 } else { 211 try { 212 jarPlan = new JarPlan(); 213 cursor = readClasses(jarPlan, args); 214 jarPlan.setArchivePath(args[cursor]); 215 } catch (ArchiveException e) { 216 throw new ArgumentException(e.getMessage()); 217 } 218 } 219 return jarPlan; 220 } 221 222 private WarPlan parseWarArgs(String [] args) throws ArgumentException { 223 WarPlan warPlan = null; 224 ArrayList list = new ArrayList (); 225 StringTokenizer tokenizer = null; 226 StringBuffer buf = new StringBuffer (); 227 PathHandle path = null; 228 int cursor = 1; 229 230 if (args.length < 4) { 231 throw new ArgumentException(res.getString("Too_few_arguments")); 232 } else if (args.length > 10) { 233 throw new ArgumentException(res.getString("Too_many_arguments1")); 234 } else { 235 try { 236 Descriptor[] desc = new Descriptor[1]; 237 238 desc[0] = new Descriptor(true, Descriptor.WEB); 239 warPlan = new WarPlan(); 240 cursor = readClasses(warPlan, args); 241 warPlan.setContentRoot(args[cursor++]); 242 243 if (args[cursor].equalsIgnoreCase(PARAM_FILES)) { 245 String [] files = new String [0]; 246 247 cursor++; 248 tokenizer = new StringTokenizer (args[cursor++], ":"); 249 while (tokenizer.hasMoreTokens()) { 250 buf.setLength(0); 251 buf.append(warPlan.getContentRoot()); 252 buf.append(File.separator); 253 buf.append(tokenizer.nextToken()); 254 path = PathHandle.createPathHandle(buf.toString()); 255 if (path.isFile()) { 256 list.add(path.getPath()); 257 } else { 258 System.err.println("File not found: " 259 + path.getPath()); 260 } 261 } 262 list.trimToSize(); 263 files = (String []) list.toArray(files); 264 list.clear(); 265 warPlan.setContentFiles(files); 266 } 267 cursor = readLibFiles(warPlan, args, cursor); 268 desc[0].setPath(args[cursor++]); 269 desc[0].validate(); 270 warPlan.setDescriptors(desc); 271 warPlan.setArchivePath(args[cursor]); 272 } catch (ArrayIndexOutOfBoundsException e) { 273 throw new ArgumentException("Archiving syntax error"); 274 } catch (ArchiveException e) { 275 throw new ArgumentException(e.getMessage()); 276 } 277 } 278 return warPlan; 279 } 280 281 private int readClasses(JarPlan plan, 282 String [] args) throws ArchiveException { 283 StringTokenizer tokenizer = null; 284 ArrayList list = new ArrayList (); 285 StringBuffer buf = new StringBuffer (); 286 PathHandle path = null; 287 int cursor = 1; 288 289 if (args[1].equalsIgnoreCase(PARAM_CLASSPATH)) { 290 plan.setClassRoot(args[2]); 291 cursor = 3; 292 if (args[cursor].equalsIgnoreCase(PARAM_FILES)) { 293 String [] files = new String [0]; 294 295 cursor++; 296 tokenizer = new StringTokenizer (args[cursor++], ":"); 297 while (tokenizer.hasMoreTokens()) { 298 buf.setLength(0); 299 buf.append(args[2]); 300 buf.append(File.separator); 301 buf.append(tokenizer.nextToken()); 302 path = PathHandle.createPathHandle(buf.toString()); 303 if (path.isFile()) { 304 list.add(path.getPath()); 305 } else { 306 System.err.println("File not found: " 307 + path.getPath()); 308 } 309 } 310 list.trimToSize(); 311 files = (String []) list.toArray(files); 312 list.clear(); 313 plan.setClassFiles(files); 314 } 315 } 316 return cursor; 317 } 318 319 private int readLibFiles(JarPlan plan, String [] args, 320 int cursor) throws ArchiveException { 321 StringTokenizer tokenizer = null; 322 ArrayList list = new ArrayList (); 323 324 if (args[cursor].equalsIgnoreCase(PARAM_LIB)) { 325 cursor++; 326 String [] files = new String [0]; 327 328 tokenizer = new StringTokenizer (args[cursor++], ":"); 329 while (tokenizer.hasMoreTokens()) { 330 list.add(tokenizer.nextToken()); 331 } 332 list.trimToSize(); 333 files = (String []) list.toArray(files); 334 list.clear(); 335 plan.setLibFiles(files); 336 } 337 return cursor; 338 } 339 340 private void showUsage() { 341 System.out.println(new String ()); 342 System.out.println(res.getString("Parameters_")); 343 344 System.out.print(' '); 346 System.out.println(PARAM_EJB_ARCHIVE); 347 System.out.print(" "); 348 System.out.println(getClassUsage()); 349 System.out.print(" "); 350 System.out.print("<ejb-jar.xml>"); 351 System.out.print(' '); 352 System.out.println(res.getString("_archive_file_")); 353 System.out.println(' '); 354 355 System.out.print(' '); 357 System.out.println(PARAM_EAR_ARCHIVE); 358 System.out.print(" "); 359 System.out.println("moduleArchive1:moduleArchive2:..."); 360 System.out.print(" "); 361 System.out.println("[" + PARAM_LIB + " jar1:jar2:...]"); 362 System.out.print(" "); 363 System.out.println("[" + PARAM_NO_ALT + "]"); 364 System.out.print(" "); 365 System.out.println("<application-name> <archive-fle>"); 366 System.out.println(""); 367 368 System.out.print(' '); 370 System.out.println(PARAM_JAR_ARCHIVE); 371 System.out.print(" "); 372 System.out.print(getClassUsage()); 373 System.out.print(' '); 374 System.out.println(res.getString("_archive_file_")); 375 System.out.println(""); 376 377 System.out.print(' '); 379 System.out.println(PARAM_WEB_ARCHIVE); 380 System.out.print(" "); 381 System.out.println("[ " + getClassUsage() + " ]"); 382 System.out.print(" "); 383 System.out.println("[" + PARAM_LIB + " jar1:jar2:...]"); 384 System.out.print(" "); 385 System.out.print(res.getString("_content_root_")); 386 System.out.print(' '); 387 System.out.print('['); 388 System.out.print(PARAM_FILES); 389 System.out.print(' '); 390 System.out.print("file1:file2..."); 391 System.out.println(']'); 392 System.out.print(" "); 393 System.out.print(res.getString("_web_xml_")); 394 System.out.print(' '); 395 System.out.println(res.getString("_archive_file_")); 396 System.out.println(' '); 397 } 398 399 private String getClassUsage() { 400 StringBuffer buf = new StringBuffer (); 401 402 buf.append(PARAM_CLASSPATH); 403 buf.append(' '); 404 buf.append(res.getString("_classes_root_")); 405 buf.append(' '); 406 buf.append('['); 407 buf.append(PARAM_FILES); 408 buf.append(' '); 409 buf.append("file1:file2..."); 410 buf.append(']'); 411 return buf.toString(); 412 } 413 414 class ArgumentException extends Exception { 415 public ArgumentException(String msg) { 416 super(msg); 417 } 418 419 } 420 } 421 | Popular Tags |