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

Improve camel service discovery and filtering #340

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.quarkus.core.CamelConfig;
import org.apache.camel.quarkus.core.CamelMain;
import org.apache.camel.quarkus.core.CamelMainProducers;
import org.apache.camel.quarkus.core.CamelMainRecorder;
Expand All @@ -61,13 +60,24 @@ void beans(BuildProducer<AdditionalBeanBuildItem> beanProducer) {
beanProducer.produce(AdditionalBeanBuildItem.unremovableOf(CamelProducers.class));
}

/*
* Configure filters for core services.
*/
@BuildStep
void coreServiceFilter(BuildProducer<CamelServiceFilterBuildItem> filterBuildItems) {
filterBuildItems.produce(
new CamelServiceFilterBuildItem(si -> si.path.endsWith("META-INF/services/org/apache/camel/properties-component-factory"))
);
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelRegistryBuildItem registry(
CamelRecorder recorder,
RecorderContext recorderContext,
ApplicationArchivesBuildItem applicationArchives,
List<CamelBeanBuildItem> registryItems) {
List<CamelBeanBuildItem> registryItems,
List<CamelServiceFilterBuildItem> serviceFilters) {

RuntimeValue<Registry> registry = recorder.createRegistry();

Expand All @@ -79,10 +89,7 @@ CamelRegistryBuildItem registry(
// to the camel context directly by extension so it does not make sense to
// instantiate them in this phase.
//
boolean blacklisted = si.path.endsWith("reactive-executor")
|| si.path.endsWith("platform-http")
|| si.path.endsWith("properties-component-factory");

boolean blacklisted = serviceFilters.stream().anyMatch(filter -> filter.getPredicate().test(si));
if (blacklisted) {
LOGGER.debug("Ignore service: {}", si);
}
Expand Down Expand Up @@ -142,15 +149,13 @@ CamelContextBuildItem context(
CamelRegistryBuildItem registry,
CamelModelJAXBContextFactoryBuildItem contextFactory,
CamelXmlLoaderBuildItem xmlLoader,
BeanContainerBuildItem beanContainer,
CamelConfig.BuildTime buildTimeConfig) {
BeanContainerBuildItem beanContainer) {

RuntimeValue<CamelContext> context = recorder.createContext(
registry.getRegistry(),
contextFactory.getContextFactory(),
xmlLoader.getXmlLoader(),
beanContainer.getValue(),
buildTimeConfig);
beanContainer.getValue());

return new CamelContextBuildItem(context);
}
Expand Down
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.core.deployment;

import io.quarkus.builder.item.MultiBuildItem;
import org.apache.camel.quarkus.core.CamelServiceFilter;

public final class CamelServiceFilterBuildItem extends MultiBuildItem {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like us all to document BuildItems properly. The class level BuildItem JavaDoc should make the extension developers understand what is the purpose of the given BuildItem and when/how they should use it. If we provide no JavaDoc, the extension developers are doomed to click through our BuildProcessors and collect the information investing a lot more effort.

private final CamelServiceFilter predicate;

public CamelServiceFilterBuildItem(CamelServiceFilter predicate) {
this.predicate = predicate;
}

public CamelServiceFilter getPredicate() {
return predicate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.quarkus.core.CamelServiceInfo;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -86,23 +87,22 @@ public static Stream<String> getRouteBuilderClasses(IndexView view) {
.map(ClassInfo::toString);
}

public static Stream<ServiceInfo> services(ApplicationArchivesBuildItem applicationArchivesBuildItem) {
public static Stream<CamelServiceInfo> services(ApplicationArchivesBuildItem applicationArchivesBuildItem) {
return CamelSupport.resources(applicationArchivesBuildItem, CamelSupport.CAMEL_SERVICE_BASE_PATH)
.map(CamelSupport::services)
.flatMap(Collection::stream);
}

private static List<ServiceInfo> services(Path p) {
List<ServiceInfo> answer = new ArrayList<>();
private static List<CamelServiceInfo> services(Path p) {
List<CamelServiceInfo> answer = new ArrayList<>();

String name = p.getFileName().toString();
try (InputStream is = Files.newInputStream(p)) {
Properties props = new Properties();
props.load(is);
for (Map.Entry<Object, Object> entry : props.entrySet()) {
String k = entry.getKey().toString();
if (k.equals("class")) {
answer.add(new ServiceInfo(p, name, entry.getValue().toString()));
answer.add(new CamelServiceInfo(p, entry.getValue().toString()));
}
}
} catch (Exception e) {
Expand All @@ -112,28 +112,4 @@ private static List<ServiceInfo> services(Path p) {
return answer;
}

/**
* Utility class to describe a camel service which is a result of reading
* services from resources belonging to META-INF/services/org/apache/camel.
*/
public static class ServiceInfo {
public final Path path;
public final String name;
public final String type;

public ServiceInfo(Path path, String name, String type) {
this.path = path;
this.name = name;
this.type = type;
}

@Override
public String toString() {
return "ServiceInfo{"
+ "path='" + path.toString() + '\''
+ ", name=" + name
+ ", type=" + type
+ '}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.CamelContext;
import org.apache.camel.main.RoutesCollector;
import org.apache.camel.model.ValidateDefinition;
import org.apache.camel.model.validator.PredicateValidatorDefinition;
import org.apache.camel.reifier.ProcessorReifier;
Expand All @@ -39,8 +38,7 @@ public RuntimeValue<CamelContext> createContext(
RuntimeValue<Registry> registry,
RuntimeValue<ModelJAXBContextFactory> contextFactory,
RuntimeValue<XmlLoader> xmlLoader,
BeanContainer beanContainer,
CamelConfig.BuildTime buildTimeConfig) {
BeanContainer beanContainer) {
FastCamelContext context = new FastCamelContext();
context.setRegistry(registry.getValue());
context.setLoadTypeConverters(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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;

import java.util.function.Predicate;

@FunctionalInterface
public interface CamelServiceFilter extends Predicate<CamelServiceInfo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to see what value is CamelServiceFilter interface adding. I'd personally prefer using Predicate<CamelServiceInfo> directly because that way I'd immediately see that it is just a Predicate and nothing else.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

import java.nio.file.Path;

/**
* Utility class to describe a camel service which is a result of reading
* services from resources belonging to META-INF/services/org/apache/camel.
*/
public class CamelServiceInfo {
/**
* The path of the service file like META-INF/services/org/apache/camel/component/file.
*/
public final Path path;

/**
* The name of the service entry which is derived from the service path. As example the
* name for a service with path <code>META-INF/services/org/apache/camel/component/file</code>
* will be <code>file</code>
*/
public final String name;

/**
* The full qualified class name of the service.
*/
public final String type;

public CamelServiceInfo(Path path, String type) {
this(path, path.getFileName().toString(), type);
}

public CamelServiceInfo(Path path, String name, String type) {
this.path = path;
this.name = name;
this.type = type;
}

@Override
public String toString() {
return "ServiceInfo{"
+ "path='" + path.toString() + '\''
+ ", name=" + name
+ ", type=" + type
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
Expand All @@ -33,18 +34,29 @@
import org.apache.camel.quarkus.component.platform.http.runtime.PlatformHttpRecorder;
import org.apache.camel.quarkus.component.platform.http.runtime.QuarkusPlatformHttpEngine;
import org.apache.camel.quarkus.core.deployment.CamelRuntimeBeanBuildItem;
import org.apache.camel.quarkus.core.deployment.CamelServiceFilterBuildItem;
import org.apache.camel.quarkus.core.deployment.UploadAttacherBuildItem;


class PlatformHttpProcessor {

private static final String FEATURE = "camel-platform-http";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

/*
* The platform-http component is programmatically configured by the extension thus
* we can safely prevent camel-quarkus-core to instantiate a default instance.
*/
@BuildStep
void serviceFilter(BuildProducer<CamelServiceFilterBuildItem> filterBuildItems) {
filterBuildItems.produce(
new CamelServiceFilterBuildItem(si -> si.path.endsWith("META-INF/services/org/apache/camel/component/platform-http"))
);
}

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
PlatformHttpEngineBuildItem platformHttpEngine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,28 @@
*/
package org.apache.camel.quarkus.reactive.executor.deployment;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.vertx.deployment.VertxBuildItem;
import org.apache.camel.quarkus.core.Flags;
import org.apache.camel.quarkus.core.deployment.CamelReactiveExecutorBuildItem;
import org.apache.camel.quarkus.core.deployment.CamelServiceFilterBuildItem;
import org.apache.camel.quarkus.reactive.executor.ReactiveExecutorRecorder;

public class BuildProcessor {
/*
* The reactive executor is programmatically configured by the extension thus
* we can safely prevent camel-quarkus-core to instantiate a default instance.
*/
@BuildStep
void serviceFilter(BuildProducer<CamelServiceFilterBuildItem> filterBuildItems) {
filterBuildItems.produce(
new CamelServiceFilterBuildItem(si -> si.path.endsWith("META-INF/services/org/apache/camel/reactive-executor"))
);
}

@Record(value = ExecutionTime.RUNTIME_INIT, optional = true)
@BuildStep(onlyIf = Flags.MainEnabled.class)
CamelReactiveExecutorBuildItem reactiveExecutor(ReactiveExecutorRecorder recorder, VertxBuildItem vertx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#
quarkus.ssl.native=true
quarkus.log.file.enable = false
quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = DEBUG
quarkus.log.category."org.apache.camel.quarkus.component.platform.http".level = DEBUG
quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = INFO
quarkus.log.category."org.apache.camel.quarkus.component.platform.http".level = INFO
#
# Quarkus :: Camel
#
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,5 @@
quarkus.ssl.native=true
quarkus.http.body.uploads-directory=target/uploads
quarkus.log.file.enable = false
quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = DEBUG
quarkus.log.category."org.apache.camel.quarkus.component.platform.http".level = DEBUG

#
# Quarkus :: Camel
#
quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = INFO
quarkus.log.category."org.apache.camel.quarkus.component.platform.http".level = INFO