View Javadoc

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