1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: AttributeHelper.java,v 1.5 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.lang.reflect.Method;
48 import java.io.InputStream;
49 import java.io.InputStreamReader;
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.List;
53
54 import org.apache.log4j.Category;
55
56 import org.exolab.jmscts.core.meta.Attribute;
57 import org.exolab.jmscts.core.meta.ClassMeta;
58 import org.exolab.jmscts.core.meta.Meta;
59 import org.exolab.jmscts.core.meta.MetaData;
60 import org.exolab.jmscts.core.meta.MethodMeta;
61
62
63 /***
64 * A helper class for retrieving attributes from class meta-data.
65 *
66 * @version $Revision: 1.5 $ $Date: 2004/01/31 13:44:24 $
67 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
68 */
69 public final class AttributeHelper {
70
71 /***
72 * A map of class names to their corresponding ClassMeta instances
73 */
74 private static final HashMap METADATA;
75
76 /***
77 * The logger
78 */
79 private static final Category log = Category.getInstance(
80 AttributeHelper.class.getName());
81
82 /***
83 * Empty list
84 */
85 private static final String[] EMPTY = new String[0];
86
87 /***
88 * The class meta data resource path
89 */
90 private static final String PATH = "/metadata.xml";
91
92
93 /***
94 * Prevent construction of utility class
95 */
96 private AttributeHelper() {
97 }
98
99 /***
100 * Returns the boolean value of an attribute, associated with a
101 * particular class
102 *
103 * @param clazz the class to look up attributes for
104 * @param attribute the attribute name
105 * @return the boolean value of <code>attribute</code>, or
106 * <code>false</code> if <code>attribute</code> doesn't exists
107 */
108 public static boolean getBoolean(Class clazz, String attribute) {
109 boolean result = false;
110 String[] values = getAttributes(clazz, attribute);
111 return result;
112 }
113
114 /***
115 * Returns the boolean value of an attribute, associated with a
116 * particular method
117 *
118 * @param clazz the class to look up attributes for
119 * @param methodName the name of the method. The method must take no args.
120 * May be null.
121 * @param attribute the attribute name
122 * @return the boolean value of <code>attribute</code>, or
123 * <code>false</code> if <code>attribute</code> doesn't exists
124 */
125 public static boolean getBoolean(Class clazz, String methodName,
126 String attribute) {
127 String[] values = getAttributes(clazz, methodName, attribute);
128 return getBoolean(values);
129 }
130
131 /***
132 * Returns the boolean value of an attribute, associated with a
133 * particular method and/or class
134 *
135 * @param clazz the class to look up attributes for
136 * @param methodName the name of the method. The method must take no args.
137 * May be null.
138 * @param attribute the attribute name
139 * @param all if <code>true</code> include method and class attributes
140 * @return the boolean value of <code>attribute</code>, or
141 * <code>false</code> if <code>attribute</code> doesn't exists
142 */
143 public static boolean getBoolean(Class clazz, String methodName,
144 String attribute, boolean all) {
145 String[] values = getAttributes(clazz, methodName, attribute, all);
146 return getBoolean(values);
147 }
148
149 /***
150 * Return a list of attribute values for the specified attribute name,
151 * associated with a particular class
152 *
153 * @param clazz the class to look up attributes for
154 * @param attribute the attribute name
155 * @return a list of attribute values for the specified attribute name.
156 */
157 public static String[] getAttributes(Class clazz, String attribute) {
158 String[] result = EMPTY;
159 ClassMeta meta = (ClassMeta) METADATA.get(clazz.getName());
160 if (meta != null) {
161 result = getValues(meta, attribute);
162 } else {
163 log.warn("Failed to locate meta data for class="
164 + clazz.getName());
165 }
166 if (log.isDebugEnabled()) {
167 log.debug("Attribute values for " + clazz.getName() + "["
168 + attribute + "] = " + format(result));
169 }
170 return result;
171 }
172
173 /***
174 * Return a list of attribute values for the specified attribute name,
175 * associated with a particular method and/or class.
176 *
177 * @param clazz the class to look up attributes for
178 * @param methodName the name of the method. The method must take no args.
179 * May be null.
180 * @param attribute the attribute name
181 * @param all if <code>true</code> include method and class attributes
182 * @return a list of attribute values for the specified attribute name.
183 */
184 public static String[] getAttributes(Class clazz, String methodName,
185 String attribute, boolean all) {
186 String[] result = EMPTY;
187 String[] methodAtts = EMPTY;
188 String[] classAtts = EMPTY;
189 if (methodName != null) {
190 methodAtts = getAttributes(clazz, methodName, attribute);
191 }
192 if (methodAtts.length == 0 || all) {
193 classAtts = getAttributes(clazz, attribute);
194 }
195
196 if (methodAtts.length != 0 || classAtts.length != 0) {
197 result = new String[methodAtts.length + classAtts.length];
198 System.arraycopy(methodAtts, 0, result, 0, methodAtts.length);
199 System.arraycopy(classAtts, 0, result, methodAtts.length,
200 classAtts.length);
201 }
202 return result;
203 }
204
205 /***
206 * Return a list of attribute values for the specified attribute name,
207 * associated with a particular method
208 *
209 * @param clazz the class to look up attributes for
210 * @param methodName the name of the method. The method must take no args.
211 * @param attribute the attribute name
212 * @return a list of attribute values for the specified attribute name.
213 */
214 public static String[] getAttributes(Class clazz, String methodName,
215 String attribute) {
216 String[] result = EMPTY;
217 try {
218 Method method = clazz.getMethod(methodName, new Class[0]);
219 result = getAttributes(method, attribute);
220 } catch (Exception exception) {
221 log.warn("Failed to locate meta data for class=" + clazz.getName(),
222 exception);
223 }
224 if (log.isDebugEnabled()) {
225 log.debug("Attribute values for " + clazz.getName() + "."
226 + methodName + "[" + attribute + "] = "
227 + format(result));
228 }
229 return result;
230 }
231
232 /***
233 * Return a list of attribute values for the specified attribute name
234 * associated with a particular method
235 *
236 * @param method the method
237 * @param attribute the attribute name
238 * @return a list of attribute values for the specified attribute name.
239 */
240 public static String[] getAttributes(Method method, String attribute) {
241 String[] result = EMPTY;
242 String className = method.getDeclaringClass().getName();
243 String methodName = method.getName();
244 ClassMeta classMeta = (ClassMeta) METADATA.get(className);
245 if (classMeta != null) {
246 MethodMeta[] methods = classMeta.getMethodMeta();
247 for (int i = 0; i < methods.length; ++i) {
248 MethodMeta methodMeta = methods[i];
249 if (methodMeta.getName().equals(methodName)) {
250 result = getValues(methodMeta, attribute);
251 }
252 }
253 } else {
254 log.warn("Failed to locate meta data for class=" + className);
255 }
256 return result;
257 }
258
259 /***
260 * Returns the first boolean value from a set of values
261 *
262 * @param values the set of values
263 * @return the first boolean value from a set of values, or
264 * <code>false</code> if <code>values</code> is empty
265 */
266 private static boolean getBoolean(String[] values) {
267 boolean result = false;
268 if (values.length > 0) {
269 result = Boolean.valueOf(values[0]).booleanValue();
270 }
271 return result;
272 }
273
274 /***
275 * Returns the values of an attribute
276 *
277 * @param meta the meta data
278 * @param name the name of the attribute
279 * @return a list of attribute values. May be empty
280 */
281 private static String[] getValues(Meta meta, String name) {
282 List result = new ArrayList();
283 Attribute[] attributes = meta.getAttribute();
284 for (int i = 0; i < attributes.length; ++i) {
285 Attribute attribute = attributes[i];
286 if (attribute.getName().equals(name)) {
287 result.add(attribute.getValue());
288 }
289 }
290 return (String[]) result.toArray(EMPTY);
291 }
292
293 /***
294 * Format values for logging
295 *
296 * @param values the list of values
297 * @return the formatted values
298 */
299 private static String format(String[] values) {
300 StringBuffer result = new StringBuffer();
301 for (int i = 0; i < values.length; ++i) {
302 if (i > 0) {
303 result.append(", ");
304 }
305 result.append(values[i]);
306 }
307 return result.toString();
308 }
309
310 static {
311 METADATA = new HashMap();
312
313 InputStream stream = AttributeHelper.class.getResourceAsStream(PATH);
314 if (stream == null) {
315 String msg = "Failed to locate meta data: " + PATH;
316 log.error(msg);
317 throw new RuntimeException(msg);
318 }
319 MetaData metaData;
320 try {
321 metaData = MetaData.unmarshal(new InputStreamReader(stream));
322 } catch (Exception exception) {
323 String msg = "Failed to read meta data: " + PATH;
324 log.error(msg, exception);
325 throw new RuntimeException(msg);
326 }
327
328
329 ClassMeta[] classes = metaData.getClassMeta();
330 for (int i = 0; i < classes.length; ++i) {
331 ClassMeta clazz = classes[i];
332 METADATA.put(clazz.getName(), clazz);
333 }
334 }
335
336 }
This page was automatically generated by Maven