1 | /* |
---|
2 | * ContextFactory.java |
---|
3 | * |
---|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
5 | * or more contributor license agreements. See the NOTICE file |
---|
6 | * distributed with this work for additional information |
---|
7 | * regarding copyright ownership. The ASF licenses this file |
---|
8 | * to you under the Apache License, Version 2.0 (the |
---|
9 | * "License"); you may not use this file except in compliance |
---|
10 | * with the License. You may obtain a copy of the License at |
---|
11 | * |
---|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
13 | * |
---|
14 | * Unless required by applicable law or agreed to in writing, software |
---|
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
17 | * See the License for the specific language governing permissions and |
---|
18 | * limitations under the License. |
---|
19 | */ |
---|
20 | |
---|
21 | package org.apache.hadoop.metrics; |
---|
22 | |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | import java.util.HashMap; |
---|
26 | import java.util.Iterator; |
---|
27 | import java.util.Map; |
---|
28 | import java.util.Properties; |
---|
29 | import org.apache.hadoop.metrics.spi.AbstractMetricsContext; |
---|
30 | import org.apache.hadoop.metrics.spi.NullContext; |
---|
31 | |
---|
32 | /** |
---|
33 | * Factory class for creating MetricsContext objects. To obtain an instance |
---|
34 | * of this class, use the static <code>getFactory()</code> method. |
---|
35 | */ |
---|
36 | public class ContextFactory { |
---|
37 | |
---|
38 | private static final String PROPERTIES_FILE = |
---|
39 | "/hadoop-metrics.properties"; |
---|
40 | private static final String CONTEXT_CLASS_SUFFIX = |
---|
41 | ".class"; |
---|
42 | private static final String DEFAULT_CONTEXT_CLASSNAME = |
---|
43 | "org.apache.hadoop.metrics.spi.NullContext"; |
---|
44 | |
---|
45 | private static ContextFactory theFactory = null; |
---|
46 | |
---|
47 | private Map<String,Object> attributeMap = new HashMap<String,Object>(); |
---|
48 | private Map<String,MetricsContext> contextMap = |
---|
49 | new HashMap<String,MetricsContext>(); |
---|
50 | |
---|
51 | // Used only when contexts, or the ContextFactory itself, cannot be |
---|
52 | // created. |
---|
53 | private static Map<String,MetricsContext> nullContextMap = |
---|
54 | new HashMap<String,MetricsContext>(); |
---|
55 | |
---|
56 | /** Creates a new instance of ContextFactory */ |
---|
57 | protected ContextFactory() { |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Returns the value of the named attribute, or null if there is no |
---|
62 | * attribute of that name. |
---|
63 | * |
---|
64 | * @param attributeName the attribute name |
---|
65 | * @return the attribute value |
---|
66 | */ |
---|
67 | public Object getAttribute(String attributeName) { |
---|
68 | return attributeMap.get(attributeName); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Returns the names of all the factory's attributes. |
---|
73 | * |
---|
74 | * @return the attribute names |
---|
75 | */ |
---|
76 | public String[] getAttributeNames() { |
---|
77 | String[] result = new String[attributeMap.size()]; |
---|
78 | int i = 0; |
---|
79 | // for (String attributeName : attributeMap.keySet()) { |
---|
80 | Iterator it = attributeMap.keySet().iterator(); |
---|
81 | while (it.hasNext()) { |
---|
82 | result[i++] = (String) it.next(); |
---|
83 | } |
---|
84 | return result; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Sets the named factory attribute to the specified value, creating it |
---|
89 | * if it did not already exist. If the value is null, this is the same as |
---|
90 | * calling removeAttribute. |
---|
91 | * |
---|
92 | * @param attributeName the attribute name |
---|
93 | * @param value the new attribute value |
---|
94 | */ |
---|
95 | public void setAttribute(String attributeName, Object value) { |
---|
96 | attributeMap.put(attributeName, value); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Removes the named attribute if it exists. |
---|
101 | * |
---|
102 | * @param attributeName the attribute name |
---|
103 | */ |
---|
104 | public void removeAttribute(String attributeName) { |
---|
105 | attributeMap.remove(attributeName); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Returns the named MetricsContext instance, constructing it if necessary |
---|
110 | * using the factory's current configuration attributes. <p/> |
---|
111 | * |
---|
112 | * When constructing the instance, if the factory property |
---|
113 | * <i>contextName</i>.class</code> exists, |
---|
114 | * its value is taken to be the name of the class to instantiate. Otherwise, |
---|
115 | * the default is to create an instance of |
---|
116 | * <code>org.apache.hadoop.metrics.spi.NullContext</code>, which is a |
---|
117 | * dummy "no-op" context which will cause all metric data to be discarded. |
---|
118 | * |
---|
119 | * @param contextName the name of the context |
---|
120 | * @return the named MetricsContext |
---|
121 | */ |
---|
122 | public synchronized MetricsContext getContext(String refName, String contextName) |
---|
123 | throws IOException, ClassNotFoundException, |
---|
124 | InstantiationException, IllegalAccessException { |
---|
125 | MetricsContext metricsContext = contextMap.get(refName); |
---|
126 | if (metricsContext == null) { |
---|
127 | String classNameAttribute = refName + CONTEXT_CLASS_SUFFIX; |
---|
128 | String className = (String) getAttribute(classNameAttribute); |
---|
129 | if (className == null) { |
---|
130 | className = DEFAULT_CONTEXT_CLASSNAME; |
---|
131 | } |
---|
132 | Class contextClass = Class.forName(className); |
---|
133 | metricsContext = (MetricsContext) contextClass.newInstance(); |
---|
134 | metricsContext.init(contextName, this); |
---|
135 | contextMap.put(contextName, metricsContext); |
---|
136 | } |
---|
137 | return metricsContext; |
---|
138 | } |
---|
139 | |
---|
140 | public synchronized MetricsContext getContext(String contextName) |
---|
141 | throws IOException, ClassNotFoundException, InstantiationException, |
---|
142 | IllegalAccessException { |
---|
143 | return getContext(contextName, contextName); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Returns a "null" context - one which does nothing. |
---|
148 | */ |
---|
149 | public static synchronized MetricsContext getNullContext(String contextName) { |
---|
150 | MetricsContext nullContext = nullContextMap.get(contextName); |
---|
151 | if (nullContext == null) { |
---|
152 | nullContext = new NullContext(); |
---|
153 | nullContextMap.put(contextName, nullContext); |
---|
154 | } |
---|
155 | return nullContext; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Returns the singleton ContextFactory instance, constructing it if |
---|
160 | * necessary. <p/> |
---|
161 | * |
---|
162 | * When the instance is constructed, this method checks if the file |
---|
163 | * <code>hadoop-metrics.properties</code> exists on the class path. If it |
---|
164 | * exists, it must be in the format defined by java.util.Properties, and all |
---|
165 | * the properties in the file are set as attributes on the newly created |
---|
166 | * ContextFactory instance. |
---|
167 | * |
---|
168 | * @return the singleton ContextFactory instance |
---|
169 | */ |
---|
170 | public static synchronized ContextFactory getFactory() throws IOException { |
---|
171 | if (theFactory == null) { |
---|
172 | theFactory = new ContextFactory(); |
---|
173 | theFactory.setAttributes(); |
---|
174 | } |
---|
175 | return theFactory; |
---|
176 | } |
---|
177 | |
---|
178 | private void setAttributes() throws IOException { |
---|
179 | InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE); |
---|
180 | if (is != null) { |
---|
181 | Properties properties = new Properties(); |
---|
182 | properties.load(is); |
---|
183 | //for (Object propertyNameObj : properties.keySet()) { |
---|
184 | Iterator it = properties.keySet().iterator(); |
---|
185 | while (it.hasNext()) { |
---|
186 | String propertyName = (String) it.next(); |
---|
187 | String propertyValue = properties.getProperty(propertyName); |
---|
188 | setAttribute(propertyName, propertyValue); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|