KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > Main


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive;
25
26 // ToolBox imports
27
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 // Standard imports
33
import java.io.File JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 //
39
public class Main {
40     static public boolean debug = true;
41
42     // not to be resourced
43
static ResourceBundle JavaDoc res =
44         ResourceBundle.getBundle("org.enhydra.tool.archive.Res"); // nores
45

46     //
47
private static final String JavaDoc PARAM_HELP = "-help"; // nores
48
private static final String JavaDoc PARAM_EJB_ARCHIVE = "-ejbArchive"; // nores
49
private static final String JavaDoc PARAM_EAR_ARCHIVE = "-earArchive"; // nores
50
private static final String JavaDoc PARAM_JAR_ARCHIVE = "-jarArchive"; // nores
51
private static final String JavaDoc PARAM_WEB_ARCHIVE = "-webArchive"; // nores
52
private static final String JavaDoc PARAM_CLASSPATH = "-classpath"; // nores
53
private static final String JavaDoc PARAM_FILES = "-files"; // nores
54
private static final String JavaDoc PARAM_NO_ALT = "-noAlt"; // nores
55
private static final String JavaDoc PARAM_LIB = "-lib"; // nores
56

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 JavaDoc[] args) {
64         Main main = new Main();
65
66         main.init(args);
67         System.exit(0);
68     }
69
70     private void init(String JavaDoc[] args) {
71         ArchiveTool packager = null;
72         File JavaDoc newFile = null;
73         JarPlan plan = null;
74
75         if (args.length < 1) {
76             String JavaDoc 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] args) throws ArgumentException {
158         EarPlan earPlan = null;
159         StringTokenizer JavaDoc 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 JavaDoc[] files = new String JavaDoc[0];
173
174                     tokenizer = new StringTokenizer JavaDoc(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 JavaDoc[] 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 JavaDoc[] args) throws ArgumentException {
223         WarPlan warPlan = null;
224         ArrayList JavaDoc list = new ArrayList JavaDoc();
225         StringTokenizer JavaDoc tokenizer = null;
226         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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                 // read content
244
if (args[cursor].equalsIgnoreCase(PARAM_FILES)) {
245                     String JavaDoc[] files = new String JavaDoc[0];
246
247                     cursor++;
248                     tokenizer = new StringTokenizer JavaDoc(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 JavaDoc[]) 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 JavaDoc 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 JavaDoc[] args) throws ArchiveException {
283         StringTokenizer JavaDoc tokenizer = null;
284         ArrayList JavaDoc list = new ArrayList JavaDoc();
285         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc[] files = new String JavaDoc[0];
294
295                 cursor++;
296                 tokenizer = new StringTokenizer JavaDoc(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 JavaDoc[]) list.toArray(files);
312                 list.clear();
313                 plan.setClassFiles(files);
314             }
315         }
316         return cursor;
317     }
318
319     private int readLibFiles(JarPlan plan, String JavaDoc[] args,
320                              int cursor) throws ArchiveException {
321         StringTokenizer JavaDoc tokenizer = null;
322         ArrayList JavaDoc list = new ArrayList JavaDoc();
323
324         if (args[cursor].equalsIgnoreCase(PARAM_LIB)) {
325             cursor++;
326             String JavaDoc[] files = new String JavaDoc[0];
327
328             tokenizer = new StringTokenizer JavaDoc(args[cursor++], ":");
329             while (tokenizer.hasMoreTokens()) {
330                 list.add(tokenizer.nextToken());
331             }
332             list.trimToSize();
333             files = (String JavaDoc[]) 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 JavaDoc());
342         System.out.println(res.getString("Parameters_"));
343
344         // EJB .jar archive
345
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         // enterprise
356
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         // jar archive
369
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         // web archive
378
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 JavaDoc getClassUsage() {
400         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc {
415         public ArgumentException(String JavaDoc msg) {
416             super(msg);
417         }
418
419     }
420 }
421
Popular Tags