KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > repository > ChildBeanRepository


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.repository;
5
6 import org.apache.xmlbeans.SchemaType;
7 import org.apache.xmlbeans.XmlObject;
8
9 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, ConfigurationChangeListener {
17
18   private final BeanRepository parent;
19   private final Class JavaDoc requiredBeanClass;
20   private final ChildBeanFetcher childFetcher;
21   private final ConfigurationChangeListenerSet listeners;
22
23   public ChildBeanRepository(BeanRepository parent, Class JavaDoc requiredBeanClass, ChildBeanFetcher childFetcher) {
24     Assert.assertNotNull(parent);
25     Assert.assertNotNull(requiredBeanClass);
26     Assert.assertNotNull(childFetcher);
27
28     this.parent = parent;
29     this.requiredBeanClass = requiredBeanClass;
30     this.childFetcher = childFetcher;
31     this.listeners = new ConfigurationChangeListenerSet();
32
33     this.parent.addListener(this);
34   }
35
36   public SchemaType rootBeanSchemaType() {
37     return StandardBeanRepository.getTypeFieldFrom(this.requiredBeanClass);
38   }
39
40   public void ensureBeanIsOfClass(Class JavaDoc theClass) {
41     Assert.eval(theClass.isAssignableFrom(requiredBeanClass));
42   }
43
44   public XmlObject bean() {
45     XmlObject parentBean = this.parent.bean();
46     if (parentBean == null) return null;
47     XmlObject out = this.childFetcher.getChild(parentBean);
48     if (out != null) {
49       Assert.eval("Child bean fetcher returned " + (out == null ? "null" : "a " + out.getClass()) + ", not a "
50                   + this.requiredBeanClass, this.requiredBeanClass.isInstance(out));
51     }
52     return out;
53   }
54
55   public void addListener(ConfigurationChangeListener listener) {
56     Assert.assertNotNull(listener);
57     this.listeners.addListener(listener);
58   }
59
60   public void configurationChanged(XmlObject oldConfig, XmlObject newConfig) {
61     XmlObject oldChild = oldConfig == null ? null : this.childFetcher.getChild(oldConfig);
62     XmlObject newChild = newConfig == null ? null : this.childFetcher.getChild(newConfig);
63
64     Assert.eval(newChild == null || this.requiredBeanClass.isInstance(newChild));
65
66     if (oldChild != newChild) this.listeners.configurationChanged(oldChild, newChild);
67   }
68
69 }
70
Popular Tags