org.apache.camel.component.properties.AbstractLocationPropertiesSource Maven / Gradle / Ivy
/*
* 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.component.properties;
import java.util.Map;
import java.util.Properties;
import org.apache.camel.spi.LoadablePropertiesSource;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.OrderedProperties;
/**
* Base class for {@link LoadablePropertiesSource} which can load properties from a source such as classpath or file system.
*/
public abstract class AbstractLocationPropertiesSource extends ServiceSupport implements LoadablePropertiesSource, LocationPropertiesSource {
private final Properties properties = new OrderedProperties();
private final PropertiesComponent propertiesComponent;
private final PropertiesLocation location;
protected AbstractLocationPropertiesSource(PropertiesComponent propertiesComponent, PropertiesLocation location) {
this.propertiesComponent = propertiesComponent;
this.location = location;
}
abstract Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location);
public PropertiesLocation getLocation() {
return location;
}
@Override
public Properties loadProperties() {
return properties;
}
@Override
public String getProperty(String name) {
return properties.getProperty(name);
}
@Override
protected void doInit() throws Exception {
super.doInit();
Properties prop = loadPropertiesFromLocation(propertiesComponent, location);
if (prop != null) {
prop = prepareLoadedProperties(prop);
properties.putAll(prop);
}
}
@Override
protected void doStart() throws Exception {
// noop
}
@Override
protected void doStop() throws Exception {
// noop
}
/**
* Strategy to prepare loaded properties before being used by Camel.
*
* This implementation will ensure values are trimmed, as loading properties from
* a file with values having trailing spaces is not automatic trimmed by the Properties API
* from the JDK.
*
* @param properties the properties
* @return the prepared properties
*/
protected static Properties prepareLoadedProperties(Properties properties) {
Properties answer = new OrderedProperties();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy