Skip to content

Commit 834ddad

Browse files
committed
Polishing
(cherry picked from commit 50e50d0)
1 parent 37da706 commit 834ddad

File tree

12 files changed

+193
-176
lines changed

12 files changed

+193
-176
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: 15 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,9 @@
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.*;
3837

3938
/**
4039
* Tests integration between Environment and SecurityManagers. See SPR-9970.
@@ -44,8 +43,10 @@
4443
public class EnvironmentSecurityManagerIntegrationTests {
4544

4645
private SecurityManager originalSecurityManager;
46+
4747
private Map<String, String> env;
4848

49+
4950
@Before
5051
public void setUp() {
5152
originalSecurityManager = System.getSecurityManager();
@@ -59,16 +60,16 @@ public void tearDown() {
5960
System.setSecurityManager(originalSecurityManager);
6061
}
6162

63+
6264
@Test
6365
public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
6466
SecurityManager securityManager = new SecurityManager() {
6567
@Override
6668
public void checkPermission(Permission perm) {
67-
// disallowing access to System#getenv means that our
69+
// Disallowing access to System#getenv means that our
6870
// ReadOnlySystemAttributesMap will come into play.
6971
if ("getenv.*".equals(perm.getName())) {
70-
throw new AccessControlException(
71-
"Accessing the system environment is disallowed");
72+
throw new AccessControlException("Accessing the system environment is disallowed");
7273
}
7374
}
7475
};
@@ -85,18 +86,17 @@ public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessT
8586
SecurityManager securityManager = new SecurityManager() {
8687
@Override
8788
public void checkPermission(Permission perm) {
88-
// disallowing access to System#getenv means that our
89+
// Disallowing access to System#getenv means that our
8990
// ReadOnlySystemAttributesMap will come into play.
9091
if ("getenv.*".equals(perm.getName())) {
91-
throw new AccessControlException(
92-
"Accessing the system environment is disallowed");
92+
throw new AccessControlException("Accessing the system environment is disallowed");
9393
}
94-
// disallowing access to the spring.profiles.active property means that
94+
// Disallowing access to the spring.profiles.active property means that
9595
// the BeanDefinitionReader won't be able to determine which profiles are
9696
// active. We should see an INFO-level message in the console about this
9797
// and as a result, any components marked with a non-default profile will
9898
// be ignored.
99-
if (("getenv."+AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
99+
if (("getenv." + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
100100
throw new AccessControlException(
101101
format("Accessing system environment variable [%s] is disallowed",
102102
AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
@@ -111,8 +111,10 @@ public void checkPermission(Permission perm) {
111111
assertThat(bf.containsBean("c1"), is(false));
112112
}
113113

114+
114115
@Component("c1")
115116
@Profile("p1")
116117
static class C1 {
117118
}
119+
118120
}

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

Lines changed: 37 additions & 37 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.
@@ -40,7 +40,6 @@
4040

4141
import static org.junit.Assert.*;
4242

43-
4443
/**
4544
* @author Juergen Hoeller
4645
* @author Chris Beams
@@ -215,52 +214,53 @@ public void testCreateInitialContext() throws Exception {
215214
assertEquals(ctx.lookup(name), o2);
216215
}
217216

218-
}
219217

220-
class StubDataSource implements DataSource {
218+
static class StubDataSource implements DataSource {
221219

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

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

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

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

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

245-
}
243+
}
246244

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

250-
}
248+
}
251249

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

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

262-
@Override
263-
public Logger getParentLogger() {
264-
return null;
260+
@Override
261+
public Logger getParentLogger() {
262+
return null;
263+
}
265264
}
265+
266266
}

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
}

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ protected Set<String> doGetDefaultProfiles() {
291291
}
292292

293293
/**
294-
* {@inheritDoc}
294+
* Specify the set of profiles to be made active by default if no other profiles
295+
* are explicitly made active through {@link #setActiveProfiles}.
295296
* <p>Calling this method removes overrides any reserved default profiles
296297
* that may have been added during construction of the environment.
297298
* @see #AbstractEnvironment()
@@ -445,6 +446,44 @@ public void merge(ConfigurableEnvironment parent) {
445446
// Implementation of ConfigurablePropertyResolver interface
446447
//---------------------------------------------------------------------
447448

449+
public ConfigurableConversionService getConversionService() {
450+
return this.propertyResolver.getConversionService();
451+
}
452+
453+
public void setConversionService(ConfigurableConversionService conversionService) {
454+
this.propertyResolver.setConversionService(conversionService);
455+
}
456+
457+
public void setPlaceholderPrefix(String placeholderPrefix) {
458+
this.propertyResolver.setPlaceholderPrefix(placeholderPrefix);
459+
}
460+
461+
public void setPlaceholderSuffix(String placeholderSuffix) {
462+
this.propertyResolver.setPlaceholderSuffix(placeholderSuffix);
463+
}
464+
465+
public void setValueSeparator(String valueSeparator) {
466+
this.propertyResolver.setValueSeparator(valueSeparator);
467+
}
468+
469+
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) {
470+
this.propertyResolver.setIgnoreUnresolvableNestedPlaceholders(ignoreUnresolvableNestedPlaceholders);
471+
}
472+
473+
public void setRequiredProperties(String... requiredProperties) {
474+
this.propertyResolver.setRequiredProperties(requiredProperties);
475+
}
476+
477+
public void validateRequiredProperties() throws MissingRequiredPropertiesException {
478+
this.propertyResolver.validateRequiredProperties();
479+
}
480+
481+
482+
//---------------------------------------------------------------------
483+
// Implementation of PropertyResolver interface
484+
//---------------------------------------------------------------------
485+
486+
@Override
448487
public boolean containsProperty(String key) {
449488
return this.propertyResolver.containsProperty(key);
450489
}
@@ -477,14 +516,6 @@ public <T> T getRequiredProperty(String key, Class<T> targetType) throws Illegal
477516
return this.propertyResolver.getRequiredProperty(key, targetType);
478517
}
479518

480-
public void setRequiredProperties(String... requiredProperties) {
481-
this.propertyResolver.setRequiredProperties(requiredProperties);
482-
}
483-
484-
public void validateRequiredProperties() throws MissingRequiredPropertiesException {
485-
this.propertyResolver.validateRequiredProperties();
486-
}
487-
488519
public String resolvePlaceholders(String text) {
489520
return this.propertyResolver.resolvePlaceholders(text);
490521
}
@@ -493,30 +524,6 @@ public String resolveRequiredPlaceholders(String text) throws IllegalArgumentExc
493524
return this.propertyResolver.resolveRequiredPlaceholders(text);
494525
}
495526

496-
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) {
497-
this.propertyResolver.setIgnoreUnresolvableNestedPlaceholders(ignoreUnresolvableNestedPlaceholders);
498-
}
499-
500-
public void setConversionService(ConfigurableConversionService conversionService) {
501-
this.propertyResolver.setConversionService(conversionService);
502-
}
503-
504-
public ConfigurableConversionService getConversionService() {
505-
return this.propertyResolver.getConversionService();
506-
}
507-
508-
public void setPlaceholderPrefix(String placeholderPrefix) {
509-
this.propertyResolver.setPlaceholderPrefix(placeholderPrefix);
510-
}
511-
512-
public void setPlaceholderSuffix(String placeholderSuffix) {
513-
this.propertyResolver.setPlaceholderSuffix(placeholderSuffix);
514-
}
515-
516-
public void setValueSeparator(String valueSeparator) {
517-
this.propertyResolver.setValueSeparator(valueSeparator);
518-
}
519-
520527

521528
@Override
522529
public String toString() {

0 commit comments

Comments
 (0)