Author: julien(a)jboss.com
Date: 2007-06-14 18:06:47 -0400 (Thu, 14 Jun 2007)
New Revision: 7426
Added:
trunk/faces/src/main/org/jboss/portal/faces/el/ClassConstantPublisherBean.java
Log:
implemented a dynamic bean which allow to access class constant fields in EL
Added: trunk/faces/src/main/org/jboss/portal/faces/el/ClassConstantPublisherBean.java
===================================================================
--- trunk/faces/src/main/org/jboss/portal/faces/el/ClassConstantPublisherBean.java
(rev 0)
+++
trunk/faces/src/main/org/jboss/portal/faces/el/ClassConstantPublisherBean.java 2007-06-14
22:06:47 UTC (rev 7426)
@@ -0,0 +1,110 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.faces.el;
+
+import org.jboss.portal.faces.el.dynamic.DynamicBean;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassConstantPublisherBean implements DynamicBean
+{
+
+ /** . */
+ private String className;
+
+ /** . */
+ private volatile Map entries;
+
+ private Map getEntries()
+ {
+ if (entries == null)
+ {
+ try
+ {
+ Map entries = new HashMap();
+ Class clazz =
Thread.currentThread().getContextClassLoader().loadClass(className);
+ Field[] fields = clazz.getFields();
+ for (int i = 0; i < fields.length; i++)
+ {
+ Field field = fields[i];
+ int modifiers = field.getModifiers();
+ if ((modifiers & Modifier.STATIC | modifiers & Modifier.PUBLIC) !=
0)
+ {
+ Object value = field.get(null);
+ entries.put(field.getName(), value);
+ }
+ }
+ this.entries = entries;
+ }
+ catch (ClassNotFoundException e)
+ {
+ e.printStackTrace();
+ }
+ catch (IllegalAccessException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ return entries;
+ }
+
+ public Class getType(Object propertyName) throws IllegalArgumentException
+ {
+ Map entries = getEntries();
+ Object value = entries.get(propertyName);
+ return value != null ? value.getClass() : null;
+ }
+
+ public PropertyValue getValue(Object propertyName) throws IllegalArgumentException
+ {
+ if ("className".equals(propertyName))
+ {
+ return new PropertyValue(className);
+ }
+
+ //
+ Map entries = getEntries();
+ Object value = entries.get(propertyName);
+ return new PropertyValue(value);
+ }
+
+ public boolean setValue(Object propertyName, Object value) throws
IllegalArgumentException
+ {
+ if ("className".equals(propertyName))
+ {
+ this.className = (String)value;
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+}