Coverage Report - org.kuali.rice.core.api.resourceloader.ResourceLoaderContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceLoaderContainer
0%
0/88
0%
0/40
2.444
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.api.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.api.lifecycle.BaseLifecycle;
 28  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 29  
 
 30  
 /**
 31  
  * A {@link ResourceLoader} which acts as a container for other ResourceLoaders.
 32  
  * Effectively, implements a composite pattern for ResourceLoaders.
 33  
  *
 34  
  * @see ResourceLoader
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class ResourceLoaderContainer extends BaseLifecycle implements ResourceLoader {
 39  
 
 40  0
         private static final Logger LOG = Logger.getLogger(ResourceLoaderContainer.class);
 41  
 
 42  
         private QName name;
 43  
 
 44  0
         private List<ResourceLoader> resourceLoaders = new ArrayList<ResourceLoader>();
 45  
 
 46  0
         public ResourceLoaderContainer(QName name) {
 47  0
                 this.name = name;
 48  0
         }
 49  
 
 50  
         public void start() throws Exception {
 51  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 52  0
                         LOG.info("Starting ResourceLoader " + resourceLoader.getName());
 53  0
                         resourceLoader.start();
 54  
                 }
 55  0
                 super.start();
 56  0
         }
 57  
 
 58  
         public void stop() throws Exception {
 59  0
                 while (!this.resourceLoaders.isEmpty()) {
 60  0
                         ResourceLoader rl = this.resourceLoaders.get(this.resourceLoaders.size() - 1);
 61  0
                         rl.stop();
 62  0
                         this.resourceLoaders.remove(rl);
 63  0
                 }
 64  
 
 65  0
                 super.stop();
 66  0
                 this.resourceLoaders.clear();
 67  0
         }
 68  
 
 69  
         public void addResourceLoader(ResourceLoader resourceLoader) {
 70  0
                 this.resourceLoaders.add(resourceLoader);
 71  
                 
 72  
                 //FIXME: RICE MODULARITY
 73  0
                 moveKSBLoadersDownHack();                
 74  0
         }
 75  
         
 76  
         /** hack to move the ksb Resource loaders down in the RL stack. */
 77  
         private void moveKSBLoadersDownHack() {
 78  
                 //FIXME: RICE MODULARITY 
 79  0
                 for (int i = 0; i < resourceLoaders.size(); i++) {
 80  0
                         final boolean remote = resourceLoaders.get(i).getName().toString().toUpperCase().contains("KSB");
 81  0
                         if (remote) {
 82  0
                                 final ResourceLoader removed = this.resourceLoaders.remove(i);
 83  0
                                 this.resourceLoaders.add(this.resourceLoaders.size(), removed);
 84  
                         }
 85  
                 }
 86  0
         }
 87  
 
 88  
         public void addResourceLoaderFirst(ResourceLoader resourceLoader) {
 89  0
                 this.resourceLoaders.add(0, resourceLoader);
 90  
 
 91  
                 //FIXME: RICE MODULARITY
 92  0
                 moveKSBLoadersDownHack();
 93  0
         }
 94  
 
 95  
         public boolean containsResourceLoader(ResourceLoader resourceLoader) {
 96  0
                 return this.resourceLoaders.contains(resourceLoader);
 97  
         }
 98  
 
 99  
         public ResourceLoader getResourceLoader(QName name) {
 100  
 
 101  0
                 if (this.getName().equals(name)) {
 102  0
                         return this;
 103  
                 }
 104  
 
 105  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 106  0
                         ResourceLoader loader = iter.next();
 107  0
                         if (loader.getName().equals(name)) {
 108  0
                                 return loader;
 109  
                         }
 110  0
                         ResourceLoader loader2 = loader.getResourceLoader(name);
 111  0
                         if (loader2 != null) {
 112  0
                                 return loader2;
 113  
                         }
 114  0
                 }
 115  0
                 return null;
 116  
         }
 117  
 
 118  
         public List<QName> getResourceLoaderNames() {
 119  0
                 List<QName> names = new ArrayList<QName>();
 120  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 121  0
                         names.add(iter.next().getName());
 122  
                 }
 123  0
                 return names;
 124  
         }
 125  
 
 126  
         public void removeAllResourceLoaders() {
 127  0
             this.resourceLoaders.clear();
 128  0
         }
 129  
 
 130  
         public void removeResourceLoader(QName name) {
 131  0
                 ResourceLoader loaderToRemove = null;
 132  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 133  0
                         ResourceLoader loader = iter.next();
 134  0
                         if (loader.getName().equals(name)) {
 135  0
                                 loaderToRemove = loader;
 136  
                         }
 137  0
                 }
 138  0
                 if (loaderToRemove != null) {
 139  
                         try {
 140  0
                                 loaderToRemove.stop();
 141  0
                         } catch (Exception e) {
 142  0
                                 LOG.error("Failed to stop plugin " + loaderToRemove.getName() + " on removal", e);
 143  0
                         }
 144  0
                         this.resourceLoaders.remove(loaderToRemove);
 145  
                 }
 146  0
         }
 147  
 
 148  
         public List<ResourceLoader> getResourceLoaders() {
 149  0
                 return this.resourceLoaders;
 150  
         }
 151  
 
 152  
 
 153  
 
 154  
         public Object getObject(ObjectDefinition definition) {
 155  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 156  0
                         Object object = resourceLoader.getObject(definition);
 157  0
                         if (object != null) {
 158  0
                                 return object;
 159  
                         }
 160  0
                 }
 161  0
                 return null;
 162  
         }
 163  
 
 164  
         public Object getService(QName qname) {
 165  0
                 if (LOG.isDebugEnabled()) {
 166  0
                         LOG.debug("ResourceLoader " + getName() + " fetching service " + qname);
 167  
                 }
 168  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 169  0
                         if (LOG.isDebugEnabled()) {
 170  0
                                 LOG.debug("Delegating fetch to " + this);
 171  
                         }
 172  0
                         Object service = resourceLoader.getService(qname);
 173  0
                         if (service != null) {
 174  0
                                 if (LOG.isDebugEnabled()) {
 175  0
                                         LOG.debug("Found service from " + this);
 176  
                                 }
 177  0
                                 return service;
 178  
                         }
 179  0
                 }
 180  0
                 return null;
 181  
         }
 182  
 
 183  
         public String getContents(String indent, boolean servicePerLine) {
 184  0
                 String contents = indent + this + "\n";
 185  
 
 186  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 187  0
                         contents += resourceLoader.getContents(indent + "+++", servicePerLine);
 188  
                 }
 189  
 
 190  0
                 return contents;
 191  
         }
 192  
 
 193  
         @Override
 194  
         public String toString() {
 195  0
                 return "Resource Loader: " + this.name + " (" + ObjectUtils.identityToString(this) + ") direct children resource loaders size: " + this.resourceLoaders.size();
 196  
         }
 197  
 
 198  
         public QName getName() {
 199  0
                 return this.name;
 200  
         }
 201  
 
 202  
         public void setName(QName name) {
 203  0
                 this.name = name;
 204  0
         }
 205  
 
 206  
 
 207  
 
 208  
 
 209  
 
 210  
 }