Skip to content

Commit

Permalink
Fixes #474 : fix ConcurrentModificationException in StdSchedulerFacto…
Browse files Browse the repository at this point in the history
…ry.overrideWithSysProps
  • Loading branch information
davidmoten authored and chrisdennis committed Oct 22, 2019
1 parent 77e9826 commit 12cc7a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
21 changes: 17 additions & 4 deletions quartz-core/src/main/java/org/quartz/impl/StdSchedulerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Enumeration;

/**
* <p>
* An implementation of <code>{@link org.quartz.SchedulerFactory}</code> that
Expand Down Expand Up @@ -441,19 +443,20 @@ public void initialize() throws SchedulerException {
}
}

initialize(overrideWithSysProps(props));
initialize(overrideWithSysProps(props, getLog()));
}

/**
* Add all System properties to the given <code>props</code>. Will override
* any properties that already exist in the given <code>props</code>.
*/
private Properties overrideWithSysProps(Properties props) {
// Visible for testing
static Properties overrideWithSysProps(Properties props, Logger log) {
Properties sysProps = null;
try {
sysProps = System.getProperties();
} catch (AccessControlException e) {
getLog().warn(
log.warn(
"Skipping overriding quartz properties with System properties " +
"during initialization because of an AccessControlException. " +
"This is likely due to not having read/write access for " +
Expand All @@ -464,7 +467,17 @@ private Properties overrideWithSysProps(Properties props) {
}

if (sysProps != null) {
props.putAll(sysProps);
// Use the propertyNames to iterate to avoid
// a possible ConcurrentModificationException
Enumeration<?> en = sysProps.propertyNames();
while (en.hasMoreElements()) {
Object name = en.nextElement();
Object value = sysProps.get(name);
if (name instanceof String && value instanceof String) {
// Properties javadoc discourages use of put so we use setProperty
props.setProperty((String) name, (String) value);
}
}
}

return props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.quartz.impl;

import static org.junit.Assert.assertEquals;

import java.util.Properties;

import org.junit.Test;
import org.slf4j.helpers.NOPLogger;

public class StdSchedulerFactoryTest {

@Test
public void testOverrideSystemProperties() {
Properties p = new Properties();
p.setProperty("nonsense1", "hello1");
p.setProperty("nonsense2", "hello2");
System.setProperty("nonsense1", "boo1");
String osName = System.getProperty("os.name");
Properties q = StdSchedulerFactory.overrideWithSysProps(p, NOPLogger.NOP_LOGGER);
assertEquals("boo1", q.get("nonsense1"));
assertEquals(osName, q.get("os.name"));
}
}

0 comments on commit 12cc7a0

Please sign in to comment.