-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
SessionFactoryRegistry.java
249 lines (221 loc) · 7.45 KB
/
SessionFactoryRegistry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.internal;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.event.NamespaceChangeListener;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import javax.naming.spi.ObjectFactory;
import org.hibernate.SessionFactory;
import org.hibernate.engine.jndi.JndiException;
import org.hibernate.engine.jndi.JndiNameException;
import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.StringHelper;
/**
* A registry of all {@link SessionFactory} instances for the same classloader as this class.
* <p>
* This registry is used for serialization/deserialization as well as JNDI binding.
*
* @author Steve Ebersole
*/
public class SessionFactoryRegistry {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryRegistry.class );
/**
* Singleton access
*/
public static final SessionFactoryRegistry INSTANCE = new SessionFactoryRegistry();
/**
* A map for mapping the UUID of a SessionFactory to the corresponding SessionFactory instance
*/
private final ConcurrentHashMap<String, SessionFactoryImplementor> sessionFactoryMap = new ConcurrentHashMap<>();
/**
* A cross-reference for mapping a SessionFactory name to its UUID. Not all SessionFactories get named,
*/
private final ConcurrentHashMap<String, String> nameUuidXref = new ConcurrentHashMap<>();
private SessionFactoryRegistry() {
LOG.debugf( "Initializing SessionFactoryRegistry : %s", this );
}
/**
* Adds a SessionFactory to the registry
*
* @param uuid The uuid under which to register the SessionFactory
* @param name The optional name under which to register the SessionFactory
* @param jndiName An optional name to use for binding the SessionFactory into JNDI
* @param instance The SessionFactory instance
* @param jndiService The JNDI service, so we can register a listener if name is a JNDI name
*/
public void addSessionFactory(
String uuid,
String name,
String jndiName,
SessionFactoryImplementor instance,
JndiService jndiService) {
if ( uuid == null ) {
throw new IllegalArgumentException( "SessionFactory UUID cannot be null" );
}
LOG.debugf( "Registering SessionFactory: %s (%s)", uuid, name == null ? "<unnamed>" : name );
sessionFactoryMap.put( uuid, instance );
if ( name != null ) {
nameUuidXref.put( name, uuid );
}
if ( jndiName == null ) {
LOG.debug( "Not binding SessionFactory to JNDI, no JNDI name configured" );
return;
}
bindToJndi( jndiName, instance, jndiService );
}
private void bindToJndi(String jndiName, SessionFactoryImplementor instance, JndiService jndiService) {
try {
LOG.debugf( "Attempting to bind SessionFactory [%s] to JNDI", jndiName );
jndiService.bind( jndiName, instance );
LOG.factoryBoundToJndiName( jndiName );
try {
jndiService.addListener( jndiName, listener );
}
catch (Exception e) {
LOG.couldNotBindJndiListener();
}
}
catch (JndiNameException e) {
LOG.invalidJndiName( jndiName, e );
}
catch (JndiException e) {
LOG.unableToBindFactoryToJndi( e );
}
}
/**
* Remove a previously added SessionFactory
*
* @param uuid The uuid
* @param name The optional name
* @param jndiName An optional name to use for binding the SessionFactory nto JNDI
* @param jndiService The JNDI service
*/
public void removeSessionFactory(
String uuid,
String name,
String jndiName,
JndiService jndiService) {
if ( name != null ) {
nameUuidXref.remove( name );
}
if ( jndiName != null ) {
try {
LOG.tracef( "Unbinding SessionFactory from JNDI : %s", jndiName );
jndiService.unbind( jndiName );
LOG.factoryUnboundFromJndiName( jndiName );
}
catch (JndiNameException e) {
LOG.invalidJndiName( jndiName, e );
}
catch (JndiException e) {
LOG.unableToUnbindFactoryFromJndi( e );
}
}
sessionFactoryMap.remove( uuid );
}
/**
* Get a registered SessionFactory by name
*
* @param name The name
*
* @return The SessionFactory
*/
public SessionFactoryImplementor getNamedSessionFactory(String name) {
LOG.debugf( "Lookup: name=%s", name );
final String uuid = nameUuidXref.get( name );
// protect against NPE -- see HHH-8428
return uuid == null ? null : getSessionFactory( uuid );
}
public SessionFactoryImplementor getSessionFactory(String uuid) {
LOG.debugf( "Lookup: uid=%s", uuid );
final SessionFactoryImplementor sessionFactory = sessionFactoryMap.get( uuid );
if ( sessionFactory == null && LOG.isDebugEnabled() ) {
LOG.debugf( "Not found: %s", uuid );
LOG.debug( sessionFactoryMap.toString() );
}
return sessionFactory;
}
public SessionFactoryImplementor findSessionFactory(String uuid, String name) {
SessionFactoryImplementor sessionFactory = getSessionFactory( uuid );
if ( sessionFactory == null && StringHelper.isNotEmpty( name ) ) {
sessionFactory = getNamedSessionFactory( name );
}
return sessionFactory;
}
/**
* Does this registry currently contain registrations?
*
* @return true/false
*/
public boolean hasRegistrations() {
return !sessionFactoryMap.isEmpty();
}
public void clearRegistrations() {
nameUuidXref.clear();
for ( SessionFactory factory : sessionFactoryMap.values() ) {
try {
factory.close();
}
catch (Exception ignore) {
}
}
sessionFactoryMap.clear();
}
/**
* Implementation of {@literal JNDI} {@link NamespaceChangeListener} contract to listener for context events
* and react accordingly if necessary
*/
private final NamespaceChangeListener listener = new NamespaceChangeListener() {
@Override
public void objectAdded(NamingEvent evt) {
if ( LOG.isDebugEnabled() ) {
LOG.debugf( "A factory was successfully bound to name: %s", evt.getNewBinding().getName() );
}
}
@Override
public void objectRemoved(NamingEvent evt) {
final String jndiName = evt.getOldBinding().getName();
LOG.factoryUnboundFromName( jndiName );
final String uuid = nameUuidXref.remove( jndiName );
//noinspection StatementWithEmptyBody
if ( uuid == null ) {
// serious problem... but not sure what to do yet
}
else {
sessionFactoryMap.remove( uuid );
}
}
@Override
public void objectRenamed(NamingEvent evt) {
final String oldJndiName = evt.getOldBinding().getName();
final String newJndiName = evt.getNewBinding().getName();
LOG.factoryJndiRename( oldJndiName, newJndiName );
final String uuid = nameUuidXref.remove( oldJndiName );
nameUuidXref.put( newJndiName, uuid );
}
@Override
public void namingExceptionThrown(NamingExceptionEvent evt) {
//noinspection ThrowableResultOfMethodCallIgnored
LOG.namingExceptionAccessingFactory( evt.getException() );
}
};
public static class ObjectFactoryImpl implements ObjectFactory {
@Override
public Object getObjectInstance(Object reference, Name name, Context nameCtx, Hashtable<?, ?> environment) {
LOG.debugf( "JNDI lookup: %s", name );
final String uuid = (String) ( (Reference) reference ).get( 0 ).getContent();
LOG.tracef( "Resolved to UUID = %s", uuid );
return INSTANCE.getSessionFactory( uuid );
}
}
}