[opencms-dev] Access Constants per Expression Laguage. Example: ${aCmsWorkplaceObject.constants.A_CONSTANT_NAME}

Brabenetz, Harald harald.brabenetz at bearingpoint.com
Thu Feb 16 12:18:06 CET 2006


The following Code-Fragments can be inserted into CmsWorkplace and CmsJspBean
It will be very useful for JSTL like

<c:choose>
	<c:when test="${wp.constants.ACTION_CANCEL==wp.action}">
.......
.......

I attached a simplified Example: ConstantsMap.java (JDK 1.4: ConstantsMap14.java)


################### Members: ##############################################################
   
    /** Map<ClassName, Constants<Const_Name, Const_Value>> */
    private static final Map<String, Map<String, Object>> m_constants = new HashMap<String, Map<String, Object>>();;

###########################################################################################

################### Functions: ############################################################

	/**
	 * Get all public final static Members of this Class-Object
	 * You can access it in JSTL and JSP 2.0 through the Expression Language.
	 * Example: ${aCmsWorkplaceObject.constants.A_CONSTANT_NAME}
	 *
	 * @return the value of a public static final Member
	 */
	public Map<String, Object> getConstants() {
		Map<String, Object> constants = m_constants.get(this.getClass().getName()); //needed for Child-Objects
		if (constants == null){
	        synchronized (this) {
				constants = m_constants.get(this.getClass().getName());
	    		if (constants == null){
	    			constants = getConstantsMap(this.getClass());
	    			m_constants.put(this.getClass().getName(), constants);
	    		}
	        }
		}
		return constants;
	}

	/**
	 * Get a Map of all Constants (public static final) in a Class-Object and his parents
	 * @param clazz The class and his Constants
	 * @return the Map
	 */
	public static Map<String, Object> getConstantsMap(Class clazz){
		Map<String, Object> result = new HashMap<String, Object>();
		java.lang.reflect.Field[] fields = clazz.getFields();
		for (java.lang.reflect.Field field : fields) {
			int modifier = field.getModifiers();
			if (java.lang.reflect.Modifier.isPublic(modifier) &&
				java.lang.reflect.Modifier.isStatic(modifier) &&
				java.lang.reflect.Modifier.isFinal(modifier))
			{
				try {
					result.put(field.getName(), field.get(null));
				}
				catch (IllegalAccessException e) {}
			}
		}
		return java.util.Collections.unmodifiableMap(result);
	}
###########################################################################################

Please send me a response.

With friendly regards,

Harald Brabenetz | Senior Consultant | BearingPoint | A-8141 Graz |
Seering 6 | Phone +43 - (0)316 - 8003 - 1250
<mailto:harald.brabenetz at bearingpoint.com> | <http://www.bearingpoint.de>



***************************************************************************************************
The information in this email is confidential and may be legally privileged.  Access to this email by anyone other than the intended addressee is unauthorized.  If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful.  If you are not the intended recipient, please reply to or forward a copy of this message to the sender and delete the message, any attachments, and any copies thereof from your system.
***************************************************************************************************
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConstantsMap.java
Type: application/octet-stream
Size: 2636 bytes
Desc: ConstantsMap.java
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20060216/432258c2/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConstantsMap14.java
Type: application/octet-stream
Size: 2718 bytes
Desc: ConstantsMap14.java
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20060216/432258c2/attachment-0001.obj>


More information about the opencms-dev mailing list