examcontext.java

来自「在线考试系统」· Java 代码 · 共 54 行

JAVA
54
字号
package com.nc21.exam.util;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

/**
 * 系统上下文,可以在容器中获取资源对象
 * 
 * @author Administrator
 * 
 */
public class ExamContext {

	private final static Logger log = Logger.getLogger(Config.class);

	private static Context initContext;
	
	static {
		try {
			initContext = new InitialContext();
		} catch (NamingException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}

	/**
	 * 查询并返回容器中的资源对象
	 * 
	 * @param subName
	 *            对象jndi的子名称
	 * @return
	 */
	public static Object find(String subName) {
		if (subName == null) {
			throw new IllegalArgumentException("Illegal Argument");
		}
		Object obj = null;
		try {
			Context envContext = (Context) initContext.lookup(Config
					.getValue("jndi.env"));
			obj = envContext.lookup(subName);
		} catch (NamingException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return null;
		}
		return obj;
	}
}

⌨️ 快捷键说明

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