Open Source Repository

Home /hibernate/hibernate-3.2.6.ga | Repository Home



org/hibernate/util/ExternalSessionFactoryConfig.java
// $Id: ExternalSessionFactoryConfig.java 10859 2006-11-22 00:02:40Z [email protected] $
package org.hibernate.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import java.util.Properties;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Defines support for various externally configurable SessionFactory(s), for
 * example, {@link org.hibernate.jmx.HibernateService JMX} or the JCA
 * adapter.
 *
 @author Steve Ebersole
 */
public abstract class ExternalSessionFactoryConfig {

  private String mapResources;
  private String dialect;
  private String defaultSchema;
  private String defaultCatalog;
  private String maximumFetchDepth;
  private String jdbcFetchSize;
  private String jdbcBatchSize;
  private String batchVersionedDataEnabled;
  private String jdbcScrollableResultSetEnabled;
  private String getGeneratedKeysEnabled;
  private String streamsForBinaryEnabled;
  private String reflectionOptimizationEnabled;
  private String querySubstitutions;
  private String showSqlEnabled;
  private String commentsEnabled;
  private String cacheProviderClass;
  private String cacheProviderConfig;
  private String cacheRegionPrefix;
  private String secondLevelCacheEnabled;
  private String minimalPutsEnabled;
  private String queryCacheEnabled;

  private Map additionalProperties;
  private Set excludedPropertyNames = new HashSet();
  private Map customListeners;


  protected Set getExcludedPropertyNames() {
    return excludedPropertyNames;
  }

  public final String getMapResources() {
    return mapResources;
  }

  public final void setMapResources(String mapResources) {
    this.mapResources = mapResources;
  }

