servicelocator.java

来自「一个web service的实例」· Java 代码 · 共 61 行

JAVA
61
字号
package com.ibm.ta.webservice;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

public class ServiceLocator {
	private InitialContext ic;

	private Map cache;

	private static ServiceLocator sl = null;

	private ServiceLocator() throws TAServiceException {
		cache = Collections.synchronizedMap(new HashMap());
		try {
			ic = new InitialContext();
		} catch (NamingException ne) {
			throw new TAServiceException(ne.getMessage(), ne);
		}
	}

	static public void initializeInstance() throws TAServiceException {
		if (sl == null) {
			sl = new ServiceLocator();
		}
	}

	static public ServiceLocator getInstance() {
		return sl;
	}

	public DataSource getDataSource(String dataSourceName)
			throws TAServiceException {
		DataSource dataSource = null;
		try {
			if (cache.containsKey(dataSourceName)) {
				dataSource = (DataSource) cache.get(dataSourceName);
			} else {
				dataSource = (DataSource) ic.lookup(dataSourceName);
				cache.put(dataSourceName, dataSource);
			}
		} catch (NamingException ne) {
			throw new TAServiceException(ne.getMessage(), ne);
		}
		return dataSource;
	}

	public UserTransaction getUserTransaction() throws TAServiceException {
		try {
			return (UserTransaction) ic.lookup("java:comp/UserTransaction");
		} catch (NamingException ne) {
			throw new TAServiceException(ne.getMessage(), ne);
		}
	}
}

⌨️ 快捷键说明

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