1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3 
4 package nl.justobjects.pushlet.core;
5 
6 import nl.justobjects.pushlet.util.Log;
7 import nl.justobjects.pushlet.util.PushletException;
8 import nl.justobjects.pushlet.util.Sys;
9 
10import java.io.File;
11import java.util.Properties;
12
13/**
14 * Loads and maintains overall configuration.
15 *
16 * @author Just van den Broecke - Just Objects &copy;
17 * @version $Id: Config.java,v 1.5 2007/11/23 21:10:17 justb Exp $
18 */
19public class Config implements ConfigDefs {
20    private static final String PROPERTIES_FILE = "pushlet.properties";
21    private static Properties properties;
22
23    /**
24     * Factory method: create object from property denoting class name.
25     *
26     * @param aClassNameProp property name e.g. "session.class"
27     * @return an instance of class denoted by property
28     * @throws PushletException when class cannot be instantiated
29     */
30    public static Object createObject(String aClassNameProp, String aDefault) throws PushletException {
31        Class clazz = getClass(aClassNameProp, aDefault);
32        try {
33            return clazz.newInstance();
34        } catch (Throwable t) {
35            // Usually a misconfiguration
36            throw new PushletException("Cannot instantiate class for " + aClassNameProp + "=" + clazz, t);
37        }
38    }
39
40    /**
41     * Factory method: create object from property denoting class name.
42     *
43     * @param aClassNameProp property name e.g. "session.class"
44     * @return a Class object denoted by property
45     * @throws PushletException when class cannot be instantiated
46     */
47    public static Class getClass(String aClassNameProp, String aDefault) throws PushletException {
48        // Singleton + factory pattern:  create object instance
49        // from configured class name
50        String clazz = (aDefault == null ? getProperty(aClassNameProp) : getProperty(aClassNameProp, aDefault));
51
52        try {
53            return Class.forName(clazz);
54        } catch (ClassNotFoundException t) {
55            // Usually a misconfiguration
56            throw new PushletException("Cannot find class for " + aClassNameProp + "=" + clazz, t);
57        }
58    }
59
60    /**
61     * Initialize event sources from properties file.
62     */
63    public static void load(String aDirPath) {
64        // Load Event sources using properties file.
65        try {
66            // Try loading through classpath first (e.g. in WEB-INF/classes or from .jar)
67            Log.info("Config: loading " + PROPERTIES_FILE + " from classpath");
68            properties = Sys.loadPropertiesResource(PROPERTIES_FILE);
69        } catch (Throwable t) {
70            // Try from provided dir (e.g. WEB_INF/pushlet.properties)
71            String filePath = aDirPath + File.separator + PROPERTIES_FILE;
72            Log.info("Config: cannot load " + PROPERTIES_FILE + " from classpath, will try from " + filePath);
73
74            try {
75                properties = Sys.loadPropertiesFile(filePath);
76            } catch (Throwable t2) {
77                Log.fatal("Config: cannot load properties file from " + filePath, t);
78
79                // Give up
80                return;
81            }
82        }
83
84        Log.info("Config: loaded values=" + properties);
85    }
86
87    public static String getProperty(String aName, String aDefault) {
88        return properties.getProperty(aName, aDefault);
89    }
90
91    public static String getProperty(String aName) {
92        String value = properties.getProperty(aName);
93        if (value == null) {
94            throw new IllegalArgumentException("Unknown property: " + aName);
95        }
96        return value;
97    }
98
99    public static boolean getBoolProperty(String aName) {
00        String value = getProperty(aName);
01        try {
02            return value.equals("true");
03        } catch (Throwable t) {
04            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
05        }
06    }
07
08    public static int getIntProperty(String aName) {
09        String value = getProperty(aName);
10        try {
11            return Integer.parseInt(value);
12        } catch (Throwable t) {
13            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
14        }
15    }
16
17    public static long getLongProperty(String aName) {
18        String value = getProperty(aName);
19        try {
20            return Long.parseLong(value);
21        } catch (Throwable t) {
22            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
23        }
24    }
25
26    public static boolean hasProperty(String aName) {
27        return properties.containsKey(aName);
28    }
29
30
31}
32
33/*
34 * $Log: Config.java,v $
35 * Revision 1.5  2007/11/23 21:10:17  justb
36 * add hooks for custom logging (you can override DefaultLogger in pushlet.properties)
37 *
38 * Revision 1.4  2007/11/23 14:33:07  justb
39 * core classes now configurable through factory
40 *
41 * Revision 1.3  2007/11/10 13:44:02  justb
42 * pushlet.properties and sources.properties can now also be put under WEB-INF
43 *
44 * Revision 1.2  2006/05/06 00:10:11  justb
45 * various chgs but not too serious...
46 *
47 * Revision 1.1  2005/02/18 12:36:47  justb
48 * changes for renaming and configurability
49 *
50
51 *
52 */
53