1 17 18 package org.apache.geronimo.system.serverinfo; 19 20 import java.io.File ; 21 import java.net.URI ; 22 23 import org.apache.geronimo.gbean.GBeanInfo; 24 import org.apache.geronimo.gbean.GBeanInfoBuilder; 25 26 32 public class BasicServerInfo implements ServerInfo { 33 public static final String SERVER_NAME_SYS_PROP = "org.apache.geronimo.server.name"; 34 public static final String SERVER_DIR_SYS_PROP = "org.apache.geronimo.server.dir"; 35 public static final String HOME_DIR_SYS_PROP = "org.apache.geronimo.home.dir"; 36 37 private final String baseDirectory; 38 private final File base; 39 private final File baseServer; 40 private final URI baseURI; 41 private final URI baseServerURI; 42 43 public BasicServerInfo() { 44 baseDirectory = null; 45 base = null; 46 baseServer = null; 47 baseURI = null; 48 baseServerURI = null; 49 } 50 public BasicServerInfo(String defaultBaseDirectory) throws Exception { 51 this.baseDirectory = System.getProperty(HOME_DIR_SYS_PROP, defaultBaseDirectory); 55 56 ServerConstants.getVersion(); 58 59 if (baseDirectory == null || baseDirectory.length() == 0) { 60 base = DirectoryUtils.getGeronimoInstallDirectory(); 61 if (base == null) { 62 throw new IllegalArgumentException ("Could not determine geronimo installation directory"); 63 } 64 } else { 65 base = new File (baseDirectory); 66 } 67 68 if (!base.isDirectory()) { 69 throw new IllegalArgumentException ("Base directory is not a directory: " + baseDirectory); 70 } 71 72 baseURI = base.toURI(); 73 System.setProperty(HOME_DIR_SYS_PROP, base.getAbsolutePath()); 74 75 baseServer = deriveBaseServer(); 76 baseServerURI = baseServer.toURI(); 77 System.setProperty(SERVER_DIR_SYS_PROP, baseServer.getAbsolutePath()); 78 } 79 80 93 public String resolvePath(final String filename) { 94 return resolve(filename).getAbsolutePath(); 95 } 96 97 public String resolveServerPath(String filename) { 98 return resolveServer(filename).getAbsolutePath(); 99 } 100 101 109 public File resolve(final String filename) { 110 return resolveWithBase(base, filename); 111 } 112 113 public File resolveServer(String filename) { 114 return resolveWithBase(baseServer, filename); 115 } 116 117 public URI resolve(final URI uri) { 118 return baseURI.resolve(uri); 119 } 120 121 public URI resolveServer(URI uri) { 122 return baseServerURI.resolve(uri); 123 } 124 125 public String getBaseDirectory() { 126 return baseDirectory; 127 } 128 129 public String getCurrentBaseDirectory() { 130 return base.getAbsolutePath(); 131 } 132 133 public String getVersion() { 134 return ServerConstants.getVersion(); 135 } 136 137 public String getBuildDate() { 138 return ServerConstants.getBuildDate(); 139 } 140 141 public String getBuildTime() { 142 return ServerConstants.getBuildTime(); 143 } 144 145 public String getCopyright() { 146 return ServerConstants.getCopyright(); 147 } 148 149 private File resolveWithBase(File baseDir, String filename) { 150 File file = new File (filename); 151 if (file.isAbsolute()) { 152 return file; 153 } 154 return new File (baseDir, filename); 155 } 156 157 private File deriveBaseServer() { 158 File baseServerDir; 159 160 String baseServerDirPath = System.getProperty(SERVER_DIR_SYS_PROP); 163 if (null == baseServerDirPath) { 164 String serverName = System.getProperty(SERVER_NAME_SYS_PROP); 166 if (null == serverName) { 167 baseServerDir = base; 169 } else { 170 baseServerDir = new File (base, serverName); 171 } 172 } else { 173 baseServerDir = new File (baseServerDirPath); 174 if (false == baseServerDir.isAbsolute()) { 175 baseServerDir = new File (base, baseServerDirPath); 176 } 177 } 178 179 if (!baseServerDir.isDirectory()) { 180 throw new IllegalArgumentException ("Server directory is not a directory: " + baseServerDir); 181 } 182 183 return baseServerDir; 184 } 185 186 public static final GBeanInfo GBEAN_INFO; 187 188 static { 189 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(BasicServerInfo.class); 190 191 infoFactory.addAttribute("baseDirectory", String .class, true); 192 193 infoFactory.addInterface(ServerInfo.class); 194 195 infoFactory.setConstructor(new String []{"baseDirectory"}); 196 197 GBEAN_INFO = infoFactory.getBeanInfo(); 198 } 199 200 public static GBeanInfo getGBeanInfo() { 201 return GBEAN_INFO; 202 } 203 } 204 | Popular Tags |