Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Move to inject the CamelContext (Islandora#1)
Browse files Browse the repository at this point in the history
* Change up instantiation/initialization slightly.

* PostConstruct is allowed to apply to private methods...

... seems like a false-positive in PMD? Got example exception from:
https://stackoverflow.com/a/48679770

* Changing to camel's 3.7.6 LTS.
  • Loading branch information
adam-vessey authored Dec 6, 2021
1 parent abe052a commit 309089b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 49 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ subprojects {
versions = [
activemq: '5.16.0',
asmCommons: '5.0.3',
camel: '3.5.0',
camel: '3.7.6',
commonsIo: '2.8.0',
fcrepoCamel: '5.0.0',
fcrepoCamelToolbox: '5.0.0',
Expand Down
7 changes: 6 additions & 1 deletion gradle/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
<rule ref="category/java/bestpractices.xml/UnusedImports" />
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']" />
</properties>
</rule>
<rule ref="category/java/bestpractices.xml/UseAssertEqualsInsteadOfAssertTrue" />
<rule ref="category/java/bestpractices.xml/UseAssertNullInsteadOfAssertTrue" />
<rule ref="category/java/bestpractices.xml/UseAssertSameInsteadOfAssertTrue" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.apache.camel.CamelContext;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

Expand All @@ -41,7 +39,7 @@
* @author whikloj
*/
@Configuration
public class DerivativeOptions extends PropertyConfig implements ApplicationContextAware {
public class DerivativeOptions extends PropertyConfig {

private static final Logger LOGGER = getLogger(DerivativeOptions.class);

Expand All @@ -53,11 +51,12 @@ public class DerivativeOptions extends PropertyConfig implements ApplicationCont
private static final String DERIVATIVE_CONCURRENT_PROPERTY = "concurrent-consumers";
private static final String DERIVATIVE_MAX_CONCURRENT_PROPERTY = "max-concurrent-consumers";

private static final int MINIMUM_NUMBER_OF_CAMEL_CONTEXTS = 1;

@Autowired
private Environment environment;

@Autowired
private CamelContext camelContext;

@Value("${" + DERIVATIVE_LIST_PROPERTY + ":#{null}}")
private String derivativeSystems;

Expand All @@ -69,7 +68,8 @@ public class DerivativeOptions extends PropertyConfig implements ApplicationCont
* @throws Exception
* When unable to add routes to the camel context.
*/
private void processAllServices(final CamelContext camelContext) throws Exception {
@PostConstruct
private void processAllServices() throws Exception {
if (derivativeSystems != null && !derivativeSystems.isBlank()) {
final var systemNames = derivativeSystems.contains(",") ?
Arrays.stream(derivativeSystems.split(",")).filter(o -> !o.isBlank()).map(String::trim)
Expand All @@ -78,7 +78,7 @@ private void processAllServices(final CamelContext camelContext) throws Exceptio
final var enabled =
environment.getProperty(enabledProperty(system), Boolean.class, false);
if (enabled) {
startDerivativeService(camelContext, system);
startDerivativeService(system);
} else {
LOGGER.debug("Derivative connector (" + system + ") is disabled, skipping");
}
Expand All @@ -96,7 +96,7 @@ private void processAllServices(final CamelContext camelContext) throws Exceptio
* @throws Exception
* When unable to add routes to the camel context.
*/
private void startDerivativeService(final CamelContext camelContext, final String serviceName) throws Exception {
private void startDerivativeService(final String serviceName) throws Exception {
final var input = environment.getProperty(inputProperty(serviceName), "");
final var output = environment.getProperty(outputProperty(serviceName), "");
if (!input.isBlank() && !output.isBlank()) {
Expand Down Expand Up @@ -180,22 +180,4 @@ private String maxConcurrentConsumerProperty(final String systemName) {
return DERIVATIVE_PREFIX + "." + systemName + "." + DERIVATIVE_MAX_CONCURRENT_PROPERTY;
}

/**
* camelContext.addRoutes throws Exception, which PMD does not like you catching. So we ignore that rule.
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
final String[] names = applicationContext.getBeanNamesForType(CamelContext.class);
if (names.length >= MINIMUM_NUMBER_OF_CAMEL_CONTEXTS) {
final CamelContext camelContext = applicationContext.getBean(names[0], CamelContext.class);
try {
processAllServices(camelContext);
} catch (final Exception e) {
throw new BeanInitializationException("Failed to add derivative route to Camel Context", e);
}
} else {
LOGGER.error("No CamelContext found, unable to start derivative routes.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void testDerivativeConnector() throws Exception {
final String route = "IslandoraConnectorDerivative-testRoutes";

final var context = camelContext.adapt(ModelCamelContext.class);
AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");

// Rig Drupal REST endpoint to return canned jsonld
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ToDynamicDefinition;
Expand Down Expand Up @@ -70,10 +70,10 @@ public void testNode() throws Exception {
final String nodeSubRoute = "FcrepoIndexerNodeIndex";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a ->
AdviceWith.adviceWith(context, route, a ->
a.replaceFromWith("direct:start")
);
AdviceWithRouteBuilder.adviceWith(context, nodeSubRoute, a ->
AdviceWith.adviceWith(context, nodeSubRoute, a ->
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000" +
"/milliner/node/${exchangeProperty.uuid}")
);
Expand Down Expand Up @@ -107,10 +107,10 @@ public void testNodeVersion() throws Exception {
final String versionSubRoute = "FcrepoIndexerNodeVersion";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a ->
AdviceWith.adviceWith(context, route, a ->
a.replaceFromWith("direct:start")
);
AdviceWithRouteBuilder.adviceWith(context, versionSubRoute, a ->
AdviceWith.adviceWith(context, versionSubRoute, a ->
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000" +
"/milliner/node/${exchangeProperty.uuid}/version")
);
Expand Down Expand Up @@ -141,7 +141,7 @@ public void testNodeDelete() throws Exception {
final String route = "FcrepoIndexerDeleteNode";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000/milliner/node" +
"/${exchangeProperty.uuid}");
Expand Down Expand Up @@ -174,7 +174,7 @@ public void testExternalFile() throws Exception {
final String route = "FcrepoIndexerExternalFile";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000/milliner/" +
"external/${exchangeProperty.uuid}");
Expand Down Expand Up @@ -212,10 +212,10 @@ public void testMedia() throws Exception {
final String mediaSubRoute = "FcrepoIndexerMediaIndex";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a ->
AdviceWith.adviceWith(context, route, a ->
a.replaceFromWith("direct:start")
);
AdviceWithRouteBuilder.adviceWith(context, mediaSubRoute, a->
AdviceWith.adviceWith(context, mediaSubRoute, a->
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000/milliner/" +
"media/${exchangeProperty.sourceField}")
);
Expand Down Expand Up @@ -250,8 +250,8 @@ public void testVersionMedia() throws Exception {
final String versionSubRoute = "FcrepoIndexerMediaIndexVersion";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a -> a.replaceFromWith("direct:start"));
AdviceWithRouteBuilder.adviceWith(context, versionSubRoute, a ->
AdviceWith.adviceWith(context, route, a -> a.replaceFromWith("direct:start"));
AdviceWith.adviceWith(context, versionSubRoute, a ->
a.weaveByType(ToDynamicDefinition.class).selectIndex(0).replace().toD("mock:localhost:8000/milliner/" +
"media/${exchangeProperty.sourceField}/version")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
Expand Down Expand Up @@ -80,7 +80,7 @@ public boolean isUseRouteBuilder() {
public void testParseUrl() throws Exception {
final String route = "IslandoraTriplestoreIndexerParseUrl";

AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.weaveAddLast().to(resultEndpoint);
});
Expand Down Expand Up @@ -110,7 +110,7 @@ public void testParseUrl() throws Exception {
public void testParseUrlDiesOnNoJsonld() throws Exception {
final String route = "IslandoraTriplestoreIndexerParseUrl";

AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.weaveAddLast().to(resultEndpoint);
});
Expand All @@ -133,7 +133,7 @@ public void testParseUrlDiesOnNoJsonld() throws Exception {
public void testParseUrlDiesOnNoCanonical() throws Exception {
final String route = "IslandoraTriplestoreIndexerParseUrl";

AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.weaveAddLast().to(resultEndpoint);
});
Expand All @@ -157,7 +157,7 @@ public void testIndex() throws Exception {
final String route = "IslandoraTriplestoreIndexer";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");

// Rig Drupal REST endpoint to return canned jsonld
Expand Down Expand Up @@ -211,7 +211,7 @@ public void testDelete() throws Exception {
final String route = "IslandoraTriplestoreIndexerDelete";

context.disableJMX();
AdviceWithRouteBuilder.adviceWith(context, route, a -> {
AdviceWith.adviceWith(context, route, a -> {
a.replaceFromWith("direct:start");
a.mockEndpoints("broker:*");
a.mockEndpointsAndSkip("http://localhost:8080/bigdata/namespace/islandora/sparql?" +
Expand Down

0 comments on commit 309089b

Please sign in to comment.