001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.hibernate3.annotation;
018:
019: import org.hibernate.HibernateException;
020: import org.hibernate.cfg.AnnotationConfiguration;
021: import org.hibernate.cfg.Configuration;
022:
023: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
024:
025: /**
026: * Subclass of Spring's standard LocalSessionFactoryBean for Hibernate3,
027: * supporting JDK 1.5+ annotation metadata for mappings.
028: * Requires the Hibernate3 Annotation add-on to be present.
029: *
030: * <p>Example bean definition:
031: *
032: * <pre>
033: * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
034: * <property name="dataSource">
035: * <ref bean="dataSource"/>
036: * </property>
037: * <property name="annotatedClasses">
038: * <list>
039: * <value>test.package.Foo</value>
040: * <value>test.package.Bar</value>
041: * </list>
042: * </property>
043: * <property name="annotatedPackages">
044: * <list>
045: * <value>test.package</value>
046: * </list>
047: * </property>
048: * </bean></pre>
049: *
050: * @author Juergen Hoeller
051: * @since 1.2.2
052: * @see #setDataSource
053: * @see #setHibernateProperties
054: * @see #setAnnotatedClasses
055: * @see #setAnnotatedPackages
056: */
057: public class AnnotationSessionFactoryBean extends
058: LocalSessionFactoryBean {
059:
060: private Class[] annotatedClasses;
061:
062: private String[] annotatedPackages;
063:
064: public AnnotationSessionFactoryBean() {
065: setConfigurationClass(AnnotationConfiguration.class);
066: }
067:
068: public void setConfigurationClass(Class configurationClass) {
069: if (configurationClass == null
070: || !AnnotationConfiguration.class
071: .isAssignableFrom(configurationClass)) {
072: throw new IllegalArgumentException(
073: "AnnotationSessionFactoryBean only supports AnnotationConfiguration or subclasses");
074: }
075: super .setConfigurationClass(configurationClass);
076: }
077:
078: /**
079: * Specify annotated classes, for which mappings will be read from
080: * class-level JDK 1.5+ annotation metadata.
081: * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
082: */
083: public void setAnnotatedClasses(Class[] annotatedClasses) {
084: this .annotatedClasses = annotatedClasses;
085: }
086:
087: /**
088: * Specify the names of annotated packages, for which package-level
089: * JDK 1.5+ annotation metadata will be read.
090: * @see org.hibernate.cfg.AnnotationConfiguration#addPackage(String)
091: */
092: public void setAnnotatedPackages(String[] annotatedPackages) {
093: this .annotatedPackages = annotatedPackages;
094: }
095:
096: /**
097: * Reads metadata from annotated classes and packages into the
098: * AnnotationConfiguration instance.
099: * <p>Calls <code>postProcessAnnotationConfiguration</code> afterwards,
100: * to give subclasses the chance to perform custom post-processing.
101: * @see #postProcessAnnotationConfiguration
102: */
103: protected final void postProcessConfiguration(Configuration config)
104: throws HibernateException {
105: AnnotationConfiguration annConfig = (AnnotationConfiguration) config;
106:
107: if (this .annotatedClasses != null) {
108: for (int i = 0; i < this .annotatedClasses.length; i++) {
109: annConfig.addAnnotatedClass(this .annotatedClasses[i]);
110: }
111: }
112: if (this .annotatedPackages != null) {
113: for (int i = 0; i < this .annotatedPackages.length; i++) {
114: annConfig.addPackage(this .annotatedPackages[i]);
115: }
116: }
117:
118: // Perform custom post-processing in subclasses.
119: postProcessAnnotationConfiguration(annConfig);
120: }
121:
122: /**
123: * To be implemented by subclasses that want to to perform custom
124: * post-processing of the AnnotationConfiguration object after this
125: * FactoryBean performed its default initialization.
126: * @param config the current AnnotationConfiguration object
127: * @throws HibernateException in case of Hibernate initialization errors
128: */
129: protected void postProcessAnnotationConfiguration(
130: AnnotationConfiguration config) throws HibernateException {
131: }
132:
133: }
|