View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jndi;
5   
6   import java.util.Hashtable;
7   
8   import javax.naming.Context;
9   import javax.naming.NamingException;
10  import javax.naming.spi.InitialContextFactory;
11  
12  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
13  import org.apache.commons.lang.builder.ToStringStyle;
14  
15  /**
16   * @author gliptak
17   *
18   */
19  public class MockInitialContextFactory implements InitialContextFactory {
20  
21  	protected static MockContext singleton;
22  	
23  	/* (non-Javadoc)
24  	 * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
25  	 */
26  	public Context getInitialContext(Hashtable environment)	throws NamingException {
27  		_getInitialContext(environment);
28  		if (singleton==null){
29  			singleton=new MockContext(environment);
30  		}
31  		return singleton;
32  	}
33  	
34  	public Context _getInitialContext(Hashtable environment)	throws NamingException {
35  		return null;
36  	}
37  	
38  	/**
39  	 * Release singleton
40  	 */
41  	static protected void releaseSingleton(){
42  		singleton=null;
43  	}
44  	
45  	@Override
46  	public String toString(){
47  		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
48  	}
49  }