Skip to content

Commit cef44f6

Browse files
committed
made PersistenceAnnotationBeanPostProcessor's JNDI API references optional - for compatibility with Google App Engine (SPR-6679)
1 parent caaa45c commit cef44f6

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jndi;
18+
19+
import javax.naming.NamingException;
20+
21+
/**
22+
* {@link JndiLocatorSupport} subclass with public lookup methods,
23+
* for convenient use as a delegate.
24+
*
25+
* @author Juergen Hoeller
26+
* @since 3.0.1
27+
*/
28+
public class JndiLocatorDelegate extends JndiLocatorSupport {
29+
30+
@Override
31+
public Object lookup(String jndiName) throws NamingException {
32+
return super.lookup(jndiName);
33+
}
34+
35+
@Override
36+
public <T> T lookup(String jndiName, Class<T> requiredType) throws NamingException {
37+
return super.lookup(jndiName, requiredType);
38+
}
39+
40+
}

org.springframework.orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727
import java.util.Map;
2828
import java.util.Properties;
2929
import java.util.concurrent.ConcurrentHashMap;
30-
import javax.naming.NamingException;
3130
import javax.persistence.EntityManager;
3231
import javax.persistence.EntityManagerFactory;
3332
import javax.persistence.PersistenceContext;
@@ -52,7 +51,8 @@
5251
import org.springframework.beans.factory.support.RootBeanDefinition;
5352
import org.springframework.core.Ordered;
5453
import org.springframework.core.PriorityOrdered;
55-
import org.springframework.jndi.JndiLocatorSupport;
54+
import org.springframework.jndi.JndiLocatorDelegate;
55+
import org.springframework.jndi.JndiTemplate;
5656
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
5757
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
5858
import org.springframework.orm.jpa.EntityManagerProxy;
@@ -159,10 +159,14 @@
159159
* @see javax.persistence.PersistenceUnit
160160
* @see javax.persistence.PersistenceContext
161161
*/
162-
public class PersistenceAnnotationBeanPostProcessor extends JndiLocatorSupport
162+
public class PersistenceAnnotationBeanPostProcessor
163163
implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor,
164164
MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware, Serializable {
165165

166+
private Object jndiEnvironment;
167+
168+
private boolean resourceRef = true;
169+
166170
private transient Map<String, String> persistenceUnits;
167171

168172
private transient Map<String, String> persistenceContexts;
@@ -182,10 +186,31 @@ public class PersistenceAnnotationBeanPostProcessor extends JndiLocatorSupport
182186
new ConcurrentHashMap<Object, EntityManager>();
183187

184188

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;
187195
}
188196

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+
}
189214

190215
/**
191216
* Specify the persistence units for EntityManagerFactory lookups,
@@ -404,7 +429,7 @@ protected EntityManagerFactory getPersistenceUnit(String unitName) {
404429
try {
405430
return lookup(jndiName, EntityManagerFactory.class);
406431
}
407-
catch (NamingException ex) {
432+
catch (Exception ex) {
408433
throw new IllegalStateException("Could not obtain EntityManagerFactory [" + jndiName + "] from JNDI", ex);
409434
}
410435
}
@@ -436,7 +461,7 @@ protected EntityManager getPersistenceContext(String unitName, boolean extended)
436461
try {
437462
return lookup(jndiName, EntityManager.class);
438463
}
439-
catch (NamingException ex) {
464+
catch (Exception ex) {
440465
throw new IllegalStateException("Could not obtain EntityManager [" + jndiName + "] from JNDI", ex);
441466
}
442467
}
@@ -513,6 +538,42 @@ protected EntityManagerFactory findDefaultEntityManagerFactory(String requesting
513538
}
514539
}
515540

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+
516577

517578
/**
518579
* Class representing injection information about an annotated field

0 commit comments

Comments
 (0)