001: //$Id: PersisterFactory.java 5775 2005-02-19 07:06:06Z oneovthafew $
002: package org.hibernate.persister;
003:
004: import java.lang.reflect.Constructor;
005: import java.lang.reflect.InvocationTargetException;
006:
007: import org.hibernate.HibernateException;
008: import org.hibernate.MappingException;
009: import org.hibernate.cache.CacheConcurrencyStrategy;
010: import org.hibernate.cfg.Configuration;
011: import org.hibernate.engine.Mapping;
012: import org.hibernate.engine.SessionFactoryImplementor;
013: import org.hibernate.mapping.Collection;
014: import org.hibernate.mapping.PersistentClass;
015: import org.hibernate.persister.collection.BasicCollectionPersister;
016: import org.hibernate.persister.collection.CollectionPersister;
017: import org.hibernate.persister.collection.OneToManyPersister;
018: import org.hibernate.persister.entity.EntityPersister;
019: import org.hibernate.persister.entity.JoinedSubclassEntityPersister;
020: import org.hibernate.persister.entity.SingleTableEntityPersister;
021: import org.hibernate.persister.entity.UnionSubclassEntityPersister;
022:
023: /**
024: * Factory for <tt>EntityPersister</tt> and <tt>CollectionPersister</tt> instances
025: *
026: * @author Gavin King
027: */
028: public final class PersisterFactory {
029:
030: //TODO: make EntityPersister *not* depend on SessionFactoryImplementor
031: //interface, if possible
032:
033: // TODO : still need to make CollectionPersisters EntityMode-aware
034:
035: private PersisterFactory() {
036: }
037:
038: private static final Class[] PERSISTER_CONSTRUCTOR_ARGS = new Class[] {
039: PersistentClass.class, CacheConcurrencyStrategy.class,
040: SessionFactoryImplementor.class, Mapping.class };
041:
042: // TODO: is it really neceassry to provide Configuration to CollectionPersisters ? Should it not be enough with associated class ?
043: // or why does EntityPersister's not get access to configuration ?
044: private static final Class[] COLLECTION_PERSISTER_CONSTRUCTOR_ARGS = new Class[] {
045: Collection.class, CacheConcurrencyStrategy.class,
046: Configuration.class, SessionFactoryImplementor.class };
047:
048: public static EntityPersister createClassPersister(
049: PersistentClass model, CacheConcurrencyStrategy cache,
050: SessionFactoryImplementor factory, Mapping cfg)
051: throws HibernateException {
052: Class persisterClass = model.getEntityPersisterClass();
053: if (persisterClass == null
054: || persisterClass == SingleTableEntityPersister.class) {
055: return new SingleTableEntityPersister(model, cache,
056: factory, cfg);
057: } else if (persisterClass == JoinedSubclassEntityPersister.class) {
058: return new JoinedSubclassEntityPersister(model, cache,
059: factory, cfg);
060: } else if (persisterClass == UnionSubclassEntityPersister.class) {
061: return new UnionSubclassEntityPersister(model, cache,
062: factory, cfg);
063: } else {
064: return create(persisterClass, model, cache, factory, cfg);
065: }
066: }
067:
068: public static CollectionPersister createCollectionPersister(
069: Configuration cfg, Collection model,
070: CacheConcurrencyStrategy cache,
071: SessionFactoryImplementor factory)
072: throws HibernateException {
073: Class persisterClass = model.getCollectionPersisterClass();
074: if (persisterClass == null) { // default behavior
075: return model.isOneToMany() ? (CollectionPersister) new OneToManyPersister(
076: model, cache, cfg, factory)
077: : (CollectionPersister) new BasicCollectionPersister(
078: model, cache, cfg, factory);
079: } else {
080: return create(persisterClass, cfg, model, cache, factory);
081: }
082:
083: }
084:
085: private static EntityPersister create(Class persisterClass,
086: PersistentClass model, CacheConcurrencyStrategy cache,
087: SessionFactoryImplementor factory, Mapping cfg)
088: throws HibernateException {
089: Constructor pc;
090: try {
091: pc = persisterClass
092: .getConstructor(PERSISTER_CONSTRUCTOR_ARGS);
093: } catch (Exception e) {
094: throw new MappingException("Could not get constructor for "
095: + persisterClass.getName(), e);
096: }
097:
098: try {
099: return (EntityPersister) pc.newInstance(new Object[] {
100: model, cache, factory, cfg });
101: } catch (InvocationTargetException ite) {
102: Throwable e = ite.getTargetException();
103: if (e instanceof HibernateException) {
104: throw (HibernateException) e;
105: } else {
106: throw new MappingException(
107: "Could not instantiate persister "
108: + persisterClass.getName(), e);
109: }
110: } catch (Exception e) {
111: throw new MappingException(
112: "Could not instantiate persister "
113: + persisterClass.getName(), e);
114: }
115: }
116:
117: private static CollectionPersister create(Class persisterClass,
118: Configuration cfg, Collection model,
119: CacheConcurrencyStrategy cache,
120: SessionFactoryImplementor factory)
121: throws HibernateException {
122: Constructor pc;
123: try {
124: pc = persisterClass
125: .getConstructor(COLLECTION_PERSISTER_CONSTRUCTOR_ARGS);
126: } catch (Exception e) {
127: throw new MappingException("Could not get constructor for "
128: + persisterClass.getName(), e);
129: }
130:
131: try {
132: return (CollectionPersister) pc.newInstance(new Object[] {
133: model, cache, cfg, factory });
134: } catch (InvocationTargetException ite) {
135: Throwable e = ite.getTargetException();
136: if (e instanceof HibernateException) {
137: throw (HibernateException) e;
138: } else {
139: throw new MappingException(
140: "Could not instantiate collection persister "
141: + persisterClass.getName(), e);
142: }
143: } catch (Exception e) {
144: throw new MappingException(
145: "Could not instantiate collection persister "
146: + persisterClass.getName(), e);
147: }
148: }
149:
150: }
|