~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/jmx/SessionFactoryStub.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: SessionFactoryStub.java 8754 2005-12-05 23:36:59Z steveebersole $
 
2
package org.hibernate.jmx;
 
3
 
 
4
import java.io.InvalidObjectException;
 
5
import java.io.ObjectStreamException;
 
6
import java.io.Serializable;
 
7
import java.sql.Connection;
 
8
import java.util.Map;
 
9
import java.util.Set;
 
10
 
 
11
import javax.naming.NamingException;
 
12
import javax.naming.Reference;
 
13
import javax.naming.StringRefAddr;
 
14
 
 
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;
 
29
 
 
30
/**
 
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
 
34
 * access.
 
35
 * @author Gavin King
 
36
 */
 
37
public class SessionFactoryStub implements SessionFactory {
 
38
 
 
39
        private static final Log log = LogFactory.getLog(SessionFactoryStub.class);
 
40
 
 
41
        private static final IdentifierGenerator UUID_GENERATOR = new UUIDHexGenerator();
 
42
 
 
43
        private transient SessionFactory impl;
 
44
        private transient HibernateService service;
 
45
        private String uuid;
 
46
        private String name;
 
47
 
 
48
        SessionFactoryStub(HibernateService service) {
 
49
                this.service = service;
 
50
                this.name = service.getJndiName();
 
51
                try {
 
52
                        uuid = (String) UUID_GENERATOR.generate(null, null);
 
53
                }
 
54
                catch (Exception e) {
 
55
                        throw new AssertionFailure("Could not generate UUID");
 
56
                }
 
57
 
 
58
                SessionFactoryObjectFactory.addInstance( uuid, name, this, service.getProperties() );
 
59
        }
 
60
 
 
61
        public org.hibernate.classic.Session openSession(Connection connection, Interceptor interceptor) {
 
62
                return getImpl().openSession(connection, interceptor);
 
63
        }
 
64
 
 
65
        public org.hibernate.classic.Session openSession(Interceptor interceptor) throws HibernateException {
 
66
                return getImpl().openSession(interceptor);
 
67
        }
 
68
 
 
69
        public org.hibernate.classic.Session openSession() throws HibernateException {
 
70
                return getImpl().openSession();
 
71
        }
 
72
        
 
73
        public org.hibernate.classic.Session openSession(Connection conn) {
 
74
                return getImpl().openSession(conn);
 
75
        }
 
76
 
 
77
        public org.hibernate.classic.Session getCurrentSession() {
 
78
                return getImpl().getCurrentSession();
 
79
        }
 
80
        
 
81
        private synchronized SessionFactory getImpl() {
 
82
                if (impl==null) impl = service.buildSessionFactory();
 
83
                return impl;
 
84
        }
 
85
 
 
86
        //readResolveObject
 
87
        private Object readResolve() throws ObjectStreamException {
 
88
                // look for the instance by uuid
 
89
                Object result = SessionFactoryObjectFactory.getInstance(uuid);
 
90
                if (result==null) {
 
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);
 
94
                        if (result==null) {
 
95
                                throw new InvalidObjectException("Could not find a stub SessionFactory named: " + name);
 
96
                        }
 
97
                        else {
 
98
                                log.debug("resolved stub SessionFactory by name");
 
99
                        }
 
100
                }
 
101
                else {
 
102
                        log.debug("resolved stub SessionFactory by uid");
 
103
                }
 
104
                return result;
 
105
        }
 
106
 
 
107
        /**
 
108
         * @see javax.naming.Referenceable#getReference()
 
109
         */
 
110
        public Reference getReference() throws NamingException {
 
111
                return new Reference(
 
112
                        SessionFactoryStub.class.getName(),
 
113
                        new StringRefAddr("uuid", uuid),
 
114
                        SessionFactoryObjectFactory.class.getName(),
 
115
                        null
 
116
                );
 
117
        }
 
118
 
 
119
        public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException {
 
120
                return getImpl().getClassMetadata(persistentClass);
 
121
        }
 
122
 
 
123
        public ClassMetadata getClassMetadata(String entityName)
 
124
        throws HibernateException {
 
125
                return getImpl().getClassMetadata(entityName);
 
126
        }
 
127
 
 
128
        public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException {
 
129
                return getImpl().getCollectionMetadata(roleName);
 
130
        }
 
131
 
 
132
        public Map getAllClassMetadata() throws HibernateException {
 
133
                return getImpl().getAllClassMetadata();
 
134
        }
 
135
 
 
136
        public Map getAllCollectionMetadata() throws HibernateException {
 
137
                return getImpl().getAllCollectionMetadata();
 
138
        }
 
139
 
 
140
        public void close() throws HibernateException {
 
141
        }
 
142
        
 
143
        public boolean isClosed() {
 
144
                return false;
 
145
        }
 
146
 
 
147
        public void evict(Class persistentClass, Serializable id)
 
148
                throws HibernateException {
 
149
                getImpl().evict(persistentClass, id);
 
150
        }
 
151
 
 
152
        public void evict(Class persistentClass) throws HibernateException {
 
153
                getImpl().evict(persistentClass);
 
154
        }
 
155
 
 
156
        public void evictEntity(String entityName, Serializable id)
 
157
        throws HibernateException {
 
158
                getImpl().evictEntity(entityName, id);
 
159
        }
 
160
        
 
161
        public void evictEntity(String entityName) throws HibernateException {
 
162
                getImpl().evictEntity(entityName);
 
163
        }
 
164
 
 
165
        public void evictCollection(String roleName, Serializable id)
 
166
                throws HibernateException {
 
167
                getImpl().evictCollection(roleName, id);
 
168
        }
 
169
 
 
170
        public void evictCollection(String roleName) throws HibernateException {
 
171
                getImpl().evictCollection(roleName);
 
172
        }
 
173
 
 
174
        public void evictQueries() throws HibernateException {
 
175
                getImpl().evictQueries();
 
176
        }
 
177
 
 
178
        public void evictQueries(String cacheRegion) throws HibernateException {
 
179
                getImpl().evictQueries(cacheRegion);
 
180
        }
 
181
 
 
182
        public Statistics getStatistics() {
 
183
                return getImpl().getStatistics();
 
184
        }
 
185
 
 
186
        public StatelessSession openStatelessSession() {
 
187
                return getImpl().openStatelessSession();
 
188
        }
 
189
 
 
190
        public StatelessSession openStatelessSession(Connection conn) {
 
191
                return getImpl().openStatelessSession(conn);
 
192
        }
 
193
 
 
194
        public Set getDefinedFilterNames() {
 
195
                return getImpl().getDefinedFilterNames();
 
196
        }
 
197
 
 
198
        public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {
 
199
                return getImpl().getFilterDefinition( filterName );
 
200
        }
 
201
}