001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema;
005:
006: import org.apache.commons.lang.ClassUtils;
007: import org.apache.xmlbeans.XmlObject;
008:
009: import com.tc.config.schema.context.ConfigContext;
010: import com.tc.config.schema.dynamic.ConfigItem;
011: import com.tc.config.schema.dynamic.ConfigItemListener;
012: import com.tc.logging.TCLogger;
013: import com.tc.logging.TCLogging;
014: import com.tc.util.Assert;
015:
016: import java.lang.reflect.Array;
017:
018: /**
019: * A base class for all new config objects.
020: */
021: public class BaseNewConfigObject implements NewConfig {
022:
023: private static final TCLogger logger = TCLogging
024: .getLogger(BaseNewConfigObject.class);
025:
026: protected final ConfigContext context;
027:
028: public BaseNewConfigObject(ConfigContext context) {
029: Assert.assertNotNull(context);
030: this .context = context;
031: }
032:
033: private static class IgnoringConfigItemListener implements
034: ConfigItemListener {
035: private final ConfigItem item;
036:
037: public IgnoringConfigItemListener(ConfigItem item) {
038: Assert.assertNotNull(item);
039: this .item = item;
040: }
041:
042: public void valueChanged(Object oldValue, Object newValue) {
043: logger
044: .warn("The attempt to change the value of "
045: + item
046: + " from "
047: + oldValue
048: + " to "
049: + newValue
050: + " was ignored; runtime changes in this configuration value are not yet supported.");
051: }
052: }
053:
054: public void changesInItemIgnored(ConfigItem item) {
055: Assert.assertNotNull(item);
056: item.addListener(new IgnoringConfigItemListener(item));
057: }
058:
059: private class ForbiddenConfigItemListener implements
060: ConfigItemListener {
061: private final ConfigItem item;
062:
063: public ForbiddenConfigItemListener(ConfigItem item) {
064: Assert.assertNotNull(item);
065: this .item = item;
066: }
067:
068: private boolean isEqual(Object one, Object two) {
069: if (one != null
070: && two != null
071: && one.getClass().isArray()
072: && two.getClass().isArray()
073: && one.getClass().getComponentType().equals(
074: two.getClass().getComponentType())) {
075: if (Array.getLength(one) != Array.getLength(two))
076: return false;
077:
078: for (int i = 0; i < Array.getLength(one); ++i) {
079: if (!isEqual(Array.get(one, i), Array.get(two, i)))
080: return false;
081: }
082:
083: return true;
084: } else if (one != null && two != null) {
085: return one.equals(two);
086: } else
087: return one == two;
088: }
089:
090: public void valueChanged(Object oldValue, Object newValue) {
091: if (oldValue == null)
092: return;
093: if (newValue != null && isEqual(oldValue, newValue))
094: return;
095:
096: context.illegalConfigurationChangeHandler().changeFailed(
097: item, oldValue, newValue);
098: }
099: }
100:
101: public void changesInItemForbidden(ConfigItem item) {
102: Assert.assertNotNull(item);
103: item.addListener(new ForbiddenConfigItemListener(item));
104: }
105:
106: public String toString() {
107: return ClassUtils.getShortClassName(getClass())
108: + " around bean:\n" + context.bean();
109: }
110:
111: public XmlObject getBean() {
112: return this.context.bean();
113: }
114:
115: }
|