001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.util;
016:
017: import java.io.File;
018: import java.io.FileFilter;
019: import java.io.FileInputStream;
020: import java.io.IOException;
021: import java.util.Properties;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /** Useful collection of MetaBoss internal utilities.
027: * These utilities have little or no value for general use programs. */
028: public class MetaBossSpecificUtils {
029: // Commons Logging instance.
030: private static final Object sLoggerInitialisationSemaphore = new Object();
031: private static Log sLogger = null;
032:
033: // This method will initialis logger if it has not been initialised yet
034: // The rationale here is that we have to give applications a chance to
035: // call setupSystemProperties() before logger is initialised. Therefore
036: // we have to delay initialisatin of the logger for as long as possible.
037: // (and we are not using logger at all in the setupSystemProperties() method
038: private static void initialiseLoggerIfNecessary() {
039: if (sLogger == null) {
040: synchronized (sLoggerInitialisationSemaphore) {
041: if (sLogger == null) {
042: sLogger = LogFactory
043: .getLog(MetaBossSpecificUtils.class);
044: }
045: }
046: }
047: }
048:
049: /** Returns the name of the directory where metaboss is installed or
050: * null if it was unable to determine the name of this directory */
051: public static String getMetaBossHomeDir() {
052: // This method may want to use loger
053: initialiseLoggerIfNecessary();
054:
055: String lMetaBossHome = System.getProperty("MetaBoss.Home");
056: if (lMetaBossHome == null)
057: sLogger
058: .error("System property \"MetaBoss.Home\" is not defined. It is very likely that the application you are running is not configurred properly.");
059: return lMetaBossHome;
060: }
061:
062: /** This method reads all *.properties file from the location specified by
063: * MetaBoss.Home system property. After reading it proceeds to resolve possible
064: * macros inside property values and after that it updates System properties with it
065: * If MetaBoss.Home property is not defined - warning message is logged
066: * If no properties file has not been found - warning message is logged
067: * Note that files are loaded in no particular order, so duplicate key names would
068: * override each other and the surviving value is unpredictable.
069: * Also note that this method does not use commons logging in order to give
070: * application achance to load properties prior to initialising the logging
071: */
072: public static void setupSystemProperties() {
073: String lMetaBossHome = System
074: .getProperty("MetaBoss.Home", null);
075: if (lMetaBossHome == null) {
076: System.out
077: .println("System property \"MetaBoss.Home\" is not defined. It is very likely that the application you are running is not configurred properly.");
078: return;
079: }
080: File lMetaBossHomeDirectory = new File(lMetaBossHome)
081: .getAbsoluteFile();
082: if (lMetaBossHomeDirectory.exists() == false) {
083: System.out
084: .println("Directory "
085: + lMetaBossHomeDirectory.getAbsolutePath()
086: + " not found. It is very likely that the application you are running is not configurred properly.");
087: return;
088: }
089:
090: File lMetaBossConfigDirectory = new File(lMetaBossHome
091: + File.separator + "config").getAbsoluteFile();
092: if (lMetaBossConfigDirectory.exists() == false) {
093: System.out
094: .println("Directory "
095: + lMetaBossConfigDirectory
096: .getAbsolutePath()
097: + " not found. It is very likely that the application you are running is not configurred properly.");
098: return;
099: }
100:
101: File[] lAllPropertyFiles = lMetaBossConfigDirectory
102: .listFiles(new FileFilter() {
103: public boolean accept(File pPathname) {
104: // Return all *.properties files
105: if (pPathname.isFile()) {
106: String lFileName = pPathname.getName()
107: .toLowerCase();
108: if (lFileName.endsWith(".properties"))
109: return true;
110: }
111: return false;
112: }
113: });
114:
115: if (lAllPropertyFiles == null || lAllPropertyFiles.length == 0) {
116: System.out
117: .println("Not a single property file has been found in "
118: + lMetaBossConfigDirectory
119: .getAbsolutePath()
120: + " directory. It is very likely that the application you are running is not configurred properly.");
121: return;
122: }
123:
124: Properties lLoadedProperties = new Properties();
125: for (int i = 0; i < lAllPropertyFiles.length; i++) {
126: try {
127: lLoadedProperties.load(new FileInputStream(
128: lAllPropertyFiles[i]));
129: } catch (IOException e) {
130: System.err.println("Error while reading file "
131: + lAllPropertyFiles[i].getAbsolutePath());
132: e.printStackTrace(System.err);
133: }
134: }
135: Properties lSystemProperties = System.getProperties();
136: Properties lResolvedProperties = PropertiesUtils
137: .resolveProperties(lLoadedProperties, lSystemProperties);
138: lSystemProperties.putAll(lResolvedProperties);
139: System.setProperties(lSystemProperties);
140: }
141: }
|