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-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  
 package org.kuali.rice.core.api.resourceloader;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 
 24  
 import org.apache.commons.lang.ObjectUtils;
 25  
 import org.apache.log4j.Logger;
 26  
 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
 27  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 28  
 
 29  
 /**
 30  
  * A {@link ResourceLoader} which acts as a container for other ResourceLoaders.
 31  
  * Effectively, implements a composite pattern for ResourceLoaders.
 32  
  *
 33  
  * @see ResourceLoader
 34  
  *
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  */
 37  
 public class ResourceLoaderContainer extends BaseLifecycle implements ResourceLoader {
 38  
 
 39  0
         private static final Logger LOG = Logger.getLogger(ResourceLoaderContainer.class);
 40  
 
 41  
         private QName name;
 42  
 
 43  0
         private List<ResourceLoader> resourceLoaders = new ArrayList<ResourceLoader>();
 44  
 
 45  0
         public ResourceLoaderContainer(QName name) {
 46  0
                 this.name = name;
 47  0
         }
 48  
 
 49  
         public void start() throws Exception {
 50  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 51  0
                         LOG.info("Starting ResourceLoader " + resourceLoader.getName());
 52  0
                         resourceLoader.start();
 53  
                 }
 54  0
                 super.start();
 55  0
         }
 56  
 
 57  
         public void stop() throws Exception {
 58  0
                 while (!this.resourceLoaders.isEmpty()) {
 59  0
                         ResourceLoader rl = this.resourceLoaders.get(this.resourceLoaders.size() - 1);
 60  0
                         rl.stop();
 61  0
                         this.resourceLoaders.remove(rl);
 62  0
                 }
 63  
 
 64  0
                 super.stop();
 65  0
                 this.resourceLoaders.clear();
 66  0
         }
 67  
 
 68  
         public void addResourceLoader(ResourceLoader resourceLoader) {
 69  0
                 this.resourceLoaders.add(resourceLoader);
 70  
                 
 71  
                 //FIXME: RICE MODULARITY
 72  0
                 moveKSBLoadersDownHack();                
 73  0
         }
 74  
         
 75  
         /** hack to move the ksb Resource loaders down in the RL stack. */
 76  
         private void moveKSBLoadersDownHack() {
 77  
                 //FIXME: RICE MODULARITY 
 78  0
                 for (int i = 0; i < resourceLoaders.size(); i++) {
 79  0
                         final boolean remote = resourceLoaders.get(i).getName().toString().toUpperCase().contains("KSB");
 80  0
                         if (remote) {
 81  0
                                 final ResourceLoader removed = this.resourceLoaders.remove(i);
 82  0
                                 this.resourceLoaders.add(this.resourceLoaders.size(), removed);
 83  
                         }
 84  
                 }
 85  0
         }
 86  
 
 87  
         public void addResourceLoaderFirst(ResourceLoader resourceLoader) {
 88  0
                 this.resourceLoaders.add(0, resourceLoader);
 89  
 
 90  
                 //FIXME: RICE MODULARITY
 91  0
                 moveKSBLoadersDownHack();
 92  0
         }
 93  
 
 94  
         public boolean containsResourceLoader(ResourceLoader resourceLoader) {
 95  0
                 return this.resourceLoaders.contains(resourceLoader);
 96  
         }
 97  
 
 98  
         public ResourceLoader getResourceLoader(QName name) {
 99  
 
 100  0
                 if (this.getName().equals(name)) {
 101  0
                         return this;
 102  
                 }
 103  
 
 104  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 105  0
                         ResourceLoader loader = iter.next();
 106  0
                         if (loader.getName().equals(name)) {
 107  0
                                 return loader;
 108  
                         }
 109  0
                         ResourceLoader loader2 = loader.getResourceLoader(name);
 110  0
                         if (loader2 != null) {
 111  0
                                 return loader2;
 112  
                         }
 113  0
                 }
 114  0
                 return null;
 115  
         }
 116  
 
 117  
         public List<QName> getResourceLoaderNames() {
 118  0
                 List<QName> names = new ArrayList<QName>();
 119  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 120  0
                         names.add(iter.next().getName());
 121  
                 }
 122  0
                 return names;
 123  
         }
 124  
 
 125  
         public void removeAllResourceLoaders() {
 126  0
             this.resourceLoaders.clear();
 127  0
         }
 128  
 
 129  
         public void removeResourceLoader(QName name) {
 130  0
                 ResourceLoader loaderToRemove = null;
 131  0
                 for (Iterator<ResourceLoader> iter = this.resourceLoaders.iterator(); iter.hasNext();) {
 132  0
                         ResourceLoader loader = iter.next();
 133  0
                         if (loader.getName().equals(name)) {
 134  0
                                 loaderToRemove = loader;
 135  
                         }
 136  0
                 }
 137  0
                 if (loaderToRemove != null) {
 138  
                         try {
 139  0
                                 loaderToRemove.stop();
 140  0
                         } catch (Exception e) {
 141  0
                                 LOG.error("Failed to stop plugin " + loaderToRemove.getName() + " on removal", e);
 142  0
                         }
 143  0
                         this.resourceLoaders.remove(loaderToRemove);
 144  
                 }
 145  0
         }
 146  
 
 147  
         public List<ResourceLoader> getResourceLoaders() {
 148  0
                 return this.resourceLoaders;
 149  
         }
 150  
 
 151  
 
 152  
 
 153  
         public Object getObject(ObjectDefinition definition) {
 154  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 155  0
                         Object object = resourceLoader.getObject(definition);
 156  0
                         if (object != null) {
 157  0
                                 return object;
 158  
                         }
 159  0
                 }
 160  0
                 return null;
 161  
         }
 162  
 
 163  
         public Object getService(QName qname) {
 164  0
                 if (LOG.isDebugEnabled()) {
 165  0
                         LOG.debug("ResourceLoader " + getName() + " fetching service " + qname);
 166  
                 }
 167  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 168  0
                         if (LOG.isDebugEnabled()) {
 169  0
                                 LOG.debug("Delegating fetch to " + this);
 170  
                         }
 171  0
                         Object service = resourceLoader.getService(qname);
 172  0
                         if (service != null) {
 173  0
                                 if (LOG.isDebugEnabled()) {
 174  0
                                         LOG.debug("Found service from " + this);
 175  
                                 }
 176  0
                                 return service;
 177  
                         }
 178  0
                 }
 179  0
                 return null;
 180  
         }
 181  
 
 182  
         public String getContents(String indent, boolean servicePerLine) {
 183  0
                 String contents = indent + this + "\n";
 184  
 
 185  0
                 for (ResourceLoader resourceLoader : this.resourceLoaders) {
 186  0
                         contents += resourceLoader.getContents(indent + "+++", servicePerLine);
 187  
                 }
 188  
 
 189  0
                 return contents;
 190  
         }
 191  
 
 192  
         @Override
 193  
         public String toString() {
 194  0
                 return "Resource Loader: " + this.name + " (" + ObjectUtils.identityToString(this) + ") direct children resource loaders size: " + this.resourceLoaders.size();
 195  
         }
 196  
 
 197  
         public QName getName() {
 198  0
                 return this.name;
 199  
         }
 200  
 
 201  
         public void setName(QName name) {
 202  0
                 this.name = name;
 203  0
         }
 204  
 
 205  
 
 206  
 
 207  
 
 208  
 
 209  
 }