Well, for whatever reason, this seemed kind of entertaining (sad, huh?) so I
fleshed it out a bit and actually gave it a test run. Since both JSTL and Struts
taglibs can deal with a Map (JSTL doesn't have a clue about DynaBeans), that
seemed like a better way to go. So, the reflection can be done by a couple of
different methods:

public static DynaBean getConstantFieldsAsBean(Class cls)
                       throws IllegalAccessException,
                              InstantiationException,
                              InvocationTargetException

public static Map getConstantFieldsAsMap(Class cls)
                  throws IllegalAccessException

I added a context-param element to web.xml:

<context-param>
  <param-name>com.dotech.CLASS_NAMES</param-name>
 
<param-value>javax.servlet.http.HttpServletResponse,org.apache.struts.Globals</param-value>
</context-param>

along with a listener element:

<listener>
  <listener-class>com.dotech.ConstantsContextListener</listener-class>
</listener>

The listener parses class names out of the context init param, invokes the
utility method to create a Map of its constants, and then stores the Map in
application scope using the class name as the key.

For the Struts taglibs, the following kinds of access worked fine:

<bean:write name="org.apache.struts.Globals"
            property="ERROR_KEY"/>
<bean:define id="strutsGlobals"
             name="org.apache.struts.Globals" \
             scope="application"/>
<bean:write name="strutsGlobals"
            property="ERROR_KEY"/></p>

For JSTL:

<c:out value="${applicationScope['org.apache.struts.Globals'].ERROR_KEY}"/>
<c:out value="${applicationScope['org.apache.struts.Globals']['ERROR_KEY']}"/>
<c:set var="strutsGlobals"
       value="${applicationScope['org.apache.struts.Globals']}"/>
<c:out value="${strutsGlobals.ERROR_KEY}"/>
<c:out value="${strutsGlobals['ERROR_KEY']}"/>

I have no idea if that's of help to anyone, but if you want more details I'd be
happy to follow-up.

Quoting Kris Schneider <[EMAIL PROTECTED]>:

> Sounds like a job for a beanutils mechanism that does something like 
> (untested):
> 
> Class clazz = GLOBALS.class;
> String className = clazz.getName();
> 
> Map propMap = new HashMap();
> List dynaProps = new ArrayList();
> Field[] allFields = clazz.getDeclaredFields();
> for (int i = 0, n = allFields.length; i < n; i++) {
>    Field f = allFields[i];
>    int mods = f.getModifiers();
>    if (Modifier.isPublic(mods) &&
>        Modifier.isStatic(mods) &&
>        Modifier.isFinal(mods)) {
>      String name = f.getName();
>      Class type = f.getType();
>      Object value = f.get(null);
>      DynaProperty prop = new DynaProperty(name, type);
>      dynaProps.add(prop);
>      propMap.put(name, value);
>    }
> }
> 
> DynaProperty[] props = new DynaProperty[dynaProps.size()];
> dynaProps.toArray(props);
> 
> BasicDynaClass dynaClass = new BasicDynaClass(className, null, props);
> DynaBean dynaBean = dynaClass.newInstance();
> BeanUtils.populate(dynaBean, propMap);
> servletCtx.setAttribute(className, dynaBean);
> 
> Which gets you a DynaBean with all the constants exposed as properties. 
> Lots of other twists possible...
> 
> Eddie Bush wrote:
> > ... but can you even do that?  Can you use class-static methods without 
> > an instance of the class?
> > 
> > I've toyed with the idea of adding getters for constants into the 
> > Globals class, and then placing an instance of that class into 
> > application-scope.  What name should a person use though?  If you use 
> > the typical method of doing this you wind up with a chicken and egg 
> > scenario in your pages.  Of course, if we feel pretty satisfied that the 
> > location of the Globals probably isn't going to change again we could 
> > probably just decide that they will always be available under 
> > "org.apache.struts.GLOBALS" and be done with it.  You could then get 
> > ahold of the instance easily - and use that instance to reference other 
> > objects.
> > 
> > Incidentally, I don't believe there are any getters in the Globals class.
> > 
> > David Graham wrote:
> > 
> >> I see, so I have to do constants.getMyKey().  I guess that's easy
> enough.
> >>
> >> David 
> > 
> > 
> 
> -- 
> Kris Schneider <mailto:[EMAIL PROTECTED]>
> D.O.Tech       <http://www.dotech.com/>
> 
> 
> --
> To unsubscribe, e-mail:  
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> 


-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to