001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.template.propertymanagers;
014:
015: import bsh.Interpreter;
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.wings.SComponent;
019: import org.wings.template.PropertyManager;
020: import org.wings.template.DefaultPropertyValueConverter;
021: import org.wings.template.PropertyValueConverter;
022: import org.wings.session.SessionManager;
023: import org.wings.session.Session;
024:
025: import java.lang.reflect.Method;
026: import java.util.HashMap;
027:
028: /**
029: * The default property handler for the <code>OBJECT</code> tags in STemplateLayout.
030: *
031: * @author (c) mercatis information systems gmbh, 1999-2002
032: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
033: */
034: public class DefaultPropertyManager implements PropertyManager {
035: /**
036: * Apache commons logger
037: */
038: private static final transient Log log = LogFactory
039: .getLog(DefaultPropertyManager.class);
040:
041: static final Class[] classes = { SComponent.class };
042:
043: public final HashMap propertyValueConverters = new HashMap();
044:
045: public static final DefaultPropertyValueConverter DEFAULT_PROPERTY_VALUE_CONVERTER = DefaultPropertyValueConverter.INSTANCE;
046:
047: /**
048: * Declare a value "true" for this string in web.xml to enable BeanScript support.
049: */
050: public final static String BEANSCRIPT_ENABLE = "wings.template.beanscript";
051:
052: public DefaultPropertyManager() {
053: }
054:
055: protected Interpreter createInterpreter() {
056: return new Interpreter();
057: }
058:
059: public void setProperty(SComponent comp, String name, String value) {
060: if ("SCRIPT".equals(name)) {
061: Session session = SessionManager.getSession();
062: final Boolean scriptEnabled = Boolean
063: .valueOf((String) session
064: .getProperty(BEANSCRIPT_ENABLE));
065:
066: if (scriptEnabled.booleanValue()) {
067: final Interpreter interpreter = createInterpreter();
068:
069: try {
070: log.debug("eval script " + value);
071:
072: interpreter.set("component", comp);
073: interpreter.set("session", session);
074:
075: interpreter.eval(value);
076: } catch (Exception ex) {
077: log.warn("Error on evaluating script " + value, ex);
078: }
079: } else {
080: log
081: .warn("BeanScript support not enabled. Define value 'true' for "
082: + "property "
083: + DefaultPropertyManager.BEANSCRIPT_ENABLE
084: + " in web.xml "
085: + "to enable BeanScript support!");
086: }
087: }
088:
089: Method[] methods = comp.getClass().getMethods();
090:
091: for (int i = 0; i < methods.length; i++) {
092: Method method = methods[i];
093:
094: if (method.getName().startsWith("set")
095: && name.equals(method.getName().substring(3)
096: .toUpperCase())
097: && method.getParameterTypes().length == 1) {
098:
099: Class paramType = method.getParameterTypes()[0];
100:
101: PropertyValueConverter valueConverter = getValueConverter(paramType);
102:
103: if (valueConverter != null) {
104: try {
105: method.setAccessible(true);
106: method.invoke(comp,
107: new Object[] { valueConverter
108: .convertPropertyValue(value,
109: paramType) });
110: return;
111: } catch (Exception ex) {
112: log.debug("Unable to invoke method "
113: + method.getName() + " due to ", ex);
114: }
115: }
116: }
117: } // end of for (int i=0; i<; i++)
118: }
119:
120: public void addPropertyValueConverter(
121: PropertyValueConverter valueConverter, Class clazz) {
122:
123: propertyValueConverters.put(clazz, valueConverter);
124: }
125:
126: protected PropertyValueConverter getValueConverter(Class clazz) {
127: if (clazz == null) {
128: return DEFAULT_PROPERTY_VALUE_CONVERTER;
129: }
130:
131: if (propertyValueConverters.containsKey(clazz)) {
132: return (PropertyValueConverter) propertyValueConverters
133: .get(clazz);
134: }
135:
136: return getValueConverter(clazz.getSuperclass());
137: }
138:
139: public Class[] getSupportedClasses() {
140: return classes;
141: }
142: }
|