View Javadoc

1   /*
2    * Copyright 2002-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.clazz.common;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.clazz.Clazz;
23  import org.apache.commons.clazz.ClazzLoader;
24  import org.apache.commons.clazz.ModelClazzLoader;
25  
26  /**
27   * Aggregates multiple ClazzLoaders, which are invoked one after another
28   * according to the ChainOfResponsibility pattern.  Note that member clazz
29   * loaders are invoked in the order reverse to the order they were added. 
30   * 
31   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
32   * @version $Id: GroupClazzLoader.java 155436 2005-02-26 13:17:48Z dirkv $
33   */
34  public class GroupClazzLoader extends ClazzLoader {
35      /**
36       * List of member loaders. Note that the list is always iterated in the
37       * reverse direction, so the loaded add last is the one invoked first.
38       */
39      protected ArrayList loaders = new ArrayList();
40  
41      public GroupClazzLoader(ModelClazzLoader modelClazzLoader) {
42          super(modelClazzLoader);
43      }
44  
45      /**
46       * Returns true if the supplied loader "belongs" in the group and can be
47       * added to it.  The default implementation returns true.
48       */
49      public boolean canAddClazzLoader(ClazzLoader loader) {
50          return true;
51      }
52  
53      /**
54       * Adds a ClazzLoader to the group. ClazzLoaders added last are invoked
55       * first.  Before the group adds the loader to itself, it checks if any
56       * of its members are groups themselves and, if so, tries to add the new
57       * loader to those subgroups.  
58       */
59      public void addClazzLoader(ClazzLoader loader) {
60          for (int i = loaders.size(); --i >= 0;) {
61              ClazzLoader member = (ClazzLoader) loaders.get(i);
62              if (member instanceof GroupClazzLoader) {
63                  GroupClazzLoader group = (GroupClazzLoader) member;
64                  if (group.canAddClazzLoader(loader)) {
65                      group.addClazzLoader(loader);
66                      return;
67                  }
68              }
69          }
70          loaders.add(loader);
71      }
72  
73      /**
74       * Returns true iff this group has a member loader that has or can construct
75       * a Clazz for the supplied instance.
76       */
77      public boolean isMember(Object instance) {
78          // Note the reverse order
79          for (int i = loaders.size(); --i >= 0;) {
80              ClazzLoader loader = (ClazzLoader) loaders.get(i);
81              if (loader.isMember(instance)) {
82                  return true;
83              }
84          }
85          return false;
86      }
87  
88      public String getClazzName(Object instance) {
89          // Note the reverse order
90          for (int i = loaders.size(); --i >= 0;) {
91              ClazzLoader loader = (ClazzLoader) loaders.get(i);
92              String name = loader.getClazzName(instance);
93              if (name != null) {
94                  return name;
95              }
96          }
97          return null;
98      }
99  
100     /**
101      * Given a Clazz name, produces the corresponding Clazz by invoking member
102      * loaders one by one until the clazz is found.
103      */
104     public Clazz getClazzForName(String name) {
105         Clazz clazz = null;
106         // Note the reverse order
107         for (int i = loaders.size(); --i >= 0;) {
108             ClazzLoader loader = (ClazzLoader) loaders.get(i);
109             clazz = loader.getClazzForName(name);
110             if (clazz != null) {
111                 break;
112             }
113         }
114         return clazz;
115     }
116 
117     /**
118      * @see ClazzLoader#defineClazz(String, Class, Class)
119      */
120     public Clazz defineClazz(
121         String name,
122         Class clazzClass,
123         Class instanceClass) 
124     {
125         Clazz clazz = null;
126         // Note the reverse order
127         for (int i = loaders.size(); --i >= 0;) {
128             ClazzLoader loader = (ClazzLoader) loaders.get(i);
129             clazz = loader.defineClazz(name, clazzClass, instanceClass);
130             if (clazz != null) {
131                 break;
132             }
133         }
134         return clazz;
135     }
136 
137     /**
138      * Returns all clazz loaders registered with this group,
139      * in the order of priority
140      */
141     public List getClazzLoaders() {
142         return Collections.unmodifiableList(loaders);
143     }
144 
145     /**
146      * Returns clazzloaders matching the supplied Predicate
147      */
148 //    public List getClazzLoaders(Predicate predicate) {
149 //    }
150 }