KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > KelpInfo


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp;
24
25 // ToolBox
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.FilenameFilter;
28 import org.enhydra.tool.common.FileUtil;
29 import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.common.ToolException;
31
32 // AddinCore
33
import org.enhydra.kelp.common.Constants;
34 import org.enhydra.kelp.common.ResUtil;
35
36 // JDK
37
import java.io.File JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.ResourceBundle JavaDoc;
42 import java.util.Properties JavaDoc;
43
44 /**
45  * Class containing the version information
46  */

47 public class KelpInfo {
48     public static final String JavaDoc ADDIN_ROOT = "addin.root";
49     public static final String JavaDoc ADDIN_HELP = "using_kelp.html";
50
51     // not to be resourced
52
static ResourceBundle JavaDoc res =
53         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
54
private static final String JavaDoc VERSION = "5.2"; // nores
55
private static final String JavaDoc TOOLBOX_CLASS =
56         "org.enhydra.tool.ToolBoxInfo"; // nores
57

58     //
59
public static String JavaDoc getKelpVersion() {
60         return KelpInfo.VERSION;
61     }
62
63     public static void verifyIDEClassPath() throws Exception JavaDoc {
64         boolean valid = true;
65
66         valid = KelpInfo.isClassPathComplete();
67         StringBuffer JavaDoc message = new StringBuffer JavaDoc();
68
69         if (!valid) {
70             message.append(res.getString("MissingJars1"));
71             message.append(res.getString("MissingJars2"));
72             if (KelpInfo.isToolBoxInClassPath()) {
73                 if (!KelpInfo.isToolBoxVersionInSynch()) {
74                     message.setLength(0);
75                     message.append(res.getString("OutOfSync"));
76                     message.append(ResUtil.format(res.getString("Kelp_version_0"),
77                                                   KelpInfo.getKelpVersion()));
78                     message.append('\n');
79                     message.append(ResUtil.format(res.getString("ToolBox_version_0"),
80                                                   ToolBoxInfo.getToolBoxVersion()));
81                 } else if (!ToolBoxInfo.isEnhydraInClassPath()) {
82                     message.append(Constants.FILE_ENHYDRA_JAR);
83                     message.append('\n');
84                 } else if (!ToolBoxInfo.isXMLCInClassPath()) {
85                     message.append(Constants.FILE_XMLC_JAR);
86                     message.append('\n');
87                 }
88             } else {
89                 message.append(Constants.FILE_TOOLBOX_JAR);
90                 message.append('\n');
91             }
92             throw new Exception JavaDoc(message.toString());
93         }
94     }
95
96     public static String JavaDoc getMessageBoxTitle() {
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98
99         buf.append(Constants.KELP);
100         buf.append(Constants.TAB1);
101         buf.append(KelpInfo.VERSION);
102         return buf.toString();
103     }
104
105     public static boolean isToolBoxInClassPath() {
106         boolean inPath = true;
107
108         try {
109             Class JavaDoc testClass = Class.forName(TOOLBOX_CLASS);
110         } catch (java.lang.ClassNotFoundException JavaDoc e) {
111             inPath = false;
112         }
113         return inPath;
114     }
115
116     public static boolean isToolBoxVersionInSynch() {
117         boolean inSynch = false;
118
119         if (KelpInfo.isToolBoxInClassPath()) {
120             if (ToolBoxInfo.getToolBoxVersion().equalsIgnoreCase(KelpInfo.getKelpVersion())) {
121                 inSynch = true;
122             }
123         }
124         return inSynch;
125     }
126
127     public static boolean isClassPathComplete() {
128         boolean complete = false;
129
130         if (KelpInfo.isToolBoxInClassPath()) {
131             if (ToolBoxInfo.isXMLCInClassPath()
132                     && KelpInfo.isToolBoxVersionInSynch()) {
133                 complete = true;
134             }
135         }
136         return complete;
137     }
138
139     public static String JavaDoc getAddinHelpURL() {
140         String JavaDoc[] search = new String JavaDoc[0];
141
142         return KelpInfo.getAddinHelpURL(search);
143     }
144
145     public static String JavaDoc getAddinHelpURL(String JavaDoc s) {
146         String JavaDoc[] search = new String JavaDoc[1];
147
148         if (s == null) {
149           search = new String JavaDoc[0];
150         } else {
151           search[0] = s;
152         }
153         return KelpInfo.getAddinHelpURL(search);
154     }
155
156     public static String JavaDoc getAddinHelpURL(String JavaDoc[] search) {
157         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
158         File JavaDoc file = null;
159
160         buf.append(KelpInfo.getAddinRoot(search));
161         buf.append("/doc/");
162         buf.append(KelpInfo.ADDIN_HELP);
163         file = new File JavaDoc(buf.toString());
164         buf.setLength(0);
165         try {
166             buf.append(file.toURL().toExternalForm());
167         } catch (MalformedURLException JavaDoc e) {
168             e.printStackTrace(System.err);
169         }
170         return buf.toString();
171     }
172
173     private static String JavaDoc getAddinRoot(String JavaDoc[] search) {
174         final String JavaDoc FILE_ADDIN_JAR = "KelpAddinJBuilder7.jar";
175         ArrayList JavaDoc list = null;
176         Properties JavaDoc props = null;
177         String JavaDoc root = null;
178         String JavaDoc[] paths = new String JavaDoc[0];
179         ClassLoader JavaDoc loader = null;
180         FilenameFilter filter = null;
181         PathHandle ph = null;
182         KelpInfo info = null;
183
184         // find by class loader - will fail for forte
185
info = new KelpInfo();
186         loader = info.getClass().getClassLoader();
187         paths = FileUtil.findJarPaths(FILE_ADDIN_JAR, loader);
188         for (int i = 0; i < paths.length; i++) {
189             ph = PathHandle.createPathHandle(paths[i]);
190             ph = ph.getParent();
191             if (ph.getParent() == null) {
192                 root = ph.getPath();
193             } else {
194                 root = ph.getParent().getPath();
195             }
196             break;
197         }
198
199         // search
200
if (root == null) {
201             ph = PathHandle.createPathHandle(ToolBoxInfo.getEnhydraRoot());
202             if (ph.getParent() != null) {
203                 list = new ArrayList JavaDoc(Arrays.asList(search));
204                 list.add(ph.getParent().getPath() + "/kelp5");
205                 list.trimToSize();
206                 search = new String JavaDoc[list.size()];
207                 search = (String JavaDoc[]) list.toArray(search);
208                 list.clear();
209             }
210             filter = new FilenameFilter();
211             filter.setIncludeName(KelpInfo.ADDIN_HELP);
212             filter.setDirectoryValid(false);
213             for (int i = 0; i < search.length; i++) {
214                 ph = PathHandle.createPathHandle(FileUtil.findFirst(filter,
215                         search[i]));
216                 if (ph.isFile()) {
217                     ph = ph.getParent();
218                     if (ph.getParent() != null) {
219                         ph = ph.getParent();
220                     }
221                     root = ph.getPath();
222                     break;
223                 }
224             }
225         }
226         try {
227
228             // if can't find root load prop, otherwise save
229
props = ToolBoxInfo.loadProperties();
230             if (root == null) {
231                 root = props.getProperty(ADDIN_ROOT);
232             } else {
233                 props.setProperty(KelpInfo.ADDIN_ROOT, root);
234                 ToolBoxInfo.storeProperties(props);
235             }
236         } catch (ToolException e) {
237             e.printStackTrace(System.err);
238             root = null;
239         }
240         if (root == null) {
241             root = "../kelp5";
242         }
243         return root;
244     }
245
246     /**
247      * Prevent instantiation.
248      */

249     private KelpInfo() {}
250
251 }
252
Popular Tags