Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GroupClazzLoader |
|
| 2.75;2.75 |
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 | 0 | protected ArrayList loaders = new ArrayList(); |
40 | ||
41 | public GroupClazzLoader(ModelClazzLoader modelClazzLoader) { | |
42 | 0 | super(modelClazzLoader); |
43 | 0 | } |
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 | 0 | 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 | 0 | for (int i = loaders.size(); --i >= 0;) { |
61 | 0 | ClazzLoader member = (ClazzLoader) loaders.get(i); |
62 | 0 | if (member instanceof GroupClazzLoader) { |
63 | 0 | GroupClazzLoader group = (GroupClazzLoader) member; |
64 | 0 | if (group.canAddClazzLoader(loader)) { |
65 | 0 | group.addClazzLoader(loader); |
66 | 0 | return; |
67 | } | |
68 | } | |
69 | 0 | } |
70 | 0 | loaders.add(loader); |
71 | 0 | } |
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 | 0 | for (int i = loaders.size(); --i >= 0;) { |
80 | 0 | ClazzLoader loader = (ClazzLoader) loaders.get(i); |
81 | 0 | if (loader.isMember(instance)) { |
82 | 0 | return true; |
83 | } | |
84 | 0 | } |
85 | 0 | return false; |
86 | } | |
87 | ||
88 | public String getClazzName(Object instance) { | |
89 | // Note the reverse order | |
90 | 0 | for (int i = loaders.size(); --i >= 0;) { |
91 | 0 | ClazzLoader loader = (ClazzLoader) loaders.get(i); |
92 | 0 | String name = loader.getClazzName(instance); |
93 | 0 | if (name != null) { |
94 | 0 | return name; |
95 | } | |
96 | 0 | } |
97 | 0 | 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 | 0 | Clazz clazz = null; |
106 | // Note the reverse order | |
107 | 0 | for (int i = loaders.size(); --i >= 0;) { |
108 | 0 | ClazzLoader loader = (ClazzLoader) loaders.get(i); |
109 | 0 | clazz = loader.getClazzForName(name); |
110 | 0 | if (clazz != null) { |
111 | 0 | break; |
112 | } | |
113 | 0 | } |
114 | 0 | 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 | 0 | Clazz clazz = null; |
126 | // Note the reverse order | |
127 | 0 | for (int i = loaders.size(); --i >= 0;) { |
128 | 0 | ClazzLoader loader = (ClazzLoader) loaders.get(i); |
129 | 0 | clazz = loader.defineClazz(name, clazzClass, instanceClass); |
130 | 0 | if (clazz != null) { |
131 | 0 | break; |
132 | } | |
133 | 0 | } |
134 | 0 | 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 | 0 | return Collections.unmodifiableList(loaders); |
143 | } | |
144 | ||
145 | /** | |
146 | * Returns clazzloaders matching the supplied Predicate | |
147 | */ | |
148 | // public List getClazzLoaders(Predicate predicate) { | |
149 | // } | |
150 | } |