KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > serverinfo > BasicServerInfo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.system.serverinfo;
19
20 import java.io.File JavaDoc;
21 import java.net.URI JavaDoc;
22
23 import org.apache.geronimo.gbean.GBeanInfo;
24 import org.apache.geronimo.gbean.GBeanInfoBuilder;
25
26 /**
27  * Contains information about the server and functions for resolving
28  * pathnames.
29  *
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class BasicServerInfo implements ServerInfo {
33     public static final String JavaDoc SERVER_NAME_SYS_PROP = "org.apache.geronimo.server.name";
34     public static final String JavaDoc SERVER_DIR_SYS_PROP = "org.apache.geronimo.server.dir";
35     public static final String JavaDoc HOME_DIR_SYS_PROP = "org.apache.geronimo.home.dir";
36     
37     private final String JavaDoc baseDirectory;
38     private final File JavaDoc base;
39     private final File JavaDoc baseServer;
40     private final URI JavaDoc baseURI;
41     private final URI JavaDoc 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 JavaDoc defaultBaseDirectory) throws Exception JavaDoc {
51         // Before we try the persistent value, we always check the
52
// system properties first. This lets an admin override this
53
// on the command line.
54
this.baseDirectory = System.getProperty(HOME_DIR_SYS_PROP, defaultBaseDirectory);
55
56         // force load of server constants
57
ServerConstants.getVersion();
58
59         if (baseDirectory == null || baseDirectory.length() == 0) {
60             base = DirectoryUtils.getGeronimoInstallDirectory();
61             if (base == null) {
62                 throw new IllegalArgumentException JavaDoc("Could not determine geronimo installation directory");
63             }
64         } else {
65             base = new File JavaDoc(baseDirectory);
66         }
67
68         if (!base.isDirectory()) {
69             throw new IllegalArgumentException JavaDoc("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     /**
81      * Resolves an abstract pathname to an absolute one.
82      *
83      * @param filename a pathname that can either be
84      * fully-qualified (i.e. starts with a "/") or
85      * relative (i.e. starts with any character but "/"). If it's
86      * fully-qualified it will be resolved to an absolute pathname
87      * using system-dependent rules (@link java.io.File). If it's relative
88      * it will be resolved relative to the base directory.
89      * @return an absolute pathname
90      * @see java.io.File#File(String pathname)
91      * @see java.io.File#getAbsolutePath()
92      */

93     public String JavaDoc resolvePath(final String JavaDoc filename) {
94         return resolve(filename).getAbsolutePath();
95     }
96
97     public String JavaDoc resolveServerPath(String JavaDoc filename) {
98         return resolveServer(filename).getAbsolutePath();
99     }
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 JavaDoc resolve(final String JavaDoc filename) {
110         return resolveWithBase(base, filename);
111     }
112
113     public File JavaDoc resolveServer(String JavaDoc filename) {
114         return resolveWithBase(baseServer, filename);
115     }
116
117     public URI JavaDoc resolve(final URI JavaDoc uri) {
118         return baseURI.resolve(uri);
119     }
120
121     public URI JavaDoc resolveServer(URI JavaDoc uri) {
122         return baseServerURI.resolve(uri);
123     }
124     
125     public String JavaDoc getBaseDirectory() {
126         return baseDirectory;
127     }
128
129     public String JavaDoc getCurrentBaseDirectory() {
130         return base.getAbsolutePath();
131     }
132
133     public String JavaDoc getVersion() {
134         return ServerConstants.getVersion();
135     }
136
137     public String JavaDoc getBuildDate() {
138         return ServerConstants.getBuildDate();
139     }
140
141     public String JavaDoc getBuildTime() {
142         return ServerConstants.getBuildTime();
143     }
144
145     public String JavaDoc getCopyright() {
146         return ServerConstants.getCopyright();
147     }
148
149     private File JavaDoc resolveWithBase(File JavaDoc baseDir, String JavaDoc filename) {
150         File JavaDoc file = new File JavaDoc(filename);
151         if (file.isAbsolute()) {
152             return file;
153         }
154         return new File JavaDoc(baseDir, filename);
155     }
156
157     private File JavaDoc deriveBaseServer() {
158         File JavaDoc baseServerDir;
159         
160         // first check if the base server directory has been provided via
161
// system property override.
162
String JavaDoc baseServerDirPath = System.getProperty(SERVER_DIR_SYS_PROP);
163         if (null == baseServerDirPath) {
164             // then check if a server name has been provided
165
String JavaDoc 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 JavaDoc(base, serverName);
171             }
172         } else {
173             baseServerDir = new File JavaDoc(baseServerDirPath);
174             if (false == baseServerDir.isAbsolute()) {
175                 baseServerDir = new File JavaDoc(base, baseServerDirPath);
176             }
177         }
178
179         if (!baseServerDir.isDirectory()) {
180             throw new IllegalArgumentException JavaDoc("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 JavaDoc.class, true);
192
193         infoFactory.addInterface(ServerInfo.class);
194
195         infoFactory.setConstructor(new String JavaDoc[]{"baseDirectory"});
196
197         GBEAN_INFO = infoFactory.getBeanInfo();
198     }
199
200     public static GBeanInfo getGBeanInfo() {
201         return GBEAN_INFO;
202     }
203 }
204
Popular Tags