Skip to content

Commit

Permalink
Add cli-connector support for Camel JBang runtime=quarkus
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed May 1, 2024
1 parent a74a7da commit 6adfc3a
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/cli-connector.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ Please refer to the above link for usage and configuration details.
ifeval::[{doc-show-user-guide-link} == true]
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
endif::[]

[id="extensions-cli-connector-additional-camel-quarkus-configuration"]
== Additional Camel Quarkus configuration

[width="100%",cols="80,5,15",options="header"]
|===
| Configuration property | Type | Default


|icon:lock[title=Fixed at build time] [[quarkus.camel.cli.enabled]]`link:#quarkus.camel.cli.enabled[quarkus.camel.cli.enabled]`

Sets whether to enable Camel CLI Connector support.
| `boolean`
| `true`
|===

[.configuration-legend]
{doc-link-icon-lock}[title=Fixed at build time] Configuration property fixed at build time. All other configuration properties are overridable at runtime.

6 changes: 6 additions & 0 deletions extensions-jvm/cli-connector/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-cli-connector</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
*/
package org.apache.camel.quarkus.component.cli.connector.deployment;

import java.util.function.BooleanSupplier;

import io.quarkus.builder.Version;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import org.apache.camel.quarkus.component.cli.connector.CamelCliConnectorConfig;
import org.apache.camel.quarkus.component.cli.connector.CamelCliConnectorRecorder;
import org.apache.camel.quarkus.core.JvmOnlyRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem;
import org.apache.camel.spi.CliConnectorFactory;
import org.jboss.logging.Logger;

@BuildSteps(onlyIf = CliConnectorProcessor.CliConnectorEnabled.class)
class CliConnectorProcessor {

private static final Logger LOG = Logger.getLogger(CliConnectorProcessor.class);
Expand All @@ -34,6 +43,14 @@ FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
@Record(value = ExecutionTime.STATIC_INIT)
CamelBeanBuildItem camelBeanBuildItem(CamelCliConnectorRecorder recorder) {
return new CamelBeanBuildItem("quarkusCliConnectorFactory",
CliConnectorFactory.class.getName(),
recorder.createCliConnectorFactory(Version.getVersion()));
}

/**
* Remove this once this extension starts supporting the native mode.
*/
Expand All @@ -43,4 +60,12 @@ void warnJvmInNative(JvmOnlyRecorder recorder) {
JvmOnlyRecorder.warnJvmInNative(LOG, FEATURE); // warn at build time
recorder.warnJvmInNative(FEATURE); // warn at runtime
}

static class CliConnectorEnabled implements BooleanSupplier {
CamelCliConnectorConfig config;

public boolean getAsBoolean() {
return config.enabled;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.quarkus.component.cli.connector.deployment;

import io.quarkus.test.QuarkusUnitTest;
import jakarta.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.support.CamelContextHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.api.Assertions.assertNull;

public class CliConnectorDisabledTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest()
.overrideConfigKey("quarkus.camel.cli.enabled", "false")
.withEmptyApplication();

@Inject
CamelContext context;

@Test
void cliConnectorDisabled() {
CliConnectorFactory factory = CamelContextHelper.findSingleByType(context, CliConnectorFactory.class);
assertNull(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.quarkus.component.cli.connector.deployment;

import io.quarkus.test.QuarkusUnitTest;
import jakarta.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.quarkus.component.cli.connector.QuarkusCliConnectorFactory;
import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.support.CamelContextHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class CliConnectorEnabledTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest().withEmptyApplication();

@Inject
CamelContext context;

@Test
void cliConnectorEnabled() {
CliConnectorFactory factory = CamelContextHelper.findSingleByType(context, CliConnectorFactory.class);
assertNotNull(factory);
assertInstanceOf(QuarkusCliConnectorFactory.class, factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.quarkus.component.cli.connector;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.cli", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class CamelCliConnectorConfig {
/**
* Sets whether to enable Camel CLI Connector support.
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.quarkus.component.cli.connector;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.spi.CliConnectorFactory;

@Recorder
public class CamelCliConnectorRecorder {
public RuntimeValue<CliConnectorFactory> createCliConnectorFactory(String version) {
QuarkusCliConnectorFactory factory = new QuarkusCliConnectorFactory();
factory.setEnabled(true);
factory.setRuntime("Quarkus");
factory.setRuntimeVersion(version);
return new RuntimeValue<>(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.quarkus.component.cli.connector;

import org.apache.camel.cli.connector.DefaultCliConnectorFactory;
import org.apache.camel.spi.CliConnector;

public class QuarkusCliConnectorFactory extends DefaultCliConnectorFactory {
@Override
public CliConnector createConnector() {
return new QuarkusLocalCliConnector(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.quarkus.component.cli.connector;

import io.quarkus.runtime.LaunchMode;
import org.apache.camel.cli.connector.LocalCliConnector;
import org.apache.camel.spi.CliConnectorFactory;

public class QuarkusLocalCliConnector extends LocalCliConnector {
public QuarkusLocalCliConnector(CliConnectorFactory cliConnectorFactory) {
super(cliConnectorFactory);
}

@Override
public void sigterm() {
if (LaunchMode.current().equals(LaunchMode.DEVELOPMENT)) {
// If Camel JBang launched us in dev mode, then stopping the CamelContext is not enough as dev mode will remain running.
// Therefore, init app shutdown which will still shut Camel down gracefully
System.exit(0);
} else {
super.sigterm();
}
}
}

0 comments on commit 6adfc3a

Please sign in to comment.