001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.sandbox.utils.installation;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.security.NoSuchAlgorithmException;
042: import java.util.HashMap;
043: import org.netbeans.installer.utils.helper.ErrorLevel;
044: import org.netbeans.installer.utils.FileUtils;
045: import org.netbeans.installer.utils.LogManager;
046: import org.netbeans.installer.sandbox.utils.installation.conditions.FileCondition;
047:
048: /**
049: *
050: * @author Dmitry Lipin
051: */
052: public class InstallationFileObject {
053: private HashMap<String, Object> fileData;
054:
055: public static final String PATH_KEY = "path"; //NOI18N
056: public static final String CRC32_KEY = "crc32";//NOI18N
057: public static final String MD5_KEY = "md5"; //NOI18N
058: public static final String SHA1_KEY = "sha1";//NOI18N
059: public static final String SIZE_KEY = "size";//NOI18N
060: public static final String TIME_KEY = "time";//NOI18N
061: public static final String COND_KEY = "cond";//NOI18N
062:
063: private FileCondition condition;
064:
065: public InstallationFileObject() {
066: }
067:
068: public InstallationFileObject(File file) {
069: setFile(file);
070: }
071:
072: public InstallationFileObject(HashMap<String, Object> data) {
073: setFileData(data);
074: }
075:
076: public InstallationFileObject(File file,
077: HashMap<String, Object> data) {
078: this (data);
079: setFile(file);
080: }
081:
082: public InstallationFileObject(File file,
083: HashMap<String, Object> data, FileCondition cond) {
084: this (file, data);
085: setFileCondition(cond);
086: }
087:
088: public InstallationFileObject(File file, FileCondition conds) {
089: this (file, new HashMap<String, Object>(), conds);
090: }
091:
092: public void initDataFromFile() {
093: setCRC32(getFile());
094: setLastModified(getFile());
095: setSize(getFile());
096: setMD5(getFile());
097: setSHA1(getFile());
098: }
099:
100: public FileCondition getFileCondition() {
101: return condition;
102: }
103:
104: public void setFileCondition(FileCondition condition) {
105: this .condition = condition;
106: }
107:
108: public HashMap<String, Object> getFileData() {
109: if (fileData == null) {
110: fileData = new HashMap<String, Object>();
111: }
112: return fileData;
113: }
114:
115: public void setFileData(HashMap<String, Object> fileData) {
116: this .fileData = fileData;
117: }
118:
119: public File getFile() {
120: return new File(getStringValue(PATH_KEY));
121: }
122:
123: public void setFile(File file) {
124: setData(PATH_KEY, file.getPath());
125: }
126:
127: public long getSize() {
128: return getLongValue(SIZE_KEY);
129: }
130:
131: public long getLongValue(String key) {
132: return ((Long) getData(key)).longValue();
133: }
134:
135: public void setLongValue(String key, String value) {
136: setData(key, Long.parseLong(value));
137: }
138:
139: public void setSize(long size) {
140: setData(SIZE_KEY, size);
141: }
142:
143: public void setSize(String size) {
144: setLongValue(SIZE_KEY, size);
145: }
146:
147: public void setSize(File file) {
148: if (file != null && file.exists() && file.isFile()) {
149: setSize(FileUtils.getFileSize(file));
150: }
151: }
152:
153: public long getLastModified() {
154: return getLongValue(TIME_KEY);
155: }
156:
157: public String getStringValue(String key) {
158: return (String) getData(key);
159: }
160:
161: public void setLastModified(long tm) {
162: setData(TIME_KEY, tm);
163: }
164:
165: public void setLastModified(String tm) {
166: setLongValue(TIME_KEY, tm);
167: }
168:
169: public void setLastModified(File file) {
170: if (file != null && file.exists() && file.isFile()) {
171: setLastModified(FileUtils.getLastModified(file).getTime());
172: }
173: }
174:
175: public String getCRC32() {
176: return getStringValue(CRC32_KEY);
177: }
178:
179: public void setCRC32(String value) {
180: setData(CRC32_KEY, value);
181: }
182:
183: public void setCRC32(File file) {
184: try {
185: if (file != null && file.exists() && file.isFile()) {
186: setCRC32(FileUtils.getCrc32String(file));
187: }
188: } catch (IOException ex) {
189: LogManager.log(ErrorLevel.WARNING, ex);
190: }
191: }
192:
193: public String getMD5() {
194: return getStringValue(MD5_KEY);
195: }
196:
197: public void setMD5(String value) {
198: setData(MD5_KEY, value);
199: }
200:
201: public void setMD5(File file) {
202: try {
203: if (file != null && file.exists() && file.isFile()) {
204: setMD5(FileUtils.getMd5String(file));
205: }
206: } catch (IOException ex) {
207: LogManager.log(ErrorLevel.WARNING, ex);
208: } catch (NoSuchAlgorithmException ex) {
209: LogManager.log(ErrorLevel.WARNING, ex);
210: }
211: }
212:
213: public String getSHA1() {
214: return getStringValue(SHA1_KEY);
215: }
216:
217: public String getCondition() {
218: return getStringValue(COND_KEY);
219: }
220:
221: public void setSHA1(String value) {
222: setData(SHA1_KEY, value);
223: }
224:
225: public void setSHA1(File file) {
226: try {
227: if (file != null && file.exists() && file.isFile()) {
228: setSHA1(FileUtils.getSha1String(file));
229: }
230: } catch (IOException ex) {
231: LogManager.log(ErrorLevel.WARNING, ex);
232: } catch (NoSuchAlgorithmException ex) {
233: LogManager.log(ErrorLevel.WARNING, ex);
234: }
235: }
236:
237: public void setData(String key, Object value) {
238: getFileData().put(key, value);
239: }
240:
241: public Object getData(String key) {
242: return getFileData().get(key);
243: }
244:
245: public boolean accept() {
246: return (condition == null) ? false : condition.accept(this);
247: }
248: }
|