Home » geronimo-2.2-source-release » org.apache.geronimo.system.serverinfo » [javadoc | source]

    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;
   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: 708650 $ $Date: 2008-10-28 12:48:22 -0700 (Tue, 28 Oct 2008) $
   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   
   51       public BasicServerInfo(String defaultBaseDirectory) throws Exception {
   52           this(defaultBaseDirectory, true);
   53       }
   54   
   55       public BasicServerInfo(String defaultBaseDirectory, boolean useSystemProperties) throws Exception {
   56           // Before we try the persistent value, we always check the
   57           // system properties first.  This lets an admin override this
   58           // on the command line.
   59           this.baseDirectory = useSystemProperties? System.getProperty(HOME_DIR_SYS_PROP, defaultBaseDirectory): defaultBaseDirectory;
   60   
   61           // force load of server constants
   62           ServerConstants.getVersion();
   63   
   64           if (baseDirectory == null || baseDirectory.length() == 0) {
   65               base = DirectoryUtils.getGeronimoInstallDirectory();
   66               if (base == null) {
   67                   throw new IllegalArgumentException("Could not determine geronimo installation directory");
   68               }
   69           } else {
   70               base = new File(baseDirectory);
   71           }
   72   
   73           if (!base.isDirectory()) {
   74               throw new IllegalArgumentException("Base directory is not a directory: " + baseDirectory);
   75           }
   76   
   77           baseURI = base.toURI();
   78           baseServer = deriveBaseServer(useSystemProperties);
   79           baseServerURI = baseServer.toURI();
   80           if (useSystemProperties) {
   81               System.setProperty(HOME_DIR_SYS_PROP, base.getAbsolutePath());
   82               System.setProperty(SERVER_DIR_SYS_PROP, baseServer.getAbsolutePath());
   83           }
   84           String tmpDir = resolveServerPath(System.getProperty("java.io.tmpdir"));       
   85           System.setProperty("java.io.tmpdir", tmpDir);
   86       }
   87   
   88       public BasicServerInfo(String baseDirectory, String serverName) {
   89           this.baseDirectory = baseDirectory;
   90           this.base = new File(baseDirectory);
   91           this.baseURI = base.toURI();
   92           this.baseServerURI = baseURI.resolve(serverName);
   93           this.baseServer = new File(baseServerURI);
   94       }
   95   
   96       /**
   97        * Resolves an abstract pathname to an absolute one.
   98        *
   99        * @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.getProperty(SERVER_DIR_SYS_PROP);
  179           if (!useSystemProperties || null == baseServerDirPath) {
  180               // then check if a server name has been provided
  181               String serverName = System.getProperty(SERVER_NAME_SYS_PROP);
  182               if (!useSystemProperties || null == serverName) {
  183                   // default base server directory.
  184                   baseServerDir = base;
  185               } else {
  186                   baseServerDir = new File(base, serverName);
  187               }
  188           } else {
  189               baseServerDir = new File(baseServerDirPath);
  190               if (!baseServerDir.isAbsolute()) {
  191                   baseServerDir = new File(base, baseServerDirPath);
  192               }
  193           }
  194   
  195           if (!baseServerDir.isDirectory()) {
  196               throw new IllegalArgumentException("Server directory is not a directory: " + baseServerDir);
  197           }
  198           
  199           return baseServerDir;
  200       }
  201       
  202       public static final GBeanInfo GBEAN_INFO;
  203   
  204       static {
  205           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(BasicServerInfo.class);
  206   
  207           infoFactory.addAttribute("baseDirectory", String.class, true);
  208   
  209           infoFactory.addInterface(ServerInfo.class);
  210   
  211           infoFactory.setConstructor(new String[]{"baseDirectory"});
  212   
  213           GBEAN_INFO = infoFactory.getBeanInfo();
  214       }
  215   
  216       public static GBeanInfo getGBeanInfo() {
  217           return GBEAN_INFO;
  218       }
  219   }

Home » geronimo-2.2-source-release » org.apache.geronimo.system.serverinfo » [javadoc | source]