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

add servlet support #65

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@

public final class Constants {
public static final String ENV_CAMEL_K_ROUTES = "CAMEL_K_ROUTES";
public static final String PROPERTY_CAMEL_K_ROUTES = "camel.k.routes";

public static final String ENV_CAMEL_K_CONF = "CAMEL_K_CONF";
public static final String PROPERTY_CAMEL_K_CONF = "camel.k.conf";

public static final String ENV_CAMEL_K_CONF_D = "CAMEL_K_CONF_D";
public static final String PROPERTY_CAMEL_K_CONF_D = "camel.k.conf.d";

public static final String ENV_CAMEL_K_CUSTOMIZERS = "CAMEL_K_CUSTOMIZERS";
public static final String PROPERTY_CAMEL_K_CUSTOMIZER = "camel.k.customizer";

public static final String SCHEME_CLASSPATH = "classpath:";
public static final String SCHEME_FILE = "file:";
public static final String SCHEME_ENV = "env:";
public static final String LOGGING_LEVEL_PREFIX = "logging.level.";
public static final String ROUTES_LOADER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/loader/";
public static final String CONTEXT_CUSTOMIZER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/customizer/";
public static final String PROPERTY_CAMEL_K_CUSTOMIZER = "camel.k.customizer";

private Constants() {
}
Expand Down
33 changes: 31 additions & 2 deletions camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Properties;

import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.component.properties.PropertiesComponent;

public interface Runtime {
Expand All @@ -42,17 +43,45 @@ default void setProperties(Properties properties) {
enum Phase {
Starting,
ConfigureContext,
ContextConfigured,
ConfigureRoutes,
RoutesConfigured,
Started,
Stopping,
Stopped
}

@FunctionalInterface
interface Listener {
void accept(Phase phase, Runtime runtime);
interface Listener extends Ordered {
boolean accept(Phase phase, Runtime runtime);

@Override
default int getOrder() {
return Ordered.LOWEST;
}
}

interface Registry extends org.apache.camel.k.adapter.Registry {
}

/**
* Helper to create a simple runtime from a given Camel Context and Runtime Registry.
*
* @param camelContext the camel context
* @param registry the runtime registry
* @return the runtime
*/
static Runtime of( CamelContext camelContext, Registry registry) {
return new Runtime() {
@Override
public CamelContext getContext() {
return camelContext;
}

@Override
public Registry getRegistry() {
return registry;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ protected AbstractPhaseListener(Runtime.Phase phase) {
}

@Override
public void accept(Runtime.Phase phase, Runtime runtime) {
if (this.phase == phase) {
public boolean accept(Runtime.Phase phase, Runtime runtime) {
boolean run = this.phase == phase;
if (run) {
accept(runtime);
}

return run;
}

protected abstract void accept(Runtime runtime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ public RoutesConfigurer() {

@Override
protected void accept(Runtime runtime) {
final String routes = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_ROUTES, "");
String routes = System.getProperty(Constants.PROPERTY_CAMEL_K_ROUTES);

if (ObjectHelper.isEmpty(routes)) {
routes = System.getenv(Constants.ENV_CAMEL_K_ROUTES);
}

if (ObjectHelper.isEmpty(routes)) {
LOGGER.warn("No routes found in {} environment variable", Constants.ENV_CAMEL_K_ROUTES);
return;
}

load(runtime, routes.split(",", -1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public static void configureRest(CamelContext context) {
}
}

public static String resolvePropertyPlaceholders(CamelContext context, String text) throws Exception {
return context.resolvePropertyPlaceholders(
context.getPropertyPrefixToken() + text + context.getPropertySuffixToken()
);
}

public static int bindProperties(CamelContext context, Object target, String prefix) {
final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
final Properties properties = component.getInitialProperties();
Expand Down Expand Up @@ -191,8 +197,8 @@ public static RoutesLoader lookupLoaderFromResource(CamelContext context, Source
}

public static Properties loadProperties() {
final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF);
final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D);
final String conf = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF, System.getenv(Constants.ENV_CAMEL_K_CONF));
final String confd = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF_D, System.getenv(Constants.ENV_CAMEL_K_CONF_D));

return loadProperties(conf, confd);
}
Expand Down
134 changes: 134 additions & 0 deletions camel-k-runtime-examples/camel-k-runtime-example-servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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">
<parent>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-examples</artifactId>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-k-runtime-example-servlet</artifactId>

<dependencies>

<!-- ****************************** -->
<!-- -->
<!-- RUNTIME -->
<!-- -->
<!-- ****************************** -->

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-groovy</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.apache.camel.k.jvm.Application</mainClass>
<classpathScope>runtime</classpathScope>
<systemProperties>
<systemProperty>
<key>camel.k.conf</key>
<value>${project.basedir}/src/main/resources/application.properties</value>
</systemProperty>
<systemProperty>
<key>camel.k.routes</key>
<value>file:${project.basedir}/src/main/resources/routes.groovy</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>camel3</id>
<activation>
<property>
<name>camel3</name>
</property>
</activation>
<dependencies>
<!-- runtime -->
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-adapter-camel-3</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>camel2</id>
<activation>
<property>
<name>!camel3</name>
</property>
</activation>
<dependencies>
<!-- runtime -->
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-adapter-camel-2</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.example;

import org.apache.camel.CamelContext;
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.servlet.ServletRegistration;

public class WebhookCustomizer implements ContextCustomizer {
@Override
public void apply(CamelContext camelContext, Runtime.Registry registry) {
registry.bind(
"webhook-servlet",
new ServletRegistration("CamelServlet", new CamelHttpTransportServlet(), "/webhook/*")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

class=org.apache.camel.k.example.WebhookCustomizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Camel
#
camel.context.streamCaching = true

#
# Camel K
#
camel.k.customizer = webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


from('servlet:/test')
.convertBodyTo(String.class)
.to('log:info')
36 changes: 36 additions & 0 deletions camel-k-runtime-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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">
<parent>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-parent</artifactId>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<packaging>pom</packaging>
<artifactId>camel-k-runtime-examples</artifactId>


<modules>
<module>camel-k-runtime-example-servlet</module>
</modules>

</project>
Loading