KickJava   Java API By Example, From Geeks To Geeks.

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


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 Feb 27, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan.containers;
15
16
17 import java.util.Iterator JavaDoc;
18 import java.util.LinkedHashMap JavaDoc;
19
20 import jfun.yan.AmbiguousComponentResolutionException;
21 import jfun.yan.Component;
22 import jfun.yan.ComponentMap;
23 import jfun.yan.Dependency;
24 import jfun.yan.Registrar;
25 import jfun.yan.SimpleDependency;
26 import jfun.yan.YanException;
27 import jfun.yan.util.ReflectionUtil;
28
29 /**
30  * A simple implementation of Registrar.
31  * It ensures the Component objects and instances are retrieved and
32  * created in the same order as the Component objects are registered.
33  * <br>
34  * This implementation enables auto-wiring.
35  * It uses {@link SimpleDependency} to resolve parameters and properties.
36  * <br>
37  * It is also thread-safe.
38  * <p>
39  * Codehaus.org.
40  *
41  * @author Ben Yu
42  *
43  */

44 public class SimpleRegistrar
45 implements Registrar, java.io.Serializable JavaDoc{
46   private final LinkedHashMap JavaDoc creators = new LinkedHashMap JavaDoc();
47   public synchronized int size(){
48     return creators.size();
49   }
50   public synchronized java.util.Set JavaDoc keys(){
51     return java.util.Collections.unmodifiableSet(creators.keySet());
52   }
53   public synchronized java.util.Collection JavaDoc getComponents(){
54     return java.util.Collections.unmodifiableCollection(creators.values());
55   }
56   public synchronized boolean containsKey(final Object JavaDoc key){
57     return creators.containsKey(key);
58   }
59   public synchronized Component getComponent(final Object JavaDoc key) {
60     return (Component)creators.get(key);
61   }
62   public synchronized boolean containsType(final Class JavaDoc type){
63     Component cc = getComponent(type);
64     if(cc!=null){
65       final Class JavaDoc ctype = cc.getType();
66       if(ctype == null
67           || ReflectionUtil.isAssignableFrom(type, ctype)){
68         //if the type is dynamically bound, we trust the key.
69
return true;
70       }
71       cc = null;
72     }
73     for(Iterator JavaDoc it=creators.values().iterator();it.hasNext();){
74       final Component cci = (Component)it.next();
75       final Class JavaDoc typei = cci.getType();
76       if(typei!=null && !void.class.equals(typei) &&
77           //void type does actively feed itself to a by-type search.
78
//It does suppress type error when forced to match against another type.
79
ReflectionUtil.isAssignableFrom(
80           type, typei)){
81         return true;
82       }
83     }
84     return false;
85   }
86   public synchronized Component getComponentOfType(final Class JavaDoc type){
87     Component cc = getComponent(type);
88     if(cc!=null){
89       final Class JavaDoc ctype = cc.getType();
90       if(ctype == null
91           || ReflectionUtil.isAssignableFrom(type, ctype)){
92         //if the type is dynamically bound, we trust the key.
93
return cc;
94       }
95       cc = null;
96     }
97     Class JavaDoc subtype = null;
98     for(Iterator JavaDoc it=creators.values().iterator();it.hasNext();){
99       final Component cci = (Component)it.next();
100       final Class JavaDoc typei = cci.getType();
101       if(typei!=null && !void.class.equals(typei) &&
102           //void type does actively feed itself to a by-type search.
103
//It does suppress type error when forced to match against another type.
104
ReflectionUtil.isAssignableFrom(
105           type, typei)){
106         if(subtype!=null){
107           throw
108             new AmbiguousComponentResolutionException(type, subtype, typei);
109         }
110         else{
111           subtype = typei;
112           cc = cci;
113         }
114       }
115     }
116     return cc;
117   }
118   public synchronized java.util.List JavaDoc getComponentsOfType(final Class JavaDoc type){
119     final java.util.ArrayList JavaDoc ret = new java.util.ArrayList JavaDoc();
120     for(Iterator JavaDoc it=creators.values().iterator();it.hasNext();){
121       final Component cci = (Component)it.next();
122       final Class JavaDoc typei = cci.getType();
123       if(typei!=null &&
124           !void.class.equals(typei) && ReflectionUtil.isAssignableFrom(
125           type, typei)){
126         ret.add(cci);
127       }
128     }
129     return ret;
130   }
131   public synchronized void unregisterComponentsOfType(Class JavaDoc type){
132     for(Iterator JavaDoc it=creators.values().iterator();it.hasNext();){
133       final Component cci = (Component)it.next();
134       final Class JavaDoc typei = cci.getType();
135       if(typei!=null && ReflectionUtil.isAssignableFrom(
136           type, typei)){
137         it.remove();
138       }
139     }
140   }
141   public synchronized void registerComponent(Object JavaDoc key,
142       Component cc) {
143     creators.put(key, cc);
144   }
145
146   public synchronized void unregisterComponent(Object JavaDoc key) {
147     creators.remove(key);
148   }
149   public synchronized void verify(final ComponentMap cmap) {
150     for(Iterator JavaDoc it=creators.keySet().iterator();it.hasNext();){
151       final Object JavaDoc ki = it.next();
152       try{
153         final Component cc = (Component)creators.get(ki);
154         cc.verify(cmap.getDependency(ki, cmap));
155       }
156       catch(YanException e){
157         e.push("verify <" + ki +">");
158         throw e;
159       }
160     }
161   }
162   /**
163    * This uses SimpleDependency which resolves parameters and
164    * arguments by type.
165    * Different resolution policy can be provided by overriding this method.
166    */

167   public synchronized Dependency getDependency(final Object JavaDoc ckey
168       ,final ComponentMap cmap){
169     return new SimpleDependency(ckey, cmap);
170   }
171   /**
172    * This uses SimpleDependency which resolves parameters and
173    * arguments by type.
174    * Different resolution policy can be provided by overriding this method.
175    */

176   public synchronized Dependency getDependencyOfType(
177       final Class JavaDoc type, final ComponentMap cmap){
178     return new SimpleDependency(type, cmap);
179   }
180   public synchronized boolean equals(Object JavaDoc obj) {
181     if(obj instanceof SimpleRegistrar){
182       final SimpleRegistrar sc2 = (SimpleRegistrar)obj;
183       return creators.equals(sc2.creators);
184     }
185     else return false;
186   }
187   public synchronized int hashCode() {
188     return creators.hashCode();
189   }
190   public synchronized String JavaDoc toString() {
191     return creators.toString();
192   }
193 }
194
Popular Tags