1 4 package com.tc.config.schema; 5 6 import org.apache.commons.lang.ClassUtils; 7 import org.apache.xmlbeans.XmlObject; 8 9 import com.tc.config.schema.context.ConfigContext; 10 import com.tc.config.schema.dynamic.ConfigItem; 11 import com.tc.config.schema.dynamic.ConfigItemListener; 12 import com.tc.logging.TCLogger; 13 import com.tc.logging.TCLogging; 14 import com.tc.util.Assert; 15 16 import java.lang.reflect.Array ; 17 18 21 public class BaseNewConfigObject implements NewConfig { 22 23 private static final TCLogger logger = TCLogging.getLogger(BaseNewConfigObject.class); 24 25 protected final ConfigContext context; 26 27 public BaseNewConfigObject(ConfigContext context) { 28 Assert.assertNotNull(context); 29 this.context = context; 30 } 31 32 private static class IgnoringConfigItemListener implements ConfigItemListener { 33 private final ConfigItem item; 34 35 public IgnoringConfigItemListener(ConfigItem item) { 36 Assert.assertNotNull(item); 37 this.item = item; 38 } 39 40 public void valueChanged(Object oldValue, Object newValue) { 41 logger.warn("The attempt to change the value of " + item + " from " + oldValue + " to " + newValue 42 + " was ignored; runtime changes in this configuration value are not yet supported."); 43 } 44 } 45 46 public void changesInItemIgnored(ConfigItem item) { 47 Assert.assertNotNull(item); 48 item.addListener(new IgnoringConfigItemListener(item)); 49 } 50 51 private class ForbiddenConfigItemListener implements ConfigItemListener { 52 private final ConfigItem item; 53 54 public ForbiddenConfigItemListener(ConfigItem item) { 55 Assert.assertNotNull(item); 56 this.item = item; 57 } 58 59 private boolean isEqual(Object one, Object two) { 60 if (one != null && two != null && one.getClass().isArray() && two.getClass().isArray() 61 && one.getClass().getComponentType().equals(two.getClass().getComponentType())) { 62 if (Array.getLength(one) != Array.getLength(two)) return false; 63 64 for (int i = 0; i < Array.getLength(one); ++i) { 65 if (!isEqual(Array.get(one, i), Array.get(two, i))) return false; 66 } 67 68 return true; 69 } else if (one != null && two != null) { 70 return one.equals(two); 71 } else return one == two; 72 } 73 74 public void valueChanged(Object oldValue, Object newValue) { 75 if (oldValue == null) return; 76 if (newValue != null && isEqual(oldValue, newValue)) return; 77 78 context.illegalConfigurationChangeHandler().changeFailed(item, oldValue, newValue); 79 } 80 } 81 82 public void changesInItemForbidden(ConfigItem item) { 83 Assert.assertNotNull(item); 84 item.addListener(new ForbiddenConfigItemListener(item)); 85 } 86 87 public String toString() { 88 return ClassUtils.getShortClassName(getClass()) + " around bean:\n" + context.bean(); 89 } 90 91 public XmlObject getBean() { 92 return this.context.bean(); 93 } 94 95 } 96 | Popular Tags |