/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.orm.hibernate3.annotation; import java.io.IOException; import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternUtils; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.TypeFilter; import org.springframework.orm.hibernate3.LocalSessionFactoryBean; import org.springframework.util.ClassUtils; /** * Subclass of Spring's standard LocalSessionFactoryBean for Hibernate, * supporting JDK 1.5+ annotation metadata for mappings. * * <p>Note: This class requires Hibernate 3.2 or higher, with the * Java Persistence API and the Hibernate Annotations add-on present. * * <p>Example for an AnnotationSessionFactoryBean bean definition: * * <pre class="code"> * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> * <property name="dataSource" ref="dataSource"/> * <property name="annotatedClasses"> * <list> * <value>test.package.Foo</value> * <value>test.package.Bar</value> * </list> * </property> * </bean></pre> * * Or when using classpath scanning for autodetection of entity classes: * * <pre class="code"> * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> * <property name="dataSource" ref="dataSource"/> * <property name="packagesToScan" value="test.package"/> * </bean></pre> * * @author Juergen Hoeller * @since 1.2.2 * @see #setDataSource * @see #setHibernateProperties * @see #setAnnotatedClasses * @see #setAnnotatedPackages */ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implements ResourceLoaderAware { private static final String RESOURCE_PATTERN = "/**/*.class"; private Class[] annotatedClasses; private String[] annotatedPackages; private String[] packagesToScan; private TypeFilter[] entityTypeFilters = new TypeFilter[] { new AnnotationTypeFilter(Entity.class, false), new AnnotationTypeFilter(Embeddable.class, false), new AnnotationTypeFilter(MappedSuperclass.class, false)}; private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); public AnnotationSessionFactoryBean() { setConfigurationClass(AnnotationConfiguration.class); } public void setConfigurationClass(Class configurationClass) { if (configurationClass == null || !AnnotationConfiguration.class.isAssignableFrom(configurationClass)) { throw new IllegalArgumentException( "AnnotationSessionFactoryBean only supports AnnotationConfiguration or subclasses"); } super.setConfigurationClass(configurationClass); } /** * Specify annotated classes, for which mappings will be read from * class-level JDK 1.5+ annotation metadata. * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class) */ public void setAnnotatedClasses(Class[] annotatedClasses) { this.annotatedClasses = annotatedClasses; } /** * Specify the names of annotated packages, for which package-level * JDK 1.5+ annotation metadata will be read. * @see org.hibernate.cfg.AnnotationConfiguration#addPackage(String) */ public void setAnnotatedPackages(String[] annotatedPackages) { this.annotatedPackages = annotatedPackages; } /** * Set whether to use Spring-based scanning for entity classes in the classpath * instead of listing annotated classes explicitly. * <p>Default is none. Specify packages to search for autodetection of your entity * classes in the classpath. This is analogous to Spring's component-scan feature * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}). */ public void setPackagesToScan(String[] packagesToScan) { this.packagesToScan = packagesToScan; } /** * Specify custom type filters for Spring-based scanning for entity classes. * <p>Default is to search all specified packages for classes annotated with * <code>@javax.persistence.Entity</code>, <code>@javax.persistence.Embeddable</code> * or <code>@javax.persistence.MappedSuperclass</code>. * @see #setPackagesToScan */ public void setEntityTypeFilters(TypeFilter[] entityTypeFilters) { this.entityTypeFilters = entityTypeFilters; } public void setResourceLoader(ResourceLoader resourceLoader) { this.resourcePatternResolver = (resourceLoader != null ? ResourcePatternUtils.getResourcePatternResolver(resourceLoader) : new PathMatchingResourcePatternResolver()); } /** * Reads metadata from annotated classes and packages into the * AnnotationConfiguration instance. */ protected void postProcessMappings(Configuration config) throws HibernateException { AnnotationConfiguration annConfig = (AnnotationConfiguration) config; if (this.annotatedClasses != null) { for (int i = 0; i < this.annotatedClasses.length; i++) { annConfig.addAnnotatedClass(this.annotatedClasses[i]); } } if (this.annotatedPackages != null) { for (int i = 0; i < this.annotatedPackages.length; i++) { annConfig.addPackage(this.annotatedPackages[i]); } } scanPackages(annConfig); } /** * Perform Spring-based scanning for entity classes. * @see #setPackagesToScan */ protected void scanPackages(AnnotationConfiguration config) { if (this.packagesToScan != null) { try { for (String pkg : this.packagesToScan) { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN; Resource[] resources = this.resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); String className = reader.getClassMetadata().getClassName(); if (matchesFilter(reader, readerFactory)) { config.addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className)); } } } } } catch (IOException ex) { throw new MappingException("Failed to scan classpath for unlisted classes", ex); } catch (ClassNotFoundException ex) { throw new MappingException("Failed to load annotated classes from classpath", ex); } } } /** * Check whether any of the configured entity type filters matches * the current class descriptor contained in the metadata reader. */ private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { if (this.entityTypeFilters != null) { for (TypeFilter filter : this.entityTypeFilters) { if (filter.match(reader, readerFactory)) { return true; } } } return false; } /** * Delegates to {@link #postProcessAnnotationConfiguration}. */ protected final void postProcessConfiguration(Configuration config) throws HibernateException { postProcessAnnotationConfiguration((AnnotationConfiguration) config); } /** * To be implemented by subclasses that want to to perform custom * post-processing of the AnnotationConfiguration object after this * FactoryBean performed its default initialization. * @param config the current AnnotationConfiguration object * @throws HibernateException in case of Hibernate initialization errors */ protected void postProcessAnnotationConfiguration(AnnotationConfiguration config) throws HibernateException { } }