KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > jmx > SessionFactoryStub


1 //$Id: SessionFactoryStub.java,v 1.15 2005/05/27 03:53:59 oneovthafew Exp $
2
package org.hibernate.jmx;
3
4 import java.io.InvalidObjectException JavaDoc;
5 import java.io.ObjectStreamException JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.sql.Connection JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.naming.NamingException JavaDoc;
11 import javax.naming.Reference JavaDoc;
12 import javax.naming.StringRefAddr JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.hibernate.AssertionFailure;
17 import org.hibernate.HibernateException;
18 import org.hibernate.Interceptor;
19 import org.hibernate.SessionFactory;
20 import org.hibernate.StatelessSession;
21 import org.hibernate.id.IdentifierGenerator;
22 import org.hibernate.id.UUIDHexGenerator;
23 import org.hibernate.impl.SessionFactoryObjectFactory;
24 import org.hibernate.metadata.ClassMetadata;
25 import org.hibernate.metadata.CollectionMetadata;
26 import org.hibernate.stat.Statistics;
27
28 /**
29  * A flyweight for <tt>SessionFactory</tt>. If the MBean itself does not
30  * have classpath to the persistent classes, then a stub will be registered
31  * with JNDI and the actual <tt>SessionFactoryImpl</tt> built upon first
32  * access.
33  * @author Gavin King
34  */

35 public class SessionFactoryStub implements SessionFactory {
36
37     private static final Log log = LogFactory.getLog(SessionFactoryStub.class);
38
39     private static final IdentifierGenerator UUID_GENERATOR = new UUIDHexGenerator();
40
41     private transient SessionFactory impl;
42     private transient HibernateService service;
43     private String JavaDoc uuid;
44     private String JavaDoc name;
45
46     SessionFactoryStub(HibernateService service) {
47         this.service = service;
48         this.name = service.getJndiName();
49         try {
50             uuid = (String JavaDoc) UUID_GENERATOR.generate(null, null);
51         }
52         catch (Exception JavaDoc e) {
53             throw new AssertionFailure("Could not generate UUID");
54         }
55
56         SessionFactoryObjectFactory.addInstance( uuid, name, this, service.getProperties() );
57     }
58
59     public org.hibernate.classic.Session openSession(Connection JavaDoc connection, Interceptor interceptor) {
60         return getImpl().openSession(connection, interceptor);
61     }
62
63     public org.hibernate.classic.Session openSession(Interceptor interceptor) throws HibernateException {
64         return getImpl().openSession(interceptor);
65     }
66
67     public org.hibernate.classic.Session openSession() throws HibernateException {
68         return getImpl().openSession();
69     }
70     
71     public org.hibernate.classic.Session openSession(Connection JavaDoc conn) {
72         return getImpl().openSession(conn);
73     }
74
75     public org.hibernate.classic.Session getCurrentSession() {
76         return getImpl().getCurrentSession();
77     }
78     
79     private synchronized SessionFactory getImpl() {
80         if (impl==null) impl = service.buildSessionFactory();
81         return impl;
82     }
83
84     //readResolveObject
85
private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc {
86         // look for the instance by uuid
87
Object JavaDoc result = SessionFactoryObjectFactory.getInstance(uuid);
88         if (result==null) {
89             // in case we were deserialized in a different JVM, look for an instance with the same name
90
// (alternatively we could do an actual JNDI lookup here....)
91
result = SessionFactoryObjectFactory.getNamedInstance(name);
92             if (result==null) {
93                 throw new InvalidObjectException JavaDoc("Could not find a stub SessionFactory named: " + name);
94             }
95             else {
96                 log.debug("resolved stub SessionFactory by name");
97             }
98         }
99         else {
100             log.debug("resolved stub SessionFactory by uid");
101         }
102         return result;
103     }
104
105     /**
106      * @see javax.naming.Referenceable#getReference()
107      */

108     public Reference JavaDoc getReference() throws NamingException JavaDoc {
109         return new Reference JavaDoc(
110             SessionFactoryStub.class.getName(),
111             new StringRefAddr JavaDoc("uuid", uuid),
112             SessionFactoryObjectFactory.class.getName(),
113             null
114         );
115     }
116
117     public ClassMetadata getClassMetadata(Class JavaDoc persistentClass) throws HibernateException {
118         return getImpl().getClassMetadata(persistentClass);
119     }
120
121     public ClassMetadata getClassMetadata(String JavaDoc entityName)
122     throws HibernateException {
123         return getImpl().getClassMetadata(entityName);
124     }
125
126     public CollectionMetadata getCollectionMetadata(String JavaDoc roleName) throws HibernateException {
127         return getImpl().getCollectionMetadata(roleName);
128     }
129
130     public Map JavaDoc getAllClassMetadata() throws HibernateException {
131         return getImpl().getAllClassMetadata();
132     }
133
134     public Map JavaDoc getAllCollectionMetadata() throws HibernateException {
135         return getImpl().getAllCollectionMetadata();
136     }
137
138     public void close() throws HibernateException {
139     }
140     
141     public boolean isClosed() {
142         return false;
143     }
144
145     public void evict(Class JavaDoc persistentClass, Serializable JavaDoc id)
146         throws HibernateException {
147         getImpl().evict(persistentClass, id);
148     }
149
150     public void evict(Class JavaDoc persistentClass) throws HibernateException {
151         getImpl().evict(persistentClass);
152     }
153
154     public void evictEntity(String JavaDoc entityName, Serializable JavaDoc id)
155     throws HibernateException {
156         getImpl().evictEntity(entityName, id);
157     }
158     
159     public void evictEntity(String JavaDoc entityName) throws HibernateException {
160         getImpl().evictEntity(entityName);
161     }
162
163     public void evictCollection(String JavaDoc roleName, Serializable JavaDoc id)
164         throws HibernateException {
165         getImpl().evictCollection(roleName, id);
166     }
167
168     public void evictCollection(String JavaDoc roleName) throws HibernateException {
169         getImpl().evictCollection(roleName);
170     }
171
172     public void evictQueries() throws HibernateException {
173         getImpl().evictQueries();
174     }
175
176     public void evictQueries(String JavaDoc cacheRegion) throws HibernateException {
177         getImpl().evictQueries(cacheRegion);
178     }
179
180     public Statistics getStatistics() {
181         return getImpl().getStatistics();
182     }
183
184     public StatelessSession openStatelessSession() {
185         return getImpl().openStatelessSession();
186     }
187
188     public StatelessSession openStatelessSession(Connection JavaDoc conn) {
189         return getImpl().openStatelessSession(conn);
190     }
191 }
192
Popular Tags