
org.apache.camel.main.MainHelper 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.main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.PropertyBindingException;
import org.apache.camel.spi.ExtendedPropertyConfigurerGetter;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.OrderedLocationProperties;
import org.apache.camel.util.OrderedProperties;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class MainHelper {
private static final Logger LOG = LoggerFactory.getLogger(MainHelper.class);
private final String version;
private final long startDate;
private final Set componentEnvNames = new HashSet<>();
private final Set dataformatEnvNames = new HashSet<>();
private final Set languageEnvNames = new HashSet<>();
public MainHelper() {
startDate = System.currentTimeMillis();
try {
InputStream is = MainHelper.class.getResourceAsStream("/org/apache/camel/main/components.properties");
loadLines(is, componentEnvNames, s -> "CAMEL_COMPONENT_" + s.toUpperCase(Locale.US).replace('-', '_'));
IOHelper.close(is);
is = MainHelper.class.getResourceAsStream("/org/apache/camel/main/dataformats.properties");
loadLines(is, dataformatEnvNames, s -> "CAMEL_DATAFORMAT_" + s.toUpperCase(Locale.US).replace('-', '_'));
IOHelper.close(is);
is = MainHelper.class.getResourceAsStream("/org/apache/camel/main/languages.properties");
loadLines(is, languageEnvNames, s -> "CAMEL_LANGUAGE_" + s.toUpperCase(Locale.US).replace('-', '_'));
IOHelper.close(is);
} catch (Exception e) {
throw new RuntimeException("Error loading catalog information from classpath", e);
}
version = doGetVersion();
}
public String getVersion() {
return version;
}
public String getUptime() {
long delta = System.currentTimeMillis() - startDate;
if (delta == 0) {
return "";
}
return TimeUtils.printDuration(delta);
}
public void bootstrapDone() {
// after bootstrap then these maps are no longer needed
componentEnvNames.clear();
dataformatEnvNames.clear();
languageEnvNames.clear();
}
public static String toEnvVar(String name) {
return name.toUpperCase(Locale.US).replaceAll("[^\\w]", "-").replace('-', '_');
}
public static Optional lookupPropertyFromSysOrEnv(String name) {
String answer = System.getProperty(name);
if (answer == null) {
answer = System.getenv(toEnvVar(name));
}
return Optional.ofNullable(answer);
}
public static Properties loadEnvironmentVariablesAsProperties(String[] prefixes) {
Properties answer = new OrderedProperties();
if (prefixes == null || prefixes.length == 0) {
return answer;
}
for (String prefix : prefixes) {
final String pk = prefix.toUpperCase(Locale.US).replaceAll("[^\\w]", "-");
final String pk2 = pk.replace('-', '_');
System.getenv().forEach((k, v) -> {
k = k.toUpperCase(Locale.US);
if (k.startsWith(pk) || k.startsWith(pk2)) {
String key = k.toLowerCase(Locale.US).replace('_', '.');
answer.put(key, v);
}
});
}
return answer;
}
public static Map filterEnvVariables(String[] prefixes) {
Map answer = new HashMap<>();
System.getenv().forEach((k, v) -> {
final String uk = k.toUpperCase(Locale.US);
for (String prefix : prefixes) {
if (uk.startsWith(prefix)) {
answer.put(uk, v);
}
}
});
return answer;
}
public void addComponentEnvVariables(Map env, Properties properties, boolean custom) {
Set toRemove = new HashSet<>();
env.forEach((k, v) -> {
if (custom) {
toRemove.add(k);
String ck = "camel.component." + k.substring(16).toLowerCase(Locale.US).replace('_', '-');
ck = ck.replaceFirst("-", ".");
properties.put(ck, v);
} else {
Optional e
= componentEnvNames.stream().filter(k::startsWith).findFirst();
if (e.isPresent()) {
toRemove.add(k);
String cname = "camel.component." + e.get().substring(16).toLowerCase(Locale.US).replace('_', '-');
String option = k.substring(cname.length() + 1).toLowerCase(Locale.US).replace('_', '-');
properties.put(cname + "." + option, v);
}
}
});
toRemove.forEach(env::remove);
}
public void addDataFormatEnvVariables(Map env, Properties properties, boolean custom) {
Set toRemove = new HashSet<>();
env.forEach((k, v) -> {
if (custom) {
toRemove.add(k);
String ck = "camel.dataformat." + k.substring(17).toLowerCase(Locale.US).replace('_', '-');
ck = ck.replaceFirst("-", ".");
properties.put(ck, v);
} else {
Optional e
= dataformatEnvNames.stream().filter(k::startsWith).findFirst();
if (e.isPresent()) {
toRemove.add(k);
String cname = "camel.dataformat." + e.get().substring(17).toLowerCase(Locale.US).replace('_', '-');
String option = k.substring(cname.length() + 1).toLowerCase(Locale.US).replace('_', '-');
properties.put(cname + "." + option, v);
}
}
});
toRemove.forEach(env::remove);
}
public void addLanguageEnvVariables(Map env, Properties properties, boolean custom) {
Set toRemove = new HashSet<>();
env.forEach((k, v) -> {
if (custom) {
toRemove.add(k);
String ck = "camel.language." + k.substring(15).toLowerCase(Locale.US).replace('_', '-');
ck = ck.replaceFirst("-", ".");
properties.put(ck, v);
} else {
Optional e
= languageEnvNames.stream().filter(k::startsWith).findFirst();
if (e.isPresent()) {
toRemove.add(k);
String cname = "camel.language." + e.get().substring(15).toLowerCase(Locale.US).replace('_', '-');
String option = k.substring(cname.length() + 1).toLowerCase(Locale.US).replace('_', '-');
properties.put(cname + "." + option, v);
}
}
});
toRemove.forEach(env::remove);
}
public static Properties loadJvmSystemPropertiesAsProperties(String[] prefixes) {
Properties answer = new OrderedProperties();
if (prefixes == null || prefixes.length == 0) {
return answer;
}
for (String prefix : prefixes) {
final String pk = prefix.toUpperCase(Locale.US).replaceAll("[^\\w]", "-");
final String pk2 = pk.replace('-', '.');
System.getProperties().forEach((k, v) -> {
String key = k.toString().toUpperCase(Locale.US);
if (key.startsWith(pk) || key.startsWith(pk2)) {
answer.put(k.toString(), v);
}
});
}
return answer;
}
public static String optionKey(String key) {
// as we ignore case for property names we should use keys in same case and without dashes
key = StringHelper.dashToCamelCase(key);
return key;
}
public static boolean setPropertiesOnTarget(CamelContext context, Object target, Object source) throws Exception {
ObjectHelper.notNull(context, "context");
ObjectHelper.notNull(target, "target");
boolean rc = false;
PropertyConfigurer targetConfigurer = null;
if (target instanceof Component) {
// the component needs to be initialized to have the configurer ready
ServiceHelper.initService(target);
targetConfigurer = ((Component) target).getComponentPropertyConfigurer();
}
if (targetConfigurer == null) {
String name = target.getClass().getName();
// see if there is a configurer for it
targetConfigurer = context.adapt(ExtendedCamelContext.class)
.getConfigurerResolver().resolvePropertyConfigurer(name, context);
}
PropertyConfigurer sourceConfigurer = null;
if (source instanceof Component) {
// the component needs to be initialized to have the configurer ready
ServiceHelper.initService(source);
sourceConfigurer = ((Component) source).getComponentPropertyConfigurer();
}
if (sourceConfigurer == null) {
String name = source.getClass().getName();
// see if there is a configurer for it
sourceConfigurer = context.adapt(ExtendedCamelContext.class)
.getConfigurerResolver().resolvePropertyConfigurer(name, context);
}
if (targetConfigurer != null && sourceConfigurer instanceof ExtendedPropertyConfigurerGetter) {
ExtendedPropertyConfigurerGetter getter = (ExtendedPropertyConfigurerGetter) sourceConfigurer;
for (String key : getter.getAllOptions(source).keySet()) {
Object value = getter.getOptionValue(source, key, true);
if (value != null) {
rc |= targetConfigurer.configure(context, target, key, value, true);
}
}
}
return rc;
}
public static boolean setPropertiesOnTarget(
CamelContext context, Object target, OrderedLocationProperties properties,
String optionPrefix, boolean failIfNotSet, boolean ignoreCase,
OrderedLocationProperties autoConfiguredProperties) {
ObjectHelper.notNull(context, "context");
ObjectHelper.notNull(target, "target");
ObjectHelper.notNull(properties, "properties");
boolean rc = false;
PropertyConfigurer configurer = null;
if (target instanceof Component) {
// the component needs to be initialized to have the configurer ready
ServiceHelper.initService(target);
configurer = ((Component) target).getComponentPropertyConfigurer();
}
if (configurer == null) {
String name = target.getClass().getName();
// see if there is a configurer for it (use bootstrap)
configurer = context.adapt(ExtendedCamelContext.class)
.getBootstrapConfigurerResolver().resolvePropertyConfigurer(name, context);
}
try {
// keep a reference of the original keys
OrderedLocationProperties backup = new OrderedLocationProperties();
backup.putAll(properties);
rc = PropertyBindingSupport.build()
.withMandatory(failIfNotSet)
.withRemoveParameters(true)
.withConfigurer(configurer)
.withIgnoreCase(ignoreCase)
.bind(context, target, properties.asMap());
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy