01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.repository;
05:
06: import org.apache.xmlbeans.SchemaType;
07: import org.apache.xmlbeans.XmlObject;
08:
09: import com.tc.config.schema.listen.ConfigurationChangeListener;
10: import com.tc.config.schema.listen.ConfigurationChangeListenerSet;
11: import com.tc.util.Assert;
12:
13: /**
14: * A {@link BeanRepository} that selects out a child of the parent bean.
15: */
16: public class ChildBeanRepository implements BeanRepository,
17: ConfigurationChangeListener {
18:
19: private final BeanRepository parent;
20: private final Class requiredBeanClass;
21: private final ChildBeanFetcher childFetcher;
22: private final ConfigurationChangeListenerSet listeners;
23:
24: public ChildBeanRepository(BeanRepository parent,
25: Class requiredBeanClass, ChildBeanFetcher childFetcher) {
26: Assert.assertNotNull(parent);
27: Assert.assertNotNull(requiredBeanClass);
28: Assert.assertNotNull(childFetcher);
29:
30: this .parent = parent;
31: this .requiredBeanClass = requiredBeanClass;
32: this .childFetcher = childFetcher;
33: this .listeners = new ConfigurationChangeListenerSet();
34:
35: this .parent.addListener(this );
36: }
37:
38: public SchemaType rootBeanSchemaType() {
39: return StandardBeanRepository
40: .getTypeFieldFrom(this .requiredBeanClass);
41: }
42:
43: public void ensureBeanIsOfClass(Class theClass) {
44: Assert.eval(theClass.isAssignableFrom(requiredBeanClass));
45: }
46:
47: public XmlObject bean() {
48: XmlObject parentBean = this .parent.bean();
49: if (parentBean == null)
50: return null;
51: XmlObject out = this .childFetcher.getChild(parentBean);
52: if (out != null) {
53: Assert.eval("Child bean fetcher returned "
54: + (out == null ? "null" : "a " + out.getClass())
55: + ", not a " + this .requiredBeanClass,
56: this .requiredBeanClass.isInstance(out));
57: }
58: return out;
59: }
60:
61: public void addListener(ConfigurationChangeListener listener) {
62: Assert.assertNotNull(listener);
63: this .listeners.addListener(listener);
64: }
65:
66: public void configurationChanged(XmlObject oldConfig,
67: XmlObject newConfig) {
68: XmlObject oldChild = oldConfig == null ? null
69: : this .childFetcher.getChild(oldConfig);
70: XmlObject newChild = newConfig == null ? null
71: : this.childFetcher.getChild(newConfig);
72:
73: Assert.eval(newChild == null
74: || this.requiredBeanClass.isInstance(newChild));
75:
76: if (oldChild != newChild)
77: this.listeners.configurationChanged(oldChild, newChild);
78: }
79:
80: }
|