Skip to content

Commit

Permalink
Ensure the PlatformHttpComponent is registered before the routes are …
Browse files Browse the repository at this point in the history
…started #218
  • Loading branch information
lburgazzoli committed Oct 6, 2019
1 parent 8100e8c commit 4d619d1
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 51 deletions.
Expand Up @@ -102,6 +102,28 @@ CamelContextBuildItem context(
RuntimeValue<CamelContext> context = recorder.createContext(registry.getRegistry(), beanContainer.getValue(), buildTimeConfig);
return new CamelContextBuildItem(context);
}

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
CamelRuntimeRegistryBuildItem bindRuntimeBeransToRegistry(
CamelRecorder recorder,
CamelRegistryBuildItem registry,
List<CamelRuntimeBeanBuildItem> registryItems) {


for (CamelRuntimeBeanBuildItem item : registryItems) {
LOGGER.debug("Binding runtime bean with name: {}, type {}", item.getName(), item.getType());

recorder.bind(
registry.getRegistry(),
item.getName(),
item.getType(),
item.getValue()
);
}

return new CamelRuntimeRegistryBuildItem(registry.getRegistry());
}
}

/*
Expand Down Expand Up @@ -151,6 +173,9 @@ CamelMainBuildItem main(
void start(
CamelMainRecorder recorder,
CamelMainBuildItem main,
// TODO: keep this as placeholder to ensure the registry is fully configured
// befire startuing the camel context
CamelRuntimeRegistryBuildItem registry,
ShutdownContextBuildItem shutdown,
// TODO: keep this list as placeholder to ensure the ArC container is fully
// started before starting main
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.RuntimeValue;

/**
* A {@link MultiBuildItem} holding beans to add to {@link org.apache.camel.spi.Registry} during
Expand All @@ -28,15 +29,9 @@
public final class CamelBeanBuildItem extends MultiBuildItem {
private final String name;
private final Class<?> type;
private final Object value;
private final RuntimeValue<?> value;

public CamelBeanBuildItem(String name, Object value) {
this.name = Objects.requireNonNull(name);
this.value = Objects.requireNonNull(value);
this.type = Objects.requireNonNull(value).getClass();
}

public CamelBeanBuildItem(String name, Class<?> type, Object value) {
public CamelBeanBuildItem(String name, Class<?> type, RuntimeValue<?> value) {
this.name = Objects.requireNonNull(name);
this.type = Objects.requireNonNull(type);
this.value = Objects.requireNonNull(value);
Expand All @@ -50,7 +45,7 @@ public Class<?> getType() {
return type;
}

public Object getValue() {
public RuntimeValue<?> getValue() {
return value;
}
}
@@ -0,0 +1,51 @@
/*
* 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.core.deployment;

import java.util.Objects;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.RuntimeValue;

/**
* A {@link MultiBuildItem} holding beans to add to {@link org.apache.camel.spi.Registry} during
* runtime initialization phase. Note that the field type should refer to the most specialized
* class to avoid the issue described in https://issues.apache.org/jira/browse/CAMEL-13948.
*/
public final class CamelRuntimeBeanBuildItem extends MultiBuildItem {
private final String name;
private final Class<?> type;
private final RuntimeValue<?> value;

public CamelRuntimeBeanBuildItem(String name, Class<?> type, RuntimeValue<?> value) {
this.name = Objects.requireNonNull(name);
this.type = Objects.requireNonNull(type);
this.value = Objects.requireNonNull(value);
}

public String getName() {
return name;
}

public Class<?> getType() {
return type;
}

public RuntimeValue<?> getValue() {
return value;
}
}
@@ -0,0 +1,36 @@
/*
* 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.core.deployment;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.spi.Registry;

/**
* Holds the {@link Registry} {@link RuntimeValue}.
*/
public final class CamelRuntimeRegistryBuildItem extends SimpleBuildItem {
private final RuntimeValue<Registry> value;

public CamelRuntimeRegistryBuildItem(RuntimeValue<Registry> value) {
this.value = value;
}

public RuntimeValue<Registry> getRegistry() {
return value;
}
}
Expand Up @@ -73,6 +73,15 @@ public void bind(
runtime.getValue().bind(name, type, instance);
}

public void bind(
RuntimeValue<Registry> runtime,
String name,
Class<?> type,
RuntimeValue<?> instance) {

runtime.getValue().bind(name, type, instance.getValue());
}

