1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.api.resourceloader; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import javax.xml.namespace.QName; |
24 | |
|
25 | |
import org.apache.commons.lang.ObjectUtils; |
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.rice.core.api.lifecycle.BaseLifecycle; |
28 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
29 | |
import org.kuali.rice.core.api.resourceloader.ResourceLoader; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ResourceLoaderContainer extends BaseLifecycle implements ResourceLoader { |
40 | |
|
41 | 0 | private static final Logger LOG = Logger.getLogger(ResourceLoaderContainer.class); |
42 | |
|
43 | |
private QName name; |
44 | |
|
45 | 0 | private List<ResourceLoader> resourceLoaders = new ArrayList<ResourceLoader>(); |
46 | |
|
47 | 0 | public ResourceLoaderContainer(QName name) { |
48 | 0 | this.name = name; |
49 | 0 | } |
50 | |
|
51 | |
public void start() throws Exception { |
52 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
53 | 0 | LOG.info("Starting ResourceLoader " + resourceLoader.getName()); |
54 | 0 | resourceLoader.start(); |
55 | |
} |
56 | 0 | super.start(); |
57 | 0 | } |
58 | |
|
59 | |
public void stop() throws Exception { |
60 | 0 | while (!this.resourceLoaders.isEmpty()) { |
61 | 0 | ResourceLoader rl = this.resourceLoaders.get(this.resourceLoaders.size() - 1); |
62 | 0 | rl.stop(); |
63 | 0 | this.resourceLoaders.remove(rl); |
64 | 0 | } |
65 | |
|
66 | 0 | super.stop(); |
67 | 0 | this.resourceLoaders.clear(); |
68 | 0 | } |
69 | |
|
70 | |
public void addResourceLoader(ResourceLoader resourceLoader) { |
71 | 0 | this.resourceLoaders.add(resourceLoader); |
72 | |
|
73 | |
|
74 | 0 | moveKSBLoadersDownHack(); |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
private void moveKSBLoadersDownHack() { |
79 | |
|
80 | 0 | for (int i = 0; i < resourceLoaders.size(); i++) { |
81 | 0 | final boolean remote = resourceLoaders.get(i).getName().toString().toUpperCase().contains("KSB"); |
82 | 0 | if (remote) { |
83 | 0 | final ResourceLoader removed = this.resourceLoaders.remove(i); |
84 | 0 | this.resourceLoaders.add(this.resourceLoaders.size(), removed); |
85 | |
} |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
public void addResourceLoaderFirst(ResourceLoader resourceLoader) { |
90 | 0 | this.resourceLoaders.add(0, resourceLoader); |
91 | |
|
92 | |
|
93 | 0 | moveKSBLoadersDownHack(); |
94 | 0 | } |
95 | |
|
96 | |
public boolean containsResourceLoader(ResourceLoader resourceLoader) { |
97 | 0 | return this.resourceLoaders.contains(resourceLoader); |
98 | |
} |
99 | |
|
100 | |
public ResourceLoader getResourceLoader(QName name) { |
101 | |
|
102 | 0 | if (this.getName().equals(name)) { |
103 | 0 | return this; |
104 | |
} |
105 | |
|
106 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
107 | 0 | ResourceLoader loader = iter.next(); |
108 | 0 | if (loader.getName().equals(name)) { |
109 | 0 | return loader; |
110 | |
} |
111 | 0 | ResourceLoader loader2 = loader.getResourceLoader(name); |
112 | 0 | if (loader2 != null) { |
113 | 0 | return loader2; |
114 | |
} |
115 | 0 | } |
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | |
public List<QName> getResourceLoaderNames() { |
120 | 0 | List<QName> names = new ArrayList<QName>(); |
121 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
122 | 0 | names.add(iter.next().getName()); |
123 | |
} |
124 | 0 | return names; |
125 | |
} |
126 | |
|
127 | |
public void removeAllResourceLoaders() { |
128 | 0 | this.resourceLoaders.clear(); |
129 | 0 | } |
130 | |
|
131 | |
public void removeResourceLoader(QName name) { |
132 | 0 | ResourceLoader loaderToRemove = null; |
133 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
134 | 0 | ResourceLoader loader = iter.next(); |
135 | 0 | if (loader.getName().equals(name)) { |
136 | 0 | loaderToRemove = loader; |
137 | |
} |
138 | 0 | } |
139 | 0 | if (loaderToRemove != null) { |
140 | |
try { |
141 | 0 | loaderToRemove.stop(); |
142 | 0 | } catch (Exception e) { |
143 | 0 | LOG.error("Failed to stop plugin " + loaderToRemove.getName() + " on removal", e); |
144 | 0 | } |
145 | 0 | this.resourceLoaders.remove(loaderToRemove); |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
public List<ResourceLoader> getResourceLoaders() { |
150 | 0 | return this.resourceLoaders; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
public Object getObject(ObjectDefinition definition) { |
156 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
157 | 0 | Object object = resourceLoader.getObject(definition); |
158 | 0 | if (object != null) { |
159 | 0 | return object; |
160 | |
} |
161 | 0 | } |
162 | 0 | return null; |
163 | |
} |
164 | |
|
165 | |
public Object getService(QName qname) { |
166 | 0 | if (LOG.isDebugEnabled()) { |
167 | 0 | LOG.debug("ResourceLoader " + getName() + " fetching service " + qname); |
168 | |
} |
169 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
170 | 0 | if (LOG.isDebugEnabled()) { |
171 | 0 | LOG.debug("Delegating fetch to " + this); |
172 | |
} |
173 | 0 | Object service = resourceLoader.getService(qname); |
174 | 0 | if (service != null) { |
175 | 0 | if (LOG.isDebugEnabled()) { |
176 | 0 | LOG.debug("Found service from " + this); |
177 | |
} |
178 | 0 | return service; |
179 | |
} |
180 | 0 | } |
181 | 0 | return null; |
182 | |
} |
183 | |
|
184 | |
public String getContents(String indent, boolean servicePerLine) { |
185 | 0 | String contents = indent + this + "\n"; |
186 | |
|
187 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
188 | 0 | contents += resourceLoader.getContents(indent + "+++", servicePerLine); |
189 | |
} |
190 | |
|
191 | 0 | return contents; |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public String toString() { |
196 | 0 | return "Resource Loader: " + this.name + " (" + ObjectUtils.identityToString(this) + ") direct children resource loaders size: " + this.resourceLoaders.size(); |
197 | |
} |
198 | |
|
199 | |
public QName getName() { |
200 | 0 | return this.name; |
201 | |
} |
202 | |
|
203 | |
public void setName(QName name) { |
204 | 0 | this.name = name; |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
} |