<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Once and Only Once : Accessing constants in JSF</title>
    <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Darren Tarbard's ramblings</description>
    <item>
      <title>Accessing constants in JSF</title>
      <description>

&lt;pre&gt;

marker interface:

package uk.co.tarbard.wfd;

public interface BackingPage {

}

custom property resolver:

package uk.co.tarbard.wfd;

import java.lang.reflect.Field;

import javax.faces.el.EvaluationException;
import javax.faces.el.PropertyNotFoundException;
import javax.faces.el.PropertyResolver;

public class ConstantPropertyResolver extends PropertyResolver {

	private PropertyResolver original;

	public ConstantPropertyResolver(PropertyResolver original) {
		this.original = original;
	}

	@Override
	public Class getType(Object arg0, int arg1) throws EvaluationException,
			PropertyNotFoundException {

		return original.getType(arg0, arg1);
	}

	@Override
	public Class getType(Object arg0, Object arg1) throws EvaluationException,
			PropertyNotFoundException {

		return original.getType(arg0, arg1);
	}

	@Override
	public Object getValue(Object arg0, int arg1) throws EvaluationException,
			PropertyNotFoundException {
		return original.getValue(arg0, arg1);
	}

	@Override
	public Object getValue(Object base, Object property)
			throws EvaluationException, PropertyNotFoundException {
		Object value = null;
		try {
			value = original.getValue(base, property);

		} catch (PropertyNotFoundException e) {

		}

		if (null != value) {
			return value;
		}
		if (null == base) {
			return null;
		}
		if (base instanceof BackingPage) {
			Field[] declaredFields = base.getClass().getDeclaredFields();
			for (int i = 0; i &lt; declaredFields.length; i++) {
				Field x = declaredFields[i];
				Object constantValue = null;
				if (property.equals(x.getName())){
					try {
						constantValue =  x.get(x);
					} catch (IllegalArgumentException e) {
						throw new PropertyNotFoundException("unable to access property " + property) ;
					} catch (IllegalAccessException e) {
						throw new PropertyNotFoundException("unable to access property " + property + " check that it is public") ;
					}
					return constantValue;
				}
				
			}
			throw new PropertyNotFoundException("unable to locate constant with name " + property + " perhaps you made a typo.");
		}

		return null;

	}

	@Override
	public boolean isReadOnly(Object arg0, int arg1)
			throws EvaluationException, PropertyNotFoundException {
		return original.isReadOnly(arg0, arg1);
	}

	@Override
	public boolean isReadOnly(Object arg0, Object arg1)
			throws EvaluationException, PropertyNotFoundException {
		return original.isReadOnly(arg0, arg1);
	}

	@Override
	public void setValue(Object arg0, int arg1, Object arg2)
			throws EvaluationException, PropertyNotFoundException {
		original.setValue(arg0, arg1, arg2);
	}

	@Override
	public void setValue(Object arg0, Object arg1, Object arg2)
			throws EvaluationException, PropertyNotFoundException {
		original.setValue(arg0, arg1, arg2);
	}

}

&lt;/pre&gt;
</description>
      <pubDate>Fri, 28 Aug 2009 00:11:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:150d3b00-7086-4b3f-9022-3bd281b8448c</guid>
      <author>Daz</author>
      <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf</link>
      <category>software</category>
      <category>Software dev</category>
    </item>
    <item>
      <title>"Accessing constants in JSF" by used lease cars</title>
      <description>I enjoyed reading it. I admiring time and effort you put in your blog, because it is obviously one great place where I can find lot of useful info..
</description>
      <pubDate>Tue, 31 Aug 2010 10:54:05 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2dde81fe-bacf-4349-9d61-15ba53d267c3</guid>
      <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf#comment-6355</link>
    </item>
    <item>
      <title>"Accessing constants in JSF" by angular cheilitis</title>
      <description>thanks for the work on the blog</description>
      <pubDate>Tue, 20 Jul 2010 06:18:22 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:5a0fd7f8-26c3-4a64-8b7e-320ff387d9da</guid>
      <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf#comment-6346</link>
    </item>
    <item>
      <title>"Accessing constants in JSF" by used cars indianapolis indiana</title>
      <description>The exceptions you have permitted in this piece of code really need to be swallowed in order to be executable. Definitely a band aid for surgery at this point. </description>
      <pubDate>Wed, 23 Jun 2010 20:05:27 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:67e7def7-ca23-4537-b02b-b4acfb80ee23</guid>
      <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf#comment-6327</link>
    </item>
    <item>
      <title>"Accessing constants in JSF" by daz</title>
      <description>I need to sort out a swallowed exception but this article is a quick pastebin for me for now.</description>
      <pubDate>Fri, 28 Aug 2009 00:21:12 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:1aeda441-6632-4bb1-a15a-c2fa8c2f181f</guid>
      <link>http://tarbard.co.uk/articles/2009/08/28/accessing-constants-in-jsf#comment-6191</link>
    </item>
  </channel>
</rss>
