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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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            this(defaultBaseDirectory, true);
052        }
053    
054        public BasicServerInfo(String defaultBaseDirectory, boolean useSystemProperties) throws Exception {
055            // Before we try the persistent value, we always check the
056            // system properties first.  This lets an admin override this
057            // on the command line.
058            this.baseDirectory = useSystemProperties? System.getProperty(HOME_DIR_SYS_PROP, defaultBaseDirectory): defaultBaseDirectory;
059    
060            // force load of server constants
061            ServerConstants.getVersion();
062    
063            if (baseDirectory == null || baseDirectory.length() == 0) {
064                base = DirectoryUtils.getGeronimoInstallDirectory();
065                if (base == null) {
066                    throw new IllegalArgumentException("Could not determine geronimo installation directory");
067                }
068            } else {
069                base = new File(baseDirectory);
070            }
071    
072            if (!base.isDirectory()) {
073                throw new IllegalArgumentException("Base directory is not a directory: " + baseDirectory);
074            }
075    
076            baseURI = base.toURI();
077            baseServer = deriveBaseServer(useSystemProperties);
078            baseServerURI = baseServer.toURI();
079            if (useSystemProperties) {
080                System.setProperty(HOME_DIR_SYS_PROP, base.getAbsolutePath());
081                System.setProperty(SERVER_DIR_SYS_PROP, baseServer.getAbsolutePath());
082            }
083            String tmpDir = resolveServerPath(System.getProperty("java.io.tmpdir"));       
084            System.setProperty("java.io.tmpdir", tmpDir);
085        }
086    
087        /**
088         * Resolves an abstract pathname to an absolute one.
089         *
090         * @param filename a pathname that can either be
091         * fully-qualified (i.e. starts with a "/") or
092         * relative (i.e. starts with any character but "/").  If it's
093         * fully-qualified it will be resolved to an absolute pathname
094         * using system-dependent rules (@link java.io.File). If it's relative
095         * it will be resolved relative to the base directory.
096         * @return an absolute pathname
097         * @see java.io.File#File(String pathname)
098         * @see java.io.File#getAbsolutePath()
099         */
100        public String resolvePath(final String filename) {
101            return resolve(filename).getAbsolutePath();
102        }
103    
104        public String resolveServerPath(String filename) {
105            return resolveServer(filename).getAbsolutePath();
106        }
107        
108        /**
109         * Resolves an abstract pathname to a File.
110         *
111         * @param filename a <code>String</code> containing a pathname,
112         * which will be resolved by {@link #resolvePath(String
113                * filename)}.
114         * @return a <code>File</code> value
115         */
116        public File resolve(final String filename) {
117            return resolveWithBase(base, filename);
118        }
119    
120        public File resolveServer(String filename) {
121            return resolveWithBase(baseServer, filename);
122        }
123    
124        public URI resolve(final URI uri) {
125            return baseURI.resolve(uri);
126        }
127    
128        public URI resolveServer(URI uri) {
129            return baseServerURI.resolve(uri);
130        }
131        
132        public String getBaseDirectory() {
133            return baseDirectory;
134        }
135    
136        public String getCurrentBaseDirectory() {
137            return base.getAbsolutePath();
138        }
139    
140        public String getVersion() {
141            return ServerConstants.getVersion();
142        }
143    
144        public String getBuildDate() {
145            return ServerConstants.getBuildDate();
146        }
147    
148        public String getBuildTime() {
149            return ServerConstants.getBuildTime();
150        }
151    
152        public String getCopyright() {
153            return ServerConstants.getCopyright();
154        }
155    
156        private File resolveWithBase(File baseDir, String filename) {
157            File file = new File(filename);
158            if (file.isAbsolute()) {
159                return file;
160            }
161            return new File(baseDir, filename);
162        }
163    
164        private File deriveBaseServer(boolean useSystemProperties) {
165            File baseServerDir;
166            
167            // first check if the base server directory has been provided via
168            // system property override.
169            String baseServerDirPath = System.getProperty(SERVER_DIR_SYS_PROP);
170            if (!useSystemProperties || null == baseServerDirPath) {
171                // then check if a server name has been provided
172                String serverName = System.getProperty(SERVER_NAME_SYS_PROP);
173                if (!useSystemProperties || null == serverName) {
174                    // default base server directory.
175                    baseServerDir = base;
176                } else {
177                    baseServerDir = new File(base, serverName);
178                }
179            } else {
180                baseServerDir = new File(baseServerDirPath);
181                if (!baseServerDir.isAbsolute()) {
182                    baseServerDir = new File(base, baseServerDirPath);
183                }
184            }
185    
186            if (!baseServerDir.isDirectory()) {
187                throw new IllegalArgumentException("Server directory is not a directory: " + baseServerDir);
188            }
189            
190            return baseServerDir;
191        }
192        
193        public static final GBeanInfo GBEAN_INFO;
194    
195        static {
196            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(BasicServerInfo.class);
197    
198            infoFactory.addAttribute("baseDirectory", String.class, true);
199    
200            infoFactory.addInterface(ServerInfo.class);
201    
202            infoFactory.setConstructor(new String[]{"baseDirectory"});
203    
204            GBEAN_INFO = infoFactory.getBeanInfo();
205        }
206    
207        public static GBeanInfo getGBeanInfo() {
208            return GBEAN_INFO;
209        }
210    }