Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use properties to configure components and logging #140

Merged
merged 3 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ kamel run runtime/examples/Sample.java

A "Sample.java" file is included in the link:/runtime/examples[/runtime/examples] folder of this repository. You can change the content of the file and execute the command again to see the changes.

==== Configure Integration properties

Properties associated to an integration can be configured either using a ConfigMap/Secret or by setting using the "--property" flag, i.e.

```
kamel run --property my.message=test runtime/examples/props.js
```

==== Configure Integration Logging

camel-k runtime uses log4j2 as logging framework and can be configured through integration properties.
If you need to change the logging level of various loggers, you can do so by using the `logging.level` prefix:

```
logging.level.org.apache.camel = DEBUG
```

==== Configure Integration Components

camel-k component can be configured programmatically inside an integration or using properties with the following syntax.

```
camel.component.${scheme}.${property} = ${value}
```

As example if you want to change the queue size of the seda component, you can use the following property:

```
camel.component.seda.queueSize = 10
```

=== Running Integrations in "Dev" Mode for Fast Feedback

If you want to iterate quickly on a integration to have fast feedback on the code you're writing, you can use run it in **"dev" mode**:
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ type Dependencies struct {

// Add a dependency to maven's dependencies
func (deps *Dependencies) Add(dep Dependency) {
for _, d := range deps.Dependencies {
// Check if the given dependency is already included in the dependency list
if d == dep {
return
}
}

deps.Dependencies = append(deps.Dependencies, dep)
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
Expand Down
119 changes: 66 additions & 53 deletions runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,46 @@
*/
package org.apache.camel.k.jvm;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

static {
//
// Load properties as system properties so they are accessible through
// camel's properties component as well as for runtime configuration.
//
RuntimeSupport.configureSystemProperties();

//
// Configure the logging subsystem log4j2 using a subset of spring boot
// conventions:
//
// logging.level.${nane} = OFF|FATAL|ERROR|WARN|INFO|DEBUG|TRACE|ALL
//
// We now support setting the logging level only
//
RuntimeSupport.configureLogging();
}

// *******************************
//
// Main
//
// *******************************

public static void main(String[] args) throws Exception {
final String resource = System.getenv(Constants.ENV_CAMEL_K_ROUTES_URI);
final String language = System.getenv(Constants.ENV_CAMEL_K_ROUTES_LANGUAGE);
Expand All @@ -46,7 +64,6 @@ public static void main(String[] args) throws Exception {
throw new IllegalStateException("No valid resource found in " + Constants.ENV_CAMEL_K_ROUTES_URI + " environment variable");
}

String locations = computePropertyPlaceholderLocations();
RoutesLoader loader = RoutesLoaders.loaderFor(resource, language);
RouteBuilder routes = loader.load(resource);

Expand All @@ -56,59 +73,55 @@ public static void main(String[] args) throws Exception {

LOGGER.info("Routes: {}", resource);
LOGGER.info("Language: {}", language);
LOGGER.info("Locations: {}", locations);

Main main = new Main();

if (ObjectHelper.isNotEmpty(locations)) {
main.setPropertyPlaceholderLocations(locations);
}

main.addMainListener(new ComponentPropertiesBinder());
main.addRouteBuilder(routes);
main.run();
}

// *******************************
//
// helpers
// Listeners
//
// *******************************

private static String computePropertyPlaceholderLocations() throws IOException {
final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF);
final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D);
final List<String> locations = new ArrayList<>();

// Main location
if (ObjectHelper.isNotEmpty(conf)) {
locations.add("file:" + conf);
}

// Additional locations
if (ObjectHelper.isNotEmpty(confd)) {
Path root = Paths.get(confd);
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
static class ComponentPropertiesBinder extends MainListenerSupport {
@Override
public void configure(CamelContext context) {
context.addLifecycleStrategy(new LifecycleStrategySupport() {
@SuppressWarnings("unchecked")
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);

String path = file.toFile().getAbsolutePath();
String ext = FilenameUtils.getExtension(path);

if (Objects.equals("properties", ext)) {
locations.add("file:" + path);
}

return FileVisitResult.CONTINUE;
public void onComponentAdd(String name, Component component) {
// Integration properties are defined as system properties
final Properties properties = System.getProperties();

// Set the prefix used by setProperties to filter
// and apply properties to match the one used by
// camel spring boot:
//
// camel.component.${scheme}.${value}
//
final String prefix = "camel.component." + name + ".";

properties.entrySet().stream()
.filter(entry -> entry.getKey() instanceof String)
.filter(entry -> entry.getValue() != null)
.filter(entry -> ((String)entry.getKey()).startsWith(prefix))
.forEach(entry -> {
final String key = ((String)entry.getKey()).substring(prefix.length());
final Object val = entry.getValue();

try {
IntrospectionSupport.setProperty(component, key, val, false);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
);
}
};

if (Files.exists(root)) {
Files.walkFileTree(root, visitor);
}
});
}

return String.join(",", locations);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public final class Constants {
public static final String ENV_CAMEL_K_CONF_D = "CAMEL_K_CONF_D";
public static final String SCHEME_CLASSPATH = "classpath:";
public static final String SCHEME_FILE = "file:";
public static final String LOGGING_LEVEL_PREFIX = "logging.level.";

private Constants() {
}
Expand Down
110 changes: 110 additions & 0 deletions runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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.k.jvm;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
import java.util.Properties;

import org.apache.camel.util.ObjectHelper;
import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;

public final class RuntimeSupport {
private RuntimeSupport() {
}

public static void configureSystemProperties() {
final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF);
final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D);
final Properties properties = new Properties();

// Main location
if (ObjectHelper.isNotEmpty(conf)) {
try (Reader reader = Files.newBufferedReader(Paths.get(conf))) {
properties.load(reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// Additional locations
if (ObjectHelper.isNotEmpty(confd)) {
Path root = Paths.get(confd);
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);

String path = file.toFile().getAbsolutePath();
String ext = FilenameUtils.getExtension(path);

if (Objects.equals("properties", ext)) {
try (Reader reader = Files.newBufferedReader(Paths.get(path))) {
properties.load(reader);
}
}

return FileVisitResult.CONTINUE;
}
};

if (Files.exists(root)) {
try {
Files.walkFileTree(root, visitor);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

System.getProperties().putAll(properties);
}

public static void configureLogging() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Properties properties = System.getProperties();

properties.entrySet().stream()
.filter(entry -> entry.getKey() instanceof String)
.filter(entry -> entry.getValue() instanceof String)
.filter(entry -> ((String)entry.getKey()).startsWith(Constants.LOGGING_LEVEL_PREFIX))
.forEach(entry -> {
String key = ((String)entry.getKey());
String val = ((String)entry.getValue());

String logger = key.substring(Constants.LOGGING_LEVEL_PREFIX.length());
Level level = Level.getLevel(val);
LoggerConfig config = new LoggerConfig(logger, level, true);

ctx.getConfiguration().addLogger(logger, config);
}
);
}
}
Loading