Skip to content

Commit

Permalink
Update to Camel RC1 #134
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 2, 2019
1 parent 2cb49aa commit 140cc41
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package org.apache.camel.k.loader.groovy.dsl


import org.apache.camel.support.IntrospectionSupport
import org.apache.camel.ExtendedCamelContext
import org.apache.camel.support.PropertyBindingSupport

class ComponentConfiguration {
Expand Down Expand Up @@ -49,20 +48,23 @@ class ComponentConfiguration {
}
}

if (!PropertyBindingSupport.bindProperty(component.camelContext, component, name, value)) {
if (!PropertyBindingSupport.build().withCamelContext(component.camelContext).withTarget(component).withProperty(name, value).bind()) {
throw new MissingMethodException(name, this.component.class, args as Object[])
}
}

def propertyMissing(String name, value) {
if (!PropertyBindingSupport.bindProperty(component.camelContext, component, name, value,)) {
if (!PropertyBindingSupport.build().withCamelContext(component.camelContext).withTarget(component).withProperty(name, value).bind()) {
throw new MissingMethodException(name, this.component.class, value)
}
}

def propertyMissing(String name) {
def properties = IntrospectionSupport.getProperties(component, properties, null, false)
def props = new HashMap<String, Object>()
def context = component.getCamelContext().adapt(ExtendedCamelContext.class)

context.getBeanIntrospection().getProperties(component, props, null, false)

return properties[name]
return props[name]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.spi.Registry;

public class IntegrationConfiguration extends BuilderSupport implements EndpointBuilderFactory {
public class IntegrationConfiguration extends BuilderSupport implements EndpointBuilderFactory, ProcessorSupport {
public final Registry registry;
public final Components components;
public final EndpointRouteBuilder builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.loader.js.dsl;

import java.util.function.Consumer;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public interface ProcessorSupport {
default Processor processor(Consumer<Exchange> consumer) {
return e -> consumer.accept(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ public void testProcessors() throws Exception {
runtime.addListener(Runtime.Phase.Started, r -> {
ProducerTemplate template = r.getCamelContext().createProducerTemplate();

String a = template.requestBody("direct:arrow", "", String.class);
assertThat(a).isEqualTo("arrow");

String f = template.requestBody("direct:function", "", String.class);
assertThat(f).isEqualTo("function");
assertThat(template.requestBody("direct:arrow", "")).isEqualTo("arrow");
assertThat(template.requestBody("direct:wrapper", "")).isEqualTo("wrapper");
assertThat(template.requestBody("direct:function", "")).isEqualTo("function");

runtime.stop();
});
Expand Down
12 changes: 10 additions & 2 deletions camel-k-loader-js/src/test/resources/routes-with-processors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const Processor = Java.type("org.apache.camel.Processor");
const p = Java.extend(Processor);
const f = new p(function(e) { e.getMessage().setBody('function') });
const a = new p(e => { e.getMessage().setBody('arrow') });
const w = processor(e => { e.getMessage().setBody('wrapper') });

from('direct:function')
.process(function(e) { e.getMessage().setBody('function') });
.process(f);

from('direct:arrow')
.process(e => { e.getMessage().setBody('arrow') });
.process(a);

from('direct:wrapper')
.process(w);

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.engine.AbstractCamelContext;
import org.apache.camel.k.RoutesLoader;
import org.apache.camel.k.Source;
import org.apache.camel.k.support.RuntimeSupport;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void addRoutesToCamelContext(CamelContext context) throws Exception {
// to be registered to the camel registry but as the
// original builder is masked by this wrapping builder,
// beans can't be automatically discovered
context.adapt(AbstractCamelContext.class)
context.adapt(ExtendedCamelContext.class)
.getBeanPostProcessor()
.postProcessBeforeInitialization(builder, builder.getClass().getName());

Expand Down
2 changes: 1 addition & 1 deletion camel-k-loader-yaml/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ import org.apache.camel.k.loader.yaml.parser.ProcessorStepParser;public class Ac
// create the definition
ToDefinition to = new ToDefinition()
to.setUri(String.format("http4://%s:%d/fixed/path"), definition.host, definition.port)
to.setUri(String.format("http://%s:%d/fixed/path"), definition.host, definition.port)
return to;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public interface StepParser {
static <T extends StepParser> T lookup(CamelContext camelContext, Class<T> type, String stepId) throws NoFactoryAvailableException {
T converter = camelContext.getRegistry().lookupByNameAndType(stepId, type);
if (converter == null) {
converter = (T) camelContext.adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH).newInstance(stepId);
}
if (converter == null) {
throw new IllegalStateException("No handler for step with id: " + stepId);
converter = camelContext.adapt(ExtendedCamelContext.class)
.getFactoryFinder(RESOURCE_PATH)
.newInstance(stepId, type)
.orElseThrow(() -> new RuntimeException("No handler for step with id: " + stepId));
}

return converter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.model.OutputNode;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.support.IntrospectionSupport;
import org.apache.camel.spi.BeanIntrospection;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.ObjectHelper;

Expand All @@ -51,9 +53,12 @@ public static <T extends ProcessorDefinition<?>, I> T adaptProcessor(CamelContex
}

final T answer = context.getInjector().newInstance(type);
final Map<String, Object> properties = IntrospectionSupport.getNonNullProperties(instance);
final Map<String, Object> properties = new HashMap<>();
final BeanIntrospection introspection = context.adapt(ExtendedCamelContext.class).getBeanIntrospection();

PropertyBindingSupport.bindProperties(context, answer, properties);
if (introspection.getProperties(instance, properties, null, false)) {
PropertyBindingSupport.build().withCamelContext(context).withTarget(answer).withProperties(properties).bind();
}

return answer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
Expand All @@ -41,6 +40,7 @@ public final class PropertiesSupport {
private PropertiesSupport() {
}

@SuppressWarnings("unchecked")
public static boolean bindProperties(CamelContext context, Object target, String prefix) {
final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
final Properties properties = component.loadProperties();
Expand All @@ -49,12 +49,13 @@ public static boolean bindProperties(CamelContext context, Object target, String
return false;
}

Map<String, Object> props = new HashMap<>();
for (String key : properties.stringPropertyNames()) {
props.put(key, properties.get(key));
}

return PropertyBindingSupport.bindProperties(context, target, props, prefix);
return PropertyBindingSupport.build()
.withCamelContext(context)
.withTarget(target)
.withProperties((Map)properties)
.withOptionPrefix(prefix)
.withRemoveParameters(false)
.bind();
}

public static Properties loadProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.RoutesLoader;
import org.apache.camel.k.Source;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -114,7 +113,10 @@ public static ContextCustomizer lookupCustomizerByID(CamelContext context, Strin
ContextCustomizer customizer = context.getRegistry().lookupByNameAndType(customizerId, ContextCustomizer.class);
if (customizer == null) {
try {
customizer = (ContextCustomizer) context.adapt(ExtendedCamelContext.class).getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH).newInstance(customizerId);
customizer = context.adapt(ExtendedCamelContext.class)
.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH)
.newInstance(customizerId, ContextCustomizer.class)
.orElseThrow(() -> new RuntimeException("Error creating instance for customizer: " + customizerId));
} catch (NoFactoryAvailableException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -177,16 +179,13 @@ public static RoutesLoader lookupLoaderByLanguage(CamelContext context, String l
}

public static RoutesLoader lookupLoaderFromResource(CamelContext context, String loaderId) {
final FactoryFinder finder;
final RoutesLoader loader;

try {
finder = context.adapt(ExtendedCamelContext.class).getFactoryFinder(Constants.ROUTES_LOADER_RESOURCE_PATH);
loader = (RoutesLoader)finder.newInstance(loaderId);
return context.adapt(ExtendedCamelContext.class)
.getFactoryFinder(Constants.ROUTES_LOADER_RESOURCE_PATH)
.newInstance(loaderId, RoutesLoader.class)
.orElseThrow(() -> new RuntimeException("Error creating instance of loader: " + loaderId));
} catch (NoFactoryAvailableException e) {
throw new IllegalArgumentException("Unable to find loader for: " + loaderId, e);
}

return loader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.Future;
import java.util.regex.Matcher;

import static java.lang.Integer.*;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServerOptions;
Expand All @@ -32,8 +34,8 @@
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
import org.apache.camel.support.IntrospectionSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.PropertiesHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -135,7 +137,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
}

KnativeHttpEndpoint ep = new KnativeHttpEndpoint(uri, this);
ep.setHeaderFilter(IntrospectionSupport.extractProperties(parameters, "filter.", true));
ep.setHeaderFilter(PropertiesHelper.extractProperties(parameters, "filter.", true));

switch (matcher.groupCount()) {
case 1:
Expand All @@ -145,12 +147,12 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
break;
case 2:
ep.setHost(matcher.group(1));
ep.setPort(Integer.parseInt(matcher.group(2)));
ep.setPort(parseInt(matcher.group(2)));
ep.setPath(KnativeHttp.DEFAULT_PATH);
break;
case 3:
ep.setHost(matcher.group(1));
ep.setPort(Integer.parseInt(matcher.group(2)));
ep.setPort(parseInt(matcher.group(2)));
ep.setPath(KnativeHttp.DEFAULT_PATH + matcher.group(3));
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion camel-knative/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http4</artifactId>
<artifactId>camel-http</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import org.apache.camel.Endpoint;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
import org.apache.camel.support.IntrospectionSupport;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.PropertiesHelper;
import org.apache.camel.util.StringHelper;

@Component("knative")
public class KnativeComponent extends DefaultComponent {
public static final String CONFIGURATION_ENV_VARIABLE = "CAMEL_KNATIVE_CONFIGURATION";

private final KnativeConfiguration configuration;
private KnativeConfiguration configuration;
private String environmentPath;

public KnativeComponent() {
Expand All @@ -50,6 +51,17 @@ public KnativeComponent(CamelContext context) {
//
// ************************

public KnativeConfiguration getConfiguration() {
return configuration;
}

/**
* Set the configuration.
*/
public void setConfiguration(KnativeConfiguration configuration) {
this.configuration = ObjectHelper.notNull(configuration, "configuration");
}

public String getEnvironmentPath() {
return environmentPath;
}
Expand All @@ -61,10 +73,6 @@ public void setEnvironmentPath(String environmentPath) {
this.environmentPath = environmentPath;
}

public KnativeConfiguration getConfiguration() {
return configuration;
}

public KnativeEnvironment getEnvironment() {
return configuration.getEnvironment();
}
Expand Down Expand Up @@ -116,7 +124,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
final KnativeConfiguration conf = getKnativeConfiguration();

conf.getTransportOptions().putAll(
IntrospectionSupport.extractProperties(parameters, "transport.", true)
PropertiesHelper.extractProperties(parameters, "transport.", true)
);

// set properties from the endpoint uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.camel.component.knative.ce.CloudEventsProcessors;
import org.apache.camel.processor.Pipeline;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.service.ServiceHelper;
Expand All @@ -52,7 +51,6 @@ public class KnativeEndpoint extends DefaultEndpoint implements DelegateEndpoint
@UriPath(description = "The Knative name")
private final String name;

@UriParam
private final KnativeConfiguration configuration;

private final KnativeEnvironment environment;
Expand Down
Loading

0 comments on commit 140cc41

Please sign in to comment.