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.utils.system.launchers;
038:
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.FileNotFoundException;
042: import java.io.IOException;
043: import java.io.InputStream;
044: import org.netbeans.installer.utils.FileUtils;
045: import org.netbeans.installer.utils.LogManager;
046: import org.netbeans.installer.utils.ResourceUtils;
047: import org.netbeans.installer.utils.StringUtils;
048:
049: /**
050: *
051: * @author Dmitry Lipin
052: */
053: public class LauncherResource {
054:
055: public static enum Type {
056: BUNDLED, ABSOLUTE, RELATIVE_JAVAHOME, RELATIVE_USERHOME, RELATIVE_LAUNCHER_PARENT, RELATIVE_LAUNCHER_TMPDIR;
057:
058: public long toLong() {
059: switch (this ) {
060: case BUNDLED:
061: return 0L;
062: case ABSOLUTE:
063: return 1L;
064: case RELATIVE_JAVAHOME:
065: return 2L;
066: case RELATIVE_USERHOME:
067: return 3L;
068: case RELATIVE_LAUNCHER_PARENT:
069: return 4L;
070: case RELATIVE_LAUNCHER_TMPDIR:
071: return 5L;
072: }
073: return 1L;
074: }
075:
076: public String toString() {
077: switch (this ) {
078: case BUNDLED:
079: return "nbi.launcher.tmp.dir";
080: case ABSOLUTE:
081: return StringUtils.EMPTY_STRING;
082: case RELATIVE_JAVAHOME:
083: return "nbi.launcher.java.home";
084: case RELATIVE_USERHOME:
085: return "nbi.launcher.user.home";
086: case RELATIVE_LAUNCHER_PARENT:
087: return "nbi.launcher.parent.dir";
088: case RELATIVE_LAUNCHER_TMPDIR:
089: return "nbi.launcher.tmp.dir";
090: default:
091: return null;
092:
093: }
094: }
095:
096: public String getPathString(String path) {
097: if (this .equals(Type.ABSOLUTE)) {
098: return path;
099: } else if (this .equals(Type.BUNDLED)) {
100: return "$L{" + Type.BUNDLED.toString() + "}/"
101: + new File(path).getName();
102: } else {
103: return "$L{" + this .toString() + "}/" + path;
104: }
105: }
106: };
107:
108: private Type type;
109: private String path;
110: private boolean resourceBased;
111:
112: /**
113: * Bundled launcher file
114: */
115: public LauncherResource(File file) {
116: this (true, file);
117: }
118:
119: public LauncherResource(boolean bundled, File file) {
120: this .type = (bundled) ? Type.BUNDLED : Type.ABSOLUTE;
121: this .path = file.getPath();
122: }
123:
124: /**
125: * External or bundled launcher file
126: */
127: public LauncherResource(Type type, String path) {
128: this .type = type;
129: this .path = path;
130: }
131:
132: /** Bundled launcher resource with NBI resource as a source */
133: public LauncherResource(String resourceURI) {
134: this .type = Type.BUNDLED;
135: this .path = resourceURI;
136: this .resourceBased = true;
137: }
138:
139: public Type getPathType() {
140: return type;
141: }
142:
143: public boolean isBundled() {
144: return type.equals(Type.BUNDLED);
145: }
146:
147: public String getPath() {
148: return path;
149: }
150:
151: public boolean isBasedOnResource() {
152: return resourceBased;
153: }
154:
155: public InputStream getInputStream() {
156: if (isBundled()) {
157: if (resourceBased) {
158: return ResourceUtils.getResource(path);
159: } else {
160: File f = new File(getPath());
161: if (FileUtils.exists(f)) {
162: try {
163: return new FileInputStream(f);
164: } catch (FileNotFoundException ex) {
165: LogManager.log(ex);
166: }
167: }
168: return null;
169: }
170: } else {
171: return null;
172: }
173:
174: }
175:
176: public String getAbsolutePath() {
177: return type.getPathString(path);
178: }
179:
180: public long getSize() {
181: if (isBundled()) {
182: return isBasedOnResource() ? ResourceUtils
183: .getResourceSize(path) : FileUtils
184: .getSize(new File(path));
185: } else {
186: return 0;
187: }
188: }
189:
190: public String getMD5() {
191: final InputStream is = getInputStream();
192: String md5 = null;
193: if (is != null) {
194: try {
195: md5 = FileUtils.getMd5(is);
196: } catch (IOException e) {
197: LogManager.log(e);
198: } finally {
199: try {
200: is.close();
201: } catch (IOException e) {
202: LogManager.log(e);
203: }
204: }
205: }
206:
207: return md5;
208: }
209: }
|