001: /*
002: * Created on Nov 26, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.resource;
008:
009: import java.util.HashMap;
010:
011: import org.xdev.base.xssl.XReference;
012: import org.xdev.base.xssl.XSSLReturn;
013: import org.xdev.base.xssl.XSSLActionIterator;
014: import org.xdev.base.xssl.manage.ITransactionFinalizable;
015:
016: /**
017: * @author AYegorov
018: *
019: * To change the template for this generated type comment go to
020: * Window>Preferences>Java>Code Generation>Code and Comments
021: */
022: public abstract class AbstractDataResource extends XSSLActionIterator
023: implements ITransactionFinalizable {
024: public static final String DS_CONFIG = "datasource-id";
025:
026: protected AbstractDataSource ds = null;
027: protected Object resourceConnection = null;
028:
029: /**
030: * @param id
031: */
032: public AbstractDataResource(String id) {
033: super (id);
034: // TODO Auto-generated constructor stub
035: }
036:
037: /**
038: * @param id
039: * @param properties
040: */
041: public AbstractDataResource(String id, HashMap properties) {
042: super (id, properties);
043: // TODO Auto-generated constructor stub
044: }
045:
046: public String getDatasourceId() {
047: String dataSourceId = null;
048:
049: if (this .hasReference() && this .resourceConnection == null
050: && !this .hasProperty(AbstractDataResource.DS_CONFIG)) {
051: dataSourceId = this .getReferenceId();
052: } else {
053: dataSourceId = this
054: .getProperty(AbstractDataResource.DS_CONFIG);
055: }
056:
057: return dataSourceId;
058: }
059:
060: public void setDatasourceId(String datasourceId) {
061:
062: this .setProperty(AbstractDataResource.DS_CONFIG, datasourceId);
063: }
064:
065: /* (non-Javadoc)
066: * @see org.xdev.base.transaction.AbstractTransactionResult#set()
067: */
068: protected void set() throws Exception {
069: String dataSourceId = null;
070:
071: try {
072: this .resourceConnection = this .getEvaluatedReference();
073:
074: if (this .resourceConnection instanceof XReference) {
075: this .resourceConnection = ((XReference) this .resourceConnection)
076: .getObjectValue();
077: }
078:
079: if (this .resourceConnection instanceof XSSLReturn) {
080: this .resourceConnection = ((XSSLReturn) this .resourceConnection)
081: .getObjectValue();
082: }
083:
084: dataSourceId = this .getDatasourceId();
085:
086: this .logDebug("Intializing datasource: " + dataSourceId);
087: } catch (Exception ex) {
088: this .logDebug(ex);
089: }
090:
091: if (this .resourceConnection == null && dataSourceId != null
092: && !"".equals(dataSourceId)) {
093:
094: this .logDebug("Getting datasource: " + dataSourceId);
095:
096: this .ds = (AbstractDataSource) this
097: .getDataSource(dataSourceId);
098:
099: if (this .ds == null) {
100: throw new DataSourceNotFoundException(
101: "No datasource found for id: " + dataSourceId);
102: } else {
103: this .logDebug("Datasource received: " + this .ds);
104:
105: this .ds.setConfig(this .getConfig());
106:
107: this .resourceConnection = this .ds.getConnection();
108: }
109: }
110:
111: this .logDebug("Datasource Resource Connection received: "
112: + this .resourceConnection);
113:
114: this .initialize();
115: }
116:
117: protected abstract void initialize() throws Exception;
118:
119: /* (non-Javadoc)
120: * @see org.xdev.base.transaction.AbstractTransactionResult#getValue()
121: */
122: public Object getObjectValue() {
123: // TODO Auto-generated method stub
124: return this .resourceConnection;
125: }
126:
127: public Object getResourceConnection() {
128: return this.resourceConnection;
129: }
130: }
|