001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.faces.el;
023:
024: import org.jboss.portal.faces.el.dynamic.DynamicBean;
025:
026: import java.lang.reflect.Field;
027: import java.lang.reflect.Modifier;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: /**
032: * An implementation of <code>DynamicBean</code> which exposes all the fields on a class which are public and static.
033: * The properties are read only and cannot be modified. It allows to use constant names from expression language rather
034: * than the constant values directly.
035: * <p/>
036: * <ul> <li>Class objects of type class or interface are supported.</li> <li>Class inheritence is supported, Class
037: * object returned from <code>Class.getSuperClass()</code> is examined.</li> <li>Interface implementation is not yet
038: * supported, Class objects returned from <code>Class.getInterfaces()</code> are not examined.</li> </ul>
039: * <p/>
040: * todo: implement and test interfaces returned by <code>Class.getInterfaces()</code>.
041: *
042: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
043: * @version $Revision: 8784 $
044: */
045: public class ClassConstantPublisherBean implements DynamicBean {
046:
047: /** The class name. */
048: private String className;
049:
050: /** The cached entries. */
051: private volatile Map entries;
052:
053: private static Map getEntries(Class clazz) {
054: Class super Clazz = clazz.getSuperclass();
055: Map entries = clazz.getSuperclass() == null ? new HashMap()
056: : getEntries(super Clazz);
057:
058: //
059: try {
060: Field[] fields = clazz.getDeclaredFields();
061: for (int i = 0; i < fields.length; i++) {
062: Field field = fields[i];
063: final int modifiers = field.getModifiers();
064: if (Modifier.isPublic(modifiers)
065: && Modifier.isStatic(modifiers)) {
066: Object value = field.get(null);
067: entries.put(field.getName(), value);
068: }
069: }
070: } catch (IllegalAccessException e) {
071: e.printStackTrace();
072: }
073:
074: //
075: return entries;
076: }
077:
078: private Map getEntries() {
079: if (entries == null) {
080: try {
081: Class clazz = Thread.currentThread()
082: .getContextClassLoader().loadClass(className);
083: this .entries = getEntries(clazz);
084: } catch (ClassNotFoundException e) {
085: e.printStackTrace();
086: }
087: }
088: return entries;
089: }
090:
091: public Class getType(Object propertyName)
092: throws IllegalArgumentException {
093: if ("className".equals(propertyName)) {
094: return String.class;
095: }
096:
097: //
098: Map entries = getEntries();
099: Object value = entries.get(propertyName);
100: return value != null ? value.getClass() : null;
101: }
102:
103: public PropertyValue getValue(Object propertyName)
104: throws IllegalArgumentException {
105: if ("className".equals(propertyName)) {
106: return new PropertyValue(className);
107: }
108:
109: //
110: Map entries = getEntries();
111: Object value = entries.get(propertyName);
112: return new PropertyValue(value);
113: }
114:
115: public boolean setValue(Object propertyName, Object value)
116: throws IllegalArgumentException {
117: if ("className".equals(propertyName)) {
118: this .className = (String) value;
119: return true;
120: } else {
121: return false;
122: }
123: }
124:
125: public String getClassName() {
126: return className;
127: }
128:
129: public void setClassName(String className) {
130: this.className = className;
131: }
132: }
|