KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > BaseNewConfigObject


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
17
18 /**
19  * A base class for all new config objects.
20  */

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 JavaDoc oldValue, Object JavaDoc 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 JavaDoc one, Object JavaDoc 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 JavaDoc oldValue, Object JavaDoc 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 JavaDoc 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