1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2010 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
import java .util .Map ;
28
28
import java .util .Properties ;
29
29
import java .util .concurrent .ConcurrentHashMap ;
30
- import javax .naming .NamingException ;
31
30
import javax .persistence .EntityManager ;
32
31
import javax .persistence .EntityManagerFactory ;
33
32
import javax .persistence .PersistenceContext ;
52
51
import org .springframework .beans .factory .support .RootBeanDefinition ;
53
52
import org .springframework .core .Ordered ;
54
53
import org .springframework .core .PriorityOrdered ;
55
- import org .springframework .jndi .JndiLocatorSupport ;
54
+ import org .springframework .jndi .JndiLocatorDelegate ;
55
+ import org .springframework .jndi .JndiTemplate ;
56
56
import org .springframework .orm .jpa .EntityManagerFactoryInfo ;
57
57
import org .springframework .orm .jpa .EntityManagerFactoryUtils ;
58
58
import org .springframework .orm .jpa .EntityManagerProxy ;
159
159
* @see javax.persistence.PersistenceUnit
160
160
* @see javax.persistence.PersistenceContext
161
161
*/
162
- public class PersistenceAnnotationBeanPostProcessor extends JndiLocatorSupport
162
+ public class PersistenceAnnotationBeanPostProcessor
163
163
implements InstantiationAwareBeanPostProcessor , DestructionAwareBeanPostProcessor ,
164
164
MergedBeanDefinitionPostProcessor , PriorityOrdered , BeanFactoryAware , Serializable {
165
165
166
+ private Object jndiEnvironment ;
167
+
168
+ private boolean resourceRef = true ;
169
+
166
170
private transient Map <String , String > persistenceUnits ;
167
171
168
172
private transient Map <String , String > persistenceContexts ;
@@ -182,10 +186,31 @@ public class PersistenceAnnotationBeanPostProcessor extends JndiLocatorSupport
182
186
new ConcurrentHashMap <Object , EntityManager >();
183
187
184
188
185
- public PersistenceAnnotationBeanPostProcessor () {
186
- setResourceRef (true );
189
+ /**
190
+ * Set the JNDI template to use for JNDI lookups.
191
+ * @see org.springframework.jndi.JndiAccessor#setJndiTemplate
192
+ */
193
+ public void setJndiTemplate (Object jndiTemplate ) {
194
+ this .jndiEnvironment = jndiTemplate ;
187
195
}
188
196
197
+ /**
198
+ * Set the JNDI environment to use for JNDI lookups.
199
+ * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment
200
+ */
201
+ public void setJndiEnvironment (Properties jndiEnvironment ) {
202
+ this .jndiEnvironment = jndiEnvironment ;
203
+ }
204
+
205
+ /**
206
+ * Set whether the lookup occurs in a J2EE container, i.e. if the prefix
207
+ * "java:comp/env/" needs to be added if the JNDI name doesn't already
208
+ * contain it. PersistenceAnnotationBeanPostProcessor's default is "true".
209
+ * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef
210
+ */
211
+ public void setResourceRef (boolean resourceRef ) {
212
+ this .resourceRef = resourceRef ;
213
+ }
189
214
190
215
/**
191
216
* Specify the persistence units for EntityManagerFactory lookups,
@@ -404,7 +429,7 @@ protected EntityManagerFactory getPersistenceUnit(String unitName) {
404
429
try {
405
430
return lookup (jndiName , EntityManagerFactory .class );
406
431
}
407
- catch (NamingException ex ) {
432
+ catch (Exception ex ) {
408
433
throw new IllegalStateException ("Could not obtain EntityManagerFactory [" + jndiName + "] from JNDI" , ex );
409
434
}
410
435
}
@@ -436,7 +461,7 @@ protected EntityManager getPersistenceContext(String unitName, boolean extended)
436
461
try {
437
462
return lookup (jndiName , EntityManager .class );
438
463
}
439
- catch (NamingException ex ) {
464
+ catch (Exception ex ) {
440
465
throw new IllegalStateException ("Could not obtain EntityManager [" + jndiName + "] from JNDI" , ex );
441
466
}
442
467
}
@@ -513,6 +538,42 @@ protected EntityManagerFactory findDefaultEntityManagerFactory(String requesting
513
538
}
514
539
}
515
540
541
+ /**
542
+ * Perform a JNDI lookup for the given resource by name.
543
+ * <p>Called for EntityManagerFactory and EntityManager lookup
544
+ * when JNDI names are mapped for specific persistence units.
545
+ * @param jndiName the JNDI name to look up
546
+ * @param requiredType the required type of the object
547
+ * @return the obtained object
548
+ * @throws Exception if the JNDI lookup failed
549
+ */
550
+ protected <T > T lookup (String jndiName , Class <T > requiredType ) throws Exception {
551
+ return new LocatorDelegate ().lookup (jndiName , requiredType );
552
+ }
553
+
554
+
555
+ /**
556
+ * Separate inner class to isolate the JNDI API dependency
557
+ * (for compatibility with Google App Engine's API white list).
558
+ */
559
+ private class LocatorDelegate {
560
+
561
+ public <T > T lookup (String jndiName , Class <T > requiredType ) throws Exception {
562
+ JndiLocatorDelegate locator = new JndiLocatorDelegate ();
563
+ if (jndiEnvironment instanceof JndiTemplate ) {
564
+ locator .setJndiTemplate ((JndiTemplate ) jndiEnvironment );
565
+ }
566
+ else if (jndiEnvironment instanceof Properties ) {
567
+ locator .setJndiEnvironment ((Properties ) jndiEnvironment );
568
+ }
569
+ else if (jndiEnvironment != null ) {
570
+ throw new IllegalStateException ("Illegal 'jndiEnvironment' type: " + jndiEnvironment .getClass ());
571
+ }
572
+ locator .setResourceRef (resourceRef );
573
+ return locator .lookup (jndiName , requiredType );
574
+ }
575
+ }
576
+
516
577
517
578
/**
518
579
* Class representing injection information about an annotated field
0 commit comments