Coverage report

  %line %branch
org.apache.jetspeed.components.SpringComponentManager
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 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.apache.jetspeed.components;
 18  
 
 19  
 import java.io.File;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.Map;
 25  
 import java.util.Properties;
 26  
 
 27  
 import javax.servlet.ServletContext;
 28  
 
 29  
 import org.apache.jetspeed.engine.JetspeedEngineConstants;
 30  
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 31  
 import org.springframework.context.ApplicationContext;
 32  
 import org.springframework.context.ConfigurableApplicationContext;
 33  
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 34  
 import org.springframework.context.support.GenericApplicationContext;
 35  
 import org.springframework.web.context.WebApplicationContext;
 36  
 import org.springframework.web.context.support.XmlWebApplicationContext;
 37  
 
 38  
 /**
 39  
  * <p>
 40  
  * SpringComponentManager
 41  
  * </p>
 42  
  * <p>
 43  
  * 
 44  
  * </p>
 45  
  * 
 46  
  * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
 47  
  * @version $Id: SpringComponentManager.java 517719 2007-03-13 15:05:48Z ate $
 48  
  * 
 49  
  */
 50  
 public class SpringComponentManager implements ComponentManager
 51  
 {
 52  
     protected ConfigurableApplicationContext appContext;
 53  
 
 54  
     private ConfigurableApplicationContext bootCtx;
 55  
 
 56  
     protected ArrayList factories;
 57  
 
 58  
     private Map preconfiguredBeans;
 59  
 
 60  0
     private boolean started = false;
 61  
 
 62  
     public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext,
 63  
             String appRoot)
 64  0
     {
 65  0
         File appRootDir = new File(appRoot);
 66  0
         System.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, appRootDir.getAbsolutePath());
 67  
 
 68  0
         if (bootConfigs != null && bootConfigs.length > 0)
 69  
         {
 70  0
             bootCtx = new XmlWebApplicationContext();
 71  0
             ((XmlWebApplicationContext) bootCtx).setServletContext(servletContext);
 72  0
             ((XmlWebApplicationContext) bootCtx).setConfigLocations(bootConfigs);
 73  
         }
 74  
         else
 75  
         {
 76  0
             bootCtx = new GenericApplicationContext();
 77  
         }
 78  
 
 79  0
         appContext = new XmlWebApplicationContext();
 80  0
         ((XmlWebApplicationContext) appContext).setParent(bootCtx);
 81  0
         ((XmlWebApplicationContext) appContext).setServletContext(servletContext);
 82  0
         ((XmlWebApplicationContext) appContext).setConfigLocations(appConfigs);
 83  
 
 84  0
         factories = new ArrayList();
 85  0
         factories.add(appContext);
 86  
 
 87  0
         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);
 88  0
     }
 89  
 
 90  
     public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext,
 91  
             String appRoot, Map preconfiguredBeans)
 92  
     {
 93  0
         this(bootConfigs, appConfigs, servletContext, appRoot);
 94  0
         this.preconfiguredBeans = preconfiguredBeans;
 95  0
     }
 96  
 
 97  
     
 98  
     public SpringComponentManager(String[] bootConfigs, String[] appConfigs, String appRoot)
 99  0
     {        
 100  0
         PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
 101  0
         Properties p = new Properties();
 102  
         //p.setProperty(APPLICATION_ROOT_KEY,appRootDir.getAbsolutePath());
 103  0
         ppc.setProperties(p);
 104  
         
 105  0
         if (bootConfigs != null && bootConfigs.length > 0)
 106  
         {
 107  0
             bootCtx = new FileSystemXmlApplicationContext(bootConfigs, false);
 108  0
             bootCtx.addBeanFactoryPostProcessor(ppc);
 109  0
             bootCtx.refresh();
 110  
         }
 111  
         else
 112  
         {
 113  0
             bootCtx = new GenericApplicationContext();
 114  
         }
 115  
 
 116  0
         appContext = new FileSystemXmlApplicationContext(appConfigs, false, bootCtx);
 117  0
         appContext.addBeanFactoryPostProcessor(ppc);
 118  0
         appContext.refresh();
 119  0
         factories = new ArrayList();
 120  0
         factories.add(appContext);
 121  
         
 122  0
     }
 123  
     
 124  
     /**
 125  
      * <p>
 126  
      * getComponent
 127  
      * </p>
 128  
      * 
 129  
      * @see org.apache.jetspeed.components.ComponentManagement#getComponent(java.lang.Object)
 130  
      * @param componentName
 131  
      * @return
 132  
      */
 133  
     public Object getComponent(Object componentName)
 134  
     {
 135  0
         if (componentName instanceof Class)
 136  
         {
 137  0
             return appContext.getBean(((Class) componentName).getName());
 138  
         }
 139  
         else
 140  
         {
 141  0
             return appContext.getBean(componentName.toString());
 142  
         }
 143  
     }
 144  
 
 145  
     /**
 146  
      * <p>
 147  
      * getComponent
 148  
      * </p>
 149  
      * 
 150  
      * @see org.apache.jetspeed.components.ComponentManagement#getComponent(java.lang.Object,
 151  
      *      java.lang.Object)
 152  
      * @param containerName
 153  
      * @param componentName
 154  
      * @return
 155  
      */
 156  
     public Object getComponent(Object containerName, Object componentName)
 157  
     {
 158  0
         return getComponent(componentName);
 159  
     }
 160  
 
 161  
     /**
 162  
      * <p>
 163  
      * getContainer
 164  
      * </p>
 165  
      * 
 166  
      * @see org.apache.jetspeed.components.ContainerManagement#getContainer(java.lang.String)
 167  
      * @param containerName
 168  
      * @return
 169  
      */
 170  
     public Object getContainer(String containerName)
 171  
     {
 172  0
         return appContext;
 173  
     }
 174  
 
 175  
     /**
 176  
      * <p>
 177  
      * getRootContainer
 178  
      * </p>
 179  
      * 
 180  
      * @see org.apache.jetspeed.components.ContainerManagement#getRootContainer()
 181  
      * @return
 182  
      */
 183  
     public Object getRootContainer()
 184  
     {
 185  0
         return appContext;
 186  
     }
 187  
 
 188  
     /**
 189  
      * <p>
 190  
      * getContainers
 191  
      * </p>
 192  
      * 
 193  
      * @see org.apache.jetspeed.components.ContainerManagement#getContainers()
 194  
      * @return
 195  
      */
 196  
     public Collection getContainers()
 197  
     {
 198  0
         return factories;
 199  
     }
 200  
 
 201  
     /**
 202  
      * <p>
 203  
      * stop
 204  
      * </p>
 205  
      * 
 206  
      * @see org.apache.jetspeed.components.ContainerManagement#stop()
 207  
      * 
 208  
      */
 209  
     public void stop()
 210  
     {
 211  0
         appContext.close();
 212  0
         bootCtx.close();
 213  0
         started = false;
 214  0
     }
 215  
 
 216  
     public ApplicationContext getApplicationContext()
 217  
     {
 218  0
         return appContext;
 219  
     }
 220  
 
 221  
     public void addComponent(String name, Object bean)
 222  
     {
 223  0
         if (preconfiguredBeans == null)
 224  
         {
 225  0
             preconfiguredBeans = new HashMap();
 226  
         }
 227  0
         preconfiguredBeans.put(name, bean);
 228  
 
 229  0
         if (started)
 230  
         {
 231  0
             bootCtx.getBeanFactory().registerSingleton(name, bean);
 232  
         }
 233  0
     }
 234  
 
 235  
     public void start()
 236  
     {
 237  0
         bootCtx.refresh();
 238  0
         if (preconfiguredBeans != null)
 239  
         {
 240  0
             Iterator itr = preconfiguredBeans.entrySet().iterator();
 241  0
             while (itr.hasNext())
 242  
             {
 243  0
                 Map.Entry entry = (Map.Entry) itr.next();
 244  0
                 bootCtx.getBeanFactory().registerSingleton(entry.getKey().toString(), entry.getValue());
 245  0
             }
 246  
         }
 247  
 
 248  0
         appContext.refresh();
 249  0
         started = true;
 250  0
     }
 251  
 
 252  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.