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