Skip to content

Commit

Permalink
jvm: improve language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and nicolaferraro committed Sep 15, 2018
1 parent e417081 commit 8517c33
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) throws Exception {
final String resource = System.getenv(Routes.ENV_CAMEL_K_ROUTES_URI);
final String language = System.getenv(Routes.ENV_CAMEL_K_ROUTES_LANGUAGE);
final String resource = System.getenv(Constants.ENV_CAMEL_K_ROUTES_URI);
final String language = System.getenv(Constants.ENV_CAMEL_K_ROUTES_LANGUAGE);

if (ObjectHelper.isEmpty(resource)) {
throw new IllegalStateException("No valid resource found in " + Routes.ENV_CAMEL_K_ROUTES_URI + " environment variable");
throw new IllegalStateException("No valid resource found in " + Constants.ENV_CAMEL_K_ROUTES_URI + " environment variable");
}

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

if (routes == null) {
throw new IllegalStateException("Unable to load route from: " + resource);
}

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

Main main = new Main();

Expand All @@ -75,8 +75,8 @@ public static void main(String[] args) throws Exception {
// *******************************

private static String computePropertyPlaceholderLocations() throws IOException {
final String conf = System.getenv(Routes.ENV_CAMEL_K_CONF);
final String confd = System.getenv(Routes.ENV_CAMEL_K_CONF_D);
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package org.apache.camel.k.jvm;

import org.junit.Ignore;
import org.junit.Test;
public final class Constants {
public static final String ENV_CAMEL_K_ROUTES_URI = "CAMEL_K_ROUTES_URI";
public static final String ENV_CAMEL_K_ROUTES_LANGUAGE = "CAMEL_K_ROUTES_LANGUAGE";
public static final String ENV_CAMEL_K_CONF = "CAMEL_K_CONF";
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 class ApplicationTest {
@Test
@Ignore
public void applicationTest() throws Exception {
Application.main(new String[] { MyRoutes.class.getCanonicalName() });
private Constants() {
}

}
71 changes: 71 additions & 0 deletions runtime/jvm/src/main/java/org/apache/camel/k/jvm/Language.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public enum Language {
Unknow("", Collections.emptyList()),
JavaClass("class", Collections.singletonList("class")),
JavaSource("java", Collections.singletonList("java")),
JavaScript("js", Collections.singletonList("js")),
Groovy("groovy", Collections.singletonList("groovy")),
Xml("xml", Collections.singletonList("xml"));

private final String name;
private final List<String> extensions;

Language(String name, List<String> extensions) {
this.name = name;
this.extensions = extensions;
}

public String getName() {
return name;
}

public List<String> getExtensions() {
return extensions;
}

public static Language fromLanguageName(String name) {
for (Language language: values()) {
if (language.getName().equals(name)) {
return language;
}
}

return Unknow;
}

public static Language fromResource(String resource) {
for (Language language: values()) {
String path = StringUtils.substringAfter(resource, ":");

for (String ext : language.getExtensions()) {
if (path.endsWith("." + ext)) {
return language;
}
}
}

return Unknow;
}
}
90 changes: 0 additions & 90 deletions runtime/jvm/src/main/java/org/apache/camel/k/jvm/Routes.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
package org.apache.camel.k.jvm;

import java.util.List;
import java.util.function.Predicate;

import org.apache.camel.builder.RouteBuilder;

public interface RoutesLoader extends Predicate<String> {
public interface RoutesLoader {
/**
* Provides a list of the languages supported by this loader.
*
* @return the supported languages.
*/
List<String> getSupportedLanguages();
List<Language> getSupportedLanguages();

/**
* Creates a camel {@link RouteBuilder} from the given resource.
Expand Down
Loading

0 comments on commit 8517c33

Please sign in to comment.