  public void addMapResource(String mapResource) {
    if mapResources==null || mapResources.length()==) {
      mapResources = mapResource.trim();
    }
    else {
      mapResources += ", " + mapResource.trim();
    }
  }

  public final String getDialect() {
    return dialect;
  }

  public final void setDialect(String dialect) {
    this.dialect = dialect;
  }

  public final String getDefaultSchema() {
    return defaultSchema;
  }

  public final void setDefaultSchema(String defaultSchema) {
    this.defaultSchema = defaultSchema;
  }

  public final String getDefaultCatalog() {
    return defaultCatalog;
  }

  public final void setDefaultCatalog(String defaultCatalog) {
    this.defaultCatalog = defaultCatalog;
  }

  public final String getMaximumFetchDepth() {
    return maximumFetchDepth;
  }

  public final void setMaximumFetchDepth(String maximumFetchDepth) {
    verifyIntmaximumFetchDepth );
    this.maximumFetchDepth = maximumFetchDepth;
  }

  public final String getJdbcFetchSize() {
    return jdbcFetchSize;
  }

  public final void setJdbcFetchSize(String jdbcFetchSize) {
    verifyIntjdbcFetchSize );
    this.jdbcFetchSize = jdbcFetchSize;
  }

  public final String getJdbcBatchSize() {
    return jdbcBatchSize;
  }

  public final void setJdbcBatchSize(String jdbcBatchSize) {
    verifyIntjdbcBatchSize );
    this.jdbcBatchSize = jdbcBatchSize;
  }

  public final String getBatchVersionedDataEnabled() {
    return batchVersionedDataEnabled;
  }

  public final void setBatchVersionedDataEnabled(String batchVersionedDataEnabled) {
    this.batchVersionedDataEnabled = batchVersionedDataEnabled;
  }

  public final String getJdbcScrollableResultSetEnabled() {
    return jdbcScrollableResultSetEnabled;
  }

  public final void setJdbcScrollableResultSetEnabled(String jdbcScrollableResultSetEnabled) {
    this.jdbcScrollableResultSetEnabled = jdbcScrollableResultSetEnabled;
  }

  public final String getGetGeneratedKeysEnabled() {
    return getGeneratedKeysEnabled;
  }

  public final void setGetGeneratedKeysEnabled(String getGeneratedKeysEnabled) {
    this.getGeneratedKeysEnabled = getGeneratedKeysEnabled;
  }

  public final String getStreamsForBinaryEnabled() {
    return streamsForBinaryEnabled;
  }

  public final void setStreamsForBinaryEnabled(String streamsForBinaryEnabled) {
    this.streamsForBinaryEnabled = streamsForBinaryEnabled;
  }

  public final String getReflectionOptimizationEnabled() {
    return reflectionOptimizationEnabled;
  }

  public final void setReflectionOptimizationEnabled(String reflectionOptimizationEnabled) {
    this.reflectionOptimizationEnabled = reflectionOptimizationEnabled;
  }

  public final String getQuerySubstitutions() {
    return querySubstitutions;
  }

  public final void setQuerySubstitutions(String querySubstitutions) {
    this.querySubstitutions = querySubstitutions;
  }

  public final String getShowSqlEnabled() {
    return showSqlEnabled;
  }

  public final void setShowSqlEnabled(String showSqlEnabled) {
    this.showSqlEnabled = showSqlEnabled;
  }

  public final String getCommentsEnabled() {
    return commentsEnabled;
  }

  public final void setCommentsEnabled(String commentsEnabled) {
    this.commentsEnabled = commentsEnabled;
  }

  public final String getSecondLevelCacheEnabled() {
    return secondLevelCacheEnabled;
  }

  public final void setSecondLevelCacheEnabled(String secondLevelCacheEnabled) {
    this.secondLevelCacheEnabled = secondLevelCacheEnabled;
  }

  public final String getCacheProviderClass() {
    return cacheProviderClass;
  }

  public final void setCacheProviderClass(String cacheProviderClass) {
    this.cacheProviderClass = cacheProviderClass;
  }

  public String getCacheProviderConfig() {
    return cacheProviderConfig;
  }

  public void setCacheProviderConfig(String cacheProviderConfig) {
    this.cacheProviderConfig = cacheProviderConfig;
  }

  public final String getCacheRegionPrefix() {
    return cacheRegionPrefix;
  }

  public final void setCacheRegionPrefix(String cacheRegionPrefix) {
    this.cacheRegionPrefix = cacheRegionPrefix;
  }

  public final String getMinimalPutsEnabled() {
    return minimalPutsEnabled;
  }

  public final void setMinimalPutsEnabled(String minimalPutsEnabled) {
    this.minimalPutsEnabled = minimalPutsEnabled;
  }

  public final String getQueryCacheEnabled() {
    return queryCacheEnabled;
  }

  public final void setQueryCacheEnabled(String queryCacheEnabled) {
    this.queryCacheEnabled = queryCacheEnabled;
  }

  public final Map getCustomListeners() {
    return customListeners;
  }

  public void setCustomListeners(Map customListeners) {
    this.customListeners = customListeners;
  }

  public void setCustomListenersAsString(String customListenersString) {
    // Note : expected in the syntax:
    //      type=listenerClass
    //          ({sep}type=listenerClass)*
    // where {sep} is any whitespace or comma
    if StringHelper.isNotEmptycustomListenersString) ) {
      String[] listenerEntries = PropertiesHelper.toStringArraycustomListenersString, " ,\n\t\r\f" );
      for int i = 0; i < listenerEntries.length; i++ ) {
        final int keyValueSepPosition = listenerEntries[i].indexOf'=' );
        final String type = listenerEntries[i].substring0, keyValueSepPosition );
        final String listenerClass = listenerEntries[i].substringkeyValueSepPosition + );
        setCustomListenertype, listenerClass );
      }
    }
  }

  public void setCustomListener(String type, String listenerClass) {
    if customListeners == null ) {
      customListeners = new HashMap();
    }
    customListeners.puttype, listenerClass );
  }

  public final void addAdditionalProperty(String name, String value) {
    if !getExcludedPropertyNames().containsname ) ) {
      if additionalProperties == null ) {
        additionalProperties = new HashMap();
      }
      additionalProperties.putname, value );
    }
  }

  protected final Configuration buildConfiguration() {

    Configuration cfg = new Configuration().setPropertiesbuildProperties() );


    String[] mappingFiles = PropertiesHelper.toStringArraymapResources, " ,\n\t\r\f" );
    for int i = 0; i < mappingFiles.length; i++ ) {
      cfg.addResourcemappingFiles[i] );
    }

    if customListeners != null && !customListeners.isEmpty() ) {
      Iterator entries = customListeners.entrySet().iterator();
      while entries.hasNext() ) {
        final Map.Entry entry = Map.Entry entries.next();
        final String type = String entry.getKey();
        final Object value = entry.getValue();
        if value != null ) {
          if String.class.isAssignableFromvalue.getClass() ) ) {
            // Its the listener class name
            cfg.setListenertype, ( ( String value ) );
          }
          else {
            // Its the listener instance (or better be)
            cfg.setListenertype, value );
          }
        }
      }
    }

    return cfg;
  }

  protected final Properties buildProperties() {
    Properties props = new Properties();
    setUnlessNullprops, Environment.DIALECT, dialect );
    setUnlessNullprops, Environment.DEFAULT_SCHEMA, defaultSchema );
    setUnlessNullprops, Environment.DEFAULT_CATALOG, defaultCatalog );
    setUnlessNullprops, Environment.MAX_FETCH_DEPTH, maximumFetchDepth );
    setUnlessNullprops, Environment.STATEMENT_FETCH_SIZE, jdbcFetchSize );
    setUnlessNullprops, Environment.STATEMENT_BATCH_SIZE, jdbcBatchSize );
    setUnlessNullprops, Environment.BATCH_VERSIONED_DATA, batchVersionedDataEnabled );
    setUnlessNullprops, Environment.USE_SCROLLABLE_RESULTSET, jdbcScrollableResultSetEnabled );
    setUnlessNullprops, Environment.USE_GET_GENERATED_KEYS, getGeneratedKeysEnabled );
    setUnlessNullprops, Environment.USE_STREAMS_FOR_BINARY, streamsForBinaryEnabled );
    setUnlessNullprops, Environment.USE_REFLECTION_OPTIMIZER, reflectionOptimizationEnabled );
    setUnlessNullprops, Environment.QUERY_SUBSTITUTIONS, querySubstitutions );
    setUnlessNullprops, Environment.SHOW_SQL, showSqlEnabled );
    setUnlessNullprops, Environment.USE_SQL_COMMENTS, commentsEnabled );
    setUnlessNullprops, Environment.CACHE_PROVIDER, cacheProviderClass );
    setUnlessNullprops, Environment.CACHE_PROVIDER_CONFIG, cacheProviderConfig );
    setUnlessNullprops, Environment.CACHE_REGION_PREFIX, cacheRegionPrefix );
    setUnlessNullprops, Environment.USE_MINIMAL_PUTS, minimalPutsEnabled );
    setUnlessNullprops, Environment.USE_SECOND_LEVEL_CACHE, secondLevelCacheEnabled );
    setUnlessNullprops, Environment.USE_QUERY_CACHE, queryCacheEnabled );

    Map extraProperties = getExtraProperties();
    if extraProperties != null ) {
      addAllprops, extraProperties );
    }

    if additionalProperties != null ) {
      addAllprops, additionalProperties );
    }

    return props;
  }

  protected void addAllProperties target, Map source ) {
    Iterator itr = source.entrySet().iterator();
    while itr.hasNext() ) {
      final Map.Entry entry = Map.Entry itr.next();
      final String propertyName = String entry.getKey();
      final String propertyValue = String entry.getValue();
      if propertyName != null && propertyValue != null ) {
        // Make sure we don't override previous set values
        if !target.keySet().containspropertyName ) ) {
          if !getExcludedPropertyNames().containspropertyName) ) {
            target.putpropertyName, propertyValue );
          }
        }
      }
    }
  }

  protected Map getExtraProperties() {
    return null;
  }

  private void setUnlessNull(Properties props, String key, String value) {
    if value != null ) {
      props.setPropertykey, value );
    }
  }

  private void verifyInt(String value)
  {
    if value != null ) {
      Integer.parseIntvalue );
    }
  }
}