1 package org.ocltf.common;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.ocltf.utils.ExceptionUtils;
9
10 /***
11 * Dictionary of configurable Context objects.
12 * Context objects are used for configuring Plugin
13 * instances.
14 *
15 * @see org.ocltf.common.Context
16 *
17 * @author Chad Brandon
18 */
19 public class Contexts {
20
21 private static Log logger = LogFactory.getLog(Contexts.class);
22
23 private final static Contexts instance = new Contexts();
24
25 /***
26 * This is passed as the cartridge name for the lookupContext
27 * method if we wish to use a 'default' Context for Plugins.
28 * This is so we don't need to define a specific mapping for each Plugin
29 * if we don't want. If a contextName exists with
30 * a specific Plugin name, then that will be used instead
31 * of the 'default'
32 */
33 public static final String DEFAULT = "default";
34
35 private Map contexts;
36
37 /***
38 * Constructs an instance of Contexts.
39 */
40 public Contexts() {
41 this.contexts = new HashMap();
42 }
43
44 /***
45 * Returns the singleton instance of this Contexts
46 * @return instance.
47 */
48 public static Contexts instance() {
49 return instance;
50 }
51
52 /***
53 * Adds a mapping for a contexts name to a physical directory.
54 *
55 * @param context the Context to add to this instance.
56 */
57 public void addContext(Context context) {
58 contexts.put(context.getName(), context);
59 }
60
61 /***
62 * Retrieves a property from the Context with the contextName.
63 *
64 * @param contextName name of the Plugin to which the contexdt applies
65 * @param propertyName name of the context property to find.
66 * @return String the context property value.
67 */
68 public String lookupContextProperty(String contextName, String propertyName) {
69 String methodName = "lookupContextProperty";
70 ExceptionUtils.checkEmpty(methodName, "contextName", contextName);
71 ExceptionUtils.checkEmpty(methodName, "propertyName", propertyName);
72
73 Property property = null;
74
75 Context context = (Context)contexts.get(contextName);
76
77 if (context != null) {
78 property = context.getProperty(propertyName);
79 }
80
81
82
83 if (property == null) {
84 if (logger.isDebugEnabled()) {
85 logger.debug("no context with name '"
86 + contextName + "' found, looking for " + DEFAULT);
87 }
88 context = (Context)contexts.get(DEFAULT);
89
90 if (context != null) {
91 property = context.getProperty(propertyName);
92 }
93 }
94
95 String result = null;
96 if (property == null) {
97 if (logger.isErrorEnabled()) {
98 logger.error("ERROR! No 'default' or '"
99 + contextName + "' context defined for property '"
100 + propertyName + "' --> please define a context with"
101 + " at least one of these names for property '"
102 + propertyName + "' in your build file");
103 }
104 } else {
105 result = property.getValue();
106 }
107
108 return result;
109 }
110 }