001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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: 533500 $ $Date: 2007-04-29 09:10:34 -0400 (Sun, 29 Apr 2007) $
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 String tmpDir = resolveServerPath(System.getProperty("java.io.tmpdir"));
079 System.setProperty("java.io.tmpdir", tmpDir);
080 }
081
082 /**
083 * Resolves an abstract pathname to an absolute one.
084 *
085 * @param filename a pathname that can either be
086 * fully-qualified (i.e. starts with a "/") or
087 * relative (i.e. starts with any character but "/"). If it's
088 * fully-qualified it will be resolved to an absolute pathname
089 * using system-dependent rules (@link java.io.File). If it's relative
090 * it will be resolved relative to the base directory.
091 * @return an absolute pathname
092 * @see java.io.File#File(String pathname)
093 * @see java.io.File#getAbsolutePath()
094 */
095 public String resolvePath(final String filename) {
096 return resolve(filename).getAbsolutePath();
097 }
098
099 public String resolveServerPath(String filename) {
100 return resolveServer(filename).getAbsolutePath();
101 }
102
103 /**
104 * Resolves an abstract pathname to a File.
105 *
106 * @param filename a <code>String</code> containing a pathname,
107 * which will be resolved by {@link #resolvePath(String
108 * filename)}.
109 * @return a <code>File</code> value
110 */
111 public File resolve(final String filename) {
112 return resolveWithBase(base, filename);
113 }
114
115 public File resolveServer(String filename) {
116 return resolveWithBase(baseServer, filename);
117 }
118
119 public URI resolve(final URI uri) {
120 return baseURI.resolve(uri);
121 }
122
123 public URI resolveServer(URI uri) {
124 return baseServerURI.resolve(uri);
125 }
126
127 public String getBaseDirectory() {
128 return baseDirectory;
129 }
130
131 public String getCurrentBaseDirectory() {
132 return base.getAbsolutePath();
133 }
134
135 public String getVersion() {
136 return ServerConstants.getVersion();
137 }
138
139 public String getBuildDate() {
140 return ServerConstants.getBuildDate();
141 }
142
143 public String getBuildTime() {
144 return ServerConstants.getBuildTime();
145 }
146
147 public String getCopyright() {
148 return ServerConstants.getCopyright();
149 }
150
151 private File resolveWithBase(File baseDir, String filename) {
152 File file = new File(filename);
153 if (file.isAbsolute()) {
154 return file;
155 }
156 return new File(baseDir, filename);
157 }
158
159 private File deriveBaseServer() {
160 File baseServerDir;
161
162 // first check if the base server directory has been provided via
163 // system property override.
164 String baseServerDirPath = System.getProperty(SERVER_DIR_SYS_PROP);
165 if (null == baseServerDirPath) {
166 // then check if a server name has been provided
167 String serverName = System.getProperty(SERVER_NAME_SYS_PROP);
168 if (null == serverName) {
169 // default base server directory.
170 baseServerDir = base;
171 } else {
172 baseServerDir = new File(base, serverName);
173 }
174 } else {
175 baseServerDir = new File(baseServerDirPath);
176 if (false == baseServerDir.isAbsolute()) {
177 baseServerDir = new File(base, baseServerDirPath);
178 }
179 }
180
181 if (!baseServerDir.isDirectory()) {
182 throw new IllegalArgumentException("Server directory is not a directory: " + baseServerDir);
183 }
184
185 return baseServerDir;
186 }
187
188 public static final GBeanInfo GBEAN_INFO;
189
190 static {
191 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(BasicServerInfo.class);
192
193 infoFactory.addAttribute("baseDirectory", String.class, true);
194
195 infoFactory.addInterface(ServerInfo.class);
196
197 infoFactory.setConstructor(new String[]{"baseDirectory"});
198
199 GBEAN_INFO = infoFactory.getBeanInfo();
200 }
201
202 public static GBeanInfo getGBeanInfo() {
203 return GBEAN_INFO;
204 }
205 }