01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util;
11:
12: import java.util.*;
13: import javax.naming.*;
14:
15: /**
16: * @javadoc
17: *
18: * @author Nico Klasens
19: * @since MMBase 1.8.1
20: * @version $Id: ApplicationContextReader.java,v 1.4 2007/02/24 21:57:50 nklasens Exp $
21: */
22: public class ApplicationContextReader {
23:
24: /**
25: * @javadoc
26: */
27: public static Map<String, String> getProperties(String path)
28: throws NamingException {
29: if (path == null || "".equals(path)) {
30: throw new IllegalArgumentException("Path is empty");
31: }
32: Map<String, String> properties = new HashMap<String, String>();
33: Context env = getContext();
34: if (env != null) {
35: NamingEnumeration<NameClassPair> ne = env.list(path);
36: while (ne.hasMoreElements()) {
37: NameClassPair element = ne.nextElement();
38: String contextName = element.getName();
39: String lookupName = env.composeName(contextName, path);
40: String value = env.lookup(lookupName).toString();
41: properties.put(contextName, value);
42: }
43: }
44: return properties;
45: }
46:
47: /**
48: * @javadoc
49: */
50: public static Context getContext() throws NamingException {
51: InitialContext context = new InitialContext();
52: return (Context) context.lookup("java:comp/env");
53: }
54:
55: }
|