Skip to content

Commit

Permalink
Fix findings apache#9
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 4, 2019
1 parent 07e25cf commit 9787bbd
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ CamelRegistryBuildItem registry(
CamelContextBuildItem context(
CamelRecorder recorder,
CamelRegistryBuildItem registry,
// TODO: this add a dependency on Arc
BeanContainerBuildItem beanContainerBuildItem,
BeanContainerBuildItem beanContainer,
CamelConfig.BuildTime buildTimeConfig) {

RuntimeValue<CamelContext> context = recorder.createContext(registry.getRegistry(), beanContainerBuildItem.getValue(), buildTimeConfig);
RuntimeValue<CamelContext> context = recorder.createContext(registry.getRegistry(), beanContainer.getValue(), buildTimeConfig);
return new CamelContextBuildItem(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
package org.apache.camel.quarkus.core.deployment;


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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.spi.Registry;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ 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 String name;
public final Class<?> type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.apache.camel.ProducerTemplate;
import org.apache.camel.spi.Registry;

/**
* Producers of beans that are injectable via CDI.
*/
@Singleton
public class CamelProducers {
private CamelContext context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.camel.quarkus.core.runtime;

import java.util.function.Supplier;

import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
Expand All @@ -32,7 +30,6 @@

@Recorder
public class CamelRecorder {

public RuntimeValue<Registry> createRegistry() {
return new RuntimeValue<>(new RuntimeRegistry());
}
Expand All @@ -45,17 +42,15 @@ public RuntimeValue<CamelContext> createContext(RuntimeValue<Registry> registry,
context.getTypeConverterRegistry().setInjector(context.getInjector());

try {
// The creation of the JAXB context is very time consuming, so always prepare it
// when running in native mode, but lazy create it in java mode so that we don't
// waste time if using java routes
if (buildTimeConfig.disableJaxb) {
context.adapt(ExtendedCamelContext.class).setModelJAXBContextFactory(() -> {
throw new UnsupportedOperationException();
});
} else {
// The creation of the JAXB context is very time consuming, so always prepare it
// when running in native mode, but lazy create it in java mode so that we don't
// waste time if using java routes
if (ImageInfo.inImageBuildtimeCode()) {
context.adapt(ExtendedCamelContext.class).getModelJAXBContextFactory().newJAXBContext();
}
} else if (ImageInfo.inImageBuildtimeCode()) {
context.adapt(ExtendedCamelContext.class).getModelJAXBContextFactory().newJAXBContext();
}
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeCamelException(e);
Expand Down Expand Up @@ -92,22 +87,4 @@ public void bind(
throw new RuntimeException(e);
}
}

public Supplier<CamelConfig.BuildTime> buildTimeSupplier(CamelConfig.BuildTime config) {
return new Supplier<CamelConfig.BuildTime>() {
@Override
public CamelConfig.BuildTime get() {
return config;
}
};
}

public Supplier<CamelConfig.Runtime> runtimeSupplier(CamelConfig.Runtime config) {
return new Supplier<CamelConfig.Runtime>() {
@Override
public CamelConfig.Runtime get() {
return config;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;

/**
* Helper methods retrieve beans from the {@link Arc} container.
*/
@Vetoed
final class BeanManagerHelper {

Expand Down
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.core.runtime.support;

import java.util.Map;
import java.util.Set;

import org.apache.camel.spi.BeanRepository;

public final class RuntimeBeanRepository implements BeanRepository {
@Override
public Object lookupByName(String name) {
return lookupByNameAndType(name, Object.class);
}

@Override
public <T> T lookupByNameAndType(String name, Class<T> type) {
return BeanManagerHelper.getReferenceByName(name, type).orElse(null);
}

@Override
public <T> Map<String, T> findByTypeWithName(Class<T> type) {
return BeanManagerHelper.getReferencesByTypeWithName(type);
}

@Override
public <T> Set<T> findByType(Class<T> type) {
return BeanManagerHelper.getReferencesByType(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,18 @@
*/
package org.apache.camel.quarkus.core.runtime.support;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A {@link Map}-based registry.
*/
public class RuntimeRegistry extends HashMap<String, Map<Class<?>, Object>> implements Registry {

protected final Logger log = LoggerFactory.getLogger(getClass());

public void bind(String name, Object object) {
bind(name, object.getClass(), object);
}

public void bind(String name, Class<?> clazz, Object object) {
this.computeIfAbsent(name, k -> new HashMap<>()).put(clazz, object);
}

public Object lookupByName(String name) {
return lookupByNameAndType(name, Object.class);
}

public <T> T lookupByNameAndType(String name, Class<T> type) {
Optional<T> t = BeanManagerHelper.getReferenceByName(name, type);
if (t.isPresent()) {
return t.get();
}
Map<Class<?>, Object> map = this.get(name);
if (map == null) {
return null;
}
Object answer = map.get(type);
if (answer == null) {
for (Map.Entry<Class<?>, Object> entry : map.entrySet()) {
if (type.isAssignableFrom(entry.getKey())) {
answer = entry.getValue();
break;
}
}
}
if (answer instanceof RuntimeValue) {
log.debug("Creating {} for name {}", type.toString(), name);
answer = ((RuntimeValue) answer).getValue();
}
try {
return type.cast(answer);
} catch (Throwable e) {
String msg = "Found bean: " + name + " in SimpleRegistry: " + this
+ " of type: " + answer.getClass().getName() + " expected type was: " + type;
throw new NoSuchBeanException(name, msg, e);
}
}
import org.apache.camel.support.DefaultRegistry;

public <T> Map<String, T> findByTypeWithName(Class<T> type) {
Map<String, T> result = new HashMap<>();
for (Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
for (Object answer : entry.getValue().values()) {
if (answer instanceof RuntimeValue) {
answer = ((RuntimeValue) answer).getValue();
}
if (type.isInstance(answer)) {
result.put(entry.getKey(), type.cast(answer));
}
}
}
result.putAll(BeanManagerHelper.getReferencesByTypeWithName(type));
return result;
public class RuntimeRegistry extends DefaultRegistry {
public RuntimeRegistry() {
super(new RuntimeBeanRepository());
}

public <T> Set<T> findByType(Class<T> type) {
Set<T> result = new HashSet<>();
for (Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
for (Object answer : entry.getValue().values()) {
if (answer instanceof RuntimeValue) {
answer = ((RuntimeValue) answer).getValue();
}
if (type.isInstance(answer)) {
result.add(type.cast(answer));
}
}
}
result.addAll(BeanManagerHelper.getReferencesByType(type));
return result;
@Override
public Object unwrap(Object value) {
return (value instanceof RuntimeValue)
? ((RuntimeValue)value).getValue()
: value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.apache.camel.quarkus.main.CamelMain;
import org.apache.camel.quarkus.main.CamelMainProducers;
import org.apache.camel.quarkus.main.CamelMainRecorder;
import org.apache.camel.quarkus.main.deployment.support.CamelMainBuildItem;
import org.apache.camel.quarkus.main.deployment.support.CamelMainListenerBuildItem;

public class BuildProcessor {
@BuildStep
Expand All @@ -49,7 +47,6 @@ CamelMainBuildItem create(
CamelMainRecorder recorder,
CamelContextBuildItem context,
List<CamelMainListenerBuildItem> listeners,
// TODO: keep is as it adds a dependency on Arc
BeanContainerBuildItem beanContainerBuildItem) {

RuntimeValue<CamelMain> main = recorder.create(context.getCamelContext(), beanContainerBuildItem.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.main.deployment.support;
package org.apache.camel.quarkus.main.deployment;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.runtime.RuntimeValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.main.deployment.support;
package org.apache.camel.quarkus.main.deployment;

import io.quarkus.builder.item.MultiBuildItem;
import org.apache.camel.main.MainListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.apache.camel.CamelContext;
import org.apache.camel.main.MainSupport;

/**
* Bridges {@link MainSupport} events to CDI.
*/
public class CamelMainEventDispatcher implements org.apache.camel.main.MainListener {
@Override
public void beforeStart(MainSupport main) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import org.apache.camel.quarkus.main.deployment.support.CamelMainListenerBuildItem;
import org.apache.camel.quarkus.main.deployment.CamelMainListenerBuildItem;
import org.apache.camel.quarkus.main.support.SupportListener;

public class SupportBuildStep {
Expand Down

0 comments on commit 9787bbd

Please sign in to comment.