KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > annotation > AnnotationSessionFactoryBean


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.hibernate3.annotation;
18
19 import org.hibernate.HibernateException;
20 import org.hibernate.cfg.AnnotationConfiguration;
21 import org.hibernate.cfg.Configuration;
22
23 import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
24
25 /**
26  * Subclass of Spring's standard LocalSessionFactoryBean for Hibernate3,
27  * supporting JDK 1.5+ annotation metadata for mappings.
28  * Requires the Hibernate3 Annotation add-on to be present.
29  *
30  * <p>Example bean definition:
31  *
32  * <pre>
33  * &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
34  * &lt;property name="dataSource"&gt;
35  * &lt;ref bean="dataSource"/&gt;
36  * &lt;/property&gt;
37  * &lt;property name="annotatedClasses"&gt;
38  * &lt;list&gt;
39  * &lt;value&gt;test.package.Foo&lt;/value&gt;
40  * &lt;value&gt;test.package.Bar&lt;/value&gt;
41  * &lt;/list&gt;
42  * &lt;/property&gt;
43  * &lt;property name="annotatedPackages"&gt;
44  * &lt;list&gt;
45  * &lt;value&gt;test.package&lt;/value&gt;
46  * &lt;/list&gt;
47  * &lt;/property&gt;
48  * &lt;/bean&gt;</pre>
49  *
50  * @author Juergen Hoeller
51  * @since 1.2.2
52  * @see #setDataSource
53  * @see #setHibernateProperties
54  * @see #setAnnotatedClasses
55  * @see #setAnnotatedPackages
56  */

57 public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean {
58
59     private Class JavaDoc[] annotatedClasses;
60
61     private String JavaDoc[] annotatedPackages;
62
63
64     public AnnotationSessionFactoryBean() {
65         setConfigurationClass(AnnotationConfiguration.class);
66     }
67
68     public void setConfigurationClass(Class JavaDoc configurationClass) {
69         if (configurationClass == null || !AnnotationConfiguration.class.isAssignableFrom(configurationClass)) {
70             throw new IllegalArgumentException JavaDoc(
71                     "AnnotationSessionFactoryBean only supports AnnotationConfiguration or subclasses");
72         }
73         super.setConfigurationClass(configurationClass);
74     }
75
76     /**
77      * Specify annotated classes, for which mappings will be read from
78      * class-level JDK 1.5+ annotation metadata.
79      * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
80      */

81     public void setAnnotatedClasses(Class JavaDoc[] annotatedClasses) {
82         this.annotatedClasses = annotatedClasses;
83     }
84
85     /**
86      * Specify the names of annotated packages, for which package-level
87      * JDK 1.5+ annotation metadata will be read.
88      * @see org.hibernate.cfg.AnnotationConfiguration#addPackage(String)
89      */

90     public void setAnnotatedPackages(String JavaDoc[] annotatedPackages) {
91         this.annotatedPackages = annotatedPackages;
92     }
93
94
95     /**
96      * Reads metadata from annotated classes and packages into the
97      * AnnotationConfiguration instance.
98      * <p>Calls <code>postProcessAnnotationConfiguration</code> afterwards,
99      * to give subclasses the chance to perform custom post-processing.
100      * @see #postProcessAnnotationConfiguration
101      */

102     protected final void postProcessConfiguration(Configuration config) throws HibernateException {
103         AnnotationConfiguration annConfig = (AnnotationConfiguration) config;
104
105         if (this.annotatedClasses != null) {
106             for (int i = 0; i < this.annotatedClasses.length; i++) {
107                 annConfig.addAnnotatedClass(this.annotatedClasses[i]);
108             }
109         }
110         if (this.annotatedPackages != null) {
111             for (int i = 0; i < this.annotatedPackages.length; i++) {
112                 annConfig.addPackage(this.annotatedPackages[i]);
113             }
114         }
115
116         // Perform custom post-processing in subclasses.
117
postProcessAnnotationConfiguration(annConfig);
118     }
119
120     /**
121      * To be implemented by subclasses that want to to perform custom
122      * post-processing of the AnnotationConfiguration object after this
123      * FactoryBean performed its default initialization.
124      * @param config the current AnnotationConfiguration object
125      * @throws HibernateException in case of Hibernate initialization errors
126      */

127     protected void postProcessAnnotationConfiguration(AnnotationConfiguration config)
128             throws HibernateException {
129     }
130
131 }
132
Popular Tags