001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.kuali.rice.resourceloader;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.apache.commons.lang.ObjectUtils;
026: import org.apache.log4j.Logger;
027: import org.kuali.rice.definition.ObjectDefinition;
028: import org.kuali.rice.lifecycle.BaseLifecycle;
029:
030: /**
031: * A {@link ResourceLoader} which acts as a container for other ResourceLoaders.
032: * Effectively, implements a composite pattern for ResourceLoaders.
033: *
034: * @see ResourceLoader
035: *
036: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
037: */
038: public class ResourceLoaderContainer extends BaseLifecycle implements
039: ResourceLoader {
040:
041: private static final Logger LOG = Logger
042: .getLogger(ResourceLoaderContainer.class);
043:
044: private QName name;
045:
046: private List<ResourceLoader> resourceLoaders = new ArrayList<ResourceLoader>();
047:
048: public ResourceLoaderContainer(QName name) {
049: this .name = name;
050: }
051:
052: public void start() throws Exception {
053: for (ResourceLoader resourceLoader : this .resourceLoaders) {
054: LOG.info("Starting ResourceLoader "
055: + resourceLoader.getName());
056: resourceLoader.start();
057: }
058: super .start();
059: }
060:
061: public void stop() throws Exception {
062: while (!this .resourceLoaders.isEmpty()) {
063: ResourceLoader rl = this .resourceLoaders
064: .get(this .resourceLoaders.size() - 1);
065: rl.stop();
066: this .resourceLoaders.remove(rl);
067: }
068:
069: super .stop();
070: this .resourceLoaders.clear();
071: }
072:
073: public void addResourceLoader(ResourceLoader resourceLoader) {
074: this .resourceLoaders.add(resourceLoader);
075: }
076:
077: public void addResourceLoaderFirst(ResourceLoader resourceLoader) {
078: this .resourceLoaders.add(0, resourceLoader);
079: }
080:
081: public boolean containsResourceLoader(ResourceLoader resourceLoader) {
082: return this .resourceLoaders.contains(resourceLoader);
083: }
084:
085: public ResourceLoader getResourceLoader(QName name) {
086:
087: if (this .getName().equals(name)) {
088: return this ;
089: }
090:
091: for (Iterator<ResourceLoader> iter = this .resourceLoaders
092: .iterator(); iter.hasNext();) {
093: ResourceLoader loader = iter.next();
094: if (loader.getName().equals(name)) {
095: return loader;
096: }
097: ResourceLoader loader2 = loader.getResourceLoader(name);
098: if (loader2 != null) {
099: return loader2;
100: }
101: }
102: return null;
103: }
104:
105: public List<QName> getResourceLoaderNames() {
106: List<QName> names = new ArrayList<QName>();
107: for (Iterator<ResourceLoader> iter = this .resourceLoaders
108: .iterator(); iter.hasNext();) {
109: names.add(iter.next().getName());
110: }
111: return names;
112: }
113:
114: public void removeAllResourceLoaders() {
115: this .resourceLoaders.clear();
116: }
117:
118: public void removeResourceLoader(QName name) {
119: ResourceLoader loaderToRemove = null;
120: for (Iterator<ResourceLoader> iter = this .resourceLoaders
121: .iterator(); iter.hasNext();) {
122: ResourceLoader loader = iter.next();
123: if (loader.getName().equals(name)) {
124: loaderToRemove = loader;
125: }
126: }
127: if (loaderToRemove != null) {
128: try {
129: loaderToRemove.stop();
130: } catch (Exception e) {
131: LOG.error("Failed to stop plugin "
132: + loaderToRemove.getName() + " on removal", e);
133: }
134: this .resourceLoaders.remove(loaderToRemove);
135: }
136: }
137:
138: public List<ResourceLoader> getResourceLoaders() {
139: return this .resourceLoaders;
140: }
141:
142: public Object getObject(ObjectDefinition definition) {
143: for (ResourceLoader resourceLoader : this .resourceLoaders) {
144: Object object = resourceLoader.getObject(definition);
145: if (object != null) {
146: return object;
147: }
148: }
149: return null;
150: }
151:
152: public Object getService(QName qname) {
153: if (LOG.isDebugEnabled()) {
154: LOG.debug("ResourceLoader " + getName()
155: + " fetching service " + qname);
156: }
157: for (ResourceLoader resourceLoader : this .resourceLoaders) {
158: if (LOG.isDebugEnabled()) {
159: LOG.debug("Delegating fetch to " + this );
160: }
161: Object service = resourceLoader.getService(qname);
162: if (service != null) {
163: if (LOG.isDebugEnabled()) {
164: LOG.debug("Found service from " + this );
165: }
166: return service;
167: }
168: }
169: return null;
170: }
171:
172: public String getContents(String indent, boolean servicePerLine) {
173: String contents = indent + this + "\n";
174:
175: for (ResourceLoader resourceLoader : this .resourceLoaders) {
176: contents += resourceLoader.getContents(indent + "+++",
177: servicePerLine);
178: }
179:
180: return contents;
181: }
182:
183: @Override
184: public String toString() {
185: return "Resource Loader: " + this .name + " ("
186: + ObjectUtils.identityToString(this )
187: + ") direct children resource loaders size: "
188: + this .resourceLoaders.size();
189: }
190:
191: public QName getName() {
192: return this .name;
193: }
194:
195: public void setName(QName name) {
196: this.name = name;
197: }
198:
199: }
|