KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > compressor > PackCompressorFactory


1 /*
2  * $Id: PackCompressorFactory.java 1708 2007-01-13 18:31:26Z jponge $
3  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
4  *
5  * http://www.izforge.com/izpack/
6  * http://developer.berlios.de/projects/izpack/
7  *
8  * Copyright 2005 Klaus Bartz
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.compressor;
24
25 import java.util.HashMap JavaDoc;
26
27 import com.izforge.izpack.compiler.CompilerException;
28
29
30
31 /**
32  * IzPack will be able to support different compression methods for the
33  * packs included in the installation jar file.
34  * This class is the factory which offers different "compressors" to
35  * IzPack. It is made to mask the internal structure of each "compressor"
36  * and gaves a common API for all supported compression methods to IzPack.
37  * IzPacks compiler uses this class to get an encoder and the informations
38  * which are needed to support the decompression in the installation.
39  * All "compressors" should use this class as API and should not be
40  * included directly in the IzPack compiler.
41  *
42  * @author Klaus Bartz
43  */

44 public class PackCompressorFactory
45 {
46     /** This map contains all registered "compressors".
47      * The keys are the symbolic names which are used for a particular
48      * compression format.
49      */

50     private static HashMap JavaDoc typeMap = new HashMap JavaDoc();
51     private static CompilerException ShitHappens = null;
52     
53     static
54     { // Add the well known pack compressors to this factory
55
catchedRegister(new RawPackCompressor());
56         catchedRegister(new DefaultPackCompressor());
57         catchedRegister(new BZip2PackCompressor());
58     }
59     
60     /**
61      * No object of this factory needed.
62      */

63     private PackCompressorFactory()
64     {
65         super();
66     }
67     
68     
69     /**
70      * Register a particular pack compressor to this factory.
71      * The used symbolic name will be handled case insensitive.
72      * @param pc an instance of the pack compressor which describes
73      * encoder and decoder for a special compression format
74      */

75     public static void catchedRegister(PackCompressor pc)
76     {
77         if( !good())
78             return;
79         try
80         {
81             register(pc);
82         }
83         catch (CompilerException e)
84         {
85             ShitHappens = e;
86         }
87
88     }
89     
90     /**
91      * Register a particular pack compressor to this factory.
92      * The used symbolic name will be handled case insensitive.
93      * @param pc an instance of the pack compressor which describes
94      * encoder and decoder for a special compression format
95      * @throws CompilerException if the symbol already exist or if
96      * the compressor is not valid
97      */

98     public static void register(PackCompressor pc)
99         throws CompilerException
100     {
101         String JavaDoc [] syms = pc.getCompressionFormatSymbols();
102         for(int i = 0; i < syms.length; ++i)
103         {
104             String JavaDoc sym = syms[i].toLowerCase();
105             if( typeMap.containsKey(sym))
106                 throw new CompilerException("PackCompressor for symbol "
107                         + sym + " allready registered");
108             typeMap.put(sym, pc);
109             // TODO: add verify of PackCompressor.
110
}
111      }
112     
113     /**
114      * Returns whether a compressor exists for the given symbolic
115      * name or not.
116      * @param type symbolic compression name to be tested
117      * @return whether the given compression format will be supported
118      * or not
119      * @throws CompilerException
120      */

121     public static boolean isTypeSupported( String JavaDoc type ) throws CompilerException
122     {
123         if( ! good())
124             throw(ShitHappens);
125         type = type.toLowerCase();
126         return( typeMap.containsKey(type));
127     }
128
129     /**
130      * Returns a newly created pack compressor with the given
131      * compression format.
132      * @param type symbol name of compression format to be used
133      * @return a newly created pack compressor
134      * @throws CompilerException if no encoder is registered for
135      * the chosen compression format
136      */

137     public static PackCompressor get( String JavaDoc type)
138         throws CompilerException
139     {
140         if( ! good())
141             throw(ShitHappens);
142         type = type.toLowerCase();
143         if( ! typeMap.containsKey(type))
144             throw new CompilerException(
145                 "No PackCompressor registered for the given symbol "
146                 + type + ".");
147         return((PackCompressor) typeMap.get(type));
148         
149     }
150     /**
151      * Returns the exception which was thrown during
152      * registering of a pack compressor.
153      * @return the exception which was thrown during
154      * registering of a pack compressor
155      */

156     public static CompilerException getRegisterException()
157     {
158         return ShitHappens;
159     }
160     /**
161      * Sets an exception which was thrown during registering a pack compressor.
162      * @param registerException The register exception to set.
163      */

164     public static void setRegisterException(CompilerException registerException)
165     {
166         ShitHappens = registerException;
167     }
168     
169     public static boolean good()
170     {
171         return( ShitHappens == null );
172     }
173 }
174
Popular Tags