KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.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: PropertyUtil.java 14:47:22 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.net.URL JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.objectweb.petals.PetalsException;
28
29 /**
30  * TODO the configuration of Joram must be defined with those iinformation,
31  * and not use the a3...xml file.
32  * HOST parameter has to be defined only once.
33  * This class is a util to load properties from a properties file
34  *
35  * @author ddesjardins - eBMWebsourcing
36  */

37 public final class PropertyUtil {
38
39     /**
40      * Name of the server properties file
41      */

42     public final static String JavaDoc SERVER_PROPS = "/server.properties";
43     
44     private PropertyUtil() {
45         // Do nothing
46
}
47
48     /**
49      * Retreive a property from a properties file
50      *
51      * @param propertiesFile
52      * name of the properties file (must be in the classpath)
53      * @param key
54      * key of the property we want to retreive
55      * @return property associated with the key
56      * @throws PetalsException
57      * if any problems occurs
58      */

59     public static String JavaDoc getProperty(String JavaDoc propertiesFile, String JavaDoc key)
60         throws PetalsException {
61         try {
62             URL JavaDoc url = PropertyUtil.class.getResource(propertiesFile);
63             Properties JavaDoc properties = new Properties JavaDoc();
64             properties.load(url.openStream());
65             return properties.getProperty(key);
66         } catch (Exception JavaDoc e) {
67             throw new PetalsException("Problem while trying to get property "
68                 + key + " of " + propertiesFile, e);
69         }
70     }
71
72     /**
73      * Retreive the JNDI properties of a properties file
74      *
75      * @param propertiesFile
76      * name of the properties file
77      * @return bunch of JNDI properties
78      * @throws PetalsException
79      */

80     public static Properties JavaDoc retreiveJNDIProperties(String JavaDoc propertiesFile)
81         throws PetalsException {
82         try {
83             URL JavaDoc url = PropertyUtil.class.getResource(propertiesFile);
84             Properties JavaDoc properties = new Properties JavaDoc();
85             properties.load(url.openStream());
86             Properties JavaDoc outProperties = new Properties JavaDoc();
87             outProperties.setProperty("java.naming.factory.host", properties
88                 .getProperty("java.naming.factory.host"));
89             outProperties.setProperty("java.naming.factory.port", properties
90                 .getProperty("java.naming.factory.port"));
91             outProperties.setProperty("java.naming.factory.initial", properties
92                 .getProperty("java.naming.factory.initial"));
93             return outProperties;
94         } catch (Exception JavaDoc e) {
95             throw new PetalsException(
96                 "Problem while trying to get JNDI properties of "
97                     + propertiesFile, e);
98         }
99     }
100 }
101
Popular Tags