Skip to content

Commit

Permalink
CAMEL-17257: camel-core - RouteBuilder should store source location o…
Browse files Browse the repository at this point in the history
…f the file from the DSL loader
  • Loading branch information
davsclaus committed Dec 1, 2021
1 parent 216e074 commit 9b6cf15
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 69 deletions.
8 changes: 8 additions & 0 deletions core/camel-api/src/main/java/org/apache/camel/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.camel.spi.InterceptStrategy;
import org.apache.camel.spi.ManagementInterceptStrategy;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RouteController;
import org.apache.camel.spi.RouteError;
import org.apache.camel.spi.RoutePolicy;
Expand Down Expand Up @@ -133,6 +134,13 @@ public interface Route extends RuntimeConfiguration {
*/
String getConfigurationId();

/**
* Gets the source resource that this route is located from
*
* @return the source, or null if this route is not loaded from a resource
*/
Resource getSourceResource();

/**
* Gets the camel context
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public interface RouteFactory {
* @param routeId the route id
* @param routeDescription the route description
* @param endpoint the input endpoint (consumer)
* @param resource the source resource (if loaded via a DSL routes loader)
* @return the created route
*/
Route createRoute(
CamelContext camelContext, NamedNode routeDefinition,
String routeId, String routeDescription, Endpoint endpoint);
String routeId, String routeDescription, Endpoint endpoint,
Resource resource);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3068,8 +3068,13 @@ protected void logStartSummary() {
// use basic endpoint uri to not log verbose details or potential sensitive data
String uri = order.getRoute().getEndpoint().getEndpointBaseUri();
uri = URISupport.sanitizeUri(uri);
lines.add(String.format(" %s %s (%s)", status, id, uri));

String loc = order.getRoute().getSourceResource() != null
? order.getRoute().getSourceResource().getLocation() : null;
if (startupSummaryLevel == StartupSummaryLevel.Verbose && loc != null) {
lines.add(String.format(" %s %s (%s) (source: %s)", status, id, uri, loc));
} else {
lines.add(String.format(" %s %s (%s)", status, id, uri));
}
String cid = order.getRoute().getConfigurationId();
if (cid != null) {
configs.add(String.format(" %s (%s)", id, cid));
Expand All @@ -3087,7 +3092,12 @@ protected void logStartSummary() {
// use basic endpoint uri to not log verbose details or potential sensitive data
String uri = route.getEndpoint().getEndpointBaseUri();
uri = URISupport.sanitizeUri(uri);
lines.add(String.format(" %s %s (%s)", status, id, uri));
String loc = route.getSourceResource() != null ? route.getSourceResource().getLocation() : null;
if (startupSummaryLevel == StartupSummaryLevel.Verbose && loc != null) {
lines.add(String.format(" %s %s (%s) (source: %s)", status, id, uri, loc));
} else {
lines.add(String.format(" %s %s (%s)", status, id, uri));
}

String cid = route.getConfigurationId();
if (cid != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.camel.spi.IdAware;
import org.apache.camel.spi.InterceptStrategy;
import org.apache.camel.spi.ManagementInterceptStrategy;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RouteController;
import org.apache.camel.spi.RouteError;
import org.apache.camel.spi.RouteIdAware;
Expand All @@ -66,6 +67,7 @@ public class DefaultRoute extends ServiceSupport implements Route {
private NamedNode route;
private final String routeId;
private final String routeDescription;
private final Resource sourceResource;
private final List<Processor> eventDrivenProcessors = new ArrayList<>();
private final List<InterceptStrategy> interceptStrategies = new ArrayList<>(0);
private ManagementInterceptStrategy managementInterceptStrategy;
Expand Down Expand Up @@ -100,12 +102,13 @@ public class DefaultRoute extends ServiceSupport implements Route {
private Consumer consumer;

public DefaultRoute(CamelContext camelContext, NamedNode route, String routeId,
String routeDescription, Endpoint endpoint) {
String routeDescription, Endpoint endpoint, Resource resource) {
this.camelContext = camelContext;
this.route = route;
this.routeId = routeId;
this.routeDescription = routeDescription;
this.endpoint = endpoint;
this.sourceResource = resource;
}

@Override
Expand Down Expand Up @@ -167,6 +170,11 @@ public String getConfigurationId() {
return value != null ? (String) value : null;
}

@Override
public Resource getSourceResource() {
return sourceResource;
}

@Override
public void initializeServices() throws Exception {
// gather all the services for this route
Expand Down Expand Up @@ -631,7 +639,7 @@ protected void gatherRootServices(List<Service> services) throws Exception {
public Navigate<Processor> navigate() {
Processor answer = getProcessor();

// we want navigating routes to be easy, so skip the initial channel
// we want to navigate routes to be easy, so skip the initial channel
// and navigate to its output where it all starts from end user point of view
if (answer instanceof Navigate) {
Navigate<Processor> nav = (Navigate<Processor>) answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.camel.Endpoint;
import org.apache.camel.NamedNode;
import org.apache.camel.Route;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RouteFactory;

/**
Expand All @@ -29,7 +30,8 @@ public class DefaultRouteFactory implements RouteFactory {

@Override
public Route createRoute(
CamelContext camelContext, NamedNode routeDefinition, String routeId, String routeDescription, Endpoint endpoint) {
return new DefaultRoute(camelContext, routeDefinition, routeId, routeDescription, endpoint);
CamelContext camelContext, NamedNode routeDefinition, String routeId, String routeDescription,
Endpoint endpoint, Resource resource) {
return new DefaultRoute(camelContext, routeDefinition, routeId, routeDescription, endpoint, resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
public abstract class RouteBuilder extends BuilderSupport implements RoutesBuilder, Ordered {
protected Logger log = LoggerFactory.getLogger(getClass());

private Resource resource;
private final AtomicBoolean initialized = new AtomicBoolean();
private final List<RouteBuilderLifecycleStrategy> lifecycleInterceptors = new ArrayList<>();
private final List<TransformerBuilder> transformerBuilders = new ArrayList<>();
Expand All @@ -83,6 +84,20 @@ public RouteBuilder(CamelContext context) {
super(context);
}

/**
* The {@link Resource} which is the source code for this route (such as XML, YAML, Groovy or Java source file)
*/
public Resource getResource() {
return resource;
}

/**
* Sets the {@link Resource} which is the source code for this route (such as XML, YAML, Groovy or Java source file)
*/
public void setResource(Resource resource) {
this.resource = resource;
}

/**
* Add routes to a context using a lambda expression. It can be used as following:
*
Expand Down Expand Up @@ -591,6 +606,9 @@ protected void checkInitialized() throws Exception {

configure();

// remember the source resource
getRouteCollection().setResource(getResource());

for (RouteDefinition route : getRouteCollection().getRoutes()) {
// ensure the route is prepared after configure method is complete
getRouteCollection().prepareRoute(route);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.spi.AsEndpointUri;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutePolicy;

/**
Expand Down Expand Up @@ -88,6 +89,7 @@ public class RouteDefinition extends OutputDefinition<RouteDefinition> implement
private List<PropertyDefinition> routeProperties;
private Map<String, Object> templateParameters;
private RouteTemplateContext routeTemplateContext;
private Resource resource;

public RouteDefinition() {
}
Expand Down Expand Up @@ -702,6 +704,15 @@ public void setRouteTemplateContext(RouteTemplateContext routeTemplateContext) {
this.routeTemplateContext = routeTemplateContext;
}

public Resource getResource() {
return resource;
}

@XmlTransient
public void setResource(Resource resource) {
this.resource = resource;
}

// Properties
// -----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.camel.builder.EndpointConsumerBuilder;
import org.apache.camel.spi.AsEndpointUri;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.OrderedComparator;
import org.apache.camel.support.PatternHelper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -64,6 +65,8 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit
private CamelContext camelContext;
@XmlTransient
private ErrorHandlerFactory errorHandlerFactory;
@XmlTransient
private Resource resource;

public RoutesDefinition() {
}
Expand Down Expand Up @@ -151,6 +154,14 @@ public void setErrorHandlerFactory(ErrorHandlerFactory errorHandlerFactory) {
this.errorHandlerFactory = errorHandlerFactory;
}

public Resource getResource() {
return resource;
}

public void setResource(Resource resource) {
this.resource = resource;
}

// Fluent API
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -222,6 +233,9 @@ public void prepareRoute(RouteDefinition route) {
// reset before preparing route
route.resetPrepare();

// remember the source resource
route.setResource(resource);

// merge global and route scoped together
List<OnExceptionDefinition> oe = new ArrayList<>(onExceptions);
List<InterceptDefinition> icp = new ArrayList<>(intercepts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected Route doCreateRoute() throws Exception {
String id = definition.idOrCreate(camelContext.adapt(ExtendedCamelContext.class).getNodeIdFactory());
String desc = definition.getDescriptionText();
Route route = camelContext.adapt(ExtendedCamelContext.class).getRouteFactory().createRoute(camelContext, definition, id,
desc, endpoint);
desc, endpoint, definition.getResource());

// configure error handler
route.setErrorHandlerFactory(definition.getErrorHandlerFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class DefaultExceptionPolicyStrategyTest {

private ExceptionPolicy exceptionPolicy(Class<? extends Throwable> exceptionClass) {
CamelContext cc = new DefaultCamelContext();
Route context = new DefaultRoute(cc, null, null, null, null);
Route context = new DefaultRoute(cc, null, null, null, null, null);
return new DefaultErrorHandlerReifier<>(context, null)
.createExceptionPolicy(new OnExceptionDefinition(exceptionClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class ProcessorReifierTest {
@Test
public void testHandleCustomProcessorDefinition() {
Route ctx = new DefaultRoute(null, null, null, null, null);
Route ctx = new DefaultRoute(null, null, null, null, null, null);
ProcessorReifier.registerReifier(MyProcessorDefinition.class, ProcessReifier::new);
ProcessReifier ref = (ProcessReifier) ProcessorReifier.reifier(ctx, new MyProcessorDefinition());
Assertions.assertTrue(ref.definition instanceof MyProcessorDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static EndpointRouteBuilder loadEndpointRoutesBuilder(
@Override
public void configure() throws Exception {
CamelContextAware.trySetCamelContext(resource, getContext());
setResource(resource);

try (Reader reader = resource.getReader()) {
consumer.accept(reader, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public interface ManagedRouteMBean extends ManagedPerformanceCounterMBean {
@ManagedAttribute(description = "Route Description")
String getDescription();

@ManagedAttribute(description = "Route Source Location")
String getSourceLocation();

@ManagedAttribute(description = "Route Configuration ID")
String getRouteConfigurationId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class ManagedRoute extends ManagedPerformanceCounter implements TimerList
protected final Route route;
protected final String description;
protected final String configurationId;
protected final String sourceLocation;
protected final CamelContext context;
private final LoadTriplet load = new LoadTriplet();
private final String jmxDomain;
Expand All @@ -81,6 +82,7 @@ public ManagedRoute(CamelContext context, Route route) {
this.context = context;
this.description = route.getDescription();
this.configurationId = route.getConfigurationId();
this.sourceLocation = route.getSourceResource() != null ? route.getSourceResource().getLocation() : null;
this.jmxDomain = context.getManagementStrategy().getManagementAgent().getMBeanObjectDomainName();
}

Expand Down Expand Up @@ -144,6 +146,11 @@ public String getDescription() {
return description;
}

@Override
public String getSourceLocation() {
return sourceLocation;
}

@Override
public String getRouteConfigurationId() {
return configurationId;
Expand Down
Loading

0 comments on commit 9b6cf15

Please sign in to comment.