This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch camel-3.0.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.0.x by this push: new d3e5233 Add an option to set the default application.properties location as weel as disabling it d3e5233 is described below commit d3e52337e634a39e949bbd158a640cfeb7b4bd8e Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Mon Dec 2 16:44:26 2019 +0100 Add an option to set the default application.properties location as weel as disabling it --- .../org/apache/camel/main/BaseMainSupport.java | 25 ++++++++-- .../main/MainPropertyPlaceholderLocationsTest.java | 57 ++++++++++++++++++++++ .../src/test/resources/default.properties | 17 +++++++ 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java index 01799ec..6a03bbb 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java @@ -88,6 +88,7 @@ public abstract class BaseMainSupport extends ServiceSupport { protected List<Object> configurations = new ArrayList<>(); protected String configurationClasses; protected String propertyPlaceholderLocations; + protected String defaultPropertyPlaceholderLocation = "classpath:application.properties;optional=true"; protected Properties initialProperties; protected Properties overrideProperties; @@ -243,6 +244,20 @@ public abstract class BaseMainSupport extends ServiceSupport { this.propertyPlaceholderLocations = location; } + public String getDefaultPropertyPlaceholderLocation() { + return defaultPropertyPlaceholderLocation; + } + + /** + * Set the default location for application properties if no locations have been set. + * If the value is set to "false" or empty, the default location is not taken into account. + * <p/> + * Default value is "classpath:application.properties;optional=true". + */ + public void setDefaultPropertyPlaceholderLocation(String defaultPropertyPlaceholderLocation) { + this.defaultPropertyPlaceholderLocation = defaultPropertyPlaceholderLocation; + } + @Deprecated public boolean isAutoConfigurationEnabled() { return mainConfigurationProperties.isAutoConfigurationEnabled(); @@ -480,12 +495,12 @@ public abstract class BaseMainSupport extends ServiceSupport { pc.setOverrideProperties(overrideProperties); } LOG.info("Using properties from: {}", propertyPlaceholderLocations); - } else { - // lets default to application.properties and ignore if its missing - // if there are no existing locations configured + } else if (ObjectHelper.isNotEmpty(defaultPropertyPlaceholderLocation) && !ObjectHelper.equal("false", defaultPropertyPlaceholderLocation)) { + // lets default to defaultPropertyPlaceholderLocation if + // there are no existing locations configured PropertiesComponent pc = camelContext.getPropertiesComponent(); if (pc.getLocations().isEmpty()) { - pc.addLocation("classpath:application.properties;optional=true"); + pc.addLocation(defaultPropertyPlaceholderLocation); } if (initialProperties != null) { pc.setInitialProperties(initialProperties); @@ -493,7 +508,7 @@ public abstract class BaseMainSupport extends ServiceSupport { if (overrideProperties != null) { pc.setOverrideProperties(overrideProperties); } - LOG.info("Using properties from classpath:application.properties"); + LOG.info("Using properties from {}", defaultPropertyPlaceholderLocation); } if (mainConfigurationProperties.getDurationMaxMessages() > 0 || mainConfigurationProperties.getDurationMaxIdleSeconds() > 0) { diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainPropertyPlaceholderLocationsTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainPropertyPlaceholderLocationsTest.java new file mode 100644 index 0000000..fb74aee --- /dev/null +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainPropertyPlaceholderLocationsTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.main; + +import org.junit.Assert; +import org.junit.Test; + +public class MainPropertyPlaceholderLocationsTest extends Assert { + @Test(expected = IllegalArgumentException.class) + public void testDefaultPropertyPlaceholderLocationDisabled() { + Main main = new Main(); + try { + main.setDefaultPropertyPlaceholderLocation("false"); + main.start(); + main.getCamelContext().resolvePropertyPlaceholders("{{hello}}"); + } finally { + main.stop(); + } + } + + @Test + public void testDefaultPropertyPlaceholderLocationEnabled() { + Main main = new Main(); + try { + main.start(); + assertEquals("World", main.getCamelContext().resolvePropertyPlaceholders("{{hello}}")); + } finally { + main.stop(); + } + } + + @Test + public void testCustomPropertyPlaceholderLocationEnabled() { + Main main = new Main(); + try { + main.setDefaultPropertyPlaceholderLocation("classpath:default.properties"); + main.start(); + assertEquals("default", main.getCamelContext().resolvePropertyPlaceholders("{{hello}}")); + } finally { + main.stop(); + } + } +} diff --git a/core/camel-main/src/test/resources/default.properties b/core/camel-main/src/test/resources/default.properties new file mode 100644 index 0000000..02cf755 --- /dev/null +++ b/core/camel-main/src/test/resources/default.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You 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. +## --------------------------------------------------------------------------- +hello=default \ No newline at end of file