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 javax.xml.namespace.QName;
020:
021: import org.kuali.rice.core.Core;
022: import org.kuali.rice.definition.ObjectDefinition;
023: import org.kuali.rice.lifecycle.Lifecycle;
024: import org.kuali.rice.util.ClassLoaderUtils;
025:
026: /**
027: * A simple ResourceLoader implementation which will load objects from the
028: * specified classloader and also locate services in an optional ServiceLocator.
029: *
030: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
031: */
032: public class BaseResourceLoader extends ResourceLoaderContainer
033: implements ResourceLoader {
034:
035: static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(BaseResourceLoader.class);
037:
038: private ServiceLocator serviceLocator;
039:
040: private ClassLoader classLoader;
041:
042: private boolean postProcessContainer = true;
043:
044: public BaseResourceLoader(QName name, ClassLoader classLoader) {
045: this (name, classLoader, null);
046: }
047:
048: public BaseResourceLoader(QName name) {
049: this (name, ClassLoaderUtils.getDefaultClassLoader());
050: }
051:
052: public BaseResourceLoader(QName name, ServiceLocator serviceLocator) {
053: this (name, ClassLoaderUtils.getDefaultClassLoader(),
054: serviceLocator);
055: }
056:
057: public BaseResourceLoader(QName name, ClassLoader classLoader,
058: ServiceLocator serviceLocator) {
059: super (name);
060: this .classLoader = classLoader;
061: this .serviceLocator = serviceLocator;
062: }
063:
064: public Object getObject(ObjectDefinition objectDefinition) {
065: // if this resource locator has no NameSpaceURI(M.E.) or the
066: // objectDefinition has no M.E. just try to find the class here
067: // or if the M.E. of the object is the same as the M.E. of the locator
068: if (getName().getNamespaceURI() == null
069: || getName().getNamespaceURI().equals(
070: objectDefinition.getMessageEntity())
071: || objectDefinition.getMessageEntity() == null ||
072: // TODO did we really want to check for the KEW_MESSAGING_ENTITY here???
073: //(EdenConstants.KEW_MESSAGING_ENTITY.equals(objectDefinition.getMessageEntity()) && Core.getCurrentContextConfig().getRunningEmbeddedServerMode())) {
074: Core.getCurrentContextConfig()
075: .getRunningEmbeddedServerMode()) {
076: Object object = ObjectDefinitionResolver.createObject(
077: objectDefinition, this .classLoader, true);
078: if (object != null) {
079: return postProcessObject(objectDefinition, object);
080: }
081: }
082: Object super Object = super .getObject(objectDefinition);
083: return (isPostProcessContainer() ? postProcessObject(
084: objectDefinition, super Object) : super Object);
085: }
086:
087: public Object getService(QName serviceName) {
088: if (LOG.isDebugEnabled()) {
089: LOG.debug("ResourceLoader " + getName()
090: + " fetching service " + serviceName
091: + getMemStatus());
092: }
093: if (this .serviceLocator != null) {
094: if (LOG.isDebugEnabled()) {
095: LOG
096: .debug("Using internal service locator to fetch service "
097: + serviceName);
098: }
099: Object service = this .serviceLocator
100: .getService(serviceName);
101: if (service != null) {
102: return postProcessService(serviceName, service);
103: }
104: }
105: if (LOG.isDebugEnabled()) {
106: LOG
107: .debug("ResourceLoader "
108: + getName()
109: + " didn't find service differing to child resource loaders ");
110: }
111: Object super Service = super .getService(serviceName);
112: return (isPostProcessContainer() ? postProcessService(
113: serviceName, super Service) : super Service);
114: }
115:
116: public void start() throws Exception {
117: if (this .classLoader instanceof Lifecycle) {
118: ((Lifecycle) this .classLoader).start();
119: }
120: if (this .serviceLocator != null) {
121: LOG.info("Starting ResourceLoader " + this .getName());
122: this .serviceLocator.start();
123: }
124: super .start();
125: }
126:
127: public void stop() throws Exception {
128: super .stop();
129: if (this .serviceLocator != null) {
130: LOG.info("Stopping ResourceLoader " + this .getName());
131: this .serviceLocator.stop();
132: }
133: if (this .classLoader instanceof Lifecycle) {
134: ((Lifecycle) this .classLoader).stop();
135: }
136: this .classLoader = null;
137: this .serviceLocator = null;
138: }
139:
140: public ClassLoader getClassLoader() {
141: return this .classLoader;
142: }
143:
144: public void setClassLoader(ClassLoader classLoader) {
145: this .classLoader = classLoader;
146: }
147:
148: protected Object postProcessObject(ObjectDefinition definition,
149: Object object) {
150: return object;
151: }
152:
153: protected Object postProcessService(QName serviceName,
154: Object service) {
155: return service;
156: }
157:
158: public boolean isPostProcessContainer() {
159: return postProcessContainer;
160: }
161:
162: public void setPostProcessContainer(boolean postProcessContainer) {
163: this .postProcessContainer = postProcessContainer;
164: }
165:
166: /**
167: * @deprecated use {@link #postProcessService(QName, Object)} instead
168: */
169: protected Object wrap(QName serviceName, Object service) {
170: return postProcessService(serviceName, service);
171: }
172:
173: public String getContents(String indent, boolean servicePerLine) {
174: String contents = indent + this + "\n";
175:
176: if (this .serviceLocator != null) {
177: contents += this .serviceLocator.getContents(indent + "+++",
178: servicePerLine);
179: }
180:
181: for (ResourceLoader resourceLoader : this .getResourceLoaders()) {
182: contents += resourceLoader.getContents(indent + "+++",
183: servicePerLine);
184: }
185:
186: return contents;
187: }
188:
189: private String getMemStatus() {
190: return "\n############################################################## \n"
191: + "# "
192: + dumpMemory()
193: + "\n##############################################################\n";
194: }
195:
196: private String dumpMemory() {
197: long total = Runtime.getRuntime().totalMemory() / 1024;
198: long free = Runtime.getRuntime().freeMemory() / 1024;
199: long max = Runtime.getRuntime().maxMemory() / 1024;
200: return "[Memory] max: " + max + "K, total: " + total
201: + "K, free: " + free + "K, used: " + (total - free)
202: + "K";
203: }
204:
205: public ServiceLocator getServiceLocator() {
206: return this.serviceLocator;
207: }
208: }
|