KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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 package org.eclipse.update.internal.jarprocessor;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Properties JavaDoc;
17
18 /**
19  * @author aniefer
20  *
21  */

22 public class UnpackStep extends CommandStep {
23     public static final String JavaDoc UNPACKER_PROPERTY = "org.eclipse.update.jarprocessor.Unpacker"; //$NON-NLS-1$
24
private static Boolean JavaDoc canUnpack = null;
25     private static String JavaDoc unpackCommand = null;
26
27     public static boolean canUnpack() {
28         if (canUnpack != null)
29             return canUnpack.booleanValue();
30
31         String JavaDoc[] locations = Utils.getPack200Commands("unpack200"); //$NON-NLS-1$
32
if (locations == null) {
33             canUnpack = Boolean.FALSE;
34             unpackCommand = null;
35             return false;
36         }
37
38         int result;
39         for (int i = 0; i < locations.length; i++) {
40             if (locations[i] == null)
41                 continue;
42             result = execute(new String JavaDoc[] {locations[i], "-V"}); //$NON-NLS-1$
43
if (result == 0) {
44                 unpackCommand = locations[i];
45                 canUnpack = Boolean.TRUE;
46                 return true;
47             }
48         }
49
50         canUnpack = Boolean.FALSE;
51         return false;
52     }
53
54     public UnpackStep(Properties JavaDoc options) {
55         super(options, null, null, false);
56     }
57
58     public UnpackStep(Properties JavaDoc options, boolean verbose) {
59         super(options, null, null, verbose);
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.update.jarprocessor.IProcessStep#recursionEffect(java.lang.String)
64      */

65     public String JavaDoc recursionEffect(String JavaDoc entryName) {
66         if (canUnpack() && entryName.endsWith(Utils.PACKED_SUFFIX)) {
67             return entryName.substring(0, entryName.length() - Utils.PACKED_SUFFIX.length());
68         }
69         return null;
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.update.jarprocessor.IProcessStep#preProcess(java.io.File, java.io.File)
74      */

75     public File JavaDoc preProcess(File JavaDoc input, File JavaDoc workingDirectory, List JavaDoc containers) {
76         if (canUnpack() && unpackCommand != null) {
77             String JavaDoc name = input.getName();
78             if (name.endsWith(Utils.PACKED_SUFFIX)) {
79                 name = name.substring(0, name.length() - Utils.PACKED_SUFFIX.length());
80
81                 File JavaDoc unpacked = new File JavaDoc(workingDirectory, name);
82                 File JavaDoc parent = unpacked.getParentFile();
83                 if (!parent.exists())
84                     parent.mkdirs();
85                 try {
86                     String JavaDoc options = getOptions().getProperty(input.getName() + ".unpack.args"); //$NON-NLS-1$
87
String JavaDoc[] cmd = null;
88                     if (options != null) {
89                         cmd = new String JavaDoc[] {unpackCommand, options, input.getCanonicalPath(), unpacked.getCanonicalPath()};
90                     } else {
91                         cmd = new String JavaDoc[] {unpackCommand, input.getCanonicalPath(), unpacked.getCanonicalPath()};
92                     }
93                     int result = execute(cmd, verbose);
94                     if (result != 0 && verbose)
95                         System.out.println("Error: " + result + " was returned from command: " + Utils.concat(cmd)); //$NON-NLS-1$ //$NON-NLS-2$
96
} catch (IOException JavaDoc e) {
97                     if (verbose)
98                         e.printStackTrace();
99                     return null;
100                 }
101                 return unpacked;
102             }
103         }
104         return null;
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.update.jarprocessor.IProcessStep#postProcess(java.io.File, java.io.File)
109      */

110     public File JavaDoc postProcess(File JavaDoc input, File JavaDoc workingDirectory, List JavaDoc containers) {
111         return null;
112     }
113
114     public String JavaDoc getStepName() {
115         return "Unpack"; //$NON-NLS-1$
116
}
117 }
118
Popular Tags