Skip to content

Commit 50e50d0

Browse files
committed
Polishing
(cherry picked from commit 16325c2)
1 parent 205e681 commit 50e50d0

File tree

12 files changed

+201
-183
lines changed

12 files changed

+201
-183
lines changed

spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -69,9 +69,9 @@ public JndiPropertySource(String name, JndiLocatorDelegate jndiLocator) {
6969
super(name, jndiLocator);
7070
}
7171

72+
7273
/**
73-
* {@inheritDoc}
74-
* <p>This implementation looks up and returns the value associated with the given
74+
* This implementation looks up and returns the value associated with the given
7575
* name from the underlying {@link JndiLocatorDelegate}. If a {@link NamingException}
7676
* is thrown during the call to {@link JndiLocatorDelegate#lookup(String)}, returns
7777
* {@code null} and issues a DEBUG-level log statement with the exception message.
@@ -80,12 +80,16 @@ public JndiPropertySource(String name, JndiLocatorDelegate jndiLocator) {
8080
public Object getProperty(String name) {
8181
try {
8282
Object value = this.source.lookup(name);
83-
logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
83+
if (logger.isDebugEnabled()) {
84+
logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
85+
}
8486
return value;
8587
}
8688
catch (NamingException ex) {
87-
logger.debug("JNDI lookup for name [" + name + "] threw NamingException " +
88-
"with message: " + ex.getMessage() + ". Returning null.");
89+
if (logger.isDebugEnabled()) {
90+
logger.debug("JNDI lookup for name [" + name + "] threw NamingException " +
91+
"with message: " + ex.getMessage() + ". Returning null.");
92+
}
8993
return null;
9094
}
9195
}

spring-context/src/test/java/org/springframework/context/support/EnvironmentSecurityManagerIntegrationTests.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.context.support;
1818

19-
import static java.lang.String.format;
20-
import static org.hamcrest.CoreMatchers.is;
21-
import static org.junit.Assert.assertThat;
22-
2319
import java.security.AccessControlException;
2420
import java.security.Permission;
2521
import java.util.Map;
@@ -35,6 +31,10 @@
3531
import org.springframework.core.env.StandardEnvironmentTests;
3632
import org.springframework.stereotype.Component;
3733

34+
import static java.lang.String.format;
35+
import static org.hamcrest.CoreMatchers.*;
36+
import static org.junit.Assert.*;
37+
3838
/**
3939
* Tests integration between Environment and SecurityManagers. See SPR-9970.
4040
*
@@ -43,8 +43,10 @@
4343
public class EnvironmentSecurityManagerIntegrationTests {
4444

4545
private SecurityManager originalSecurityManager;
46+
4647
private Map<String, String> env;
4748

49+
4850
@Before
4951
public void setUp() {
5052
originalSecurityManager = System.getSecurityManager();
@@ -58,16 +60,16 @@ public void tearDown() {
5860
System.setSecurityManager(originalSecurityManager);
5961
}
6062

63+
6164
@Test
6265
public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
6366
SecurityManager securityManager = new SecurityManager() {
6467
@Override
6568
public void checkPermission(Permission perm) {
66-
// disallowing access to System#getenv means that our
69+
// Disallowing access to System#getenv means that our
6770
// ReadOnlySystemAttributesMap will come into play.
6871
if ("getenv.*".equals(perm.getName())) {
69-
throw new AccessControlException(
70-
"Accessing the system environment is disallowed");
72+
throw new AccessControlException("Accessing the system environment is disallowed");
7173
}
7274
}
7375
};
@@ -84,18 +86,17 @@ public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessT
8486
SecurityManager securityManager = new SecurityManager() {
8587
@Override
8688
public void checkPermission(Permission perm) {
87-
// disallowing access to System#getenv means that our
89+
// Disallowing access to System#getenv means that our
8890
// ReadOnlySystemAttributesMap will come into play.
8991
if ("getenv.*".equals(perm.getName())) {
90-
throw new AccessControlException(
91-
"Accessing the system environment is disallowed");
92+
throw new AccessControlException("Accessing the system environment is disallowed");
9293
}
93-
// disallowing access to the spring.profiles.active property means that
94+
// Disallowing access to the spring.profiles.active property means that
9495
// the BeanDefinitionReader won't be able to determine which profiles are
9596
// active. We should see an INFO-level message in the console about this
9697
// and as a result, any components marked with a non-default profile will
9798
// be ignored.
98-
if (("getenv."+AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
99+
if (("getenv." + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
99100
throw new AccessControlException(
100101
format("Accessing system environment variable [%s] is disallowed",
101102
AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
@@ -110,8 +111,10 @@ public void checkPermission(Permission perm) {
110111
assertThat(bf.containsBean("c1"), is(false));
111112
}
112113

114+
113115
@Component("c1")
114116
@Profile("p1")
115117
static class C1 {
116118
}
119+
117120
}

spring-context/src/test/java/org/springframework/jndi/SimpleNamingContextTests.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -214,52 +214,53 @@ public void testCreateInitialContext() throws Exception {
214214
assertEquals(ctx.lookup(name), o2);
215215
}
216216

217-
}
218217

219-
class StubDataSource implements DataSource {
218+
static class StubDataSource implements DataSource {
220219

221-
@Override
222-
public Connection getConnection() throws SQLException {
223-
return null;
224-
}
220+
@Override
221+
public Connection getConnection() throws SQLException {
222+
return null;
223+
}
225224

226-
@Override
227-
public Connection getConnection(String username, String password) throws SQLException {
228-
return null;
229-
}
225+
@Override
226+
public Connection getConnection(String username, String password) throws SQLException {
227+
return null;
228+
}
230229

231-
@Override
232-
public PrintWriter getLogWriter() throws SQLException {
233-
return null;
234-
}
230+
@Override
231+
public PrintWriter getLogWriter() throws SQLException {
232+
return null;
233+
}
235234

236-
@Override
237-
public int getLoginTimeout() throws SQLException {
238-
return 0;
239-
}
235+
@Override
236+
public int getLoginTimeout() throws SQLException {
237+
return 0;
238+
}
240239

241-
@Override
242-
public void setLogWriter(PrintWriter arg0) throws SQLException {
240+
@Override
241+
public void setLogWriter(PrintWriter arg0) throws SQLException {
243242

244-
}
243+
}
245244

246-
@Override
247-
public void setLoginTimeout(int arg0) throws SQLException {
245+
@Override
246+
public void setLoginTimeout(int arg0) throws SQLException {
248247

249-
}
248+
}
250249

251-
@Override
252-
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
253-
return false;
254-
}
250+
@Override
251+
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
252+
return false;
253+
}
255254

256-
@Override
257-
public <T> T unwrap(Class<T> arg0) throws SQLException {
258-
return null;
259-
}
255+
@Override
256+
public <T> T unwrap(Class<T> arg0) throws SQLException {
257+
return null;
258+
}
260259

261-
@Override
262-
public Logger getParentLogger() {
263-
return null;
260+
@Override
261+
public Logger getParentLogger() {
262+
return null;
263+
}
264264
}
265+
265266
}

spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContextBuilder.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.tests.mock.jndi;
1818

1919
import java.util.Hashtable;
20-
2120
import javax.naming.Context;
2221
import javax.naming.NamingException;
2322
import javax.naming.spi.InitialContextFactory;
@@ -40,9 +39,9 @@
4039
*
4140
* <p>There are various choices for DataSource implementations:
4241
* <ul>
43-
* <li>SingleConnectionDataSource (using the same Connection for all getConnection calls);
44-
* <li>DriverManagerDataSource (creating a new Connection on each getConnection call);
45-
* <li>Apache's Jakarta Commons DBCP offers BasicDataSource (a real pool).
42+
* <li>{@code SingleConnectionDataSource} (using the same Connection for all getConnection calls)
43+
* <li>{@code DriverManagerDataSource} (creating a new Connection on each getConnection call)
44+
* <li>Apache's Jakarta Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
4645
* </ul>
4746
*
4847
* <p>Typical usage in bootstrap code:
@@ -77,7 +76,6 @@
7776
* @see SimpleNamingContext
7877
* @see org.springframework.jdbc.datasource.SingleConnectionDataSource
7978
* @see org.springframework.jdbc.datasource.DriverManagerDataSource
80-
* @see org.apache.commons.dbcp.BasicDataSource
8179
*/
8280
public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder {
8381

@@ -197,7 +195,7 @@ public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environm
197195
if (activated == null && environment != null) {
198196
Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
199197
if (icf != null) {
200-
Class<?> icfClass = null;
198+
Class<?> icfClass;
201199
if (icf instanceof Class) {
202200
icfClass = (Class<?>) icf;
203201
}
@@ -216,10 +214,7 @@ else if (icf instanceof String) {
216214
return (InitialContextFactory) icfClass.newInstance();
217215
}
218216
catch (Throwable ex) {
219-
IllegalStateException ise =
220-
new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf);
221-
ise.initCause(ex);
222-
throw ise;
217+
throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
223218
}
224219
}
225220
}

0 commit comments

Comments
 (0)