Skip to content

Commit

Permalink
Error while deploying integration in Knative based environment apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Feb 15, 2019
1 parent 0024079 commit 344af95
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
2 changes: 2 additions & 0 deletions pkg/trait/knative_service.go
Expand Up @@ -140,6 +140,8 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e
envvar.SetVal(&environment, envName, s.Content)

params := make([]string, 0)
params = append(params, "name="+s.Name)

if s.InferLanguage() != "" {
params = append(params, "language="+string(s.InferLanguage()))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/knative_test.go
Expand Up @@ -111,7 +111,7 @@ func TestKnativeTraitWithCompressedSources(t *testing.T) {

routes := util.LookupEnvVar(vars, "CAMEL_K_ROUTES")
assert.NotNil(t, routes)
assert.Equal(t, "env:CAMEL_K_ROUTE_000?language=js&compression=true", routes.Value)
assert.Equal(t, "env:CAMEL_K_ROUTE_000?name=routes.js&language=js&compression=true", routes.Value)

route := util.LookupEnvVar(vars, "CAMEL_K_ROUTE_000")
assert.NotNil(t, route)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestKnativeTraitWithConfigMapSources(t *testing.T) {

routes := util.LookupEnvVar(vars, "CAMEL_K_ROUTES")
assert.NotNil(t, routes)
assert.Equal(t, "env:CAMEL_K_ROUTE_000?language=js&compression=true", routes.Value)
assert.Equal(t, "env:CAMEL_K_ROUTE_000?name=routes.js&language=js&compression=true", routes.Value)

route := util.LookupEnvVar(vars, "CAMEL_K_ROUTE_000")
assert.NotNil(t, route)
Expand Down
Expand Up @@ -23,16 +23,22 @@
import org.apache.commons.lang3.StringUtils;

public class Source {
private final String name;
private final String location;
private final String language;
private final boolean compressed;

private Source(String location, String language, boolean compression) {
private Source(String name, String location, String language, boolean compression) {
this.name = name;
this.location = location;
this.language = language;
this.compressed = compression;
}

public String getName() {
return name;
}

public String getLocation() {
return location;
}
Expand Down Expand Up @@ -66,7 +72,8 @@ public static Source create(String uri) throws Exception {
final String query = StringUtils.substringAfter(uri, "?");
final Map<String, Object> params = URISupport.parseQuery(query);
final String languageName = (String) params.get("language");
final boolean compression = Boolean.valueOf((String) params.get("compression"));
final String compression = (String) params.get("compression");


String language = languageName;
if (ObjectHelper.isEmpty(language)) {
Expand All @@ -77,7 +84,16 @@ public static Source create(String uri) throws Exception {
throw new IllegalArgumentException("Unknown language " + language);
}

String name = (String) params.get("name");
if (name == null) {
name = StringUtils.substringAfter(location, ":");
name = StringUtils.substringBeforeLast(name, ".");

if (name.contains("/")) {
name = StringUtils.substringAfterLast(name, "/");
}
}

return new Source(location, language, compression);
return new Source(name, location, language, Boolean.valueOf(compression));
}
}
Expand Up @@ -20,7 +20,6 @@
import java.util.List;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.k.Constants;
import org.apache.camel.k.RoutesLoader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
Expand All @@ -34,14 +33,13 @@ public List<String> getSupportedLanguages() {

@Override
public RouteBuilder load(Runtime.Registry registry, Source source) throws Exception {
String path = source.getLocation();
path = StringUtils.removeStart(path, Constants.SCHEME_CLASSPATH);
path = StringUtils.removeEnd(path, ".class");
String name = source.getName();
name = StringUtils.removeEnd(name, ".class");

Class<?> type = Class.forName(path);
Class<?> type = Class.forName(name);

if (!RouteBuilder.class.isAssignableFrom(type)) {
throw new IllegalStateException("The class provided (" + path + ") is not a org.apache.camel.builder.RouteBuilder");
throw new IllegalStateException("The class provided (" + source.getLocation() + ") is not a org.apache.camel.builder.RouteBuilder");
}

return (RouteBuilder)type.newInstance();
Expand Down
Expand Up @@ -49,15 +49,15 @@ public void configure() throws Exception {
final CamelContext context = getContext();

try (InputStream is = URIResolver.resolve(context, source)) {
String name = source.getLocation();
name = StringUtils.substringAfter(name, ":");
String name = source.getName();
name = StringUtils.removeEnd(name, ".java");

if (name.contains("/")) {
name = StringUtils.substringAfterLast(name, "/");
}
// compile the source in memory
String content = IOUtils.toString(is, StandardCharsets.UTF_8);
Reflect compiled = Reflect.compile(name, content);

RoutesBuilder builder = Reflect.compile(name, IOUtils.toString(is, StandardCharsets.UTF_8)).create().get();
// create the builder
RoutesBuilder builder = compiled.create().get();

// Wrap routes builder
includeRoutes(builder);
Expand Down
Expand Up @@ -88,6 +88,23 @@ public void testLoadJava() throws Exception {
assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
}

@Test
public void testLoadJavaWithNameOverride() throws Exception {
Source source = Source.create("classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java");
RoutesLoader loader = RuntimeSupport.loaderFor(new DefaultCamelContext(), source);
RouteBuilder builder = loader.load(new InMemoryRegistry(), source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(builder).isNotNull();

builder.configure();

List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
assertThat(routes).hasSize(1);
assertThat(routes.get(0).getInputs().get(0).getEndpointUri()).isEqualTo("timer:tick");
assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
}

@Test
public void testLoadJavaWithNestedClass() throws Exception {
Source source = Source.create("classpath:MyRoutesWithNestedClass.java");
Expand Down
@@ -0,0 +1,9 @@
import org.apache.camel.builder.RouteBuilder;

public class MyRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:tick")
.to("log:info");
}
}

0 comments on commit 344af95

Please sign in to comment.