1 package org.hibernate.util; 3 4 import org.hibernate.cfg.Configuration; 5 import org.hibernate.cfg.Environment; 6 7 import java.util.Properties ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import java.util.Set ; 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 14 21 public abstract class ExternalSessionFactoryConfig { 22 23 private String mapResources; 24 private String dialect; 25 private String defaultSchema; 26 private String defaultCatalog; 27 private String maximumFetchDepth; 28 private String jdbcFetchSize; 29 private String jdbcBatchSize; 30 private String batchVersionedDataEnabled; 31 private String jdbcScrollableResultSetEnabled; 32 private String getGeneratedKeysEnabled; 33 private String streamsForBinaryEnabled; 34 private String reflectionOptimizationEnabled; 35 private String querySubstitutions; 36 private String showSqlEnabled; 37 private String commentsEnabled; 38 private String secondLevelCacheEnabled; 39 private String cacheProviderClass; 40 private String cacheRegionPrefix; 41 private String minimalPutsEnabled; 42 private String queryCacheEnabled; 43 44 private Map additionalProperties; 45 private Set excludedPropertyNames = new HashSet (); 46 private Map customListeners; 47 48 49 protected Set getExcludedPropertyNames() { 50 return excludedPropertyNames; 51 } 52 53 public final String getMapResources() { 54 return mapResources; 55 } 56 57 public final void setMapResources(String mapResources) { 58 this.mapResources = mapResources; 59 } 60 61 public void addMapResource(String mapResource) { 62 if ( mapResources==null || mapResources.length()==0 ) { 63 mapResources = mapResource.trim(); 64 } 65 else { 66 mapResources += ", " + mapResource.trim(); 67 } 68 } 69 70 public final String getDialect() { 71 return dialect; 72 } 73 74 public final void setDialect(String dialect) { 75 this.dialect = dialect; 76 } 77 78 public final String getDefaultSchema() { 79 return defaultSchema; 80 } 81 82 public final void setDefaultSchema(String defaultSchema) { 83 this.defaultSchema = defaultSchema; 84 } 85 86 public final String getDefaultCatalog() { 87 return defaultCatalog; 88 } 89 90 public final void setDefaultCatalog(String defaultCatalog) { 91 this.defaultCatalog = defaultCatalog; 92 } 93 94 public final String getMaximumFetchDepth() { 95 return maximumFetchDepth; 96 } 97 98 public final void setMaximumFetchDepth(String maximumFetchDepth) { 99 verifyInt( maximumFetchDepth ); 100 this.maximumFetchDepth = maximumFetchDepth; 101 } 102 103 public final String getJdbcFetchSize() { 104 return jdbcFetchSize; 105 } 106 107 public final void setJdbcFetchSize(String jdbcFetchSize) { 108 verifyInt( jdbcFetchSize ); 109 this.jdbcFetchSize = jdbcFetchSize; 110 } 111 112 public final String getJdbcBatchSize() { 113 return jdbcBatchSize; 114 } 115 116 public final void setJdbcBatchSize(String jdbcBatchSize) { 117 verifyInt( jdbcBatchSize ); 118 this.jdbcBatchSize = jdbcBatchSize; 119 } 120 121 public final String getBatchVersionedDataEnabled() { 122 return batchVersionedDataEnabled; 123 } 124 125 public final void setBatchVersionedDataEnabled(String batchVersionedDataEnabled) { 126 this.batchVersionedDataEnabled = batchVersionedDataEnabled; 127 } 128 129 public final String getJdbcScrollableResultSetEnabled() { 130 return jdbcScrollableResultSetEnabled; 131 } 132 133 public final void setJdbcScrollableResultSetEnabled(String jdbcScrollableResultSetEnabled) { 134 this.jdbcScrollableResultSetEnabled = jdbcScrollableResultSetEnabled; 135 } 136 137 public final String getGetGeneratedKeysEnabled() { 138 return getGeneratedKeysEnabled; 139 } 140 141 public final void setGetGeneratedKeysEnabled(String getGeneratedKeysEnabled) { 142 this.getGeneratedKeysEnabled = getGeneratedKeysEnabled; 143 } 144 145 public final String getStreamsForBinaryEnabled() { 146 return streamsForBinaryEnabled; 147 } 148 149 public final void setStreamsForBinaryEnabled(String streamsForBinaryEnabled) { 150 this.streamsForBinaryEnabled = streamsForBinaryEnabled; 151 } 152 153 public final String getReflectionOptimizationEnabled() { 154 return reflectionOptimizationEnabled; 155 } 156 157 public final void setReflectionOptimizationEnabled(String reflectionOptimizationEnabled) { 158 this.reflectionOptimizationEnabled = reflectionOptimizationEnabled; 159 } 160 161 public final String getQuerySubstitutions() { 162 return querySubstitutions; 163 } 164 165 public final void setQuerySubstitutions(String querySubstitutions) { 166 this.querySubstitutions = querySubstitutions; 167 } 168 169 public final String getShowSqlEnabled() { 170 return showSqlEnabled; 171 } 172 173 public final void setShowSqlEnabled(String showSqlEnabled) { 174 this.showSqlEnabled = showSqlEnabled; 175 } 176 177 public final String getCommentsEnabled() { 178 return commentsEnabled; 179 } 180 181 public final void setCommentsEnabled(String commentsEnabled) { 182 this.commentsEnabled = commentsEnabled; 183 } 184 185 public final String getSecondLevelCacheEnabled() { 186 return secondLevelCacheEnabled; 187 } 188 189 public final void setSecondLevelCacheEnabled(String secondLevelCacheEnabled) { 190 this.secondLevelCacheEnabled = secondLevelCacheEnabled; 191 } 192 193 public final String getCacheProviderClass() { 194 return cacheProviderClass; 195 } 196 197 public final void setCacheProviderClass(String cacheProviderClass) { 198 this.cacheProviderClass = cacheProviderClass; 199 } 200 201 public final String getCacheRegionPrefix() { 202 return cacheRegionPrefix; 203 } 204 205 public final void setCacheRegionPrefix(String cacheRegionPrefix) { 206 this.cacheRegionPrefix = cacheRegionPrefix; 207 } 208 209 public final String getMinimalPutsEnabled() { 210 return minimalPutsEnabled; 211 } 212 213 public final void setMinimalPutsEnabled(String minimalPutsEnabled) { 214 this.minimalPutsEnabled = minimalPutsEnabled; 215 } 216 217 public final String getQueryCacheEnabled() { 218 return queryCacheEnabled; 219 } 220 221 public final void setQueryCacheEnabled(String queryCacheEnabled) { 222 this.queryCacheEnabled = queryCacheEnabled; 223 } 224 225 public final Map getCustomListeners() { 226 return customListeners; 227 } 228 229 public void setCustomListeners(Map customListeners) { 230 this.customListeners = customListeners; 231 } 232 233 public void setCustomListenersAsString(String customListenersString) { 234 if ( StringHelper.isNotEmpty( customListenersString) ) { 239 String [] listenerEntries = PropertiesHelper.toStringArray( customListenersString, " ,\n\t\r\f" ); 240 for ( int i = 0; i < listenerEntries.length; i++ ) { 241 final int keyValueSepPosition = listenerEntries[i].indexOf( '=' ); 242 final String type = listenerEntries[i].substring( 0, keyValueSepPosition ); 243 final String listenerClass = listenerEntries[i].substring( keyValueSepPosition + 1 ); 244 setCustomListener( type, listenerClass ); 245 } 246 } 247 } 248 249 public void setCustomListener(String type, String listenerClass) { 250 if ( customListeners == null ) { 251 customListeners = new HashMap (); 252 } 253 customListeners.put( type, listenerClass ); 254 } 255 256 public final void addAdditionalProperty(String name, String value) { 257 if ( !getExcludedPropertyNames().contains( name ) ) { 258 if ( additionalProperties == null ) { 259 additionalProperties = new HashMap (); 260 } 261 additionalProperties.put( name, value ); 262 } 263 } 264 265 protected final Configuration buildConfiguration() { 266 267 Configuration cfg = new Configuration().setProperties( buildProperties() ); 268 269 270 String [] mappingFiles = PropertiesHelper.toStringArray( mapResources, " ,\n\t\r\f" ); 271 for ( int i = 0; i < mappingFiles.length; i++ ) { 272 cfg.addResource( mappingFiles[i] ); 273 } 274 275 if ( customListeners != null && !customListeners.isEmpty() ) { 276 Iterator entries = customListeners.entrySet().iterator(); 277 while ( entries.hasNext() ) { 278 final Map.Entry entry = ( Map.Entry ) entries.next(); 279 final String type = ( String ) entry.getKey(); 280 final Object value = entry.getValue(); 281 if ( value != null ) { 282 if ( String .class.isAssignableFrom( value.getClass() ) ) { 283 cfg.setListener( type, ( ( String ) value ) ); 285 } 286 else { 287 cfg.setListener( type, value ); 289 } 290 } 291 } 292 } 293 294 return cfg; 295 } 296 297 protected final Properties buildProperties() { 298 Properties props = new Properties (); 299 setUnlessNull( props, Environment.DIALECT, dialect ); 300 setUnlessNull( props, Environment.DEFAULT_SCHEMA, defaultSchema ); 301 setUnlessNull( props, Environment.DEFAULT_CATALOG, defaultCatalog ); 302 setUnlessNull( props, Environment.MAX_FETCH_DEPTH, maximumFetchDepth ); 303 setUnlessNull( props, Environment.STATEMENT_FETCH_SIZE, jdbcFetchSize ); 304 setUnlessNull( props, Environment.STATEMENT_BATCH_SIZE, jdbcBatchSize ); 305 setUnlessNull( props, Environment.BATCH_VERSIONED_DATA, batchVersionedDataEnabled ); 306 setUnlessNull( props, Environment.USE_SCROLLABLE_RESULTSET, jdbcScrollableResultSetEnabled ); 307 setUnlessNull( props, Environment.USE_GET_GENERATED_KEYS, getGeneratedKeysEnabled ); 308 setUnlessNull( props, Environment.USE_STREAMS_FOR_BINARY, streamsForBinaryEnabled ); 309 setUnlessNull( props, Environment.USE_REFLECTION_OPTIMIZER, reflectionOptimizationEnabled ); 310 setUnlessNull( props, Environment.QUERY_SUBSTITUTIONS, querySubstitutions ); 311 setUnlessNull( props, Environment.SHOW_SQL, showSqlEnabled ); 312 setUnlessNull( props, Environment.USE_SQL_COMMENTS, commentsEnabled ); 313 setUnlessNull( props, Environment.USE_SECOND_LEVEL_CACHE, secondLevelCacheEnabled ); 314 setUnlessNull( props, Environment.CACHE_PROVIDER, cacheProviderClass ); 315 setUnlessNull( props, Environment.CACHE_REGION_PREFIX, cacheRegionPrefix ); 316 setUnlessNull( props, Environment.USE_MINIMAL_PUTS, minimalPutsEnabled ); 317 setUnlessNull( props, Environment.USE_QUERY_CACHE, queryCacheEnabled ); 318 319 Map extraProperties = getExtraProperties(); 320 if ( extraProperties != null ) { 321 addAll( props, extraProperties ); 322 } 323 324 if ( additionalProperties != null ) { 325 addAll( props, additionalProperties ); 326 } 327 328 return props; 329 } 330 331 protected void addAll( Properties target, Map source ) { 332 Iterator itr = source.entrySet().iterator(); 333 while ( itr.hasNext() ) { 334 final Map.Entry entry = ( Map.Entry ) itr.next(); 335 final String propertyName = ( String ) entry.getKey(); 336 final String propertyValue = ( String ) entry.getValue(); 337 if ( propertyName != null && propertyValue != null ) { 338 if ( !target.keySet().contains( propertyName ) ) { 340 if ( !getExcludedPropertyNames().contains( propertyName) ) { 341 target.put( propertyName, propertyValue ); 342 } 343 } 344 } 345 } 346 } 347 348 protected Map getExtraProperties() { 349 return null; 350 } 351 352 private void setUnlessNull(Properties props, String key, String value) { 353 if ( value != null ) { 354 props.setProperty( key, value ); 355 } 356 } 357 358 private void verifyInt(String value) 359 { 360 if ( value != null ) { 361 Integer.parseInt( value ); 362 } 363 } 364 } 365 | Popular Tags |