Coverage Report - org.mule.object.JndiObjectFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
JndiObjectFactory
0%
0/60
0%
0/18
0
 
 1  
 /*
 2  
  * $Id: JndiObjectFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.object;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.lifecycle.InitialisationCallback;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.api.object.ObjectFactory;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 
 19  
 import java.util.Hashtable;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.naming.Context;
 23  
 import javax.naming.InitialContext;
 24  
 import javax.naming.NamingException;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 public class JndiObjectFactory implements ObjectFactory
 30  
 {
 31  
     /**
 32  
      * If true, the object is looked up from JNDI each time create() is called, otherwise it
 33  
      * is looked up once and stored locally.  Default value is false.
 34  
      */
 35  0
     private boolean lookupOnEachCall = false;
 36  
     
 37  
     private String objectName;
 38  
 
 39  
     private String initialFactory;
 40  
 
 41  
     private String url;
 42  
 
 43  
     private Map properties;
 44  
     
 45  
     private Context _context;
 46  
     
 47  
     private Object _object;
 48  
 
 49  0
     protected final Log logger = LogFactory.getLog(getClass());
 50  
     
 51  
     public JndiObjectFactory()
 52  0
     {
 53  
         // for IoC only
 54  0
     }
 55  
     
 56  
     public JndiObjectFactory(String objectName, String initialFactory, String url)
 57  
     {
 58  0
         this(objectName, initialFactory, url, null);
 59  0
     }
 60  
     
 61  
     public JndiObjectFactory(String objectName, String initialFactory, String url, Map properties)
 62  0
     {
 63  0
         this.objectName = objectName;
 64  0
         this.initialFactory = initialFactory;
 65  0
         this.url = url;
 66  0
         this.properties = properties;
 67  0
     }
 68  
     
 69  
     public void initialise() throws InitialisationException
 70  
     {
 71  0
         if (_context == null)
 72  
         {
 73  0
             Hashtable props = new Hashtable();
 74  
 
 75  0
             if (initialFactory != null)
 76  
             {
 77  0
                 props.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory);
 78  
             }
 79  0
             else if (properties == null
 80  
                     || !properties.containsKey(Context.INITIAL_CONTEXT_FACTORY))
 81  
             {
 82  0
                 throw new InitialisationException(CoreMessages.objectIsNull("jndiInitialFactory"), this);
 83  
             }
 84  
 
 85  0
             if (url != null)
 86  
             {
 87  0
                 props.put(Context.PROVIDER_URL, url);
 88  
             }
 89  
 
 90  0
             if (properties != null)
 91  
             {
 92  0
                 props.putAll(properties);
 93  
             }
 94  
             
 95  
             try
 96  
             {
 97  0
                 _context = new InitialContext(props);
 98  
             }
 99  0
             catch (NamingException e)
 100  
             {
 101  0
                 throw new InitialisationException(e, this);
 102  0
             }
 103  
         }
 104  0
     }
 105  
     
 106  
     public void dispose() 
 107  
     {
 108  0
         if (_context != null)
 109  
         {
 110  
             try
 111  
             {
 112  0
                 _context.close();
 113  
             }
 114  0
             catch (NamingException e)
 115  
             {
 116  0
                 logger.error("JNDI Context failed to dispose properly: ", e);
 117  
             }
 118  
             finally
 119  
             {
 120  0
                 _context = null;
 121  0
             }
 122  
         }
 123  0
     }
 124  
     
 125  
     public Object getInstance(MuleContext muleContext) throws Exception
 126  
     {
 127  0
         if (_object == null || lookupOnEachCall == true)
 128  
         {
 129  0
             _object = _context.lookup(objectName);
 130  
         }    
 131  0
         return _object;
 132  
     }
 133  
     
 134  
     /** {@inheritDoc} */
 135  
     public Class<?> getObjectClass()
 136  
     {
 137  0
         throw new UnsupportedOperationException();
 138  
     }
 139  
 
 140  
     ///////////////////////////////////////////////////////////////////////////////////////////
 141  
     // Getters and Setters
 142  
     ///////////////////////////////////////////////////////////////////////////////////////////    
 143  
 
 144  
     public String getInitialFactory()
 145  
     {
 146  0
         return initialFactory;
 147  
     }
 148  
 
 149  
     public void setInitialFactory(String initialFactory)
 150  
     {
 151  0
         this.initialFactory = initialFactory;
 152  0
     }
 153  
 
 154  
     public boolean isLookupOnEachCall()
 155  
     {
 156  0
         return lookupOnEachCall;
 157  
     }
 158  
 
 159  
     public void setLookupOnEachCall(boolean lookupOnEachCall)
 160  
     {
 161  0
         this.lookupOnEachCall = lookupOnEachCall;
 162  0
     }
 163  
 
 164  
     public String getObjectName()
 165  
     {
 166  0
         return objectName;
 167  
     }
 168  
 
 169  
     public void setObjectName(String objectName)
 170  
     {
 171  0
         this.objectName = objectName;
 172  0
     }
 173  
 
 174  
     public Map getProperties()
 175  
     {
 176  0
         return properties;
 177  
     }
 178  
 
 179  
     public void setProperties(Map properties)
 180  
     {
 181  0
         this.properties = properties;
 182  0
     }
 183  
 
 184  
     public String getUrl()
 185  
     {
 186  0
         return url;
 187  
     }
 188  
 
 189  
     public void setUrl(String url)
 190  
     {
 191  0
         this.url = url;
 192  0
     }
 193  
 
 194  
     public Context getContext()
 195  
     {
 196  0
         return _context;
 197  
     }
 198  
 
 199  
     protected void setContext(Context context)
 200  
     {
 201  0
         this._context = context;
 202  0
     }
 203  
 
 204  
     public void addObjectInitialisationCallback(InitialisationCallback callback)
 205  
     {
 206  0
         throw new UnsupportedOperationException();
 207  
     }
 208  
 
 209  
     public boolean isSingleton()
 210  
     {
 211  0
         return false;
 212  
     }
 213  
 
 214  
     public boolean isExternallyManagedLifecycle()
 215  
     {
 216  0
         return false;
 217  
     }
 218  
 
 219  
     public boolean isAutoWireObject()
 220  
     {
 221  0
         return true;
 222  
     }
 223  
 }