Coverage Report - org.mule.impl.container.PropertiesContainerContext
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesContainerContext
0%
0/40
0%
0/9
2.714
 
 1  
 /*
 2  
  * $Id: PropertiesContainerContext.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.impl.container;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.umo.manager.ContainerException;
 15  
 import org.mule.umo.manager.ObjectNotFoundException;
 16  
 import org.mule.util.TemplateParser;
 17  
 
 18  
 import java.io.Reader;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 /**
 23  
  * Provides a facade for accessing System properties and properties on the
 24  
  * ManagementContext. This container context serves 3 functions -
 25  
  * <ol>
 26  
  * <li>Allows System properties to be set in Mule Xml (by setting the
 27  
  * #systemProperties Map)
 28  
  * <li>Allows one to load System properties into the mule context so that MuleXml
 29  
  * templates referring to System properties can be used (i.e. ${os.name}).
 30  
  * <li>Provides a consistent way to set abitary properties on the Management
 31  
  * Context. Setting properties on this container context is equivilent to using the
 32  
  * <environment-properties> element in Mule Xml. The latter element may be removed in
 33  
  * the future.
 34  
  * </ol>
 35  
  */
 36  
 public class PropertiesContainerContext extends AbstractContainerContext
 37  
 {
 38  
 
 39  
     protected Map systemProperties;
 40  
     protected Map properties;
 41  0
     protected boolean loadSystemProperties = true;
 42  0
     protected boolean enableTemplates = false;
 43  
 
 44  0
     protected TemplateParser templateParser = TemplateParser.createAntStyleParser();
 45  
 
 46  
     public PropertiesContainerContext()
 47  
     {
 48  0
         super("properties");
 49  0
     }
 50  
 
 51  
     public void configure(Reader configuration) throws ContainerException
 52  
     {
 53  0
         throw new UnsupportedOperationException("configure");
 54  
     }
 55  
 
 56  
     /**
 57  
      * Queries a component from the underlying container. For this container it will
 58  
      * look up a property on the Mule Management Context.
 59  
      * 
 60  
      * @param key the key fo find the component with. It's up to the individual
 61  
      *            implementation to check the type of this key and look up objects
 62  
      *            accordingly
 63  
      * @return the component found in the container
 64  
      * @throws org.mule.umo.manager.ObjectNotFoundException if the component is not
 65  
      *             found
 66  
      */
 67  
     public Object getComponent(Object key) throws ObjectNotFoundException
 68  
     {
 69  0
         if (key == null)
 70  
         {
 71  0
             throw new ObjectNotFoundException("null");
 72  
         }
 73  0
         Object value = MuleManager.getInstance().getProperty(key.toString());
 74  0
         if (value == null)
 75  
         {
 76  0
             throw new ObjectNotFoundException(key.toString());
 77  
         }
 78  0
         if (value instanceof String && enableTemplates)
 79  
         {
 80  0
             value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
 81  
         }
 82  0
         return value;
 83  
     }
 84  
 
 85  
     public Map getSystemProperties()
 86  
     {
 87  0
         return systemProperties;
 88  
     }
 89  
 
 90  
     public void setSystemProperties(Map properties)
 91  
     {
 92  0
         this.systemProperties = properties;
 93  
         String value;
 94  
         Map.Entry entry;
 95  0
         if (systemProperties != null)
 96  
         {
 97  0
             for (Iterator iterator = systemProperties.entrySet().iterator(); iterator.hasNext();)
 98  
             {
 99  0
                 entry = (Map.Entry) iterator.next();
 100  0
                 value = entry.getValue().toString();
 101  0
                 value = templateParser.parse(systemProperties, value);
 102  0
                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
 103  0
                 System.setProperty(entry.getKey().toString(), value);
 104  
             }
 105  
         }
 106  
 
 107  0
         if (loadSystemProperties)
 108  
         {
 109  0
             Map props = System.getProperties();
 110  
 
 111  0
             for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 112  
             {
 113  0
                 entry = (Map.Entry) iterator.next();
 114  0
                 value = entry.getValue().toString();
 115  0
                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
 116  0
                 MuleManager.getInstance().setProperty(entry.getKey(), value);
 117  
             }
 118  
         }
 119  0
     }
 120  
 
 121  
     public Map getProperties()
 122  
     {
 123  0
         return properties;
 124  
     }
 125  
 
 126  
     public void setProperties(Map properties)
 127  
     {
 128  0
         this.properties = properties;
 129  0
         if (properties != null)
 130  
         {
 131  
             Map.Entry entry;
 132  
             String value;
 133  0
             for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();)
 134  
             {
 135  0
                 entry = (Map.Entry) iterator.next();
 136  0
                 value = entry.getValue().toString();
 137  0
                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
 138  0
                 MuleManager.getInstance().setProperty(entry.getKey(), value);
 139  
             }
 140  
         }
 141  0
     }
 142  
 }