java服务定位器(3)

/**
* 得到主题工厂
*
* @param topicConnFactoryName
* @throws ServiceLocatorException
*/
public TopicConnectionFactory getTopicConnectionFactory(
String topicConnFactoryName) throws ServiceLocatorException {
TopicConnectionFactory factory = null;
try {
if (handlers.containsKey(topicConnFactoryName)) {
factory = (TopicConnectionFactory) handlers
.get(topicConnFactoryName);
} else {
factory = (TopicConnectionFactory) context
.lookup(topicConnFactoryName);
handlers.put(topicConnFactoryName, factory);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return factory;
}
/**
* 得到主题
*
* @param topicName
* @throws ServiceLocatorException
*/
public Topic getTopic(String topicName) throws ServiceLocatorException {
Topic topic = null;
try {
if (handlers.containsKey(topicName)) {
topic = (Topic) handlers.get(topicName);
} else {
topic = (Topic) context.lookup(topicName);
handlers.put(topicName, topic);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return topic;
}
/**
* 得到数据源
*
* @param dataSourceName
* @throws ServiceLocatorException
*/
public DataSource getDataSource(String dataSourceName)
throws ServiceLocatorException {
DataSource dataSource = null;
try {
if (handlers.containsKey(dataSourceName)) {
dataSource = (DataSource) handlers.get(dataSourceName);
} else {
dataSource = (DataSource) context.lookup(dataSourceName);
handlers.put(dataSourceName, dataSource);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return dataSource;
}

/**
* 得到URL
*
* @param envName
* @throws ServiceLocatorException
*/
public URL getUrl(String envName) throws ServiceLocatorException {
URL url = null;
try {
url = (URL) context.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}

return url;
}

/**
* 上下文是否存在环境变量
*
* @param envName
* @throws ServiceLocatorException
*/
public boolean getBoolean(String envName) throws ServiceLocatorException {
Boolean bool = null;
try {
bool = (Boolean) context.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return bool.booleanValue();
}

你可能感兴趣的:(java)