001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.system.serverinfo; 019 020 import java.io.File; 021 import java.net.URI; 022 023 import org.apache.geronimo.gbean.GBeanInfo; 024 import org.apache.geronimo.gbean.GBeanInfoBuilder; 025 026 /** 027 * Contains information about the server and functions for resolving 028 * pathnames. 029 * 030 * @version $Rev: 406493 $ $Date: 2006-05-14 18:14:11 -0700 (Sun, 14 May 2006) $ 031 */ 032 public class BasicServerInfo implements ServerInfo { 033 public static final String SERVER_NAME_SYS_PROP = "org.apache.geronimo.server.name"; 034 public static final String SERVER_DIR_SYS_PROP = "org.apache.geronimo.server.dir"; 035 public static final String HOME_DIR_SYS_PROP = "org.apache.geronimo.home.dir"; 036 037 private final String baseDirectory; 038 private final File base; 039 private final File baseServer; 040 private final URI baseURI; 041 private final URI baseServerURI; 042 043 public BasicServerInfo() { 044 baseDirectory = null; 045 base = null; 046 baseServer = null; 047 baseURI = null; 048 baseServerURI = null; 049 } 050 public BasicServerInfo(String defaultBaseDirectory) throws Exception { 051 // Before we try the persistent value, we always check the 052 // system properties first. This lets an admin override this 053 // on the command line. 054 this.baseDirectory = System.getProperty(HOME_DIR_SYS_PROP, defaultBaseDirectory); 055 056 // force load of server constants 057 ServerConstants.getVersion(); 058 059 if (baseDirectory == null || baseDirectory.length() == 0) { 060 base = DirectoryUtils.getGeronimoInstallDirectory(); 061 if (base == null) { 062 throw new IllegalArgumentException("Could not determine geronimo installation directory"); 063 } 064 } else { 065 base = new File(baseDirectory); 066 } 067 068 if (!base.isDirectory()) { 069 throw new IllegalArgumentException("Base directory is not a directory: " + baseDirectory); 070 } 071 072 baseURI = base.toURI(); 073 System.setProperty(HOME_DIR_SYS_PROP, base.getAbsolutePath()); 074 075 baseServer = deriveBaseServer(); 076 baseServerURI = baseServer.toURI(); 077 System.setProperty(SERVER_DIR_SYS_PROP, baseServer.getAbsolutePath()); 078 } 079 080 /** 081 * Resolves an abstract pathname to an absolute one. 082 * 083 * @param filename a pathname that can either be 084 * fully-qualified (i.e. starts with a "/") or 085 * relative (i.e. starts with any character but "/"). If it's 086 * fully-qualified it will be resolved to an absolute pathname 087 * using system-dependent rules (@link java.io.File). If it's relative 088 * it will be resolved relative to the base directory. 089 * @return an absolute pathname 090 * @see java.io.File#File(String pathname) 091 * @see java.io.File#getAbsolutePath() 092 */ 093 public String resolvePath(final String filename) { 094 return resolve(filename).getAbsolutePath(); 095 } 096 097 public String resolveServerPath(String filename) { 098 return resolveServer(filename).getAbsolutePath(); 099 } 100 101 /** 102 * Resolves an abstract pathname to a File. 103 * 104 * @param filename a <code>String</code> containing a pathname, 105 * which will be resolved by {@link #resolvePath(String 106 * filename)}. 107 * @return a <code>File</code> value 108 */ 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 // first check if the base server directory has been provided via 161 // system property override. 162 String baseServerDirPath = System.getProperty(SERVER_DIR_SYS_PROP); 163 if (null == baseServerDirPath) { 164 // then check if a server name has been provided 165 String serverName = System.getProperty(SERVER_NAME_SYS_PROP); 166 if (null == serverName) { 167 // default base server directory. 168 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 }