001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp;
024:
025: // ToolBox
026: import org.enhydra.tool.ToolBoxInfo;
027: import org.enhydra.tool.common.FilenameFilter;
028: import org.enhydra.tool.common.FileUtil;
029: import org.enhydra.tool.common.PathHandle;
030: import org.enhydra.tool.common.ToolException;
031:
032: // AddinCore
033: import org.enhydra.kelp.common.Constants;
034: import org.enhydra.kelp.common.ResUtil;
035:
036: // JDK
037: import java.io.File;
038: import java.net.MalformedURLException;
039: import java.util.ArrayList;
040: import java.util.Arrays;
041: import java.util.ResourceBundle;
042: import java.util.Properties;
043:
044: /**
045: * Class containing the version information
046: */
047: public class KelpInfo {
048: public static final String ADDIN_ROOT = "addin.root";
049: public static final String ADDIN_HELP = "using_kelp.html";
050:
051: // not to be resourced
052: static ResourceBundle res = ResourceBundle
053: .getBundle("org.enhydra.kelp.common.Res"); // nores
054: private static final String VERSION = "6"; // nores
055: private static final String TOOLBOX_CLASS = "org.enhydra.tool.ToolBoxInfo"; // nores
056:
057: //
058: public static String getKelpVersion() {
059: return KelpInfo.VERSION;
060: }
061:
062: public static void verifyIDEClassPath() throws Exception {
063: boolean valid = true;
064:
065: valid = KelpInfo.isClassPathComplete();
066: StringBuffer message = new StringBuffer();
067:
068: if (!valid) {
069: message.append(res.getString("MissingJars1"));
070: message.append(res.getString("MissingJars2"));
071: if (KelpInfo.isToolBoxInClassPath()) {
072: if (!KelpInfo.isToolBoxVersionInSynch()) {
073: message.setLength(0);
074: message.append(res.getString("OutOfSync"));
075: message.append(ResUtil.format(res
076: .getString("Kelp_version_0"), KelpInfo
077: .getKelpVersion()));
078: message.append('\n');
079: message.append(ResUtil.format(res
080: .getString("ToolBox_version_0"),
081: ToolBoxInfo.getToolBoxVersion()));
082: } else if (!ToolBoxInfo.isEnhydraInClassPath()) {
083: message.append(Constants.FILE_ENHYDRA_JAR);
084: message.append('\n');
085: } else if (!ToolBoxInfo.isXMLCInClassPath()) {
086: message.append(Constants.FILE_XMLC_JAR);
087: message.append('\n');
088: }
089: } else {
090: message.append(Constants.FILE_TOOLBOX_JAR);
091: message.append('\n');
092: }
093: throw new Exception(message.toString());
094: }
095: }
096:
097: public static String getMessageBoxTitle() {
098: StringBuffer buf = new StringBuffer();
099:
100: buf.append(Constants.KELP);
101: buf.append(Constants.TAB1);
102: buf.append(KelpInfo.VERSION);
103: return buf.toString();
104: }
105:
106: public static boolean isToolBoxInClassPath() {
107: boolean inPath = true;
108:
109: try {
110: Class testClass = Class.forName(TOOLBOX_CLASS);
111: } catch (java.lang.ClassNotFoundException e) {
112: inPath = false;
113: }
114: return inPath;
115: }
116:
117: public static boolean isToolBoxVersionInSynch() {
118: boolean inSynch = false;
119:
120: if (KelpInfo.isToolBoxInClassPath()) {
121: if (ToolBoxInfo.getToolBoxVersion().equalsIgnoreCase(
122: KelpInfo.getKelpVersion())) {
123: inSynch = true;
124: }
125: }
126: return inSynch;
127: }
128:
129: public static boolean isClassPathComplete() {
130: boolean complete = false;
131:
132: if (KelpInfo.isToolBoxInClassPath()) {
133: if (ToolBoxInfo.isXMLCInClassPath()
134: && KelpInfo.isToolBoxVersionInSynch()) {
135: complete = true;
136: }
137: }
138: return complete;
139: }
140:
141: public static String getAddinHelpURL() {
142: String[] search = new String[0];
143:
144: return KelpInfo.getAddinHelpURL(search);
145: }
146:
147: public static String getAddinHelpURL(String s) {
148: String[] search = new String[1];
149:
150: if (s == null) {
151: search = new String[0];
152: } else {
153: search[0] = s;
154: }
155: return KelpInfo.getAddinHelpURL(search);
156: }
157:
158: public static String getAddinHelpURL(String[] search) {
159: StringBuffer buf = new StringBuffer();
160: File file = null;
161:
162: buf.append(KelpInfo.getAddinRoot(search));
163: buf.append("/doc/");
164: buf.append(KelpInfo.ADDIN_HELP);
165: file = new File(buf.toString());
166: buf.setLength(0);
167: try {
168: buf.append(file.toURL().toExternalForm());
169: } catch (MalformedURLException e) {
170: e.printStackTrace(System.err);
171: }
172: return buf.toString();
173: }
174:
175: private static String getAddinRoot(String[] search) {
176: final String FILE_ADDIN_JAR = "KelpAddinJBuilder7.jar";
177: ArrayList list = null;
178: Properties props = null;
179: String root = null;
180: String[] paths = new String[0];
181: ClassLoader loader = null;
182: FilenameFilter filter = null;
183: PathHandle ph = null;
184: KelpInfo info = null;
185:
186: // find by class loader - will fail for forte
187: info = new KelpInfo();
188: loader = info.getClass().getClassLoader();
189: paths = FileUtil.findJarPaths(FILE_ADDIN_JAR, loader);
190: for (int i = 0; i < paths.length; i++) {
191: ph = PathHandle.createPathHandle(paths[i]);
192: ph = ph.getParent();
193: if (ph.getParent() == null) {
194: root = ph.getPath();
195: } else {
196: root = ph.getParent().getPath();
197: }
198: break;
199: }
200:
201: // search
202: if (root == null) {
203: ph = PathHandle.createPathHandle(ToolBoxInfo
204: .getEnhydraRoot());
205: if (ph.getParent() != null) {
206: list = new ArrayList(Arrays.asList(search));
207: list.add(ph.getParent().getPath() + "/kelp5");
208: list.trimToSize();
209: search = new String[list.size()];
210: search = (String[]) list.toArray(search);
211: list.clear();
212: }
213: filter = new FilenameFilter();
214: filter.setIncludeName(KelpInfo.ADDIN_HELP);
215: filter.setDirectoryValid(false);
216: for (int i = 0; i < search.length; i++) {
217: ph = PathHandle.createPathHandle(FileUtil.findFirst(
218: filter, search[i]));
219: if (ph.isFile()) {
220: ph = ph.getParent();
221: if (ph.getParent() != null) {
222: ph = ph.getParent();
223: }
224: root = ph.getPath();
225: break;
226: }
227: }
228: }
229: try {
230:
231: // if can't find root load prop, otherwise save
232: props = ToolBoxInfo.loadProperties();
233: if (root == null) {
234: root = props.getProperty(ADDIN_ROOT);
235: } else {
236: props.setProperty(KelpInfo.ADDIN_ROOT, root);
237: ToolBoxInfo.storeProperties(props);
238: }
239: } catch (ToolException e) {
240: e.printStackTrace(System.err);
241: root = null;
242: }
243: if (root == null) {
244: root = "../kelp5";
245: }
246: return root;
247: }
248:
249: /**
250: * Prevent instantiation.
251: */
252: private KelpInfo() {
253: }
254:
255: }
|