KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > jarprocessor > JarProcessorExecutor


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.update.internal.jarprocessor;
13
14 import java.io.*;
15 import java.util.Properties JavaDoc;
16 import java.util.zip.ZipException JavaDoc;
17 import org.eclipse.update.internal.jarprocessor.Main.Options;
18
19 public class JarProcessorExecutor {
20     public void runJarProcessor(Options options) {
21         if (options.input.isFile() && options.input.getName().endsWith(".zip")) { //$NON-NLS-1$
22
ZipProcessor processor = new ZipProcessor();
23             processor.setWorkingDirectory(options.outputDir);
24             processor.setSignCommand(options.signCommand);
25             processor.setPack(options.pack);
26             processor.setRepack(options.repack || (options.pack && options.signCommand != null));
27             processor.setUnpack(options.unpack);
28             processor.setVerbose(options.verbose);
29             processor.setProcessAll(options.processAll);
30             try {
31                 processor.processZip(options.input);
32             } catch (ZipException JavaDoc e) {
33                 if (options.verbose)
34                     e.printStackTrace();
35             } catch (IOException e) {
36                 if (options.verbose)
37                     e.printStackTrace();
38             }
39         } else {
40             JarProcessor processor = new JarProcessor();
41             JarProcessor packProcessor = null;
42
43             processor.setWorkingDirectory(options.outputDir);
44             processor.setProcessAll(options.processAll);
45             processor.setVerbose(options.verbose);
46
47             //load options file
48
Properties JavaDoc properties = new Properties JavaDoc();
49             if (options.input.isDirectory()) {
50                 File packProperties = new File(options.input, "pack.properties");
51                 if (packProperties.exists() && packProperties.isFile()) {
52                     InputStream in = null;
53                     try {
54                         in = new BufferedInputStream(new FileInputStream(packProperties));
55                         properties.load(in);
56                     } catch (IOException e) {
57                         if (options.verbose)
58                             e.printStackTrace();
59                     } finally {
60                         Utils.close(in);
61                     }
62                 }
63             }
64
65             if (options.unpack)
66                 addUnpackStep(processor, properties, options);
67
68             if (options.repack || (options.pack && options.signCommand != null))
69                 addPackUnpackStep(processor, properties, options);
70
71             if (options.signCommand != null)
72                 addSignStep(processor, properties, options);
73
74             if (options.pack) {
75                 packProcessor = new JarProcessor();
76                 packProcessor.setWorkingDirectory(options.outputDir);
77                 packProcessor.setProcessAll(options.processAll);
78                 packProcessor.setVerbose(options.verbose);
79                 addPackStep(packProcessor, properties, options);
80             }
81
82             try {
83                 process(options.input, options.unpack ? Utils.PACK_GZ_FILTER : Utils.JAR_FILTER, options.verbose, processor, packProcessor);
84             } catch (FileNotFoundException e) {
85                 if (options.verbose)
86                     e.printStackTrace();
87             }
88         }
89     }
90
91     protected void process(File input, FileFilter filter, boolean verbose, JarProcessor processor, JarProcessor packProcessor) throws FileNotFoundException {
92         if (!input.exists())
93             throw new FileNotFoundException();
94
95         File[] files = null;
96         if (input.isDirectory()) {
97             files = input.listFiles();
98         } else if (filter.accept(input)) {
99             files = new File[] {input};
100         }
101         for (int i = 0; i < files.length; i++) {
102             if (files[i].isDirectory()) {
103                 String JavaDoc dir = processor.getWorkingDirectory();
104                 processor.setWorkingDirectory(dir + "/" + files[i].getName()); //$NON-NLS-1$
105
if (packProcessor != null)
106                     packProcessor.setWorkingDirectory(dir + "/" + files[i].getName());
107                 process(files[i], filter, verbose, processor, packProcessor);
108                 processor.setWorkingDirectory(dir);
109                 if (packProcessor != null)
110                     packProcessor.setWorkingDirectory(dir);
111             } else if (filter.accept(files[i])) {
112                 try {
113                     File result = processor.processJar(files[i]);
114                     if (packProcessor != null && result != null && result.exists()) {
115                         packProcessor.processJar(result);
116                     }
117                 } catch (IOException e) {
118                     if (verbose)
119                         e.printStackTrace();
120                 }
121             }
122         }
123     }
124
125     public void addPackUnpackStep(JarProcessor processor, Properties JavaDoc properties, Options options) {
126         processor.addProcessStep(new PackUnpackStep(properties, options.verbose));
127     }
128
129     public void addSignStep(JarProcessor processor, Properties JavaDoc properties, Options options) {
130         processor.addProcessStep(new SignCommandStep(properties, options.signCommand, options.verbose));
131     }
132
133     public void addPackStep(JarProcessor processor, Properties JavaDoc properties, Options options) {
134         processor.addProcessStep(new PackStep(properties, options.verbose));
135     }
136
137     public void addUnpackStep(JarProcessor processor, Properties JavaDoc properties, Options options) {
138         processor.addProcessStep(new UnpackStep(properties, options.verbose));
139     }
140 }
141
Popular Tags