KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > ExternalSessionFactoryConfig


1 // $Id: ExternalSessionFactoryConfig.java,v 1.4 2005/04/09 01:41:00 steveebersole Exp $
2
package org.hibernate.util;
3
4 import org.hibernate.cfg.Configuration;
5 import org.hibernate.cfg.Environment;
6
7 import java.util.Properties JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13
14 /**
15  * Defines support for various externally configurable SessionFactory(s), for
16  * example, {@link org.hibernate.jmx.HibernateService JMX} or the JCA
17  * adapter.
18  *
19  * @author Steve Ebersole
20  */

21 public abstract class ExternalSessionFactoryConfig {
22
23     private String JavaDoc mapResources;
24     private String JavaDoc dialect;
25     private String JavaDoc defaultSchema;
26     private String JavaDoc defaultCatalog;
27     private String JavaDoc maximumFetchDepth;
28     private String JavaDoc jdbcFetchSize;
29     private String JavaDoc jdbcBatchSize;
30     private String JavaDoc batchVersionedDataEnabled;
31     private String JavaDoc jdbcScrollableResultSetEnabled;
32     private String JavaDoc getGeneratedKeysEnabled;
33     private String JavaDoc streamsForBinaryEnabled;
34     private String JavaDoc reflectionOptimizationEnabled;
35     private String JavaDoc querySubstitutions;
36     private String JavaDoc showSqlEnabled;
37     private String JavaDoc commentsEnabled;
38     private String JavaDoc secondLevelCacheEnabled;
39     private String JavaDoc cacheProviderClass;
40     private String JavaDoc cacheRegionPrefix;
41     private String JavaDoc minimalPutsEnabled;
42     private String JavaDoc queryCacheEnabled;
43
44     private Map JavaDoc additionalProperties;
45     private Set JavaDoc excludedPropertyNames = new HashSet JavaDoc();
46     private Map JavaDoc customListeners;
47
48
49     protected Set JavaDoc getExcludedPropertyNames() {
50         return excludedPropertyNames;
51     }
52
53     public final String JavaDoc getMapResources() {
54         return mapResources;
55     }
56
57     public final void setMapResources(String JavaDoc mapResources) {
58         this.mapResources = mapResources;
59     }
60
61     public void addMapResource(String JavaDoc 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 JavaDoc getDialect() {
71         return dialect;
72     }
73
74     public final void setDialect(String JavaDoc dialect) {
75         this.dialect = dialect;
76     }
77
78     public final String JavaDoc getDefaultSchema() {
79         return defaultSchema;
80     }
81
82     public final void setDefaultSchema(String JavaDoc defaultSchema) {
83         this.defaultSchema = defaultSchema;
84     }
85
86     public final String JavaDoc getDefaultCatalog() {
87         return defaultCatalog;
88     }
89
90     public final void setDefaultCatalog(String JavaDoc defaultCatalog) {
91         this.defaultCatalog = defaultCatalog;
92     }
93
94     public final String JavaDoc getMaximumFetchDepth() {
95         return maximumFetchDepth;
96     }
97
98     public final void setMaximumFetchDepth(String JavaDoc maximumFetchDepth) {
99         verifyInt( maximumFetchDepth );
100         this.maximumFetchDepth = maximumFetchDepth;
101     }
102
103     public final String JavaDoc getJdbcFetchSize() {
104         return jdbcFetchSize;
105     }
106
107     public final void setJdbcFetchSize(String JavaDoc jdbcFetchSize) {
108         verifyInt( jdbcFetchSize );
109         this.jdbcFetchSize = jdbcFetchSize;
110     }
111
112     public final String JavaDoc getJdbcBatchSize() {
113         return jdbcBatchSize;
114     }
115
116     public final void setJdbcBatchSize(String JavaDoc jdbcBatchSize) {
117         verifyInt( jdbcBatchSize );
118         this.jdbcBatchSize = jdbcBatchSize;
119     }
120
121     public final String JavaDoc getBatchVersionedDataEnabled() {
122         return batchVersionedDataEnabled;
123     }
124
125     public final void setBatchVersionedDataEnabled(String JavaDoc batchVersionedDataEnabled) {
126         this.batchVersionedDataEnabled = batchVersionedDataEnabled;
127     }
128
129     public final String JavaDoc getJdbcScrollableResultSetEnabled() {
130         return jdbcScrollableResultSetEnabled;
131     }
132
133     public final void setJdbcScrollableResultSetEnabled(String JavaDoc jdbcScrollableResultSetEnabled) {
134         this.jdbcScrollableResultSetEnabled = jdbcScrollableResultSetEnabled;
135     }
136
137     public final String JavaDoc getGetGeneratedKeysEnabled() {
138         return getGeneratedKeysEnabled;
139     }
140
141     public final void setGetGeneratedKeysEnabled(String JavaDoc getGeneratedKeysEnabled) {
142         this.getGeneratedKeysEnabled = getGeneratedKeysEnabled;
143     }
144
145     public final String JavaDoc getStreamsForBinaryEnabled() {
146         return streamsForBinaryEnabled;
147     }
148
149     public final void setStreamsForBinaryEnabled(String JavaDoc streamsForBinaryEnabled) {
150         this.streamsForBinaryEnabled = streamsForBinaryEnabled;
151     }
152
153     public final String JavaDoc getReflectionOptimizationEnabled() {
154         return reflectionOptimizationEnabled;
155     }
156
157     public final void setReflectionOptimizationEnabled(String JavaDoc reflectionOptimizationEnabled) {
158         this.reflectionOptimizationEnabled = reflectionOptimizationEnabled;
159     }
160
161     public final String JavaDoc getQuerySubstitutions() {
162         return querySubstitutions;
163     }
164
165     public final void setQuerySubstitutions(String JavaDoc querySubstitutions) {
166         this.querySubstitutions = querySubstitutions;
167     }
168
169     public final String JavaDoc getShowSqlEnabled() {
170         return showSqlEnabled;
171     }
172
173     public final void setShowSqlEnabled(String JavaDoc showSqlEnabled) {
174         this.showSqlEnabled = showSqlEnabled;
175     }
176
177     public final String JavaDoc getCommentsEnabled() {
178         return commentsEnabled;
179     }
180
181     public final void setCommentsEnabled(String JavaDoc commentsEnabled) {
182         this.commentsEnabled = commentsEnabled;
183     }
184
185     public final String JavaDoc getSecondLevelCacheEnabled() {
186         return secondLevelCacheEnabled;
187     }
188
189     public final void setSecondLevelCacheEnabled(String JavaDoc secondLevelCacheEnabled) {
190         this.secondLevelCacheEnabled = secondLevelCacheEnabled;
191     }
192
193     public final String JavaDoc getCacheProviderClass() {
194         return cacheProviderClass;
195     }
196
197     public final void setCacheProviderClass(String JavaDoc cacheProviderClass) {
198         this.cacheProviderClass = cacheProviderClass;
199     }
200
201     public final String JavaDoc getCacheRegionPrefix() {
202         return cacheRegionPrefix;
203     }
204
205     public final void setCacheRegionPrefix(String JavaDoc cacheRegionPrefix) {
206         this.cacheRegionPrefix = cacheRegionPrefix;
207     }
208
209     public final String JavaDoc getMinimalPutsEnabled() {
210         return minimalPutsEnabled;
211     }
212
213     public final void setMinimalPutsEnabled(String JavaDoc minimalPutsEnabled) {
214         this.minimalPutsEnabled = minimalPutsEnabled;
215     }
216
217     public final String JavaDoc getQueryCacheEnabled() {
218         return queryCacheEnabled;
219     }
220
221     public final void setQueryCacheEnabled(String JavaDoc queryCacheEnabled) {
222         this.queryCacheEnabled = queryCacheEnabled;
223     }
224
225     public final Map JavaDoc getCustomListeners() {
226         return customListeners;
227     }
228
229     public void setCustomListeners(Map JavaDoc customListeners) {
230         this.customListeners = customListeners;
231     }
232
233     public void setCustomListenersAsString(String JavaDoc customListenersString) {
234         // Note : expected in the syntax:
235
// type=listenerClass
236
// ({sep}type=listenerClass)*
237
// where {sep} is any whitespace or comma
238
if ( StringHelper.isNotEmpty( customListenersString) ) {
239             String JavaDoc[] 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 JavaDoc type = listenerEntries[i].substring( 0, keyValueSepPosition );
243                 final String JavaDoc listenerClass = listenerEntries[i].substring( keyValueSepPosition + 1 );
244                 setCustomListener( type, listenerClass );
245             }
246         }
247     }
248
249     public void setCustomListener(String JavaDoc type, String JavaDoc listenerClass) {
250         if ( customListeners == null ) {
251             customListeners = new HashMap JavaDoc();
252         }
253         customListeners.put( type, listenerClass );
254     }
255
256     public final void addAdditionalProperty(String JavaDoc name, String JavaDoc value) {
257         if ( !getExcludedPropertyNames().contains( name ) ) {
258             if ( additionalProperties == null ) {
259                 additionalProperties = new HashMap JavaDoc();
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 JavaDoc[] 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 JavaDoc entries = customListeners.entrySet().iterator();
277             while ( entries.hasNext() ) {
278                 final Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) entries.next();
279                 final String JavaDoc type = ( String JavaDoc ) entry.getKey();
280                 final Object JavaDoc value = entry.getValue();
281                 if ( value != null ) {
282                     if ( String JavaDoc.class.isAssignableFrom( value.getClass() ) ) {
283                         // Its the listener class name
284
cfg.setListener( type, ( ( String JavaDoc ) value ) );
285                     }
286                     else {
287                         // Its the listener instance (or better be)
288
cfg.setListener( type, value );
289                     }
290                 }
291             }
292         }
293
294         return cfg;
295     }
296
297     protected final Properties JavaDoc buildProperties() {
298         Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc 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 JavaDoc target, Map JavaDoc source ) {
332         Iterator JavaDoc itr = source.entrySet().iterator();
333         while ( itr.hasNext() ) {
334             final Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) itr.next();
335             final String JavaDoc propertyName = ( String JavaDoc ) entry.getKey();
336             final String JavaDoc propertyValue = ( String JavaDoc ) entry.getValue();
337             if ( propertyName != null && propertyValue != null ) {
338                 // Make sure we don't override previous set values
339
if ( !target.keySet().contains( propertyName ) ) {
340                     if ( !getExcludedPropertyNames().contains( propertyName) ) {
341                         target.put( propertyName, propertyValue );
342                     }
343                 }
344             }
345         }
346     }
347
348     protected Map JavaDoc getExtraProperties() {
349         return null;
350     }
351
352     private void setUnlessNull(Properties JavaDoc props, String JavaDoc key, String JavaDoc value) {
353         if ( value != null ) {
354             props.setProperty( key, value );
355         }
356     }
357
358     private void verifyInt(String JavaDoc value)
359     {
360         if ( value != null ) {
361             Integer.parseInt( value );
362         }
363     }
364 }
365
Popular Tags