View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.core.api.resourceloader;
17  
18  import java.util.List;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.kuali.rice.core.api.reflect.ObjectDefinition;
23  
24  /**
25   * A resource loader implementation which checks a child resource loader first and if the resource is not found there,
26   * checks a parent resource loader.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  class ParentChildResourceLoader implements ResourceLoader {
31  
32      private final ResourceLoader parent;
33      private final ResourceLoader child;
34      private QName name;
35  
36      ParentChildResourceLoader(ResourceLoader parent, ResourceLoader child) {
37      	if (parent == null) {
38      		throw new IllegalArgumentException("parent resource loader was null");
39      	}
40      	if (child == null) {
41      		throw new IllegalArgumentException("child resource loader was null");
42      	}
43      	this.parent = parent;
44      	this.child = child;
45      	this.name = new QName(child.getName().toString() + " to parent " + parent.getName().toString());
46      }
47  
48  	@Override
49  	public <T> T getObject(ObjectDefinition definition) {
50  		T object = child.<T>getObject(definition);
51      	if (object == null) {
52      		object = parent.<T>getObject(definition);
53      	}
54      	return object;
55  	}
56  
57  	@Override
58  	public <T> T getService(QName qname) {
59  		T service = child.<T>getService(qname);
60  		if (service == null) {
61  			service = parent.<T>getService(qname);
62  		}
63  		return service;
64      }
65  
66  	/**
67  	 * "Starts" this resource loader.  When started, this method will simply
68  	 * invoke start on the child resource loader.  It's assumed that the
69  	 * parent will be started from it's context.
70  	 * 
71  	 * @see org.kuali.rice.core.api.lifecycle.Lifecycle#start()
72  	 */
73  	@Override
74      public void start() throws Exception {
75  		if (!child.isStarted()) {
76  			child.start();
77  		}
78  	}
79  
80  	/**
81  	 * Just stops the internal child resource loader.
82  	 * 
83  	 * @see org.kuali.rice.core.api.lifecycle.Lifecycle#stop()
84  	 */
85  	@Override
86      public void stop() throws Exception {
87      	if (child.isStarted()) {
88      		child.stop();
89      	}
90      }
91  
92  	@Override
93      public void addResourceLoader(ResourceLoader resourceLoader) {
94      	this.child.addResourceLoader(resourceLoader);
95      }
96  
97  	@Override
98      public void addResourceLoaderFirst(ResourceLoader resourceLoader) {
99      	this.child.addResourceLoaderFirst(resourceLoader);
100     }
101 
102 	@Override
103     public ResourceLoader getResourceLoader(QName name) {
104     	ResourceLoader resourceLoader = this.child.getResourceLoader(name);
105     	if (resourceLoader == null && parent != null) {
106     		return parent.getResourceLoader(name);
107     	} else {
108     		return resourceLoader;
109     	}
110     }
111   
112 	@Override
113     public List<QName> getResourceLoaderNames() {
114 		return this.child.getResourceLoaderNames();
115     }
116 
117 	@Override
118     public List<ResourceLoader> getResourceLoaders() {
119 		return this.child.getResourceLoaders();
120     }
121 
122 	@Override
123     public void removeResourceLoader(QName name) {
124 		this.child.removeResourceLoader(name);
125     }
126 
127 	@Override
128 	public boolean isStarted() {
129 		return false;
130 	}
131 
132 	@Override
133 	public void setName(QName name) {
134 		this.name = name;
135 	}
136 
137 	@Override
138 	public QName getName() {
139 		return this.name;
140 	}
141 
142 	@Override
143 	public String getContents(String indent, boolean servicePerLine) {
144 		StringBuilder contents = new StringBuilder();
145 		contents.append(parent.getContents(indent, servicePerLine)).append("\n");
146 		contents.append(child.getContents(indent, servicePerLine));
147 		return contents.toString();
148 	}
149 
150 	
151 
152 }