initialcontextbuilder.java

来自「一个免费的CA,基于EJB平台的,老师叫我们测试,现把之共享出来让大家参考」· Java 代码 · 共 110 行

JAVA
110
字号
/************************************************************************* *                                                                       * *  EJBCA: The OpenSource Certificate Authority                          * *                                                                       * *  This software is free software; you can redistribute it and/or       * *  modify it under the terms of the GNU Lesser General Public           * *  License as published by the Free Software Foundation; either         * *  version 2.1 of the License, or any later version.                    * *                                                                       * *  See terms of license at gnu.org.                                     * *                                                                       * *************************************************************************/package org.ejbca.core.ejb;import java.io.IOException;import java.io.InputStream;import java.util.Hashtable;import java.util.Properties;import javax.naming.InitialContext;import javax.naming.NamingException;/** * Utility class to manage creation of InitialContext. * <p/> * * @version $Id: InitialContextBuilder.java,v 1.1 2006/01/17 20:30:04 anatom Exp $ */public class InitialContextBuilder {	/** Filename where find out jndi properties to apply */	private final static String PROPERTIES = "ejbca.properties";	/** Singleton instance */	static private InitialContextBuilder instance = null;	/** Cached properties found in ajbca.properties */	private Properties cacheEnv = null;	/** Cached context */	private InitialContext cacheCtx = null;    /**     * Return the only instance permited of itself. It follow a Singleton design pattern.     *     * @return the instance     */	static public InitialContextBuilder getInstance() {		if( instance == null ) {			instance = new InitialContextBuilder();		}		return instance;	}    /**     * Private constructor to avoid instance this class.     */	private InitialContextBuilder() {		// try to load ejbca.properties into cacheEnv		// it could be in any part of classpath		try {				ClassLoader cl = ClassLoader.getSystemClassLoader();			cacheEnv = new Properties();			InputStream inStream = cl.getResourceAsStream(InitialContextBuilder.PROPERTIES);			cacheEnv.load( inStream );				try { inStream.close(); } catch ( IOException ioex ) {}		} catch (Exception ex2) {			cacheEnv = null;		}	}    /**     * Return the configured context     *     * @return the requested context     * @throws NamingException if there is an error creating the context     */	public InitialContext getInitialContext() throws NamingException {		return getInitialContext(null);	}    /**     * Return the requested context. 	 * <p/>	 * Try to use env to configure the context, if env is null, 	 * try to use cacheEnv and if it is null too try to use jndi.propertied     *     * @param env the properties to configure the context.     * @return the requested context     * @throws NamingException if there is an error creating the datasource     */	public InitialContext getInitialContext(Hashtable env) throws NamingException {		if( cacheCtx == null ) {			if( env != null ) {				// try the environmento given by the user				cacheCtx = new InitialContext(env);			} else if ( cacheEnv != null ) {				// try ejbca.properties				cacheCtx = new InitialContext(cacheEnv);			} else {				// try jndi.properties				cacheCtx = new InitialContext();			}		} 		return cacheCtx;	}}// vi:ts=4 syntax=off

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?