📄 dataaccesscontroller.java
字号:
/*
* DataAccessController.java
*
* Created on 2001/11/08, 14:15
*/
package jp.co.intra_mart.framework.base.data;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
/**
* 僨乕僞傾僋僙僗傪惂屼偟傑偡丅
* DAO偺庢摼丄僐僱僋僞偺娗棟摍傪峴偄傑偡丅
* 偙偺僋儔僗傪棙梡偟偰娙堈僩儔儞僓僋僔儑儞傪幚尰偡傞偙偲傕壜擻偱偡偑丄偙偺曽朄偼悇彠偝傟傑偣傫丅
* 僩儔儞僓僋僔儑儞偼偱偒傞尷傝Java Transaction API(JTA)偺UserTransaction傪棙梡偟偰峴偭偰偔偩偝偄丅
*
* @author INTRAMART
* @version 1.0
*/
public class DataAccessController {
/**
* 僨乕僞僐僱僋僞偺廤崌
*/
private Map connectors;
/**
* 僨乕僞僾儘僷僥傿僴儞僪儔
*/
private DataPropertyHandler handler;
/**
* 僨乕僞傾僋僙僗僐儞僩儘乕儔傪惗惉偟傑偡丅
*
* @param handler 僨乕僞僾儘僷僥傿僴儞僪儔
*/
public DataAccessController(DataPropertyHandler handler) {
this.connectors = new HashMap();
this.handler = handler;
}
/**
* DAO傪庢摼偟傑偡丅
* 僉乕偲愙懕忣曬偱巜掕偝傟偨DAO傪庢摼偟傑偡丅
*
* @param application 傾僾儕働乕僔儑儞ID
* @param key DAO偺僉乕
* @param connect 愙懕忣曬
* @return DAO
* @throws DataPropertyException 僾儘僷僥傿偺庢摼偵幐攕
* @throws DataConnectorException 僨乕僞僐僱僋僞偺庢摼偵幐攕
* @throws DAOException DAO偺庢摼偵幐攕
* @throws DataConnectException 僨乕僞儕僜乕僗偲偺愙懕偵幐攕
*/
public Object getDAO(String application, String key, String connect) throws DataPropertyException, DataConnectorException, DAOException, DataConnectException {
Object connectorObject = null;
DataConnector connector = null;
String connectorName = null;
String connectorClassName = null;
String resource = null;
String daoName = null;
Object daoObject = null;
DAO result = null;
// 僨乕僞僐僱僋僞柤偺庢摼
connectorName = handler.getConnectorName(application, key, connect);
// 僨乕僞僐僱僋僞偺専嶕
if (connectorName == null || connectorName.equals("")) {
connector = null;
} else {
connector = (DataConnector)this.connectors.get(connectorName);
if (connector == null) {
// 僨乕僞僐僱僋僞偑懚嵼偟側偄応崌丄怴婯偵搊榐
connectorClassName = handler.getConnectorClassName(connectorName);
try {
connectorObject = Class.forName(connectorClassName).newInstance();
} catch (Exception e) {
String message = null;
try {
message = ResourceBundle.getBundle("jp.co.intra_mart.framework.base.data.i18n").getString("DataAccessController.FailedToCreateDataConnector");
} catch (MissingResourceException ex) {
}
throw new DataConnectorException(message + " : connector name = " + connectorName + ", class = " + connectorClassName + ", application = " + application + ", key = " + key + ", connect = " + connect, e);
}
if (connectorObject instanceof DataConnector) {
connector = (DataConnector)connectorObject;
} else {
String message = null;
try {
message = ResourceBundle.getBundle("jp.co.intra_mart.framework.base.data.i18n").getString("DataAccessController.DataConnectorNotExtended");
} catch (MissingResourceException e) {
}
throw new DataConnectorException(message + " : class = " + connectorClassName + ", application = " + application + ", key = " + key + ", connect = " + connect);
}
connector.setDataPropertyHandler(handler);
this.connectors.put(connectorName, connector);
}
// 僨乕僞儕僜乕僗偺庢摼
resource = handler.getConnectorResource(connectorName);
}
// DAO偺惗惉
daoName = handler.getDAOName(application, key, connect);
try {
daoObject = Class.forName(daoName).newInstance();
} catch (Exception e) {
String message = null;
try {
message = ResourceBundle.getBundle("jp.co.intra_mart.framework.base.data.i18n").getString("DataAccessController.FailedToCreateDAO");
} catch (MissingResourceException ex) {
}
throw new DAOException(message + " : class = " + daoName + ", application = " + application + ", key = " + key + ", connect = " + connect, e);
}
if (daoObject instanceof DAO) {
result = (DAO)daoObject;
} else {
String message = null;
try {
message = ResourceBundle.getBundle("jp.co.intra_mart.framework.base.data.i18n").getString("DataAccessController.DAONotImplemented");
} catch (MissingResourceException e) {
}
throw new DataConnectorException(message + " : class = " + daoName + ", application = " + application + ", key = " + key + ", connect = " + connect);
}
result.setConnectInfo(connector, resource, key, connect);
return result;
}
/**
* 偡傋偰偺僨乕僞僐僱僋僞傪僐儈僢僩偟傑偡丅
* 偙偺儊僜僢僪偼偙偺僋儔僗偺{@link #getDAO(String, String, String)}偱庢摼偝傟偨DAO偵娭楢偡傞僨乕僞僐僱僋僞偺{@link DataConnector#commit()}傪弴偵屇傃弌偡偙偲偱娙堈揑側僩儔儞僓僋僔儑儞傪幚尰偟偰偄傑偡丅
* 偦偺偨傔丄暋悢偺僨乕僞儀乕僗偵傾僋僙僗偟偨僩儔儞僓僋僔儑儞偺応崌偼2-Phase僐儈僢僩傎偳寴楽側僩儔儞僓僋僔儑儞偵偼側傜側偄応崌偑偁傝傑偡丅
*
* @throws DataConnectException 僐儈僢僩偵幐攕
*/
public void commit() throws DataConnectException {
Iterator connectorIterator = connectors.values().iterator();
DataConnector connector;
DataConnectException exception = null;
while (connectorIterator.hasNext()) {
connector = (DataConnector)connectorIterator.next();
try {
connector.commit();
} catch (DataConnectException e) {
exception = e;
}
}
if (exception != null) {
throw exception;
}
}
/**
* 偡傋偰偺僨乕僞僐僱僋僞傪儘乕儖僶僢僋偟傑偡丅
* 偙偺儊僜僢僪偼偙偺僋儔僗偺{@link #getDAO(String, String, String)}偱庢摼偝傟偨DAO偵娭楢偡傞僨乕僞僐僱僋僞偺{@link DataConnector#rollback()}傪弴偵屇傃弌偡偙偲偱娙堈揑側僩儔儞僓僋僔儑儞傪幚尰偟偰偄傑偡丅
* 偦偺偨傔丄暋悢偺僨乕僞儀乕僗偵傾僋僙僗偟偨僩儔儞僓僋僔儑儞偺応崌偼2-Phase僐儈僢僩傎偳寴楽側僩儔儞僓僋僔儑儞偵偼側傜側偄応崌偑偁傝傑偡丅
*
* @throws DataConnectException 儘乕儖僶僢僋偵幐攕
*/
public void rollback() throws DataConnectException {
Iterator connectorIterator = connectors.values().iterator();
DataConnector connector;
DataConnectException exception = null;
while (connectorIterator.hasNext()) {
connector = (DataConnector)connectorIterator.next();
try {
connector.rollback();
} catch (DataConnectException e) {
exception = e;
}
}
if (exception != null) {
throw exception;
}
}
/**
* 偡傋偰偺僨乕僞僗僩傾偺帒尮傪夝曻偟傑偡丅
* 偙偺儊僜僢僪偼偙偺僋儔僗偺{@link #getDAO(String, String, String)}偱庢摼偝傟偨DAO偵娭楢偡傞僨乕僞僐僱僋僞偺{@link DataConnector#release()}傪弴偵屇傃弌偟偰偄傑偡丅
*
* @throws DataConnectException 帒尮奐曻偵幐攕
*/
public void release() throws DataConnectException {
Iterator connectorIterator = connectors.values().iterator();
DataConnector connector;
DataConnectException exception = null;
while (connectorIterator.hasNext()) {
connector = (DataConnector)connectorIterator.next();
try {
connector.release();
} catch (DataConnectException e) {
exception = e;
}
}
this.connectors.clear();
if (exception != null) {
throw exception;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -