1 2 24 25 package org.enhydra.tool; 27 28 import org.enhydra.tool.common.FileUtil; 30 import org.enhydra.tool.common.PathHandle; 31 import org.enhydra.tool.common.ResUtil; 32 import org.enhydra.tool.common.ToolException; 33 34 import java.net.URLClassLoader ; 36 import java.io.IOException ; 37 import java.io.File ; 38 import java.io.FileInputStream ; 39 import java.io.FileOutputStream ; 40 import java.io.FileNotFoundException ; 41 import java.lang.reflect.Field ; 42 import java.util.Properties ; 43 import java.util.ArrayList ; 44 import java.util.Arrays ; 45 import java.util.ResourceBundle ; 46 47 public class ToolBoxInfo { 49 private static boolean rootSettable = false; 50 51 static ResourceBundle res = ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 54 public static final String FILE_KERNEL_JAR = "Kernel.jar"; public static final String FILE_ENHYDRA_JAR = "eaf.jar"; public static final String WML_FACTORY = 58 "org.enhydra.wireless.wml.WMLDomFactory"; public static final String CHTML_FACTORY = 60 "org.enhydra.wireless.chtml.CHTMLDomFactory"; public static final String XHTML_FACTORY = 62 "org.enhydra.xml.xhtml.XHTMLDomFactory"; public static final String XMLC_VERSION_CLASS = 64 "org.enhydra.xml.xmlc.XMLCVersion"; public static final String ENHYDRA_ROOT = "eas.root"; 67 private static final String DIR_LIB = "lib"; private static final String VERSION = "6.5"; private static final String PROPERTY_FILENAME = 70 "toolbox.properties"; private static final String DEFAULT_ENHYDRA_ROOT = 72 "/usr/local/enhydra-6.5"; 73 74 private static final String SERVER_RUN_CLASS = 75 "org.objectweb.jonas.server.Bootstrap"; 77 78 private ToolBoxInfo() {} 79 80 public static String getCopyright() { 81 StringBuffer buf = new StringBuffer (); 82 83 buf.append('\n'); 84 buf.append("Part of The Enhydra Application Server Suite\n"); 85 return buf.toString(); 86 } 87 88 public static Properties loadProperties() throws ToolException { 89 String filename = getPropertyFilename(); 90 File file = new File (filename); 91 Properties properties = new Properties (); 92 93 if (file.isFile() && file.canRead()) { 94 try { 95 FileInputStream in = new FileInputStream (file); 96 97 properties.load(in); 98 } catch (FileNotFoundException e) { 99 throw new ToolException(e, 100 ResUtil.format(res.getString("Unable_to_read"), file)); 101 } catch (IOException e) { 102 throw new ToolException(e, 103 ResUtil.format(res.getString("Unable_to_read"), file)); 104 } 105 } 106 return properties; 107 } 108 109 125 public static File storeProperties(Properties prop) throws ToolException { 126 File file = null; 127 FileOutputStream out = null; 128 129 file = new File (ToolBoxInfo.getPropertyFilename()); 130 if (file.getParentFile() != null) { 131 file.getParentFile().mkdirs(); 132 } 133 try { 134 out = new FileOutputStream (file); 135 prop.store(out, res.getString("Kelp_ToolBox1")); 136 } catch (IOException e) { 137 throw new ToolException(e, 138 ResUtil.format(res.getString("Unable_to_store"), file)); 139 } 140 return file; 141 } 142 143 public static String getJavaPath() { 144 final String SYS_JAVA_HOME = "java.home"; String path = null; 146 File file = null; 147 148 path = System.getProperty(SYS_JAVA_HOME, new String ()); 149 file = new File (path); 150 path = file.getParent().replace('\\', '/'); 151 return path; 152 } 153 154 public static String getEnhydraKeyJar() { 155 String jar = ToolBoxInfo.FILE_ENHYDRA_JAR; 156 return jar; 157 } 158 159 public static boolean isClassAvailable(String className) { 160 boolean available = false; 161 162 try { 163 Class.forName(className); 164 available = true; 165 } catch (ClassNotFoundException e) { 166 available = false; 167 } 168 return available; 169 } 170 171 public static void main(String args[]) { 172 System.out.println(res.getString("ToolBox_Info")); 173 System.out.println(ToolBoxInfo.getCopyright()); 174 System.out.println(ResUtil.format(res.getString("ToolBox_version_0_"), 175 ToolBoxInfo.getToolBoxVersion())); 176 System.out.println(ResUtil.format("Application Server Root: {0}", 177 ToolBoxInfo.getEnhydraRoot())); 178 System.out.println(ResUtil.format(res.getString("Enhydra_in_classpath"), 179 ToolBoxInfo.isEnhydraInClassPath())); 180 System.out.println(ResUtil.format(res.getString("Enhydra_main_class_0_"), 181 ToolBoxInfo.getEnhydraMainClass())); 182 System.out.println(ResUtil.format(res.getString("XMLC_version_0_"), 183 ToolBoxInfo.getXMLCVersion())); 184 System.out.println(ResUtil.format(res.getString("XMLC_in_classpath_0_"), 185 ToolBoxInfo.isXMLCInClassPath())); 186 } 187 188 public static boolean isXMLCVersion(int v) { 189 boolean is = false; 190 String version = ToolBoxInfo.getXMLCVersion(); 191 192 if (version.startsWith(new String () + v)) { 193 is = true; 194 } 195 return is; 196 } 197 198 public static String getXMLCVersion() { 199 final String FIELD_VERSION = "VERSION"; 201 String version = res.getString("_unknown_"); 203 Class verClass = null; 204 Field verField = null; 205 206 try { 207 verClass = Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS); 208 verField = verClass.getDeclaredField(FIELD_VERSION); 209 version = verField.get(null).toString(); 210 } catch (Exception e) { 211 212 } 214 return version; 215 } 216 217 220 public static String getEnhydraMainClass() { 221 String runClass = ToolBoxInfo.SERVER_RUN_CLASS; 222 return runClass; 223 } 224 225 public static boolean isXMLCInClassPath() { 226 boolean inPath = true; 227 228 try { 229 Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS); 230 } catch (java.lang.ClassNotFoundException e) { 231 inPath = false; 232 } 233 return inPath; 234 } 235 236 public static boolean isEnhydraInClassPath() { 237 boolean inPath = true; 238 String checkClass = ToolBoxInfo.SERVER_RUN_CLASS; 239 try { 240 Class testClass = Class.forName(checkClass); 241 } catch (java.lang.ClassNotFoundException e) { 242 inPath = false; 243 } 244 return inPath; 245 } 246 247 public static String getToolBoxVersion() { 248 return VERSION; 249 } 250 251 257 public static String getPropertyFilename() { 258 final String SYS_USER_HOME = "user.home"; final String DIR_ENHYDRA = ".enhydra"; 261 StringBuffer buf = new StringBuffer (); 263 264 buf.append(System.getProperties().getProperty(SYS_USER_HOME)); 265 buf.append(File.separator); 266 buf.append(DIR_ENHYDRA); 267 buf.append(File.separator); 268 buf.append(PROPERTY_FILENAME); 269 return buf.toString(); 270 } 271 272 public static boolean isRootSettable() { 273 ToolBoxInfo.getEnhydraRoot(); 274 return ToolBoxInfo.rootSettable; 275 } 276 277 public static String getEnhydraRoot() { 278 String path = null; 279 280 ToolBoxInfo.rootSettable = false; 281 path = getEnhydraPathByClassLoader(); 282 if (path == null) { 283 path = getEnhydraPathByWorkingDirectory(); 284 } 285 if (path == null) { 286 ToolBoxInfo.rootSettable = true; 287 path = getEnhydraPathByPropertyFile(); 288 } 289 if (path == null) { 290 path = ToolBoxInfo.DEFAULT_ENHYDRA_ROOT; 291 } 292 return PathHandle.createPathString(path); 293 } 294 295 public static String [] getSupportedDocTypes() { 296 final String TYPE_CHTML = "chtml"; final String TYPE_HTML = "html"; final String TYPE_WML = "wml"; final String TYPE_XHTML = "xhtml"; 301 String [] types = new String [0]; 303 ArrayList list = new ArrayList (Arrays.asList(types)); 304 305 list.add(TYPE_HTML); 306 if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.WML_FACTORY)) { 307 list.add(TYPE_WML); 308 } 309 if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.CHTML_FACTORY)) { 310 list.add(TYPE_CHTML); 311 } 312 if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.XHTML_FACTORY)) { 313 list.add(TYPE_XHTML); 314 } 315 list.trimToSize(); 316 types = new String [list.size()]; 317 types = (String []) list.toArray(types); 318 return types; 319 } 320 321 private static String getEnhydraPathByWorkingDirectory() { 322 File file = null; 323 String path = null; 324 325 file = new File ((new String ()) + '.'); file = new File (file.getAbsolutePath()); 327 file = file.getParentFile(); 328 if ((file.getParentFile() != null) 329 && (file.getParentFile().getParentFile() != null)) { 330 path = file.getParentFile().getParent(); 331 } 332 if (!ToolBoxInfo.isEnhydraRoot(path)) { 333 path = null; 334 } 335 return path; 336 } 337 338 public static boolean isEnhydraRoot(String path) { 339 boolean validRoot = false; 340 File [] jars = new File [0]; 341 StringBuffer buf = new StringBuffer (); 342 343 if (path != null) { 344 jars = new File [1]; 345 346 buf.append(path); 348 buf.append(File.separator); 349 buf.append(DIR_LIB); 350 buf.append(File.separator); 351 buf.append(ToolBoxInfo.FILE_ENHYDRA_JAR); 352 jars[0] = new File (buf.toString()); 353 } 354 355 for (int i = 0; i < jars.length; i++) { 357 if (jars[i].isFile()) { 358 validRoot = true; 359 break; 360 } 361 } 362 return validRoot; 363 } 364 365 private static String getEnhydraPathByClassLoader() { 366 final String FILE_TOOLBOX_JAR = "toolbox.jar"; 368 boolean found = false; 370 String [] paths = new String [0]; 371 String path = null; 372 File file = null; 373 ToolBoxInfo info = new ToolBoxInfo(); 374 ClassLoader loader = info.getClass().getClassLoader(); 375 376 paths = FileUtil.findJarPaths(FILE_TOOLBOX_JAR, loader); 377 for (int i = 0; i < paths.length; i++) { 378 file = new File (paths[i]); 379 if (file.getParentFile() != null) { 380 file = file.getParentFile(); 381 if ((file.getParentFile() != null) 382 && (file.getParentFile().getParentFile() != null)) { 383 path = file.getParentFile().getParent(); 384 found = ToolBoxInfo.isEnhydraRoot(path); 385 } 386 } 387 if (found) { 388 break; 389 } else { 390 path = null; 391 } 392 } 393 return path; 394 } 395 396 private static String getEnhydraPathByPropertyFile() { 397 String path = null; 398 Properties properties = new Properties (); 399 400 try { 401 properties = ToolBoxInfo.loadProperties(); 402 path = properties.getProperty(ToolBoxInfo.ENHYDRA_ROOT); 403 } catch (ToolException e) { 404 e.printStackTrace(); 405 path = null; 406 } 407 if (!ToolBoxInfo.isEnhydraRoot(path)) { 408 path = null; 409 } 410 return path; 411 } 412 413 private boolean isEnhydraPathValid(String home) { 414 boolean valid = false; 415 StringBuffer path = new StringBuffer (); 416 File f = null; 417 418 path.append(home); 419 path.append(File.separator); 420 path.append(ToolBoxInfo.DIR_LIB); 421 path.append(File.separator); 422 path.append(ToolBoxInfo.FILE_ENHYDRA_JAR); 423 f = new File (path.toString()); 424 valid = f.isFile(); 425 f = null; 426 return valid; 427 } 428 429 } 430 | Popular Tags |