001    /**
002     *
003     * Copyright 2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    package org.apache.geronimo.system.serverinfo;
018    
019    import java.io.File;
020    import java.net.JarURLConnection;
021    import java.net.URI;
022    import java.net.URL;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    /**
028     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
029     */
030    public final class DirectoryUtils {
031        private static final Log log = LogFactory.getLog(DirectoryUtils.class);
032        private static final File geronimoInstallDirectory;
033    
034        static {
035            // guess from the location of the jar
036            URL url = DirectoryUtils.class.getClassLoader().getResource("META-INF/startup-jar");
037    
038            File directory = null;
039            if (url != null) {
040                try {
041                    JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
042                    url = jarConnection.getJarFileURL();
043    
044                    URI baseURI = new URI(url.toString()).resolve("..");
045                    directory = new File(baseURI);
046                } catch (Exception ignored) {
047                    log.error("Error while determining the geronimo installation directory", ignored);
048                }
049            } else {
050                log.error("Cound not determin the geronimo installation directory, because the startup jar could not be found in the current class loader.");
051            }
052            geronimoInstallDirectory = directory;
053        }
054    
055        public static File getGeronimoInstallDirectory() {
056            return geronimoInstallDirectory;
057        }
058    }