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