View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
21  import java.net.URI;
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: 406493 $ $Date: 2006-05-14 18:14:11 -0700 (Sun, 14 May 2006) $
31   */
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          // 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("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      /**
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 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     /**
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 }