Skip to content

Commit 25c2cbd

Browse files
authored
Merge pull request #518 from chrisdennis/issue-474-2.3.x
Issue #474 [2.3.x]
2 parents 7a15b9a + 58d1b2c commit 25c2cbd

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

quartz-core/src/main/java/org/quartz/impl/StdSchedulerFactory.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import java.lang.reflect.Method;
6666
import java.security.AccessControlException;
6767
import java.util.Collection;
68+
import java.util.Enumeration;
6869
import java.util.Locale;
6970
import java.util.Properties;
7071

@@ -469,19 +470,20 @@ public void initialize() throws SchedulerException {
469470
}
470471
}
471472

472-
initialize(overrideWithSysProps(props));
473+
initialize(overrideWithSysProps(props, getLog()));
473474
}
474475

475476
/**
476477
* Add all System properties to the given <code>props</code>. Will override
477478
* any properties that already exist in the given <code>props</code>.
478479
*/
479-
private Properties overrideWithSysProps(Properties props) {
480+
// Visible for testing
481+
static Properties overrideWithSysProps(Properties props, Logger log) {
480482
Properties sysProps = null;
481483
try {
482484
sysProps = System.getProperties();
483485
} catch (AccessControlException e) {
484-
getLog().warn(
486+
log.warn(
485487
"Skipping overriding quartz properties with System properties " +
486488
"during initialization because of an AccessControlException. " +
487489
"This is likely due to not having read/write access for " +
@@ -492,7 +494,17 @@ private Properties overrideWithSysProps(Properties props) {
492494
}
493495

494496
if (sysProps != null) {
495-
props.putAll(sysProps);
497+
// Use the propertyNames to iterate to avoid
498+
// a possible ConcurrentModificationException
499+
Enumeration<?> en = sysProps.propertyNames();
500+
while (en.hasMoreElements()) {
501+
Object name = en.nextElement();
502+
Object value = sysProps.get(name);
503+
if (name instanceof String && value instanceof String) {
504+
// Properties javadoc discourages use of put so we use setProperty
505+
props.setProperty((String) name, (String) value);
506+
}
507+
}
496508
}
497509

498510
return props;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
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+
package org.quartz.impl;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.util.Properties;
21+
22+
import org.junit.Test;
23+
import org.slf4j.helpers.NOPLogger;
24+
25+
public class StdSchedulerFactoryTest {
26+
27+
@Test
28+
public void testOverrideSystemProperties() {
29+
Properties p = new Properties();
30+
p.setProperty("nonsense1", "hello1");
31+
p.setProperty("nonsense2", "hello2");
32+
System.setProperty("nonsense1", "boo1");
33+
String osName = System.getProperty("os.name");
34+
Properties q = StdSchedulerFactory.overrideWithSysProps(p, NOPLogger.NOP_LOGGER);
35+
assertEquals("boo1", q.get("nonsense1"));
36+
assertEquals(osName, q.get("os.name"));
37+
}
38+
}

0 commit comments

Comments
 (0)