001: /*
002: * $Id: PackCompressorFactory.java 2061 2008-02-25 20:05:31Z jponge $
003: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
004: *
005: * http://izpack.org/
006: * http://izpack.codehaus.org/
007: *
008: * Copyright 2005 Klaus Bartz
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.compressor;
024:
025: import java.util.HashMap;
026:
027: import com.izforge.izpack.compiler.CompilerException;
028:
029: /**
030: * IzPack will be able to support different compression methods for the
031: * packs included in the installation jar file.
032: * This class is the factory which offers different "compressors" to
033: * IzPack. It is made to mask the internal structure of each "compressor"
034: * and gaves a common API for all supported compression methods to IzPack.
035: * IzPacks compiler uses this class to get an encoder and the informations
036: * which are needed to support the decompression in the installation.
037: * All "compressors" should use this class as API and should not be
038: * included directly in the IzPack compiler.
039: *
040: * @author Klaus Bartz
041: */
042: public class PackCompressorFactory {
043: /** This map contains all registered "compressors".
044: * The keys are the symbolic names which are used for a particular
045: * compression format.
046: */
047: private static HashMap<String, PackCompressor> typeMap = new HashMap<String, PackCompressor>();
048: private static CompilerException ShitHappens = null;
049:
050: static { // Add the well known pack compressors to this factory
051: catchedRegister(new RawPackCompressor());
052: catchedRegister(new DefaultPackCompressor());
053: catchedRegister(new BZip2PackCompressor());
054: }
055:
056: /**
057: * No object of this factory needed.
058: */
059: private PackCompressorFactory() {
060: super ();
061: }
062:
063: /**
064: * Register a particular pack compressor to this factory.
065: * The used symbolic name will be handled case insensitive.
066: * @param pc an instance of the pack compressor which describes
067: * encoder and decoder for a special compression format
068: */
069: public static void catchedRegister(PackCompressor pc) {
070: if (!good())
071: return;
072: try {
073: register(pc);
074: } catch (CompilerException e) {
075: ShitHappens = e;
076: }
077:
078: }
079:
080: /**
081: * Register a particular pack compressor to this factory.
082: * The used symbolic name will be handled case insensitive.
083: * @param pc an instance of the pack compressor which describes
084: * encoder and decoder for a special compression format
085: * @throws CompilerException if the symbol already exist or if
086: * the compressor is not valid
087: */
088: public static void register(PackCompressor pc)
089: throws CompilerException {
090: String[] syms = pc.getCompressionFormatSymbols();
091: for (String sym1 : syms) {
092: String sym = sym1.toLowerCase();
093: if (typeMap.containsKey(sym)) {
094: throw new CompilerException(
095: "PackCompressor for symbol " + sym
096: + " allready registered");
097: }
098: typeMap.put(sym, pc);
099: // TODO: add verify of PackCompressor.
100: }
101: }
102:
103: /**
104: * Returns whether a compressor exists for the given symbolic
105: * name or not.
106: * @param type symbolic compression name to be tested
107: * @return whether the given compression format will be supported
108: * or not
109: * @throws CompilerException
110: */
111: public static boolean isTypeSupported(String type)
112: throws CompilerException {
113: if (!good())
114: throw (ShitHappens);
115: type = type.toLowerCase();
116: return (typeMap.containsKey(type));
117: }
118:
119: /**
120: * Returns a newly created pack compressor with the given
121: * compression format.
122: * @param type symbol name of compression format to be used
123: * @return a newly created pack compressor
124: * @throws CompilerException if no encoder is registered for
125: * the chosen compression format
126: */
127: public static PackCompressor get(String type)
128: throws CompilerException {
129: if (!good())
130: throw (ShitHappens);
131: type = type.toLowerCase();
132: if (!typeMap.containsKey(type))
133: throw new CompilerException(
134: "No PackCompressor registered for the given symbol "
135: + type + ".");
136: return (typeMap.get(type));
137:
138: }
139:
140: /**
141: * Returns the exception which was thrown during
142: * registering of a pack compressor.
143: * @return the exception which was thrown during
144: * registering of a pack compressor
145: */
146: public static CompilerException getRegisterException() {
147: return ShitHappens;
148: }
149:
150: /**
151: * Sets an exception which was thrown during registering a pack compressor.
152: * @param registerException The register exception to set.
153: */
154: public static void setRegisterException(
155: CompilerException registerException) {
156: ShitHappens = registerException;
157: }
158:
159: public static boolean good() {
160: return (ShitHappens == null);
161: }
162: }
|