KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > SystemUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SystemUtil.java 446 2006-05-19 14:13:50Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.File JavaDoc;
25 import java.net.JarURLConnection JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * This class is used to locate the Petals install directory
31  *
32  * @version $Rev: 446 $ $Date: 2006-05-19 16:13:50 +0200 (ven, 19 mai 2006) $
33  * @since Petals 1.0
34  * @author Rafael Marins
35  * @author ddesjardins - eBMWebsourcing
36  */

37 public final class SystemUtil {
38
39     /**
40      * Petals install directory
41      */

42     private static File JavaDoc petalsInstallDirectory;
43
44     private SystemUtil() {
45         super();
46     }
47
48     /**
49      * Find the petals install directory
50      *
51      * @return Petals install directory
52      */

53     public static File JavaDoc getPetalsInstallDirectory() {
54         if (petalsInstallDirectory == null) {
55             // first try to resolve install directory set in the environment
56
File JavaDoc dir = resolveInstallDirectoryFromEnvironment();
57             // otherwise, guess install directory using the framework
58
if (dir == null) {
59                 dir = resolveInstallDirectoryFromFramework();
60             }
61             petalsInstallDirectory = dir;
62         }
63         return petalsInstallDirectory;
64     }
65
66     /**
67      * Try to get the petals'home environment variable : petals.home system
68      * property, or PETALS_HOME env variable
69      *
70      * @return the related directory, or null
71      */

72     protected static File JavaDoc resolveInstallDirectoryFromEnvironment() {
73         File JavaDoc directory = null;
74         String JavaDoc petalsHome = System.getProperty("petals.home");
75         if (petalsHome == null || petalsHome.trim().equals("")) {
76             petalsHome = System.getenv("PETALS_HOME");
77         }
78         // not null or empty
79
if (petalsHome != null && !petalsHome.trim().equals("")) {
80             File JavaDoc theHome = new File JavaDoc(petalsHome);
81             // exists and it's a directory
82
if (theHome.exists() || theHome.isDirectory()) {
83                 directory = theHome;
84             }
85         }
86         return directory;
87     }
88
89     /**
90      * Try to find the install directory from the server.jar
91      *
92      * @return the directory or null
93      */

94     protected static File JavaDoc resolveInstallDirectoryFromFramework() {
95         // guess from the location of the server.jar using a marker
96
URL JavaDoc url = SystemUtil.class.getClassLoader().getResource(
97                 "META-INF" + File.separator + "LICENSE.txt");
98         File JavaDoc directory = null;
99         if (url != null) {
100             try {
101                 JarURLConnection JavaDoc jarConnection = (JarURLConnection JavaDoc) url
102                         .openConnection();
103                 URL JavaDoc resourceJar = jarConnection.getJarFileURL();
104                 // resolve parent directory for ${basedir}/bin/server.jar
105
URI JavaDoc baseURI = new URI JavaDoc(resourceJar.toString()).resolve("..");
106                 directory = new File JavaDoc(baseURI);
107             } catch (Exception JavaDoc ignored) {
108                 // ignored
109
}
110         } else {
111             // Failed to load resource \"META-INF/petals-jar
112
// installPath not found
113
}
114         return directory;
115     }
116
117 }
118
Popular Tags