public void bind(
RuntimeValue<Registry> runtime,
String name,
Expand Down
Expand Up @@ -21,12 +21,11 @@
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;

import org.apache.camel.component.microprofile.metrics.MicroProfileMetricsConstants;
import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsConfig;
import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsRecorder;
import org.apache.camel.quarkus.core.deployment.CamelBeanBuildItem;
import org.eclipse.microprofile.metrics.MetricRegistry;
import static org.apache.camel.component.microprofile.metrics.MicroProfileMetricsConstants.METRIC_REGISTRY_NAME;

class MicroProfileMetricsProcessor {

Expand All @@ -41,7 +40,7 @@ FeatureBuildItem feature() {
@BuildStep
CamelBeanBuildItem metricRegistry(CamelMicroProfileMetricsRecorder recorder) {
return new CamelBeanBuildItem(
METRIC_REGISTRY_NAME,
MicroProfileMetricsConstants.METRIC_REGISTRY_NAME,
MetricRegistry.class,
recorder.createApplicationRegistry()
);
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.quarkus.component.microprofile.metrics.runtime;

import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import io.smallrye.metrics.MetricRegistries;

Expand All @@ -31,8 +32,8 @@
@Recorder
public class CamelMicroProfileMetricsRecorder {

public MetricRegistry createApplicationRegistry() {
return MetricRegistries.get(MetricRegistry.Type.APPLICATION);
public RuntimeValue<MetricRegistry> createApplicationRegistry() {
return new RuntimeValue(MetricRegistries.get(MetricRegistry.Type.APPLICATION));
}

public void configureCamelContext(CamelMicroProfileMetricsConfig config, BeanContainer beanContainer) {
Expand Down
Expand Up @@ -20,6 +20,8 @@

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;

Expand All @@ -28,6 +30,8 @@
*/
@Component("platform-http")
public class PlatformHttpComponent extends DefaultComponent {
@Metadata(label = "advanced")
private PlatformHttpEngine engine;

public PlatformHttpComponent() {
super();
Expand All @@ -39,7 +43,21 @@ public PlatformHttpComponent(CamelContext context) {

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
return new PlatformHttpEndpoint(uri, remaining, this);
PlatformHttpEndpoint endpoint = new PlatformHttpEndpoint(uri, remaining, this);
endpoint.setPlatformHttpEngine(engine);

return endpoint;
}

public PlatformHttpEngine getEngine() {
return engine;
}

/**
* Sets the {@link PlatformHttpEngine} to use.
*/
public PlatformHttpComponent setEngine(PlatformHttpEngine engine) {
this.engine = engine;
return this;
}
}
Expand Up @@ -19,7 +19,7 @@
public final class PlatformHttpConstants {

public static final String PLATFORM_HTTP_COMPONENT_NAME = "platform-http";
public static final String PLATFORM_HTTP_ENGINE_NAME = "platformHttpEngine";
public static final String PLATFORM_HTTP_ENGINE_NAME = "platform-http-engine";

private PlatformHttpConstants() {
}
Expand Down
Expand Up @@ -24,13 +24,20 @@
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@UriEndpoint(/* firstVersion = "3.?.0", */ scheme = "platform-http", title = "Platform HTTP", syntax = "platform-http:[methods:]path", label = "http")
@UriEndpoint(/* firstVersion = "3.?.0", */ scheme = "platform-http", title = "Platform HTTP", syntax = "platform-http:path", label = "http", consumerOnly = true)
public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoint, HeaderFilterStrategyAware {
private static final Logger LOGGER = LoggerFactory.getLogger(PlatformHttpEndpoint.class);

@UriPath
@Metadata(required = true)
private final String path;

@UriParam(label = "consumer", description = "A comma separated list of HTTP methods to serve, e.g. GET,POST ."
Expand Down Expand Up @@ -91,16 +98,17 @@ public void setHttpMethodRestrict(String httpMethodRestrict) {
@Override
protected void doStart() throws Exception {
super.doStart();

if (platformHttpEngine == null) {
LOGGER.debug("Lookup platform http engine from registry");

platformHttpEngine = getCamelContext().getRegistry()
.lookupByNameAndType(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_NAME, PlatformHttpEngine.class);

if (platformHttpEngine == null) {
throw new IllegalStateException(PlatformHttpEngine.class.getSimpleName() + " neither set on this "
+ PlatformHttpEndpoint.class.getSimpleName() + " neither found in Camel Registry.");
}
}
}



}
@@ -0,0 +1,36 @@
/*
* 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.platform.http.deployment;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;

/**
* Holds the {@link PlatformHttpEngine} {@link RuntimeValue}.
*/
public final class PlatformHttpEngineBuildItem extends SimpleBuildItem {
private final RuntimeValue<PlatformHttpEngine> instance;

public PlatformHttpEngineBuildItem(RuntimeValue<PlatformHttpEngine> instance) {
this.instance = instance;
}

public RuntimeValue<PlatformHttpEngine> getInstance() {
return instance;
}
}
Expand Up @@ -21,8 +21,11 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.vertx.http.deployment.VertxWebRouterBuildItem;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.PlatformHttpConstants;
import org.apache.camel.quarkus.component.platform.http.runtime.PlatformHttpRecorder;
import org.apache.camel.quarkus.core.deployment.CamelContextBuildItem;
import org.apache.camel.quarkus.component.platform.http.runtime.QuarkusPlatformHttpEngine;
import org.apache.camel.quarkus.core.deployment.CamelRuntimeBeanBuildItem;

class PlatformHttpProcessor {

Expand All @@ -35,8 +38,30 @@ FeatureBuildItem feature() {

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void platformHttpComponent(PlatformHttpRecorder recorder, CamelContextBuildItem context, VertxWebRouterBuildItem router) {
recorder.registerPlatformHttpComponent(context.getCamelContext(), router.getRouter());
PlatformHttpEngineBuildItem platformHttpEngine(PlatformHttpRecorder recorder, VertxWebRouterBuildItem router) {
return new PlatformHttpEngineBuildItem(
recorder.engine(router.getRouter())
);
}

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
CamelRuntimeBeanBuildItem platformHttpEngineBean(PlatformHttpRecorder recorder, PlatformHttpEngineBuildItem engine) {
return new CamelRuntimeBeanBuildItem(
PlatformHttpConstants.PLATFORM_HTTP_ENGINE_NAME,
QuarkusPlatformHttpEngine.class,
engine.getInstance()
);
}


@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
CamelRuntimeBeanBuildItem platformHttpComponentBean(PlatformHttpRecorder recorder, PlatformHttpEngineBuildItem engine) {
return new CamelRuntimeBeanBuildItem(
PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME,
PlatformHttpComponent.class,
recorder.component(engine.getInstance())
);
}
}

0 comments on commit 4d619d1

Please sign in to comment.