1
//$Id: SessionFactoryStub.java 8754 2005-12-05 23:36:59Z steveebersole $
2
package org.hibernate.jmx;
4
import java.io.InvalidObjectException;
5
import java.io.ObjectStreamException;
6
import java.io.Serializable;
7
import java.sql.Connection;
11
import javax.naming.NamingException;
12
import javax.naming.Reference;
13
import javax.naming.StringRefAddr;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.hibernate.AssertionFailure;
18
import org.hibernate.HibernateException;
19
import org.hibernate.Interceptor;
20
import org.hibernate.SessionFactory;
21
import org.hibernate.StatelessSession;
22
import org.hibernate.engine.FilterDefinition;
23
import org.hibernate.id.IdentifierGenerator;
24
import org.hibernate.id.UUIDHexGenerator;
25
import org.hibernate.impl.SessionFactoryObjectFactory;
26
import org.hibernate.metadata.ClassMetadata;
27
import org.hibernate.metadata.CollectionMetadata;
28
import org.hibernate.stat.Statistics;
31
* A flyweight for <tt>SessionFactory</tt>. If the MBean itself does not
32
* have classpath to the persistent classes, then a stub will be registered
33
* with JNDI and the actual <tt>SessionFactoryImpl</tt> built upon first
37
public class SessionFactoryStub implements SessionFactory {
39
private static final Log log = LogFactory.getLog(SessionFactoryStub.class);
41
private static final IdentifierGenerator UUID_GENERATOR = new UUIDHexGenerator();
43
private transient SessionFactory impl;
44
private transient HibernateService service;
48
SessionFactoryStub(HibernateService service) {
49
this.service = service;
50
this.name = service.getJndiName();
52
uuid = (String) UUID_GENERATOR.generate(null, null);
55
throw new AssertionFailure("Could not generate UUID");
58
SessionFactoryObjectFactory.addInstance( uuid, name, this, service.getProperties() );
61
public org.hibernate.classic.Session openSession(Connection connection, Interceptor interceptor) {
62
return getImpl().openSession(connection, interceptor);
65
public org.hibernate.classic.Session openSession(Interceptor interceptor) throws HibernateException {
66
return getImpl().openSession(interceptor);
69
public org.hibernate.classic.Session openSession() throws HibernateException {
70
return getImpl().openSession();
73
public org.hibernate.classic.Session openSession(Connection conn) {
74
return getImpl().openSession(conn);
77
public org.hibernate.classic.Session getCurrentSession() {
78
return getImpl().getCurrentSession();
81
private synchronized SessionFactory getImpl() {
82
if (impl==null) impl = service.buildSessionFactory();
87
private Object readResolve() throws ObjectStreamException {
88
// look for the instance by uuid
89
Object result = SessionFactoryObjectFactory.getInstance(uuid);
91
// in case we were deserialized in a different JVM, look for an instance with the same name
92
// (alternatively we could do an actual JNDI lookup here....)
93
result = SessionFactoryObjectFactory.getNamedInstance(name);
95
throw new InvalidObjectException("Could not find a stub SessionFactory named: " + name);
98
log.debug("resolved stub SessionFactory by name");
102
log.debug("resolved stub SessionFactory by uid");
108
* @see javax.naming.Referenceable#getReference()
110
public Reference getReference() throws NamingException {
111
return new Reference(
112
SessionFactoryStub.class.getName(),
113
new StringRefAddr("uuid", uuid),
114
SessionFactoryObjectFactory.class.getName(),
119
public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException {
120
return getImpl().getClassMetadata(persistentClass);
123
public ClassMetadata getClassMetadata(String entityName)
124
throws HibernateException {
125
return getImpl().getClassMetadata(entityName);
128
public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException {
129
return getImpl().getCollectionMetadata(roleName);
132
public Map getAllClassMetadata() throws HibernateException {
133
return getImpl().getAllClassMetadata();
136
public Map getAllCollectionMetadata() throws HibernateException {
137
return getImpl().getAllCollectionMetadata();
140
public void close() throws HibernateException {
143
public boolean isClosed() {
147
public void evict(Class persistentClass, Serializable id)
148
throws HibernateException {
149
getImpl().evict(persistentClass, id);
152
public void evict(Class persistentClass) throws HibernateException {
153
getImpl().evict(persistentClass);
156
public void evictEntity(String entityName, Serializable id)
157
throws HibernateException {
158
getImpl().evictEntity(entityName, id);
161
public void evictEntity(String entityName) throws HibernateException {
162
getImpl().evictEntity(entityName);
165
public void evictCollection(String roleName, Serializable id)
166
throws HibernateException {
167
getImpl().evictCollection(roleName, id);
170
public void evictCollection(String roleName) throws HibernateException {
171
getImpl().evictCollection(roleName);
174
public void evictQueries() throws HibernateException {
175
getImpl().evictQueries();
178
public void evictQueries(String cacheRegion) throws HibernateException {
179
getImpl().evictQueries(cacheRegion);
182
public Statistics getStatistics() {
183
return getImpl().getStatistics();
186
public StatelessSession openStatelessSession() {
187
return getImpl().openStatelessSession();
190
public StatelessSession openStatelessSession(Connection conn) {
191
return getImpl().openStatelessSession(conn);
194
public Set getDefinedFilterNames() {
195
return getImpl().getDefinedFilterNames();
198
public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {
199
return getImpl().getFilterDefinition( filterName );