Coverage Report - org.mule.extras.picocontainer.PicoContainerContext
 
Classes in this File Line Coverage Branch Coverage Complexity
PicoContainerContext
0%
0/57
0%
0/7
2.333
 
 1  
 /*
 2  
  * $Id: PicoContainerContext.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.extras.picocontainer;
 12  
 
 13  
 import org.mule.extras.picocontainer.i18n.PicocontainerMessages;
 14  
 import org.mule.impl.container.AbstractContainerContext;
 15  
 import org.mule.impl.container.ContainerKeyPair;
 16  
 import org.mule.umo.lifecycle.InitialisationException;
 17  
 import org.mule.umo.manager.ContainerException;
 18  
 import org.mule.umo.manager.ObjectNotFoundException;
 19  
 import org.mule.util.ClassUtils;
 20  
 import org.mule.util.IOUtils;
 21  
 
 22  
 import java.io.Reader;
 23  
 import java.io.StringReader;
 24  
 
 25  
 import org.nanocontainer.integrationkit.ContainerBuilder;
 26  
 import org.nanocontainer.integrationkit.PicoCompositionException;
 27  
 import org.nanocontainer.script.ScriptedContainerBuilderFactory;
 28  
 import org.picocontainer.MutablePicoContainer;
 29  
 import org.picocontainer.defaults.SimpleReference;
 30  
 
 31  
 /**
 32  
  * <code>PicoContainerContext</code> is a Pico Context that can expose pico-managed
 33  
  * components for use in the Mule framework.
 34  
  */
 35  
 public class PicoContainerContext extends AbstractContainerContext
 36  
 {
 37  
     public static final String CONFIGEXTENSION = "CONFIG";
 38  
 
 39  0
     private String extension = ScriptedContainerBuilderFactory.XML;
 40  
 
 41  
     /**
 42  
      * The url of the config file to use
 43  
      */
 44  
     protected String configFile;
 45  
 
 46  
     /**
 47  
      * the pico container that manages the components
 48  
      */
 49  
     private MutablePicoContainer container;
 50  
 
 51  
     public PicoContainerContext()
 52  
     {
 53  0
         super("pico");
 54  0
     }
 55  
 
 56  
     public String getExtension()
 57  
     {
 58  0
         return extension;
 59  
     }
 60  
 
 61  
     public void setExtension(String extension)
 62  
     {
 63  0
         this.extension = extension;
 64  0
     }
 65  
 
 66  
     public Object getComponent(Object key) throws ObjectNotFoundException
 67  
     {
 68  0
         if (container == null)
 69  
         {
 70  0
             throw new IllegalStateException("Pico container has not been set");
 71  
         }
 72  0
         if (key == null)
 73  
         {
 74  0
             throw new ObjectNotFoundException("Component not found for null key");
 75  
         }
 76  
 
 77  0
         if (key instanceof ContainerKeyPair)
 78  
         {
 79  0
             key = ((ContainerKeyPair)key).getKey();
 80  
         }
 81  
 
 82  0
         Object component = null;
 83  0
         if (key instanceof String)
 84  
         {
 85  
             try
 86  
             {
 87  0
                 Class keyClass = ClassUtils.loadClass((String)key, getClass());
 88  0
                 component = container.getComponentInstance(keyClass);
 89  
             }
 90  0
             catch (ClassNotFoundException e)
 91  
             {
 92  0
                 component = container.getComponentInstance(key);
 93  0
             }
 94  
         }
 95  
         else
 96  
         {
 97  0
             component = container.getComponentInstance(key);
 98  
         }
 99  
 
 100  0
         if (component == null)
 101  
         {
 102  0
             throw new ObjectNotFoundException("Component not found for key: " + key.toString());
 103  
         }
 104  0
         return component;
 105  
     }
 106  
 
 107  
     /**
 108  
      * @return Returns the container.
 109  
      */
 110  
     public MutablePicoContainer getContainer()
 111  
     {
 112  0
         return container;
 113  
     }
 114  
 
 115  
     /**
 116  
      * @param container The container to set.
 117  
      */
 118  
     public void setContainer(MutablePicoContainer container)
 119  
     {
 120  0
         this.container = container;
 121  0
     }
 122  
 
 123  
     /**
 124  
      * The config file can be a resource on the classpath on a file system.
 125  
      * 
 126  
      * @param configFile The configFile to set.
 127  
      */
 128  
     public void setConfigFile(String configFile) throws PicoCompositionException
 129  
     {
 130  0
         this.configFile = configFile;
 131  
 
 132  0
     }
 133  
 
 134  
     public void configure(Reader configuration) throws ContainerException
 135  
     {
 136  0
         String className = ScriptedContainerBuilderFactory.getBuilderClassName(extension);
 137  0
         doConfigure(configuration, className);
 138  0
     }
 139  
 
 140  
     protected void doConfigure(Reader configReader, String builderClassName) throws ContainerException
 141  
     {
 142  0
         org.picocontainer.defaults.ObjectReference containerRef = new SimpleReference();
 143  0
         org.picocontainer.defaults.ObjectReference parentContainerRef = new SimpleReference();
 144  0
         ScriptedContainerBuilderFactory scriptedContainerBuilderFactory = null;
 145  
         try
 146  
         {
 147  0
             scriptedContainerBuilderFactory = new ScriptedContainerBuilderFactory(configReader,
 148  
                 builderClassName, Thread.currentThread().getContextClassLoader());
 149  
         }
 150  0
         catch (ClassNotFoundException e)
 151  
         {
 152  0
             throw new ContainerException(PicocontainerMessages.failedToConfigureContainer(), e);
 153  0
         }
 154  
 
 155  0
         ContainerBuilder builder = scriptedContainerBuilderFactory.getContainerBuilder();
 156  0
         builder.buildContainer(containerRef, parentContainerRef, null, false);
 157  0
         setContainer((MutablePicoContainer)containerRef.get());
 158  0
     }
 159  
 
 160  
     private String getBuilderClassName(String scriptName)
 161  
     {
 162  0
         String extension = scriptName.substring(scriptName.lastIndexOf('.'));
 163  0
         return ScriptedContainerBuilderFactory.getBuilderClassName(extension);
 164  
     }
 165  
 
 166  
     public void initialise() throws InitialisationException {
 167  0
         if (configFile == null)
 168  
         {
 169  0
             return;
 170  
         }
 171  
         try
 172  
         {
 173  0
             String builderClassName = getBuilderClassName(configFile);
 174  0
             String configString = IOUtils.getResourceAsString(configFile, getClass());
 175  0
             StringReader configReader = new StringReader(configString);
 176  0
             doConfigure(configReader, builderClassName);
 177  
 
 178  
         }
 179  0
         catch (Exception e)
 180  
         {
 181  0
             throw new PicoCompositionException(e);
 182  0
         }
 183  0
     }
 184  
 
 185  
     public void dispose()
 186  
     {
 187  0
         if (container != null)
 188  
         {
 189  0
             container.dispose();
 190  
         }
 191  0
     }
 192  
 }