⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicelocator.java

📁 一个web service的实例
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -