HHH-6118 - Make Metadata more user-friendly API
This commit is contained in:
parent
0f336414a2
commit
8edc1f1880
|
@ -24,148 +24,8 @@
|
|||
|
||||
package org.hibernate.metamodel.source;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface Metadata {
|
||||
/**
|
||||
* Read metadata from the annotations attached to the given class.
|
||||
*
|
||||
* @param annotatedClass The class containing annotations
|
||||
*
|
||||
* @return this (for method chaining)
|
||||
*/
|
||||
public Metadata addAnnotatedClass(Class annotatedClass);
|
||||
|
||||
/**
|
||||
* Read package-level metadata.
|
||||
*
|
||||
* @param packageName java package name
|
||||
*
|
||||
* @return this (for method chaining)
|
||||
*/
|
||||
public Metadata addPackage(String packageName);
|
||||
|
||||
/**
|
||||
* Read mappings as a application resourceName (i.e. classpath lookup).
|
||||
*
|
||||
* @param name The resource name
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addResource(String name);
|
||||
|
||||
/**
|
||||
* Read a mapping as an application resource using the convention that a class named {@code foo.bar.Foo} is
|
||||
* mapped by a file named {@code foo/bar/Foo.hbm.xml} which can be resolved as a classpath resource.
|
||||
*
|
||||
* @param entityClass The mapped class
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addClass(Class entityClass);
|
||||
|
||||
/**
|
||||
* Read mappings from a particular XML file
|
||||
*
|
||||
* @param path The path to a file. Expected to be resolvable by {@link File#File(String)}
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*
|
||||
* @see #addFile(java.io.File)
|
||||
*/
|
||||
public Metadata addFile(String path);
|
||||
|
||||
/**
|
||||
* Read mappings from a particular XML file
|
||||
*
|
||||
* @param file The reference to the XML file
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addFile(File file);
|
||||
|
||||
/**
|
||||
* See {@link #addCacheableFile(java.io.File)} for description
|
||||
*
|
||||
* @param path The path to a file. Expected to be resolvable by {@link File#File(String)}
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*
|
||||
* @see #addCacheableFile(java.io.File)
|
||||
*/
|
||||
public Metadata addCacheableFile(String path);
|
||||
|
||||
/**
|
||||
* Add a cached mapping file. A cached file is a serialized representation of the DOM structure of a
|
||||
* particular mapping. It is saved from a previous call as a file with the name {@code {xmlFile}.bin}
|
||||
* where {@code {xmlFile}} is the name of the original mapping file.
|
||||
* </p>
|
||||
* If a cached {@code {xmlFile}.bin} exists and is newer than {@code {xmlFile}}, the {@code {xmlFile}.bin}
|
||||
* file will be read directly. Otherwise {@code {xmlFile}} is read and then serialized to {@code {xmlFile}.bin} for
|
||||
* use the next time.
|
||||
*
|
||||
* @param file The cacheable mapping file to be added, {@code {xmlFile}} in above discussion.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addCacheableFile(File file);
|
||||
|
||||
/**
|
||||
* Read metadata from an {@link InputStream}.
|
||||
*
|
||||
* @param xmlInputStream The input stream containing a DOM.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addInputStream(InputStream xmlInputStream);
|
||||
|
||||
|
||||
/**
|
||||
* Read mappings from a {@link URL}
|
||||
*
|
||||
* @param url The url for the mapping document to be read.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addURL(URL url);
|
||||
|
||||
/**
|
||||
* Read mappings from a DOM {@link Document}
|
||||
*
|
||||
* @param doc The DOM document
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addDocument(Document doc);
|
||||
|
||||
|
||||
/**
|
||||
* Read all mappings from a jar file.
|
||||
* <p/>
|
||||
* Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
|
||||
*
|
||||
* @param jar a jar file
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public Metadata addJar(File jar);
|
||||
|
||||
/**
|
||||
* Read all mapping documents from a directory tree.
|
||||
* <p/>
|
||||
* Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
|
||||
*
|
||||
* @param dir The directory
|
||||
* @return this (for method chaining purposes)
|
||||
* @throws org.hibernate.MappingException Indicates problems reading the jar file or
|
||||
* processing the contained mapping documents.
|
||||
*/
|
||||
public Metadata addDirectory(File dir);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,347 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.metamodel.source;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.EntityResolver;
|
||||
|
||||
import org.hibernate.cfg.EJB3DTDEntityResolver;
|
||||
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||
import org.hibernate.cfg.NamingStrategy;
|
||||
import org.hibernate.metamodel.source.internal.JaxbHelper;
|
||||
import org.hibernate.metamodel.source.internal.JaxbRoot;
|
||||
import org.hibernate.metamodel.source.internal.MetadataImpl;
|
||||
import org.hibernate.service.BasicServiceRegistry;
|
||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class MetadataSources {
|
||||
private static final Logger LOG = Logger.getLogger( MetadataSources.class );
|
||||
|
||||
private List<JaxbRoot> jaxbRootList = new ArrayList<JaxbRoot>();
|
||||
|
||||
private final JaxbHelper jaxbHelper;
|
||||
|
||||
private final BasicServiceRegistry serviceRegistry;
|
||||
private final EntityResolver entityResolver;
|
||||
private final NamingStrategy namingStrategy;
|
||||
|
||||
public MetadataSources(BasicServiceRegistry serviceRegistry) {
|
||||
this( serviceRegistry, EJB3DTDEntityResolver.INSTANCE, EJB3NamingStrategy.INSTANCE );
|
||||
}
|
||||
|
||||
public MetadataSources(BasicServiceRegistry serviceRegistry, EntityResolver entityResolver, NamingStrategy namingStrategy) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.entityResolver = entityResolver;
|
||||
this.namingStrategy = namingStrategy;
|
||||
|
||||
this.jaxbHelper = new JaxbHelper( this );
|
||||
}
|
||||
|
||||
public List<JaxbRoot> getJaxbRootList() {
|
||||
return jaxbRootList;
|
||||
}
|
||||
|
||||
public BasicServiceRegistry getServiceRegistry() {
|
||||
return serviceRegistry;
|
||||
}
|
||||
|
||||
public NamingStrategy getNamingStrategy() {
|
||||
return namingStrategy;
|
||||
}
|
||||
|
||||
public Metadata buildMetadata() {
|
||||
return new MetadataImpl( this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Read metadata from the annotations attached to the given class.
|
||||
*
|
||||
* @param annotatedClass The class containing annotations
|
||||
*
|
||||
* @return this (for method chaining)
|
||||
*/
|
||||
public MetadataSources addAnnotatedClass(Class annotatedClass) {
|
||||
return this; // todo : implement method body
|
||||
}
|
||||
|
||||
/**
|
||||
* Read package-level metadata.
|
||||
*
|
||||
* @param packageName java package name
|
||||
*
|
||||
* @return this (for method chaining)
|
||||
*/
|
||||
public MetadataSources addPackage(String packageName) {
|
||||
return this; // todo : implement method body
|
||||
}
|
||||
|
||||
/**
|
||||
* Read mappings as a application resourceName (i.e. classpath lookup).
|
||||
*
|
||||
* @param name The resource name
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addResource(String name) {
|
||||
LOG.tracef( "reading mappings from resource : %s", name );
|
||||
|
||||
final Origin origin = new Origin( SourceType.RESOURCE, name );
|
||||
InputStream resourceInputStream = classLoaderService().locateResourceStream( name );
|
||||
if ( resourceInputStream == null ) {
|
||||
throw new MappingNotFoundException( origin );
|
||||
}
|
||||
add( resourceInputStream, origin, true );
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private ClassLoaderService classLoaderService() {
|
||||
return serviceRegistry.getService( ClassLoaderService.class );
|
||||
}
|
||||
|
||||
private JaxbRoot add(InputStream inputStream, Origin origin, boolean close) {
|
||||
try {
|
||||
JaxbRoot jaxbRoot = jaxbHelper.unmarshal( inputStream, origin );
|
||||
jaxbRootList.add( jaxbRoot );
|
||||
return jaxbRoot;
|
||||
}
|
||||
finally {
|
||||
if ( close ) {
|
||||
try {
|
||||
inputStream.close();
|
||||
}
|
||||
catch ( IOException ignore ) {
|
||||
LOG.trace( "Was unable to close input stream" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a mapping as an application resource using the convention that a class named {@code foo.bar.Foo} is
|
||||
* mapped by a file named {@code foo/bar/Foo.hbm.xml} which can be resolved as a classpath resource.
|
||||
*
|
||||
* @param entityClass The mapped class
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addClass(Class entityClass) {
|
||||
LOG.debugf( "adding resource mappings from class convention : %s", entityClass.getName() );
|
||||
final String mappingResourceName = entityClass.getName().replace( '.', '/' ) + ".hbm.xml";
|
||||
addResource( mappingResourceName );
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read mappings from a particular XML file
|
||||
*
|
||||
* @param path The path to a file. Expected to be resolvable by {@link File#File(String)}
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*
|
||||
* @see #addFile(java.io.File)
|
||||
*/
|
||||
public MetadataSources addFile(String path) {
|
||||
return addFile( new File( path ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Read mappings from a particular XML file
|
||||
*
|
||||
* @param file The reference to the XML file
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addFile(File file) {
|
||||
final String name = file.getAbsolutePath();
|
||||
LOG.tracef( "reading mappings from file : %s", name );
|
||||
final Origin origin = new Origin( SourceType.FILE, name );
|
||||
try {
|
||||
add( new FileInputStream( file ), origin, true );
|
||||
}
|
||||
catch ( FileNotFoundException e ) {
|
||||
throw new MappingNotFoundException( e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link #addCacheableFile(java.io.File)} for description
|
||||
*
|
||||
* @param path The path to a file. Expected to be resolvable by {@link File#File(String)}
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*
|
||||
* @see #addCacheableFile(java.io.File)
|
||||
*/
|
||||
public MetadataSources addCacheableFile(String path) {
|
||||
return this; // todo : implement method body
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a cached mapping file. A cached file is a serialized representation of the DOM structure of a
|
||||
* particular mapping. It is saved from a previous call as a file with the name {@code {xmlFile}.bin}
|
||||
* where {@code {xmlFile}} is the name of the original mapping file.
|
||||
* </p>
|
||||
* If a cached {@code {xmlFile}.bin} exists and is newer than {@code {xmlFile}}, the {@code {xmlFile}.bin}
|
||||
* file will be read directly. Otherwise {@code {xmlFile}} is read and then serialized to {@code {xmlFile}.bin} for
|
||||
* use the next time.
|
||||
*
|
||||
* @param file The cacheable mapping file to be added, {@code {xmlFile}} in above discussion.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addCacheableFile(File file) {
|
||||
return this; // todo : implement method body
|
||||
}
|
||||
|
||||
/**
|
||||
* Read metadata from an {@link InputStream}.
|
||||
*
|
||||
* @param xmlInputStream The input stream containing a DOM.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addInputStream(InputStream xmlInputStream) {
|
||||
add( xmlInputStream, new Origin( SourceType.INPUT_STREAM, "<unknown>" ), false );
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read mappings from a {@link URL}
|
||||
*
|
||||
* @param url The url for the mapping document to be read.
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addURL(URL url) {
|
||||
final String urlExternalForm = url.toExternalForm();
|
||||
LOG.debugf( "Reading mapping document from URL : %s", urlExternalForm );
|
||||
|
||||
final Origin origin = new Origin( SourceType.URL, urlExternalForm );
|
||||
try {
|
||||
add( url.openStream(), origin, true );
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new MappingNotFoundException( "Unable to open url stream [" + urlExternalForm + "]", e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read mappings from a DOM {@link Document}
|
||||
*
|
||||
* @param document The DOM document
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addDocument(Document document) {
|
||||
final Origin origin = new Origin( SourceType.DOM, "<unknown>" );
|
||||
JaxbRoot jaxbRoot = jaxbHelper.unmarshal( document, origin );
|
||||
jaxbRootList.add( jaxbRoot );
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all mappings from a jar file.
|
||||
* <p/>
|
||||
* Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
|
||||
*
|
||||
* @param jar a jar file
|
||||
*
|
||||
* @return this (for method chaining purposes)
|
||||
*/
|
||||
public MetadataSources addJar(File jar) {
|
||||
LOG.debugf( "Seeking mapping documents in jar file : %s", jar.getName() );
|
||||
final Origin origin = new Origin( SourceType.JAR, jar.getAbsolutePath() );
|
||||
try {
|
||||
JarFile jarFile = new JarFile( jar );
|
||||
try {
|
||||
Enumeration jarEntries = jarFile.entries();
|
||||
while ( jarEntries.hasMoreElements() ) {
|
||||
final ZipEntry zipEntry = (ZipEntry) jarEntries.nextElement();
|
||||
if ( zipEntry.getName().endsWith( ".hbm.xml" ) ) {
|
||||
LOG.tracef( "found mapping document : %s", zipEntry.getName() );
|
||||
try {
|
||||
add( jarFile.getInputStream( zipEntry ), origin, true );
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MappingException( "could not read mapping documents", e, origin );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
jarFile.close();
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MappingNotFoundException( e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all mapping documents from a directory tree.
|
||||
* <p/>
|
||||
* Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
|
||||
*
|
||||
* @param dir The directory
|
||||
* @return this (for method chaining purposes)
|
||||
* @throws org.hibernate.MappingException Indicates problems reading the jar file or
|
||||
* processing the contained mapping documents.
|
||||
*/
|
||||
public MetadataSources addDirectory(File dir) {
|
||||
File[] files = dir.listFiles();
|
||||
for ( File file : files ) {
|
||||
if ( file.isDirectory() ) {
|
||||
addDirectory( file );
|
||||
}
|
||||
else if ( file.getName().endsWith( ".hbm.xml" ) ) {
|
||||
addFile( file );
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -48,6 +48,7 @@ import org.w3c.dom.Element;
|
|||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.hibernate.metamodel.source.MappingException;
|
||||
import org.hibernate.metamodel.source.MetadataSources;
|
||||
import org.hibernate.metamodel.source.Origin;
|
||||
import org.hibernate.metamodel.source.XsdException;
|
||||
import org.hibernate.metamodel.source.annotation.xml.EntityMappings;
|
||||
|
@ -57,15 +58,15 @@ import org.hibernate.service.classloading.spi.ClassLoaderService;
|
|||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
class JaxbHelper {
|
||||
public class JaxbHelper {
|
||||
private static final Logger log = Logger.getLogger( JaxbHelper.class );
|
||||
|
||||
public static final String ASSUMED_ORM_XSD_VERSION = "2.0";
|
||||
|
||||
private final MetadataImpl metadata;
|
||||
private final MetadataSources metadataSources;
|
||||
|
||||
JaxbHelper(MetadataImpl metadata) {
|
||||
this.metadata = metadata;
|
||||
public JaxbHelper(MetadataSources metadataSources) {
|
||||
this.metadataSources = metadataSources;
|
||||
}
|
||||
|
||||
public JaxbRoot unmarshal(InputStream stream, Origin origin) {
|
||||
|
@ -233,7 +234,7 @@ class JaxbHelper {
|
|||
}
|
||||
|
||||
private Schema resolveLocalSchema(String schemaName, String schemaLanguage) {
|
||||
URL url = metadata.getServiceRegistry().getService( ClassLoaderService.class ).locateResource( schemaName );
|
||||
URL url = metadataSources.getServiceRegistry().getService( ClassLoaderService.class ).locateResource( schemaName );
|
||||
if ( url == null ) {
|
||||
throw new XsdException( "Unable to locate schema [" + schemaName + "] via classpath", schemaName );
|
||||
}
|
||||
|
|
|
@ -23,28 +23,13 @@
|
|||
*/
|
||||
package org.hibernate.metamodel.source.internal;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.EntityResolver;
|
||||
|
||||
import org.hibernate.DuplicateMappingException;
|
||||
import org.hibernate.cfg.EJB3DTDEntityResolver;
|
||||
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||
import org.hibernate.cfg.NamingStrategy;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.mapping.FetchProfile;
|
||||
|
@ -52,16 +37,12 @@ import org.hibernate.mapping.MetadataSource;
|
|||
import org.hibernate.metamodel.binding.EntityBinding;
|
||||
import org.hibernate.metamodel.binding.PluralAttributeBinding;
|
||||
import org.hibernate.metamodel.relational.Database;
|
||||
import org.hibernate.metamodel.source.MappingException;
|
||||
import org.hibernate.metamodel.source.MappingNotFoundException;
|
||||
import org.hibernate.metamodel.source.Metadata;
|
||||
import org.hibernate.metamodel.source.Origin;
|
||||
import org.hibernate.metamodel.source.SourceType;
|
||||
import org.hibernate.metamodel.source.MetadataSources;
|
||||
import org.hibernate.metamodel.source.annotations.AnnotationBinder;
|
||||
import org.hibernate.metamodel.source.hbm.HibernateXmlBinder;
|
||||
import org.hibernate.metamodel.source.spi.MetadataImplementor;
|
||||
import org.hibernate.service.BasicServiceRegistry;
|
||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
||||
|
||||
/**
|
||||
* Container for configuration data while building and binding the metamodel
|
||||
|
@ -72,44 +53,30 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable
|
|||
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, MetadataImpl.class.getName() );
|
||||
|
||||
private final BasicServiceRegistry serviceRegistry;
|
||||
private final NamingStrategy namingStrategy;
|
||||
|
||||
private final Database database = new Database();
|
||||
|
||||
// private final MetadataSourceQueue metadataSourceQueue;
|
||||
|
||||
private final JaxbHelper jaxbHelper;
|
||||
private final AnnotationBinder annotationBinder;
|
||||
private final HibernateXmlBinder hibernateXmlBinder;
|
||||
|
||||
private final EntityResolver entityResolver;
|
||||
private final NamingStrategy namingStrategy;
|
||||
private final Database database = new Database();
|
||||
|
||||
private Map<String, EntityBinding> entityBindingMap = new HashMap<String, EntityBinding>();
|
||||
private Map<String, PluralAttributeBinding> collectionBindingMap = new HashMap<String, PluralAttributeBinding>();
|
||||
private Map<String, FetchProfile> fetchProfiles = new HashMap<String, FetchProfile>();
|
||||
private Map<String, String> imports;
|
||||
|
||||
public MetadataImpl(BasicServiceRegistry serviceRegistry) {
|
||||
this( serviceRegistry, EJB3NamingStrategy.INSTANCE, EJB3DTDEntityResolver.INSTANCE );
|
||||
}
|
||||
public MetadataImpl(MetadataSources metadataSources) {
|
||||
this.serviceRegistry = metadataSources.getServiceRegistry();
|
||||
this.namingStrategy = metadataSources.getNamingStrategy();
|
||||
|
||||
public MetadataImpl(BasicServiceRegistry serviceRegistry, NamingStrategy namingStrategy, EntityResolver entityResolver) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.namingStrategy = namingStrategy;
|
||||
this.entityResolver = entityResolver;
|
||||
this.jaxbHelper = new JaxbHelper( this );
|
||||
this.annotationBinder = new AnnotationBinder( this );
|
||||
this.hibernateXmlBinder = new HibernateXmlBinder( this );
|
||||
// this.metadataSourceQueue = new MetadataSourceQueue( this );
|
||||
}
|
||||
|
||||
public BasicServiceRegistry getServiceRegistry() {
|
||||
return serviceRegistry;
|
||||
}
|
||||
|
||||
public EntityResolver getEntityResolver() {
|
||||
return entityResolver;
|
||||
}
|
||||
|
||||
public HibernateXmlBinder getHibernateXmlBinder() {
|
||||
return hibernateXmlBinder;
|
||||
}
|
||||
|
@ -118,10 +85,6 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable
|
|||
return annotationBinder;
|
||||
}
|
||||
|
||||
// public MetadataSourceQueue getMetadataSourceQueue() {
|
||||
// return metadataSourceQueue;
|
||||
// }
|
||||
|
||||
public Database getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
@ -188,175 +151,4 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable
|
|||
return profile;
|
||||
}
|
||||
|
||||
// Metadata contract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
private List<JaxbRoot> jaxbRootList = new ArrayList<JaxbRoot>();
|
||||
|
||||
/**
|
||||
* TODO : STRICTLY FOR TESTING PURPOSES, REMOVE AFTER JAXB AND BINDING STUFF HAS BEEN VALIDATED!!!!!!!
|
||||
*/
|
||||
public List<JaxbRoot> getJaxbRootList() {
|
||||
return jaxbRootList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addAnnotatedClass(Class annotatedClass) {
|
||||
return null; // todo : implement method body
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addPackage(String packageName) {
|
||||
return null; // todo : implement method body
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addResource(String name) {
|
||||
LOG.tracef( "reading mappings from resource : %s", name );
|
||||
|
||||
final Origin origin = new Origin( SourceType.RESOURCE, name );
|
||||
InputStream resourceInputStream = classLoaderService().locateResourceStream( name );
|
||||
if ( resourceInputStream == null ) {
|
||||
throw new MappingNotFoundException( origin );
|
||||
}
|
||||
add( resourceInputStream, origin, true );
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private ClassLoaderService classLoaderService() {
|
||||
return serviceRegistry.getService( ClassLoaderService.class );
|
||||
}
|
||||
|
||||
private JaxbRoot add(InputStream inputStream, Origin origin, boolean close) {
|
||||
try {
|
||||
JaxbRoot jaxbRoot = jaxbHelper.unmarshal( inputStream, origin );
|
||||
jaxbRootList.add( jaxbRoot );
|
||||
return jaxbRoot;
|
||||
}
|
||||
finally {
|
||||
if ( close ) {
|
||||
try {
|
||||
inputStream.close();
|
||||
}
|
||||
catch ( IOException ignore ) {
|
||||
LOG.trace( "Was unable to close input stream" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addClass(Class entityClass) {
|
||||
LOG.debugf( "adding resource mappings from class convention : %s", entityClass.getName() );
|
||||
final String mappingResourceName = entityClass.getName().replace( '.', '/' ) + ".hbm.xml";
|
||||
addResource( mappingResourceName );
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addFile(String path) {
|
||||
return addFile( new File( path ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addFile(File file) {
|
||||
final String name = file.getAbsolutePath();
|
||||
LOG.tracef( "reading mappings from file : %s", name );
|
||||
final Origin origin = new Origin( SourceType.FILE, name );
|
||||
try {
|
||||
add( new FileInputStream( file ), origin, true );
|
||||
}
|
||||
catch ( FileNotFoundException e ) {
|
||||
throw new MappingNotFoundException( e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addCacheableFile(String path) {
|
||||
return null; // todo : implement method body
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addCacheableFile(File file) {
|
||||
return null; // todo : implement method body
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addInputStream(InputStream xmlInputStream) {
|
||||
add( xmlInputStream, new Origin( SourceType.INPUT_STREAM, "<unknown>" ), false );
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addURL(URL url) {
|
||||
final String urlExternalForm = url.toExternalForm();
|
||||
LOG.debugf( "Reading mapping document from URL : %s", urlExternalForm );
|
||||
|
||||
final Origin origin = new Origin( SourceType.URL, urlExternalForm );
|
||||
try {
|
||||
add( url.openStream(), origin, true );
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new MappingNotFoundException( "Unable to open url stream [" + urlExternalForm + "]", e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addDocument(Document document) {
|
||||
final Origin origin = new Origin( SourceType.DOM, "<unknown>" );
|
||||
JaxbRoot jaxbRoot = jaxbHelper.unmarshal( document, origin );
|
||||
jaxbRootList.add( jaxbRoot );
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addJar(File jar) {
|
||||
LOG.debugf( "Seeking mapping documents in jar file : %s", jar.getName() );
|
||||
final Origin origin = new Origin( SourceType.JAR, jar.getAbsolutePath() );
|
||||
try {
|
||||
JarFile jarFile = new JarFile( jar );
|
||||
try {
|
||||
Enumeration jarEntries = jarFile.entries();
|
||||
while ( jarEntries.hasMoreElements() ) {
|
||||
final ZipEntry zipEntry = (ZipEntry) jarEntries.nextElement();
|
||||
if ( zipEntry.getName().endsWith( ".hbm.xml" ) ) {
|
||||
LOG.tracef( "found mapping document : %s", zipEntry.getName() );
|
||||
try {
|
||||
add( jarFile.getInputStream( zipEntry ), origin, true );
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MappingException( "could not read mapping documents", e, origin );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
jarFile.close();
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MappingNotFoundException( e, origin );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Metadata addDirectory(File dir) {
|
||||
File[] files = dir.listFiles();
|
||||
for ( File file : files ) {
|
||||
if ( file.isDirectory() ) {
|
||||
addDirectory( file );
|
||||
}
|
||||
else if ( file.getName().endsWith( ".hbm.xml" ) ) {
|
||||
addFile( file );
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,12 +25,16 @@ package org.hibernate.metamodel.binding;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.jboss.jandex.Index;
|
||||
import org.jboss.jandex.Indexer;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.metamodel.source.MetadataSources;
|
||||
import org.hibernate.metamodel.source.internal.MetadataImpl;
|
||||
import org.hibernate.service.internal.BasicServiceRegistryImpl;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
|
||||
|
@ -57,7 +61,7 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
|
|||
|
||||
public EntityBinding buildSimpleEntityBinding() {
|
||||
Index index = indexForClass( SimpleEntity.class );
|
||||
MetadataImpl metadata = new MetadataImpl( basicServiceRegistry() );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
|
||||
metadata.getAnnotationBinder().bindMappedClasses( index );
|
||||
|
||||
return metadata.getEntityBinding( SimpleEntity.class.getSimpleName() );
|
||||
|
@ -65,7 +69,7 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
|
|||
|
||||
public EntityBinding buildSimpleVersionedEntityBinding() {
|
||||
Index index = indexForClass( SimpleEntity.class );
|
||||
MetadataImpl metadata = new MetadataImpl( basicServiceRegistry() );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
|
||||
metadata.getAnnotationBinder().bindMappedClasses( index );
|
||||
|
||||
return metadata.getEntityBinding( SimpleVersionedEntity.class.getSimpleName() );
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.hibernate.internal.util.xml.MappingReader;
|
|||
import org.hibernate.internal.util.xml.Origin;
|
||||
import org.hibernate.internal.util.xml.XMLHelper;
|
||||
import org.hibernate.internal.util.xml.XmlDocument;
|
||||
import org.hibernate.metamodel.source.MetadataSources;
|
||||
import org.hibernate.metamodel.source.internal.MetadataImpl;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -46,7 +47,7 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests {
|
|||
private static final Logger log = Logger.getLogger( BasicHbmBindingTests.class.getName() );
|
||||
|
||||
public EntityBinding buildSimpleEntityBinding() {
|
||||
MetadataImpl metadata = new MetadataImpl( basicServiceRegistry() );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( basicServiceRegistry() ).buildMetadata();
|
||||
|
||||
XmlDocument xmlDocument = readResource( "/org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" );
|
||||
metadata.getHibernateXmlBinder().bindRoot( xmlDocument );
|
||||
|
@ -54,7 +55,7 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests {
|
|||
}
|
||||
|
||||
public EntityBinding buildSimpleVersionedEntityBinding() {
|
||||
MetadataImpl metadata = new MetadataImpl( basicServiceRegistry() );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( basicServiceRegistry() ).buildMetadata();
|
||||
|
||||
String fileName = "/org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml";
|
||||
XmlDocument xmlDocument = readResource( fileName );
|
||||
|
@ -65,11 +66,11 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests {
|
|||
|
||||
@Test
|
||||
public void testJaxbApproach() {
|
||||
final MetadataImpl metadata = new MetadataImpl( basicServiceRegistry() );
|
||||
|
||||
final String resourceName = "org/hibernate/metamodel/binding/SimpleVersionedEntity.xml";
|
||||
metadata.addResource( resourceName );
|
||||
assertEquals( 1, metadata.getJaxbRootList().size() );
|
||||
|
||||
MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() );
|
||||
metadataSources.addResource( resourceName );
|
||||
assertEquals( 1, metadataSources.getJaxbRootList().size() );
|
||||
}
|
||||
|
||||
private XmlDocument readResource(final String name) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.Set;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.metamodel.source.MetadataSources;
|
||||
import org.hibernate.metamodel.source.internal.MetadataImpl;
|
||||
import org.hibernate.service.internal.BasicServiceRegistryImpl;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
|
@ -16,7 +17,8 @@ import org.hibernate.testing.junit4.BaseUnitTestCase;
|
|||
public class OrmXmlParserTests extends BaseUnitTestCase {
|
||||
@Test
|
||||
public void testSingleOrmXml() {
|
||||
OrmXmlParser parser = new OrmXmlParser( new MetadataImpl( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
|
||||
OrmXmlParser parser = new OrmXmlParser( metadata );
|
||||
Set<String> xmlFiles = new HashSet<String>();
|
||||
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" );
|
||||
parser.parseAndUpdateIndex( xmlFiles, null );
|
||||
|
@ -24,7 +26,8 @@ public class OrmXmlParserTests extends BaseUnitTestCase {
|
|||
|
||||
@Test
|
||||
public void testOrmXmlWithOldSchema() {
|
||||
OrmXmlParser parser = new OrmXmlParser( new MetadataImpl( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) );
|
||||
MetadataImpl metadata = (MetadataImpl) new MetadataSources( new BasicServiceRegistryImpl( Collections.emptyMap() ) ).buildMetadata();
|
||||
OrmXmlParser parser = new OrmXmlParser( metadata );
|
||||
Set<String> xmlFiles = new HashSet<String>();
|
||||
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" );
|
||||
parser.parseAndUpdateIndex( xmlFiles, null );
|
||||
|
|
Loading…
Reference in New Issue