Skip to content

Commit

Permalink
Merge pull request #518 from chrisdennis/issue-474-2.3.x
Browse files Browse the repository at this point in the history
Issue #474 [2.3.x]
  • Loading branch information
chrisdennis authored Oct 23, 2019
2 parents 7a15b9a + 58d1b2c commit 25c2cbd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
20 changes: 16 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 @@ -65,6 +65,7 @@
import java.lang.reflect.Method;
import java.security.AccessControlException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;

Expand Down Expand Up @@ -469,19 +470,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 @@ -492,7 +494,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 25c2cbd

Please sign in to comment.