AbstractDataResource.java :  » XML » activexml » org » xdev » base » resource » Java Open Source

Java Open Source » XML » activexml 
activexml » org » xdev » base » resource » AbstractDataResource.java
/*
 * Created on Nov 26, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.xdev.base.resource;

import java.util.HashMap;

import org.xdev.base.xssl.XReference;
import org.xdev.base.xssl.XSSLReturn;
import org.xdev.base.xssl.XSSLActionIterator;
import org.xdev.base.xssl.manage.ITransactionFinalizable;

/**
 * @author AYegorov
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public abstract class AbstractDataResource extends XSSLActionIterator implements ITransactionFinalizable{
  public static final String DS_CONFIG = "datasource-id";

  protected AbstractDataSource ds = null;
  protected Object resourceConnection = null;
  
  /**
   * @param id
   */
  public AbstractDataResource(String id) {
    super(id);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param id
   * @param properties
   */
  public AbstractDataResource(String id, HashMap properties) {
    super(id, properties);
    // TODO Auto-generated constructor stub
  }

  
  public String getDatasourceId(){
    String dataSourceId = null;
  
    if (this.hasReference() && this.resourceConnection == null && !this.hasProperty(AbstractDataResource.DS_CONFIG)) {
        dataSourceId = this.getReferenceId();
    }
    else {
        dataSourceId = this.getProperty(AbstractDataResource.DS_CONFIG);
    }
    
    return dataSourceId;
  }
  
  public void setDatasourceId(String datasourceId) {
    
    this.setProperty(AbstractDataResource.DS_CONFIG, datasourceId);
  }
  
  /* (non-Javadoc)
   * @see org.xdev.base.transaction.AbstractTransactionResult#set()
   */
  protected void set() throws Exception {
    String dataSourceId = null;
    
    try {
      this.resourceConnection = this.getEvaluatedReference();
      
      if (this.resourceConnection instanceof XReference) {
        this.resourceConnection = ((XReference)this.resourceConnection).getObjectValue();
      }
      
      if (this.resourceConnection instanceof XSSLReturn) {
        this.resourceConnection = ((XSSLReturn)this.resourceConnection).getObjectValue();
      }

      dataSourceId = this.getDatasourceId();
      
      this.logDebug("Intializing datasource: "+dataSourceId);
    }catch(Exception ex) {
      this.logDebug(ex);
    }
    
    if (this.resourceConnection == null && dataSourceId != null && !"".equals(dataSourceId)) {
        
        this.logDebug("Getting datasource: "+dataSourceId);
        
      this.ds = (AbstractDataSource)this.getDataSource(dataSourceId);
      
      if (this.ds == null) {
          throw new DataSourceNotFoundException("No datasource found for id: "+dataSourceId);
      }
      else {
        this.logDebug("Datasource received: "+this.ds);
        
        this.ds.setConfig(this.getConfig());
        
        this.resourceConnection = this.ds.getConnection();
      }
    }
    
    this.logDebug("Datasource Resource Connection received: "+this.resourceConnection);
    
    this.initialize();
  }

  
  protected abstract void initialize() throws Exception; 
  /* (non-Javadoc)
   * @see org.xdev.base.transaction.AbstractTransactionResult#getValue()
   */
  public Object getObjectValue() {
    // TODO Auto-generated method stub
    return this.resourceConnection;
  }
  
  public Object getResourceConnection() { 
    return this.resourceConnection;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.