KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > persister > PersisterFactory


1 //$Id: PersisterFactory.java,v 1.6 2005/02/19 07:06:06 oneovthafew Exp $
2
package org.hibernate.persister;
3
4 import java.lang.reflect.Constructor JavaDoc;
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6
7 import org.hibernate.HibernateException;
8 import org.hibernate.MappingException;
9 import org.hibernate.cache.CacheConcurrencyStrategy;
10 import org.hibernate.cfg.Configuration;
11 import org.hibernate.engine.Mapping;
12 import org.hibernate.engine.SessionFactoryImplementor;
13 import org.hibernate.mapping.Collection;
14 import org.hibernate.mapping.PersistentClass;
15 import org.hibernate.persister.collection.BasicCollectionPersister;
16 import org.hibernate.persister.collection.CollectionPersister;
17 import org.hibernate.persister.collection.OneToManyPersister;
18 import org.hibernate.persister.entity.EntityPersister;
19 import org.hibernate.persister.entity.JoinedSubclassEntityPersister;
20 import org.hibernate.persister.entity.SingleTableEntityPersister;
21 import org.hibernate.persister.entity.UnionSubclassEntityPersister;
22
23 /**
24  * Factory for <tt>EntityPersister</tt> and <tt>CollectionPersister</tt> instances
25  *
26  * @author Gavin King
27  */

28 public final class PersisterFactory {
29
30     //TODO: make EntityPersister *not* depend on SessionFactoryImplementor
31
//interface, if possible
32

33     // TODO : still need to make CollectionPersisters EntityMode-aware
34

35     private PersisterFactory() {}
36
37     private static final Class JavaDoc[] PERSISTER_CONSTRUCTOR_ARGS = new Class JavaDoc[] {
38         PersistentClass.class, CacheConcurrencyStrategy.class, SessionFactoryImplementor.class, Mapping.class
39     };
40
41     // TODO: is it really neceassry to provide Configuration to CollectionPersisters ? Should it not be enough with associated class ?
42
// or why does EntityPersister's not get access to configuration ?
43
private static final Class JavaDoc[] COLLECTION_PERSISTER_CONSTRUCTOR_ARGS = new Class JavaDoc[] {
44             Collection.class, CacheConcurrencyStrategy.class, Configuration.class, SessionFactoryImplementor.class
45         };
46
47     public static EntityPersister createClassPersister(
48             PersistentClass model,
49             CacheConcurrencyStrategy cache,
50             SessionFactoryImplementor factory,
51             Mapping cfg)
52     throws HibernateException {
53         Class JavaDoc persisterClass = model.getEntityPersisterClass();
54         if (persisterClass==null || persisterClass==SingleTableEntityPersister.class) {
55             return new SingleTableEntityPersister(model, cache, factory, cfg);
56         }
57         else if (persisterClass==JoinedSubclassEntityPersister.class) {
58             return new JoinedSubclassEntityPersister(model, cache, factory, cfg);
59         }
60         else if (persisterClass==UnionSubclassEntityPersister.class) {
61             return new UnionSubclassEntityPersister(model, cache, factory, cfg);
62         }
63         else {
64             return create(persisterClass, model, cache, factory, cfg);
65         }
66     }
67
68     public static CollectionPersister createCollectionPersister(Configuration cfg, Collection model, CacheConcurrencyStrategy cache, SessionFactoryImplementor factory)
69     throws HibernateException {
70         Class JavaDoc persisterClass = model.getCollectionPersisterClass();
71         if(persisterClass==null) { // default behavior
72
return model.isOneToMany() ?
73                 (CollectionPersister) new OneToManyPersister(model, cache, cfg, factory) :
74                 (CollectionPersister) new BasicCollectionPersister(model, cache, cfg, factory);
75         }
76         else {
77             return create(persisterClass, cfg, model, cache, factory);
78         }
79
80     }
81
82     private static EntityPersister create(
83             Class JavaDoc persisterClass,
84             PersistentClass model,
85             CacheConcurrencyStrategy cache,
86             SessionFactoryImplementor factory,
87             Mapping cfg)
88     throws HibernateException {
89         Constructor JavaDoc pc;
90         try {
91             pc = persisterClass.getConstructor(PERSISTER_CONSTRUCTOR_ARGS);
92         }
93         catch (Exception JavaDoc e) {
94             throw new MappingException( "Could not get constructor for " + persisterClass.getName(), e );
95         }
96
97         try {
98             return (EntityPersister) pc.newInstance( new Object JavaDoc[] { model, cache, factory, cfg } );
99         }
100         catch (InvocationTargetException JavaDoc ite) {
101             Throwable JavaDoc e = ite.getTargetException();
102             if (e instanceof HibernateException) {
103                 throw (HibernateException) e;
104             }
105             else {
106                 throw new MappingException( "Could not instantiate persister " + persisterClass.getName(), e );
107             }
108         }
109         catch (Exception JavaDoc e) {
110             throw new MappingException( "Could not instantiate persister " + persisterClass.getName(), e );
111         }
112     }
113
114     private static CollectionPersister create(Class JavaDoc persisterClass, Configuration cfg, Collection model, CacheConcurrencyStrategy cache, SessionFactoryImplementor factory) throws HibernateException {
115         Constructor JavaDoc pc;
116         try {
117             pc = persisterClass.getConstructor(COLLECTION_PERSISTER_CONSTRUCTOR_ARGS);
118         }
119         catch (Exception JavaDoc e) {
120             throw new MappingException( "Could not get constructor for " + persisterClass.getName(), e );
121         }
122
123         try {
124             return (CollectionPersister) pc.newInstance( new Object JavaDoc[] { model, cache, cfg, factory } );
125         }
126         catch (InvocationTargetException JavaDoc ite) {
127             Throwable JavaDoc e = ite.getTargetException();
128             if (e instanceof HibernateException) {
129                 throw (HibernateException) e;
130             }
131             else {
132                 throw new MappingException( "Could not instantiate collection persister " + persisterClass.getName(), e );
133             }
134         }
135         catch (Exception JavaDoc e) {
136             throw new MappingException( "Could not instantiate collection persister " + persisterClass.getName(), e );
137         }
138     }
139
140
141 }
142
Popular Tags