001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2001 Johannes Lehtinen
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack;
023:
024: import com.izforge.izpack.util.OsConstraint;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.Serializable;
029: import java.util.List;
030: import java.util.Map;
031:
032: /**
033: * Encloses information about a packed file. This class abstracts the way file data is stored to
034: * package.
035: *
036: * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
037: */
038: public class PackFile implements Serializable {
039:
040: static final long serialVersionUID = -834377078706854909L;
041:
042: public static final int OVERRIDE_FALSE = 0;
043:
044: public static final int OVERRIDE_TRUE = 1;
045:
046: public static final int OVERRIDE_ASK_FALSE = 2;
047:
048: public static final int OVERRIDE_ASK_TRUE = 3;
049:
050: public static final int OVERRIDE_UPDATE = 4;
051:
052: /** Only available when compiling. Makes no sense when installing, use relativePath instead. */
053: public transient String sourcePath = null;//should not be used anymore - may deprecate it.
054: /** The Path of the file relative to the given (compiletime's) basedirectory.
055: * Can be resolved while installing with either current working directory or directory of "installer.jar". */
056: protected String relativePath = null;
057:
058: /** The full path name of the target file */
059: private String targetPath = null;
060:
061: /** The target operating system constraints of this file */
062: private List<OsConstraint> osConstraints = null;
063:
064: /** The length of the file in bytes */
065: private long length = 0;
066:
067: /** The last-modification time of the file. */
068: private long mtime = -1;
069:
070: /** True if file is a directory (length should be 0 or ignored) */
071: private boolean isDirectory = false;
072:
073: /** Whether or not this file is going to override any existing ones */
074: private int override = OVERRIDE_FALSE;
075:
076: /** Additional attributes or any else for customisation */
077: private Map additionals = null;
078:
079: public String previousPackId = null;
080:
081: public long offsetInPreviousPack = -1;
082:
083: /** condition for this packfile */
084: private String condition = null;
085:
086: /**
087: * Constructs and initializes from a source file.
088: *
089: * @param baseDir the baseDirectory of the Fileselection/compilation or null
090: * @param src file which this PackFile describes
091: * @param target the path to install the file to
092: * @param osList OS constraints
093: * @param override what to do when the file already exists
094: * @throws FileNotFoundException if the specified file does not exist.
095: */
096: public PackFile(File baseDir, File src, String target,
097: List<OsConstraint> osList, int override)
098: throws FileNotFoundException {
099: this (src, computeRelativePathFrom(baseDir, src), target,
100: osList, override, null);
101: }
102:
103: /**
104: * Constructs and initializes from a source file.
105: *
106: * @param src file which this PackFile describes
107: * @param relativeSourcePath the path relative to the compiletime's basedirectory, use computeRelativePathFrom(File, File) to compute this.
108: * @param target the path to install the file to
109: * @param osList OS constraints
110: * @param override what to do when the file already exists
111: * @param additionals additional attributes
112: * @throws FileNotFoundException if the specified file does not exist.
113: */
114: public PackFile(File src, String relativeSourcePath, String target,
115: List<OsConstraint> osList, int override, Map additionals)
116: throws FileNotFoundException {
117: if (!src.exists()) // allows cleaner client co
118: throw new FileNotFoundException("No such file: " + src);
119:
120: if ('/' != File.separatorChar)
121: target = target.replace(File.separatorChar, '/');
122: if (target.endsWith("/"))
123: target = target.substring(0, target.length() - 1);
124:
125: this .sourcePath = src.getPath();
126: this .relativePath = relativeSourcePath;
127:
128: this .targetPath = target;
129: this .osConstraints = osList;
130: this .override = override;
131:
132: this .length = src.length();
133: this .mtime = src.lastModified();
134: this .isDirectory = src.isDirectory();
135: this .additionals = additionals;
136: }
137:
138: /**
139: * Constructs and initializes from a source file.
140: *
141: * @param baseDir The Base directory that is used to search for the files. This is used to build the relative path's
142: * @param src file which this PackFile describes
143: * @param target the path to install the file to
144: * @param osList OS constraints
145: * @param override what to do when the file already exists
146: * @param additionals additional attributes
147: * @throws FileNotFoundException if the specified file does not exist.
148: */
149: public PackFile(File baseDir, File src, String target,
150: List<OsConstraint> osList, int override, Map additionals)
151: throws FileNotFoundException {
152: this (src, computeRelativePathFrom(baseDir, src), target,
153: osList, override, additionals);
154: }
155:
156: /**
157: * Builds the relative path of file to the baseDir.
158: * @param baseDir The Base Directory to build the relative path from
159: * @param file the file inside basDir
160: * @return null if file is not a inside baseDir
161: */
162: public static String computeRelativePathFrom(File baseDir, File file) {
163: if (baseDir == null || file == null)
164: return null;
165: try { //extract relative path...
166: if (file.getCanonicalPath().startsWith(
167: baseDir.getCanonicalPath())) {
168: return file.getCanonicalPath().substring(
169: baseDir.getCanonicalPath().length());
170: }
171: } catch (Exception x)//don't throw an exception here. return null instead!
172: {
173: //if we cannot build the relative path because of an error, the developer should be informed about.
174: x.printStackTrace();
175: }
176:
177: //we can not build a relative path for whatever reason
178: return null;
179: }
180:
181: public void setPreviousPackFileRef(String previousPackId,
182: Long offsetInPreviousPack) {
183: this .previousPackId = previousPackId;
184: this .offsetInPreviousPack = offsetInPreviousPack;
185: }
186:
187: /** The target operating system constraints of this file */
188: public final List<OsConstraint> osConstraints() {
189: return osConstraints;
190: }
191:
192: /** The length of the file in bytes */
193: public final long length() {
194: return length;
195: }
196:
197: /** The last-modification time of the file. */
198: public final long lastModified() {
199: return mtime;
200: }
201:
202: /** Whether or not this file is going to override any existing ones */
203: public final int override() {
204: return override;
205: }
206:
207: public final boolean isDirectory() {
208: return isDirectory;
209: }
210:
211: public final boolean isBackReference() {
212: return (previousPackId != null);
213: }
214:
215: /** The full path name of the target file, using '/' as fileseparator. */
216: public final String getTargetPath() {
217: return targetPath;
218: }
219:
220: /** The Path of the file relative to the given (compiletime's) basedirectory.
221: * Can be resolved while installing with either current working directory or directory of "installer.jar" */
222: public String getRelativeSourcePath() {
223: return relativePath;
224: }
225:
226: /**
227: * Returns the additionals map.
228: *
229: * @return additionals
230: */
231: public Map getAdditionals() {
232: return additionals;
233: }
234:
235: /**
236: * @return the condition
237: */
238: public String getCondition() {
239: return this .condition;
240: }
241:
242: /**
243: * @param condition the condition to set
244: */
245: public void setCondition(String condition) {
246: this .condition = condition;
247: }
248:
249: public boolean hasCondition() {
250: return this.condition != null;
251: }
252: }
|