1 22 package org.objectweb.petals.util; 23 24 import java.io.File ; 25 import java.net.JarURLConnection ; 26 import java.net.URI ; 27 import java.net.URL ; 28 29 37 public final class SystemUtil { 38 39 42 private static File petalsInstallDirectory; 43 44 private SystemUtil() { 45 super(); 46 } 47 48 53 public static File getPetalsInstallDirectory() { 54 if (petalsInstallDirectory == null) { 55 File dir = resolveInstallDirectoryFromEnvironment(); 57 if (dir == null) { 59 dir = resolveInstallDirectoryFromFramework(); 60 } 61 petalsInstallDirectory = dir; 62 } 63 return petalsInstallDirectory; 64 } 65 66 72 protected static File resolveInstallDirectoryFromEnvironment() { 73 File directory = null; 74 String petalsHome = System.getProperty("petals.home"); 75 if (petalsHome == null || petalsHome.trim().equals("")) { 76 petalsHome = System.getenv("PETALS_HOME"); 77 } 78 if (petalsHome != null && !petalsHome.trim().equals("")) { 80 File theHome = new File (petalsHome); 81 if (theHome.exists() || theHome.isDirectory()) { 83 directory = theHome; 84 } 85 } 86 return directory; 87 } 88 89 94 protected static File resolveInstallDirectoryFromFramework() { 95 URL url = SystemUtil.class.getClassLoader().getResource( 97 "META-INF" + File.separator + "LICENSE.txt"); 98 File directory = null; 99 if (url != null) { 100 try { 101 JarURLConnection jarConnection = (JarURLConnection ) url 102 .openConnection(); 103 URL resourceJar = jarConnection.getJarFileURL(); 104 URI baseURI = new URI (resourceJar.toString()).resolve(".."); 106 directory = new File (baseURI); 107 } catch (Exception ignored) { 108 } 110 } else { 111 } 114 return directory; 115 } 116 117 } 118 | Popular Tags |