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.FileFilter;
041: import java.io.FileInputStream;
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.PropertyResourceBundle;
048: import org.netbeans.installer.utils.FileUtils;
049: import org.netbeans.installer.utils.LogManager;
050: import org.netbeans.installer.utils.ResourceUtils;
051: import org.netbeans.installer.utils.StringUtils;
052: import org.netbeans.installer.utils.helper.JavaCompatibleProperties;
053: import static org.netbeans.installer.utils.system.launchers.LauncherResource.Type.*;
054:
055: /**
056: *
057: * @author Dmitry Lipin
058: */
059: public class LauncherProperties implements Cloneable {
060: protected File stubFile;
061: protected List<LauncherResource> jars;
062: protected List<LauncherResource> jvms;
063: protected List<LauncherResource> otherResources;
064:
065: protected HashMap<String, PropertyResourceBundle> i18nMap;
066: protected LauncherResource testJVMFile;
067: protected File outputFile;
068: protected boolean addExtenstion;
069: protected List<String> jvmArguments;
070: protected List<String> appArguments;
071: protected String mainClass;
072: protected String testJVMClass;
073: protected List<JavaCompatibleProperties> compatibleJava;
074:
075: public LauncherResource getTestJVMFile() {
076: return testJVMFile;
077: }
078:
079: public LauncherProperties(LauncherProperties nl) {
080: appArguments = nl.appArguments;
081: jvmArguments = nl.jvmArguments;
082: i18nMap = nl.i18nMap;
083: jars = nl.jars;
084: jvms = nl.jvms;
085: outputFile = nl.outputFile;
086: addExtenstion = nl.addExtenstion;
087: compatibleJava = nl.compatibleJava;
088: mainClass = nl.mainClass;
089: testJVMClass = nl.testJVMClass;
090: stubFile = nl.stubFile;
091: testJVMFile = nl.testJVMFile;
092: otherResources = nl.otherResources;
093: }
094:
095: public LauncherProperties() {
096: compatibleJava = new ArrayList<JavaCompatibleProperties>();
097: jvmArguments = new ArrayList<String>();
098: appArguments = new ArrayList<String>();
099: i18nMap = new HashMap<String, PropertyResourceBundle>();
100: jars = new ArrayList<LauncherResource>();
101: jvms = new ArrayList<LauncherResource>();
102: otherResources = new ArrayList<LauncherResource>();
103: }
104:
105: public void setLauncherStub(File launcherStub) {
106: this .stubFile = launcherStub;
107: }
108:
109: public void addJar(LauncherResource file) {
110: jars.add(file);
111: }
112:
113: public String getMainClass() {
114: return mainClass;
115: }
116:
117: public String getTestJVMClass() {
118: return testJVMClass;
119: }
120:
121: public void setJvmArguments(String[] jvmArguments) {
122: this .jvmArguments = new ArrayList<String>();
123: for (String s : jvmArguments) {
124: this .jvmArguments.add(s);
125: }
126: }
127:
128: public void setJvmArguments(List<String> jvmArguments) {
129: this .jvmArguments = jvmArguments;
130: }
131:
132: public void setI18n(File i18nDir) throws IOException {
133: loadPropertiesMap(getPropertiesFiles(i18nDir));
134: }
135:
136: public void setI18n(File[] files) throws IOException {
137: loadPropertiesMap(files);
138: }
139:
140: public void setI18n(String[] resources) throws IOException {
141: loadPropertiesMap(resources);
142: }
143:
144: public void setI18n(List<String> resources) throws IOException {
145: loadPropertiesMap(resources);
146: }
147:
148: public void setOutput(File output) {
149: setOutput(output, false);
150: }
151:
152: public void setOutput(File output, boolean addExt) {
153: this .outputFile = output;
154: this .addExtenstion = addExt;
155: }
156:
157: public void setTestJVM(LauncherResource testJVM) {
158: this .testJVMFile = testJVM;
159: }
160:
161: public void addCompatibleJava(JavaCompatibleProperties javaProp) {
162: compatibleJava.add(javaProp);
163: }
164:
165: public void setAppArguments(String[] appArguments) {
166: this .appArguments = new ArrayList<String>();
167: for (String s : appArguments) {
168: this .appArguments.add(s);
169: }
170: }
171:
172: public void setAppArguments(List<String> appArguments) {
173: this .appArguments = appArguments;
174: }
175:
176: public File getOutputFile() {
177: return outputFile;
178: }
179:
180: public List<LauncherResource> getJars() {
181: return jars;
182: }
183:
184: public List<String> getAppArguments() {
185: return appArguments;
186: }
187:
188: public List<String> getJvmArguments() {
189: return jvmArguments;
190: }
191:
192: public List<JavaCompatibleProperties> getJavaCompatibleProperties() {
193: return compatibleJava;
194: }
195:
196: public File getStubFile() {
197: return stubFile;
198: }
199:
200: public void addJVM(LauncherResource location) {
201: jvms.add(location);
202: }
203:
204: public List<LauncherResource> getJVMs() {
205: return jvms;
206: }
207:
208: public void setMainClass(String mainClass) {
209: this .mainClass = mainClass;
210: }
211:
212: public void setTestJVMClass(String testClass) {
213: this .testJVMClass = testClass;
214: }
215:
216: HashMap<String, PropertyResourceBundle> getI18nMap() {
217: return i18nMap;
218: }
219:
220: public void addOtherResource(LauncherResource resource) {
221: otherResources.add(resource);
222: }
223:
224: public List<LauncherResource> getOtherResources() {
225: return otherResources;
226: }
227:
228: private String getLocaleName(String name) {
229: String loc = StringUtils.EMPTY_STRING;
230: int idx = name.indexOf("_");
231: int end = name.indexOf(FileUtils.PROPERTIES_EXTENSION);
232: if (idx != -1) {
233: loc = name.substring(idx + 1, end);
234: }
235: return loc;
236: }
237:
238: private PropertyResourceBundle getBundle(File file)
239: throws IOException {
240: return getBundle(file.getPath(), new FileInputStream(file));
241: }
242:
243: private PropertyResourceBundle getBundle(String dest, InputStream is)
244: throws IOException {
245: if (is == null) {
246: throw new IOException(ResourceUtils.getString(
247: LauncherProperties.class,
248: ERROR_CANNOT_LOAD_BUNDLE_KEY, dest)); //NOI18N
249: }
250:
251: try {
252: return new PropertyResourceBundle(is);
253: } catch (IOException ex) {
254: throw new IOException(ResourceUtils.getString(
255: LauncherProperties.class,
256: ERROR_CANNOT_LOAD_BUNDLE_KEY, dest)); //NOI18N
257: } finally {
258: try {
259: is.close();
260: } catch (IOException ex) {
261: ex = null;
262: }
263: }
264: }
265:
266: // resources should be in form of <dir>/<dir>/<dir>/<file>
267: private void loadPropertiesMap(String[] resources)
268: throws IOException {
269: i18nMap.clear();
270: for (String resource : resources) {
271: String loc = getLocaleName(ResourceUtils
272: .getResourceFileName(resource));
273: i18nMap.put(loc, getBundle(resource, ResourceUtils
274: .getResource(resource)));
275: }
276: }
277:
278: private void loadPropertiesMap(List<String> resources)
279: throws IOException {
280: String[] array = new String[resources.size()];
281: for (int i = 0; i < resources.size(); i++) {
282: array[i] = resources.get(i);
283: }
284: loadPropertiesMap(array);
285: }
286:
287: private File[] getPropertiesFiles(File dir) throws IOException {
288: if (!dir.exists()) {
289: throw new IOException(ResourceUtils.getString(
290: LauncherProperties.class,
291: ERROR_DIRECTORY_DONT_EXIST_KEY, dir));
292: }
293: if (!dir.isDirectory()) {
294: throw new IOException(ResourceUtils.getString(
295: LauncherProperties.class, ERROR_NOT_DIRECTORY_KEY,
296: dir));
297: }
298:
299: File[] files = dir.listFiles(new FileFilter() {
300: public boolean accept(File filename) {
301: return filename.getName().endsWith(
302: FileUtils.PROPERTIES_EXTENSION);
303: }
304: });
305:
306: if (files == null || files.length == 0) {
307: throw new IOException(ResourceUtils.getString(
308: LauncherProperties.class, ERROR_NO_FILES_KEY, dir));
309: }
310: return files;
311: }
312:
313: private void loadPropertiesMap(File[] files) throws IOException {
314: i18nMap.clear();
315: for (File f : files) {
316: String loc = getLocaleName(f.getName());
317: LogManager.log("Adding bundle with locale [" + loc
318: + "] using file " + f);
319: i18nMap.put(loc, getBundle(f));
320: }
321: }
322:
323: private static final String ERROR_CANNOT_LOAD_BUNDLE_KEY = "LP.error.cannot.load.bundle";//NOI18N
324: private static final String ERROR_NO_FILES_KEY = "LP.error.no.files";//NOI18N
325: private static final String ERROR_NOT_DIRECTORY_KEY = "LP.error.not.directory";//NOI18N
326: private static final String ERROR_DIRECTORY_DONT_EXIST_KEY = "LP.error.directory.do.not.exist";//NOI18N
327: }
|