1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.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.lifecycle.BaseLifecycle; |
28 | |
import org.kuali.rice.core.reflect.ObjectDefinition; |
29 | |
import org.kuali.rice.core.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 | 0 | } |
73 | |
|
74 | |
public void addResourceLoaderFirst(ResourceLoader resourceLoader) { |
75 | 0 | this.resourceLoaders.add(0, resourceLoader); |
76 | 0 | } |
77 | |
|
78 | |
public boolean containsResourceLoader(ResourceLoader resourceLoader) { |
79 | 0 | return this.resourceLoaders.contains(resourceLoader); |
80 | |
} |
81 | |
|
82 | |
public ResourceLoader getResourceLoader(QName name) { |
83 | |
|
84 | 0 | if (this.getName().equals(name)) { |
85 | 0 | return this; |
86 | |
} |
87 | |
|
88 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
89 | 0 | ResourceLoader loader = iter.next(); |
90 | 0 | if (loader.getName().equals(name)) { |
91 | 0 | return loader; |
92 | |
} |
93 | 0 | ResourceLoader loader2 = loader.getResourceLoader(name); |
94 | 0 | if (loader2 != null) { |
95 | 0 | return loader2; |
96 | |
} |
97 | 0 | } |
98 | 0 | return null; |
99 | |
} |
100 | |
|
101 | |
public List<QName> getResourceLoaderNames() { |
102 | 0 | List<QName> names = new ArrayList<QName>(); |
103 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
104 | 0 | names.add(iter.next().getName()); |
105 | |
} |
106 | 0 | return names; |
107 | |
} |
108 | |
|
109 | |
public void removeAllResourceLoaders() { |
110 | 0 | this.resourceLoaders.clear(); |
111 | 0 | } |
112 | |
|
113 | |
public void removeResourceLoader(QName name) { |
114 | 0 | ResourceLoader loaderToRemove = null; |
115 | 0 | for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) { |
116 | 0 | ResourceLoader loader = iter.next(); |
117 | 0 | if (loader.getName().equals(name)) { |
118 | 0 | loaderToRemove = loader; |
119 | |
} |
120 | 0 | } |
121 | 0 | if (loaderToRemove != null) { |
122 | |
try { |
123 | 0 | loaderToRemove.stop(); |
124 | 0 | } catch (Exception e) { |
125 | 0 | LOG.error("Failed to stop plugin " + loaderToRemove.getName() + " on removal", e); |
126 | 0 | } |
127 | 0 | this.resourceLoaders.remove(loaderToRemove); |
128 | |
} |
129 | 0 | } |
130 | |
|
131 | |
public List<ResourceLoader> getResourceLoaders() { |
132 | 0 | return this.resourceLoaders; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
public Object getObject(ObjectDefinition definition) { |
138 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
139 | 0 | Object object = resourceLoader.getObject(definition); |
140 | 0 | if (object != null) { |
141 | 0 | return object; |
142 | |
} |
143 | 0 | } |
144 | 0 | return null; |
145 | |
} |
146 | |
|
147 | |
public Object getService(QName qname) { |
148 | 0 | if (LOG.isDebugEnabled()) { |
149 | 0 | LOG.debug("ResourceLoader " + getName() + " fetching service " + qname); |
150 | |
} |
151 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
152 | 0 | if (LOG.isDebugEnabled()) { |
153 | 0 | LOG.debug("Delegating fetch to " + this); |
154 | |
} |
155 | 0 | Object service = resourceLoader.getService(qname); |
156 | 0 | if (service != null) { |
157 | 0 | if (LOG.isDebugEnabled()) { |
158 | 0 | LOG.debug("Found service from " + this); |
159 | |
} |
160 | 0 | return service; |
161 | |
} |
162 | 0 | } |
163 | 0 | return null; |
164 | |
} |
165 | |
|
166 | |
public String getContents(String indent, boolean servicePerLine) { |
167 | 0 | String contents = indent + this + "\n"; |
168 | |
|
169 | 0 | for (ResourceLoader resourceLoader : this.resourceLoaders) { |
170 | 0 | contents += resourceLoader.getContents(indent + "+++", servicePerLine); |
171 | |
} |
172 | |
|
173 | 0 | return contents; |
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
public String toString() { |
178 | 0 | return "Resource Loader: " + this.name + " (" + ObjectUtils.identityToString(this) + ") direct children resource loaders size: " + this.resourceLoaders.size(); |
179 | |
} |
180 | |
|
181 | |
public QName getName() { |
182 | 0 | return this.name; |
183 | |
} |
184 | |
|
185 | |
public void setName(QName name) { |
186 | 0 | this.name = name; |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
} |