001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application;
022:
023: import java.io.*;
024:
025: /**
026: *
027: * @author Rodrigo
028: */
029: public class PropertiesBuilder {
030: /**
031: * Creates a text containing all the relevant properties of a
032: * JSmoothModelBean object. The properties are output in the form
033: * "key=value".
034: * <p>
035: *
036: * Note that all the paths are converted to be made relative to
037: * the basedir parameter provided. All the paths converted are
038: * expected to be relative to the targetted executable binary
039: * (before the conversion is applied, that is).
040: *
041: */
042: static public String makeProperties(File basedir,
043: JSmoothModelBean obj) {
044: StringBuffer out = new StringBuffer();
045:
046: addPair("arguments", obj.getArguments(), out);
047: addPair("mainclassname", obj.getMainClassName(), out);
048: addPair("jvmsearch", makePathConc(obj.getJVMSearchPath()), out);
049: addPair("minversion", obj.getMinimumVersion(), out);
050: addPair("maxversion", obj.getMaximumVersion(), out);
051:
052: addPair("currentdir", obj.getCurrentDirectory(), out);
053:
054: if (obj.getEmbeddedJar()
055: && (obj.getJarLocation().trim().length() > 0)) {
056: addPair("embedjar", "true", out);
057: } else {
058: addPair("embedjar", "false", out);
059: }
060:
061: if (obj.getMaximumMemoryHeap() > 1) {
062: addPair("maxheap", Integer.toString(obj
063: .getMaximumMemoryHeap()), out);
064: }
065:
066: if (obj.getInitialMemoryHeap() > 1) {
067: addPair("initialheap", Integer.toString(obj
068: .getInitialMemoryHeap()), out);
069: }
070:
071: // BundledVM & classpaths are changed to be accessible
072: // from the current directory
073: File curdir = new File(obj.getExecutableName()).getParentFile();
074:
075: if (curdir == null)
076: curdir = basedir.getAbsoluteFile();
077:
078: if (curdir.isAbsolute() == false) {
079: curdir = new File(basedir, curdir.toString());
080: }
081:
082: // System.out.println("... curdir1 : " + curdir.toString());
083:
084: if (obj.getCurrentDirectory() != null) {
085: File newcurdir = new File(obj.getCurrentDirectory());
086: // System.out.println("... curdir1.5 : " + obj.getCurrentDirectory());
087:
088: if (!"${EXECUTABLEPATH}".equalsIgnoreCase(obj
089: .getCurrentDirectory())) {
090: if (newcurdir.isAbsolute() == false) {
091: curdir = new File(curdir, newcurdir.toString());
092: } else
093: curdir = newcurdir;
094: }
095: }
096: // System.out.println("... curdir2 : " + curdir.toString());
097:
098: if (obj.getBundledJVMPath() != null)
099: addPair("bundledvm", getRenormalizedPathIfNeeded(obj
100: .getBundledJVMPath(), basedir, curdir), out);
101:
102: if (obj.getClassPath() != null) {
103: String[] relcp = new String[obj.getClassPath().length];
104: for (int i = 0; i < relcp.length; i++) {
105: relcp[i] = getRenormalizedPathIfNeeded(obj
106: .getClassPath()[i], basedir, curdir);
107: }
108: addPair("classpath", makePathConc(relcp), out);
109: }
110:
111: //
112: // Adds all the skeleton-specific properties
113: //
114: if (obj.getSkeletonProperties() != null) {
115: for (int i = 0; i < obj.getSkeletonProperties().length; i++) {
116: JSmoothModelBean.Property prop = obj
117: .getSkeletonProperties()[i];
118: if (prop.getKey() != null) {
119: String val = prop.getValue();
120: if (val == null)
121: val = "";
122: addPair("skel_" + prop.getKey(), val, out);
123: }
124: }
125: }
126:
127: //
128: // Adds all the java properties. Those properties are
129: // typically passed as -Dname=value arguments for the sun's
130: // JVM.
131: //
132:
133: JavaPropertyPair[] javapairs = obj.getJavaProperties();
134: if (javapairs != null) {
135: addPair("javapropertiescount",
136: new Integer(javapairs.length).toString(), out);
137: for (int i = 0; i < javapairs.length; i++) {
138: addPair("javaproperty_name_" + i, javapairs[i]
139: .getName(), out);
140: addPair("javaproperty_value_" + i, javapairs[i]
141: .getValue(), out);
142: }
143: }
144:
145: return out.toString();
146: }
147:
148: /**
149: * Converts a path relative to previousbasedir into a path
150: * relative to newbasedir.
151: */
152: static public String getRenormalizedPathIfNeeded(String value,
153: File previousbasedir, File newbasedir) {
154: // File f = new File(value);
155: // if (f.isAbsolute())
156: // return value;
157:
158: if (newbasedir == null)
159: return value;
160:
161: if (value == null)
162: return "";
163:
164: File abs = new File(previousbasedir, value).getAbsoluteFile();
165: File n = JSmoothModelPersistency.makePathRelativeIfPossible(
166: newbasedir, abs);
167:
168: return n.toString();
169: }
170:
171: static public String escapeString(String str) {
172: if (str == null)
173: return "";
174:
175: StringBuffer out = new StringBuffer();
176: for (int i = 0; i < str.length(); i++) {
177: char c = str.charAt(i);
178: switch (c) {
179: case '\n':
180: out.append("\\n");
181: break;
182: case '\t':
183: out.append("\\t");
184: break;
185: case '\r':
186: out.append("\\r");
187: break;
188: case '\\':
189: out.append("\\\\");
190: break;
191: default:
192: out.append(c);
193: }
194: }
195: return out.toString();
196: }
197:
198: static private void addPair(String name, String value,
199: StringBuffer out) {
200: out.append(escapeString(name));
201: out.append("=");
202: out.append(escapeString(value));
203: out.append("\n");
204: }
205:
206: static public String makePathConc(String[] elements) {
207: StringBuffer buf = new StringBuffer();
208: if (elements != null)
209: for (int i = 0; i < elements.length; i++) {
210: buf.append(elements[i]);
211: if ((i + 1) < elements.length)
212: buf.append(";");
213: }
214: return buf.toString();
215: }
216:
217: }
|