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: */package org.apache.geronimo.system.serverinfo;
017:
018: import java.io.File;
019: import java.net.URI;
020:
021: import org.apache.geronimo.gbean.GBeanInfo;
022: import org.apache.geronimo.gbean.GBeanInfoBuilder;
023:
024: /**
025: * Contains information about the server and functions for resolving
026: * pathnames.
027: *
028: * @version $Rev: 601152 $ $Date: 2007-12-04 15:49:03 -0800 (Tue, 04 Dec 2007) $
029: */
030: public class BasicServerInfo implements ServerInfo {
031: public static final String SERVER_NAME_SYS_PROP = "org.apache.geronimo.server.name";
032: public static final String SERVER_DIR_SYS_PROP = "org.apache.geronimo.server.dir";
033: public static final String HOME_DIR_SYS_PROP = "org.apache.geronimo.home.dir";
034:
035: private final String baseDirectory;
036: private final File base;
037: private final File baseServer;
038: private final URI baseURI;
039: private final URI baseServerURI;
040:
041: public BasicServerInfo() {
042: baseDirectory = null;
043: base = null;
044: baseServer = null;
045: baseURI = null;
046: baseServerURI = null;
047: }
048:
049: public BasicServerInfo(String defaultBaseDirectory)
050: throws Exception {
051: this (defaultBaseDirectory, true);
052: }
053:
054: public BasicServerInfo(String defaultBaseDirectory,
055: boolean useSystemProperties) throws Exception {
056: // Before we try the persistent value, we always check the
057: // system properties first. This lets an admin override this
058: // on the command line.
059: this .baseDirectory = useSystemProperties ? System.getProperty(
060: HOME_DIR_SYS_PROP, defaultBaseDirectory)
061: : defaultBaseDirectory;
062:
063: // force load of server constants
064: ServerConstants.getVersion();
065:
066: if (baseDirectory == null || baseDirectory.length() == 0) {
067: base = DirectoryUtils.getGeronimoInstallDirectory();
068: if (base == null) {
069: throw new IllegalArgumentException(
070: "Could not determine geronimo installation directory");
071: }
072: } else {
073: base = new File(baseDirectory);
074: }
075:
076: if (!base.isDirectory()) {
077: throw new IllegalArgumentException(
078: "Base directory is not a directory: "
079: + baseDirectory);
080: }
081:
082: baseURI = base.toURI();
083: baseServer = deriveBaseServer(useSystemProperties);
084: baseServerURI = baseServer.toURI();
085: if (useSystemProperties) {
086: System.setProperty(HOME_DIR_SYS_PROP, base
087: .getAbsolutePath());
088: System.setProperty(SERVER_DIR_SYS_PROP, baseServer
089: .getAbsolutePath());
090: }
091: String tmpDir = resolveServerPath(System
092: .getProperty("java.io.tmpdir"));
093: System.setProperty("java.io.tmpdir", tmpDir);
094: }
095:
096: /**
097: * Resolves an abstract pathname to an absolute one.
098: *
099: * @param filename a pathname that can either be
100: * fully-qualified (i.e. starts with a "/") or
101: * relative (i.e. starts with any character but "/"). If it's
102: * fully-qualified it will be resolved to an absolute pathname
103: * using system-dependent rules (@link java.io.File). If it's relative
104: * it will be resolved relative to the base directory.
105: * @return an absolute pathname
106: * @see java.io.File#File(String pathname)
107: * @see java.io.File#getAbsolutePath()
108: */
109: public String resolvePath(final String filename) {
110: return resolve(filename).getAbsolutePath();
111: }
112:
113: public String resolveServerPath(String filename) {
114: return resolveServer(filename).getAbsolutePath();
115: }
116:
117: /**
118: * Resolves an abstract pathname to a File.
119: *
120: * @param filename a <code>String</code> containing a pathname,
121: * which will be resolved by {@link #resolvePath(String
122: * filename)}.
123: * @return a <code>File</code> value
124: */
125: public File resolve(final String filename) {
126: return resolveWithBase(base, filename);
127: }
128:
129: public File resolveServer(String filename) {
130: return resolveWithBase(baseServer, filename);
131: }
132:
133: public URI resolve(final URI uri) {
134: return baseURI.resolve(uri);
135: }
136:
137: public URI resolveServer(URI uri) {
138: return baseServerURI.resolve(uri);
139: }
140:
141: public String getBaseDirectory() {
142: return baseDirectory;
143: }
144:
145: public String getCurrentBaseDirectory() {
146: return base.getAbsolutePath();
147: }
148:
149: public String getVersion() {
150: return ServerConstants.getVersion();
151: }
152:
153: public String getBuildDate() {
154: return ServerConstants.getBuildDate();
155: }
156:
157: public String getBuildTime() {
158: return ServerConstants.getBuildTime();
159: }
160:
161: public String getCopyright() {
162: return ServerConstants.getCopyright();
163: }
164:
165: private File resolveWithBase(File baseDir, String filename) {
166: File file = new File(filename);
167: if (file.isAbsolute()) {
168: return file;
169: }
170: return new File(baseDir, filename);
171: }
172:
173: private File deriveBaseServer(boolean useSystemProperties) {
174: File baseServerDir;
175:
176: // first check if the base server directory has been provided via
177: // system property override.
178: String baseServerDirPath = System
179: .getProperty(SERVER_DIR_SYS_PROP);
180: if (!useSystemProperties || null == baseServerDirPath) {
181: // then check if a server name has been provided
182: String serverName = System
183: .getProperty(SERVER_NAME_SYS_PROP);
184: if (!useSystemProperties || null == serverName) {
185: // default base server directory.
186: baseServerDir = base;
187: } else {
188: baseServerDir = new File(base, serverName);
189: }
190: } else {
191: baseServerDir = new File(baseServerDirPath);
192: if (!baseServerDir.isAbsolute()) {
193: baseServerDir = new File(base, baseServerDirPath);
194: }
195: }
196:
197: if (!baseServerDir.isDirectory()) {
198: throw new IllegalArgumentException(
199: "Server directory is not a directory: "
200: + baseServerDir);
201: }
202:
203: return baseServerDir;
204: }
205:
206: public static final GBeanInfo GBEAN_INFO;
207:
208: static {
209: GBeanInfoBuilder infoFactory = GBeanInfoBuilder
210: .createStatic(BasicServerInfo.class);
211:
212: infoFactory.addAttribute("baseDirectory", String.class, true);
213:
214: infoFactory.addInterface(ServerInfo.class);
215:
216: infoFactory.setConstructor(new String[] { "baseDirectory" });
217:
218: GBEAN_INFO = infoFactory.getBeanInfo();
219: }
220:
221: public static GBeanInfo getGBeanInfo() {
222: return GBEAN_INFO;
223: }
224: }
|