001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package java.util.jar;
018:
019: import java.beans.PropertyChangeListener;
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.security.AccessController;
025: import java.security.PrivilegedAction;
026: import java.util.SortedMap;
027:
028: /**
029: * Class that initialize Packer and Unpacker
030: *
031: * See JSR200
032: */
033: public abstract class Pack200 {
034:
035: private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer"; //$NON-NLS-1$
036:
037: private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker"; //$NON-NLS-1$
038:
039: /**
040: * Prevent this class from being instantiated.
041: */
042: private Pack200() {
043: // do nothing
044: }
045:
046: /**
047: * The method first read from system property for the classname of a Packer,
048: * if such property exists, the class shall be initialized; or the default
049: * Packer will be returned
050: *
051: * @return a instance of Packer
052: */
053: public static Pack200.Packer newPacker() {
054: return (Packer) AccessController
055: .doPrivileged(new PrivilegedAction<Object>() {
056: public Object run() {
057: String className = System
058: .getProperty(SYSTEM_PROPERTY_PACKER,
059: "org.apache.harmony.archive.internal.pack200.Pack200PackerAdapter"); //$NON-NLS-1$
060: try {
061: // TODO Not sure if this will cause problems with
062: // loading the packer
063: return ClassLoader.getSystemClassLoader()
064: .loadClass(className).newInstance();
065: } catch (Exception e) {
066: throw new Error("Can't load class "
067: + className, e);
068: }
069: }
070: });
071:
072: }
073:
074: /**
075: * The method first read from system property for the classname of a
076: * Unpacker, if such property exists, the class shall be initialized; or the
077: * default Unpacker will be returned
078: *
079: * @return a instance of Unpacker
080: */
081: public static Pack200.Unpacker newUnpacker() {
082: return (Unpacker) AccessController
083: .doPrivileged(new PrivilegedAction<Object>() {
084: public Object run() {
085: String className = System
086: .getProperty(SYSTEM_PROPERTY_UNPACKER,
087: "org.apache.harmony.archive.internal.pack200.Pack200UnpackerAdapter");//$NON-NLS-1$
088: try {
089: return ClassLoader.getSystemClassLoader()
090: .loadClass(className).newInstance();
091: } catch (Exception e) {
092: throw new Error("Can't load class "
093: + className, e);
094: }
095: }
096: });
097: }
098:
099: /**
100: * interface of Packer
101: *
102: * See JSR 200 specification
103: */
104: public static interface Packer {
105:
106: /**
107: * the format of a class attribute name
108: */
109: static final String CLASS_ATTRIBUTE_PFX = "pack.class.attribute."; //$NON-NLS-1$
110:
111: /**
112: * the format of a code attribute name
113: */
114: static final String CODE_ATTRIBUTE_PFX = "pack.code.attribute."; //$NON-NLS-1$
115:
116: /**
117: * the deflation hint to set in the output archive
118: */
119: static final String DEFLATE_HINT = "pack.deflate.hint";//$NON-NLS-1$
120:
121: /**
122: * the indicated amount of effort to use in compressing the archive.
123: */
124: static final String EFFORT = "pack.effort";//$NON-NLS-1$
125:
126: /**
127: * a String of error
128: */
129: static final String ERROR = "error";//$NON-NLS-1$
130:
131: /**
132: * a String of false
133: */
134: static final String FALSE = "false";//$NON-NLS-1$
135:
136: /**
137: * the format of a field attribute name
138: */
139: static final String FIELD_ATTRIBUTE_PFX = "pack.field.attribute.";//$NON-NLS-1$
140:
141: /**
142: * a String of keep
143: */
144: static final String KEEP = "keep";//$NON-NLS-1$
145:
146: /**
147: * decide if all elements shall transmit in their original order
148: */
149: static final String KEEP_FILE_ORDER = "pack.keep.file.order";//$NON-NLS-1$
150:
151: /**
152: * a String of latest
153: */
154: static final String LATEST = "latest";//$NON-NLS-1$
155:
156: /**
157: * the format of a method attribute name
158: */
159: static final String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";//$NON-NLS-1$
160:
161: /**
162: * Packer shall attempt to determine the latest modification time if
163: * this is set to LASTEST
164: */
165: static final String MODIFICATION_TIME = "pack.modification.time";//$NON-NLS-1$
166:
167: /**
168: * a String of pass
169: */
170: static final String PASS = "pass";//$NON-NLS-1$
171:
172: /**
173: * the file that will not be compressed.
174: */
175: static final String PASS_FILE_PFX = "pack.pass.file.";//$NON-NLS-1$
176:
177: /**
178: * packer progress as a percentage
179: */
180: static final String PROGRESS = "pack.progress";//$NON-NLS-1$
181:
182: /**
183: * The number of bytes of each archive segment.
184: */
185: static final String SEGMENT_LIMIT = "pack.segment.limit";//$NON-NLS-1$
186:
187: /**
188: * a String of strip
189: */
190: static final String STRIP = "strip";//$NON-NLS-1$
191:
192: /**
193: * a String of true
194: */
195: static final String TRUE = "true";//$NON-NLS-1$
196:
197: /**
198: * the action to take if an unknown attribute is encountered.
199: */
200: static final String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";//$NON-NLS-1$
201:
202: /**
203: *
204: * @return the properties of packer
205: */
206: SortedMap<String, String> properties();
207:
208: /**
209: * Pack jarfile with pack arithmetic
210: *
211: * @param in
212: * jarfile to be compact
213: * @param out
214: * stream of compact data
215: * @throws IOException
216: * if I/O exception occurs
217: */
218: void pack(JarFile in, OutputStream out) throws IOException;
219:
220: /**
221: * Pack jarStream with pack arithmetic
222: *
223: * @param in
224: * stream of uncompact jar data
225: * @param out
226: * stream of compact data
227: * @throws IOException
228: * if I/O exception occurs
229: */
230: void pack(JarInputStream in, OutputStream out)
231: throws IOException;
232:
233: /**
234: * add a listener for PropertyChange events
235: *
236: * @param listener
237: * the listener to listen if PropertyChange events occurs
238: */
239: void addPropertyChangeListener(PropertyChangeListener listener);
240:
241: /**
242: * remove a listener
243: *
244: * @param listener
245: * listener to remove
246: */
247: void removePropertyChangeListener(
248: PropertyChangeListener listener);
249: }
250:
251: /**
252: * interface of unpacker
253: *
254: * See JSR 200 specification
255: */
256: public static interface Unpacker {
257:
258: /**
259: * The String indicating if the unpacker should ignore all transmitted
260: * values,can be replaced by either true or false
261: */
262: static final String DEFLATE_HINT = "unpack.deflate.hint";//$NON-NLS-1$
263:
264: /**
265: * a String of false
266: */
267: static final String FALSE = "false";//$NON-NLS-1$
268:
269: /**
270: * a String of keep
271: */
272: static final String KEEP = "keep";//$NON-NLS-1$
273:
274: /**
275: * the progress as a percentage
276: */
277: static final String PROGRESS = "unpack.progress";//$NON-NLS-1$
278:
279: /**
280: * a String of true
281: */
282: static final String TRUE = "true";//$NON-NLS-1$
283:
284: /**
285: *
286: * @return the properties of unpacker
287: */
288: SortedMap<String, String> properties();
289:
290: /**
291: * unpack stream into jarfile with pack arithmetic
292: *
293: * @param in
294: * stream to uncompact
295: * @param out
296: * jarstream of uncompact data
297: * @throws IOException
298: * if I/O exception occurs
299: */
300: void unpack(InputStream in, JarOutputStream out)
301: throws IOException;
302:
303: /**
304: * unpack File into jarfile with pack arithmetic
305: *
306: * @param in
307: * file to be uncompact
308: * @param out
309: * jarstream of uncompact data
310: * @throws IOException
311: * if I/O exception occurs
312: */
313: void unpack(File in, JarOutputStream out) throws IOException;
314:
315: /**
316: * add a listener for PropertyChange events
317: *
318: * @param listener
319: * the listener to listen if PropertyChange events occurs
320: */
321: void addPropertyChangeListener(PropertyChangeListener listener);
322:
323: /**
324: * remove a listener
325: *
326: * @param listener
327: * listener to remove
328: */
329: void removePropertyChangeListener(
330: PropertyChangeListener listener);
331: }
332:
333: }
|