001: /**
002: *
003: */package org.getahead.dwrdemo.stress;
004:
005: import java.lang.reflect.Field;
006: import java.lang.reflect.Modifier;
007: import java.util.Collection;
008: import java.util.HashMap;
009: import java.util.Map;
010:
011: import org.apache.commons.logging.LogFactory;
012: import org.apache.commons.logging.Log;
013: import org.directwebremoting.Container;
014: import org.directwebremoting.WebContextFactory;
015: import org.directwebremoting.util.LocalUtil;
016:
017: /**
018: * @author Joe Walker [joe at getahead dot ltd dot uk]
019: */
020: public class Settings {
021: /**
022: *
023: */
024: public String setValue(String name, String value) {
025: StringBuffer reply = new StringBuffer();
026:
027: Container container = WebContextFactory.get().getContainer();
028: Collection<String> beanNames = container.getBeanNames();
029: for (String beanName : beanNames) {
030: Object bean = container.getBean(beanName);
031:
032: Class<? extends Object> beanClass = bean.getClass();
033: if (beanClass == String.class) {
034: continue;
035: }
036:
037: try {
038: for (Field field : LocalUtil.getAllFields(beanClass)) {
039: if (!field.getName().equals(name)) {
040: continue;
041: }
042:
043: if (Modifier.isStatic(field.getModifiers())
044: || Modifier.isFinal(field.getModifiers())) {
045: continue;
046: }
047:
048: if (!LocalUtil.isTypeSimplyConvertable(field
049: .getType())) {
050: continue;
051: }
052:
053: Object converted = LocalUtil.simpleConvert(value,
054: field.getType());
055: field.setAccessible(true);
056: field.set(bean, converted);
057:
058: reply
059: .append("Set on " + beanClass.getName()
060: + "\n");
061: }
062: } catch (Exception ex) {
063: reply.append("Failed on " + beanClass.getName() + ": "
064: + ex + "\n");
065: }
066: }
067:
068: return reply.toString();
069: }
070:
071: /**
072: *
073: */
074: public Map<String, Setting> getSettingValues() {
075: Map<String, Setting> reply = new HashMap<String, Setting>();
076:
077: Container container = WebContextFactory.get().getContainer();
078: Collection<String> beanNames = container.getBeanNames();
079: for (String beanName : beanNames) {
080: Object bean = container.getBean(beanName);
081:
082: try {
083: Class<? extends Object> beanClass = bean.getClass();
084: if (beanClass == String.class) {
085: continue;
086: }
087:
088: for (Field field : LocalUtil.getAllFields(beanClass)) {
089: String name = field.getName();
090: if (name.equals("class")) {
091: continue;
092: }
093:
094: if (Modifier.isStatic(field.getModifiers())
095: || Modifier.isFinal(field.getModifiers())) {
096: continue;
097: }
098:
099: if (!LocalUtil.isTypeSimplyConvertable(field
100: .getType())) {
101: continue;
102: }
103:
104: Setting setting = reply.get(name);
105: if (setting == null) {
106: setting = new Setting(name);
107: reply.put(name, setting);
108: }
109:
110: setting.getClassNames().add(beanClass.getName());
111: field.setAccessible(true);
112: Object value = field.get(bean);
113: Object oldValue = setting.getValue();
114:
115: if (oldValue != null && value != null
116: && !value.equals(oldValue)) {
117: setting.setWritable(false);
118: setting.setValue(oldValue.toString() + " / "
119: + value);
120: } else {
121: setting.setValue(value);
122: }
123: }
124: } catch (Exception ex) {
125: log.warn("Failed to introspect: " + beanName, ex);
126: }
127: }
128:
129: return reply;
130: }
131:
132: /**
133: * The log stream
134: */
135: private static final Log log = LogFactory.getLog(Settings.class);
136: }
|