Skip to content

Commit

Permalink
Add DSL to bind object to camel registry
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 1, 2018
1 parent 3258b92 commit a32bab5
Show file tree
Hide file tree
Showing 28 changed files with 725 additions and 111 deletions.
20 changes: 19 additions & 1 deletion runtime/catalog-builder/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand All @@ -16,7 +34,7 @@
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<id>get-syndesis-version</id>
Expand Down
18 changes: 18 additions & 0 deletions runtime/dependency-lister/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down
18 changes: 18 additions & 0 deletions runtime/groovy/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.List;

import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.GroovyShell;
import groovy.util.DelegatingScript;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.k.jvm.Language;
import org.apache.camel.k.jvm.RoutesLoader;
import org.apache.camel.k.jvm.dsl.Scripting;
import org.apache.camel.k.jvm.RuntimeRegistry;
import org.apache.camel.k.jvm.dsl.Components;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.rest.RestConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ResourceHelper;
import org.codehaus.groovy.control.CompilerConfiguration;

Expand All @@ -39,7 +49,7 @@ public List<Language> getSupportedLanguages() {
}

@Override
public RouteBuilder load(String resource) throws Exception {
public RouteBuilder load(RuntimeRegistry registry, String resource) throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
Expand All @@ -54,10 +64,89 @@ public void configure() throws Exception {
DelegatingScript script = (DelegatingScript) sh.parse(reader);

// set the delegate target
script.setDelegate(new Scripting(this));
script.setDelegate(new Scripting(registry, this));
script.run();
}
}
};
}


public static class Scripting {
private final RuntimeRegistry registry;

public final CamelContext context;
public final Components components;
public final RouteBuilder builder;

public Scripting(RuntimeRegistry registry, RouteBuilder builder) {
this.registry = registry;
this.context = builder.getContext();
this.components = new Components(this.context);
this.builder = builder;
}

public Component component(String name, Closure<Component> callable) {
final Component component = context.getComponent(name, true);

callable.setResolveStrategy(Closure.DELEGATE_ONLY);
callable.setDelegate(new GroovyObjectSupport() {
public Object invokeMethod(String name, Object arg) {
if (arg == null) {
return super.invokeMethod(name, arg);
}
if (!arg.getClass().isArray()) {
return super.invokeMethod(name, arg);
}

try {
IntrospectionSupport.setProperty(component, name, Array.get(arg, 0), true);
} catch (Exception e) {
throw new RuntimeException(e);
}

return component;
}
});

return callable.call();
}

public RouteDefinition from(String endpoint) {
return builder.from(endpoint);
}

public RestDefinition rest() {
return builder.rest();
}

public RestDefinition rest(Closure<RestDefinition> callable) {
callable.setResolveStrategy(Closure.DELEGATE_ONLY);
callable.setDelegate(builder.rest());
return callable.call();
}

public RestConfigurationDefinition restConfiguration() {
return builder.restConfiguration();
}

public void restConfiguration(Closure<?> callable) {
callable.setResolveStrategy(Closure.DELEGATE_ONLY);
callable.setDelegate(builder.restConfiguration());
callable.call();
}

public RestConfigurationDefinition restConfiguration(String component, Closure<RestConfigurationDefinition> callable) {
callable.setResolveStrategy(Closure.DELEGATE_ONLY);
callable.setDelegate(builder.restConfiguration(component));
return callable.call();
}

public RuntimeRegistry registry(Closure<RuntimeRegistry> callable) {
callable.setResolveStrategy(Closure.DELEGATE_ONLY);
callable.setDelegate(registry);

return callable.call();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.groovy;


import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.k.jvm.Runtime;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.MainSupport;
import org.junit.Test;

import static org.assertj.core.api.Java6Assertions.assertThat;

public class ComponentConfigurationTest {
@Test
public void testLoadRouteWithComponentConfiguration() throws Exception {
Runtime runtime = new Runtime();
runtime.setDuration(5);
runtime.load("classpath:routes-with-component-configuration.groovy", null);
runtime.addMainListener(new MainListenerSupport() {
@Override
public void afterStart(MainSupport main) {
try {
CamelContext context = main.getCamelContexts().get(0);
Component component = context.getComponent("seda");

assertThat(component).isNotNull();
assertThat(component).hasFieldOrPropertyWithValue("queueSize", 1234);

main.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

runtime.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.groovy;


import org.apache.camel.CamelContext;
import org.apache.camel.k.jvm.Runtime;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.MainSupport;
import org.junit.Test;

import static org.assertj.core.api.Java6Assertions.assertThat;

public class RestTest {
@Test
public void testLoadRouteWithRest() throws Exception {
Runtime runtime = new Runtime();
runtime.setDuration(5);
runtime.load("classpath:routes-with-rest.groovy", null);
runtime.addMainListener(new MainListenerSupport() {
@Override
public void afterStart(MainSupport main) {
try {
CamelContext context = main.getCamelContexts().get(0);

assertThat(context.getRestConfiguration()).hasFieldOrPropertyWithValue("host", "my-host");
assertThat(context.getRestConfiguration()).hasFieldOrPropertyWithValue("port", 9192);
assertThat(context.getRestConfiguration("undertow", false)).hasFieldOrPropertyWithValue("host", "my-undertow-host");
assertThat(context.getRestConfiguration("undertow", false)).hasFieldOrPropertyWithValue("port", 9193);

main.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

runtime.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.k.jvm.RoutesLoader;
import org.apache.camel.k.jvm.RoutesLoaders;
import org.apache.camel.k.jvm.RuntimeRegistry;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.ToDefinition;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class RoutesLoadersTest {
public class RoutesLoaderTest {
@Test
public void testLoadGroovy() throws Exception {
String resource = "classpath:routes.groovy";
RoutesLoader loader = RoutesLoaders.loaderFor(resource, null);
RouteBuilder builder = loader.load(resource);
RouteBuilder builder = loader.load(new RuntimeRegistry(), resource);

assertThat(loader).isInstanceOf(GroovyRoutesLoader.class);
assertThat(builder).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.groovy;


import org.apache.camel.CamelContext;
import org.apache.camel.k.jvm.Runtime;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.MainSupport;
import org.apache.camel.spi.Registry;
import org.junit.Test;

import static org.assertj.core.api.Java6Assertions.assertThat;

public class RuntimeRegistryTest {
@Test
public void testLoadRouteWithBindings() throws Exception {
Runtime runtime = new Runtime();
runtime.setDuration(5);
runtime.load("classpath:routes-with-bindings.groovy", null);
runtime.addMainListener(new MainListenerSupport() {
@Override
public void afterStart(MainSupport main) {
try {
CamelContext context = main.getCamelContexts().get(0);
Registry registry = context.getRegistry();

assertThat(registry.lookup("myEntry1")).isEqualTo("myRegistryEntry1");
assertThat(registry.lookup("myEntry2")).isEqualTo("myRegistryEntry2");

main.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

runtime.run();
}
}
Loading

0 comments on commit a32bab5

Please sign in to comment.