KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > containers > InheritedRegistrar


1 /*****************************************************************************
2  * Copyright (C) Codehaus.org. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Mar 1, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan.containers;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.LinkedHashSet JavaDoc;
18
19 import jfun.yan.Component;
20 import jfun.yan.ComponentMap;
21 import jfun.yan.Dependency;
22 import jfun.yan.Registrar;
23
24 /**
25  * This class manages parent-child relationship between
26  * two conainers.
27  * In this container, components in the child container
28  * can use those from parent container as dependency.
29  * But components from parent container cannot see
30  * those from child containers.
31  * <p>
32  * Codehaus.org.
33  *
34  * @author Ben Yu
35  *
36  */

37 public class InheritedRegistrar implements Registrar {
38   private final Registrar parent;
39   private final Registrar child;
40
41   /**
42    * Get the child container.
43    * @return the child container.
44    */

45   public Registrar getChild() {
46     return child;
47   }
48   /**
49    * Get the parent container.
50    * @return the parent container.
51    */

52   public Registrar getParent() {
53     return parent;
54   }
55   /**
56    * Create an InheritedRegistrar object.
57    * The parent container and the child container are not modified.
58    * @param parent the parent container.
59    * @param child the child contaiiner.
60    */

61   public InheritedRegistrar(final Registrar parent, final Registrar child) {
62     this.parent = parent;
63     this.child = child;
64   }
65
66   /**
67    * registers the component to the child container.
68    */

69   public void registerComponent(Object JavaDoc key, Component cc) {
70     child.registerComponent(key, cc);
71   }
72   /**
73    * unregisters components of a provided type from child container.
74    */

75   public void unregisterComponentsOfType(Class JavaDoc type){
76     child.unregisterComponentsOfType(type);
77   }
78   /**
79    * unregisters a component identified by a key
80    * from child container.
81    * Nothing is done if the key is not found in the child container.
82    */

83   public void unregisterComponent(Object JavaDoc key) {
84     child.unregisterComponent(key);
85     //parent.unregisterComponent(key);
86
}
87   public boolean containsKey(Object JavaDoc key){
88     return child.containsKey(key) || parent.containsKey(key);
89   }
90   public boolean containsType(Class JavaDoc type){
91     return child.containsType(type) || parent.containsType(type);
92   }
93   /**
94    * retrieves a component identified by a key.
95    * The key is first looked up in the child container,
96    * if not found, the parent container is looked up.
97    */

98   public Component getComponent(Object JavaDoc key) {
99     Component cc = child.getComponent(key);
100     if(cc==null) return parent.getComponent(key);
101     else return cc;
102   }
103   /**
104    * retrieves a component of a provided type.
105    * the child container is first looked up,
106    * if no such component is found, the parent container is looked up.
107    */

108   public Component getComponentOfType(Class JavaDoc type){
109     Component c1 = getComponent(type);
110     if(c1!=null) return c1;
111     c1 = child.getComponentOfType(type);
112     if(c1!=null) return c1;
113     else return parent.getComponentOfType(type);
114   }
115   /**
116    * retrieves all components of a provided type
117    * from both the parent container and the child container.
118    * <br>
119    * components from the parent container are placed
120    * before those from child container.
121    */

122   public java.util.List JavaDoc getComponentsOfType(Class JavaDoc type){
123     final java.util.List JavaDoc ret = new ArrayList JavaDoc(parent.getComponentsOfType(type));
124     ret.addAll(child.getComponentsOfType(type));
125     return ret;
126   }
127
128   /**
129    * gets all components from both the parent container and
130    * the child container.
131    * <p>
132    * Components from the parent container are placed before
133    * those from the child container.
134    */

135   public java.util.Collection JavaDoc getComponents(){
136     final java.util.List JavaDoc ret = new ArrayList JavaDoc(parent.getComponents());
137     ret.addAll(child.getComponents());
138     return ret;
139   }
140   /**
141    * Gets the Dependency object for a component of
142    * a provided type.
143    * <br>
144    * If the component type can be found in the child container,
145    * the dependency is looked up in the child container.
146    * Otherwise, parent container is looked up and the parent container
147    * is used to resolve all cascaded dependencies.
148    */

149   public Dependency getDependencyOfType(Class JavaDoc type, ComponentMap cmap){
150     if(type==null || child.containsType(type))
151       return child.getDependencyOfType(type, cmap);
152     else return parent.getDependencyOfType(type, parent);
153   }
154   /**
155    * Gets the Dependency object for a component identified by a key.
156    * <br>
157    * If the component key can be found in the child container,
158    * the dependency is looked up in the child container.
159    * Otherwise, parent container is looked up and the parent container
160    * is used to resolve all cascaded dependencies.
161    */

162   public Dependency getDependency(Object JavaDoc key, ComponentMap cmap){
163      //we do not use SimpleDependency because the parent and child may provide additional functionality.
164
if(key==null || child.containsKey(key))
165       return child.getDependency(key, cmap);
166     else
167       return parent.getDependency(key, parent);
168     /*
169     final Dependency ac1 = child.getDependency(key, cmap);
170     final Dependency ac2 = parent.getDependency(key, parent);
171     final ComponentMap outer = this;
172     return new Dependency(){
173       public ComponentMap getComponentMap(){return outer;}
174       public Object getArgument(int i, Class type){
175         try{
176           return ac1.getArgument(i, type);
177         }
178         catch(UnresolvedComponentException e){
179           return ac2.getArgument(i, type);
180         }
181       }
182       public Class verifyArgument(int i, Class type){
183         try{
184           return ac1.verifyArgument(i, type);
185         }
186         catch(UnresolvedComponentException e){
187           return ac2.verifyArgument(i, type);
188         }
189       }
190       public Object getProperty(Object k, Class type){
191         try{
192           return ac1.getProperty(k, type);
193         }
194         catch(UnresolvedComponentException e){
195           return ac2.getProperty(k, type);
196         }
197       }
198       public Class verifyProperty(Object k, Class type){
199         try{
200           return ac1.verifyProperty(k, type);
201         }
202         catch(UnresolvedComponentException e){
203           return ac2.verifyProperty(k, type);
204         }
205       }
206       public Object getContext(){
207         return ac1.getContext();
208       }
209     };*/

210   }
211   /**
212    * Verifies all components in this container.
213    * Components in the child container use the provided ComponentMap
214    * to resolve dependency; while those from the parent container
215    * use the parent container itself for resolving dependency.
216    */

217   public void verify(ComponentMap cmap) {
218     child.verify(cmap);
219     parent.verify(parent);
220   }
221
222
223   /*
224   public Object getComponent(Object key, ComponentMap cmap) {
225     try{
226       return child.getComponent(key, cmap);
227     }
228     catch(KeyResolutionException e){
229       return parent.getComponent(key, cmap);
230     }
231   }*/

232   /**
233    * Get the component keys from both parent and child containers.
234    * Keys from the parent container are placed before those
235    * from the child container.
236    */

237   public java.util.Set JavaDoc keys(){
238    final LinkedHashSet JavaDoc hset = new LinkedHashSet JavaDoc(parent.keys());
239    hset.addAll(child.keys());
240    return hset;
241   }
242   
243   public boolean equals(Object JavaDoc obj) {
244     if(obj instanceof InheritedRegistrar){
245       final InheritedRegistrar ic2 = (InheritedRegistrar)obj;
246       return parent.equals(ic2.parent) && child.equals(ic2.child);
247     }
248     else return false;
249   }
250   public int hashCode() {
251     return parent.hashCode()*31 + child.hashCode();
252   }
253   public String JavaDoc toString() {
254     return child.toString()+":"+parent;
255   }
256 }
257
Popular Tags