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    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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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 installation directory of Apache Geronimo", ignored);
048                }
049            } else {
050                log.error("Cound not determine the installation directory of Apache Geronimo, 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    }