001/* 002 * $Id: PackCompressorFactory.java,v 1.2 2005/08/26 11:22:54 bartzkau Exp $ 003 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 004 * 005 * http://www.izforge.com/izpack/ 006 * http://developer.berlios.de/projects/izpack/ 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 023package com.izforge.izpack.compressor; 024 025import java.util.HashMap; 026 027import com.izforge.izpack.compiler.CompilerException; 028 029 030 031/** 032 * IzPack will be able to support different compression methods for the 033 * packs included in the installation jar file. 034 * This class is the factory which offers different "compressors" to 035 * IzPack. It is made to mask the internal structure of each "compressor" 036 * and gaves a common API for all supported compression methods to IzPack. 037 * IzPacks compiler uses this class to get an encoder and the informations 038 * which are needed to support the decompression in the installation. 039 * All "compressors" should use this class as API and should not be 040 * included directly in the IzPack compiler. 041 * 042 * @author Klaus Bartz 043 */ 044public class PackCompressorFactory 045{ 046 /** This map contains all registered "compressors". 047 * The keys are the symbolic names which are used for a particular 048 * compression format. 049 */ 050 private static HashMap typeMap = new HashMap(); 051 private static CompilerException ShitHappens = null; 052 053 static 054 { // Add the well known pack compressors to this factory 055 catchedRegister(new RawPackCompressor()); 056 catchedRegister(new DefaultPackCompressor()); 057 catchedRegister(new BZip2PackCompressor()); 058 } 059 060 /** 061 * No object of this factory needed. 062 */ 063 private PackCompressorFactory() 064 { 065 super(); 066 } 067 068 069 /** 070 * Register a particular pack compressor to this factory. 071 * The used symbolic name will be handled case insensitive. 072 * @param pc an instance of the pack compressor which describes 073 * encoder and decoder for a special compression format 074 * @throws CompilerException if the symbol already exist or if 075 * the compressor is not valid 076 */ 077 public static void catchedRegister(PackCompressor pc) 078 { 079 if( !good()) 080 return; 081 try 082 { 083 register(pc); 084 } 085 catch (CompilerException e) 086 { 087 ShitHappens = e; 088 } 089 090 } 091 092 /** 093 * Register a particular pack compressor to this factory. 094 * The used symbolic name will be handled case insensitive. 095 * @param pc an instance of the pack compressor which describes 096 * encoder and decoder for a special compression format 097 * @throws CompilerException if the symbol already exist or if 098 * the compressor is not valid 099 */ 100 public static void register(PackCompressor pc) 101 throws CompilerException 102 { 103 String [] syms = pc.getCompressionFormatSymbols(); 104 for(int i = 0; i < syms.length; ++i) 105 { 106 String sym = syms[i].toLowerCase(); 107 if( typeMap.containsKey(sym)) 108 throw new CompilerException("PackCompressor for symbol " 109 + sym + " allready registered"); 110 typeMap.put(sym, pc); 111 // TODO: add verify of PackCompressor. 112 } 113 } 114 115 /** 116 * Returns whether a compressor exists for the given symbolic 117 * name or not. 118 * @param type symbolic compression name to be tested 119 * @return whether the given compression format will be supported 120 * or not 121 * @throws CompilerException 122 */ 123 public static boolean isTypeSupported( String type ) throws CompilerException 124 { 125 if( ! good()) 126 throw(ShitHappens); 127 type = type.toLowerCase(); 128 return( typeMap.containsKey(type)); 129 } 130 131 /** 132 * Returns a newly created pack compressor with the given 133 * compression format. 134 * @param type symbol name of compression format to be used 135 * @return a newly created pack compressor 136 * @throws CompilerException if no encoder is registered for 137 * the chosen compression format 138 */ 139 public static PackCompressor get( String type) 140 throws CompilerException 141 { 142 if( ! good()) 143 throw(ShitHappens); 144 type = type.toLowerCase(); 145 if( ! typeMap.containsKey(type)) 146 throw new CompilerException( 147 "No PackCompressor registered for the given symbol " 148 + type + "."); 149 return((PackCompressor) typeMap.get(type)); 150 151 } 152 /** 153 * Returns the exception which was thrown during 154 * registering of a pack compressor. 155 * @return the exception which was thrown during 156 * registering of a pack compressor 157 */ 158 public static CompilerException getRegisterException() 159 { 160 return ShitHappens; 161 } 162 /** 163 * Sets an exception which was thrown during registering a pack compressor. 164 * @param registerException The register exception to set. 165 */ 166 public static void setRegisterException(CompilerException registerException) 167 { 168 ShitHappens = registerException; 169 } 170 171 public static boolean good() 172 { 173 return( ShitHappens == null ); 174 } 175}