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

chore: refactor camel integration #2989

Merged
merged 4 commits into from
Nov 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SpringBeanProvider = Java.type("org.eclipse.dirigible.components.spring.SpringBeanProvider");
const Invoker = Java.type('org.eclipse.dirigible.components.engine.camel.invoke.Invoker');
const invoker = SpringBeanProvider.getBean(Invoker.class);

export function invokeRoute(routeId, payload, headers) {
return invoker.invokeRoute(routeId, payload, headers);
}

export function getInvokingRouteMessage() {
return __context.camelMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void configure(HttpSecurity http) throws Exception {
.permitAll()
.antMatchers("/services/js/resources-core/**")
.permitAll()
.antMatchers("/camel/*")
.antMatchers("/services/integrations/**")
.permitAll()

.antMatchers("/actuator/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@
*/
package org.eclipse.dirigible.components.data.sources.manager;

import static java.text.MessageFormat.format;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.components.data.sources.domain.DataSource;
import org.eclipse.dirigible.components.data.sources.service.CustomDataSourcesService;
Expand All @@ -32,10 +21,16 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import static java.text.MessageFormat.format;

/**
* The Class DataSourcesManager.
Expand All @@ -58,16 +53,21 @@ public class DataSourcesManager implements InitializingBean {
/** The custom data sources service. */
private CustomDataSourcesService customDataSourcesService;

private ApplicationContext applicationContext;

/**
* Instantiates a new data sources manager.
*
* @param datasourceService the datasource service
* @param customDataSourcesService the custom data sources service
* @param applicationContext
*/
@Autowired
public DataSourcesManager(DataSourceService datasourceService, CustomDataSourcesService customDataSourcesService) {
public DataSourcesManager(DataSourceService datasourceService, CustomDataSourcesService customDataSourcesService,
ApplicationContext applicationContext) {
this.datasourceService = datasourceService;
this.customDataSourcesService = customDataSourcesService;
this.applicationContext = applicationContext;
this.customDataSourcesService.initialize();
}

Expand Down Expand Up @@ -162,6 +162,8 @@ private javax.sql.DataSource initializeDataSource(String name) {
HikariDataSource hds = new HikariDataSource(config);

ManagedDataSource managedDataSource = new ManagedDataSource(hds);
((GenericApplicationContext) applicationContext).getBeanFactory()
.registerSingleton(name, managedDataSource);
DATASOURCES.put(name, managedDataSource);
if (logger.isInfoEnabled()) {
logger.info("Initialized a datasource with name: " + name);
Expand Down
7 changes: 6 additions & 1 deletion components/engine/engine-camel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>camel-ftp</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-sql</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-engine-javascript</artifactId>
Expand All @@ -77,4 +82,4 @@
<parent.pom.folder>../../../</parent.pom.folder>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible
* contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.engine.camel.config;

import org.apache.camel.CamelContext;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.component.platform.http.springboot.CamelRequestHandlerMapping;
import org.eclipse.dirigible.components.engine.camel.processor.CamelDirigibleRequestHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class CamelDirigibleConfiguration {

@Bean
@Primary
public CamelRequestHandlerMapping createCamelRequestHandlerMapping(CamelContext camelContext, PlatformHttpEngine httpEngine,
CamelRequestHandlerMapping camelRequestHandlerMapping) {
var httpComponent = camelContext.getComponent("platform-http", PlatformHttpComponent.class);
httpComponent.removePlatformHttpListener(camelRequestHandlerMapping); // necessary as the Camel configurations are still going to
// run and this class adds itself as a primary listener in its
// constructor
return new CamelDirigibleRequestHandlerMapping(httpComponent, httpEngine);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,35 @@

import org.apache.camel.Message;
import org.eclipse.dirigible.components.engine.camel.processor.CamelProcessor;
import org.eclipse.dirigible.components.engine.javascript.service.JavascriptService;
import org.eclipse.dirigible.repository.api.RepositoryPath;
import org.eclipse.dirigible.graalium.core.DirigibleJavascriptCodeRunner;
import org.eclipse.dirigible.graalium.core.javascript.CalledFromJS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.HashMap;
import java.nio.file.Path;
import java.util.Map;

@Component
public class Invoker {
private final JavascriptService javascriptService;

private final CamelProcessor processor;

@Autowired
public Invoker(JavascriptService javascriptService, CamelProcessor processor) {
this.javascriptService = javascriptService;
public Invoker(CamelProcessor processor) {
this.processor = processor;
}

public void invoke(Message camelMessage) {
String resourcePath = (String) camelMessage.getExchange()
.getProperty("resource");
RepositoryPath path = new RepositoryPath(resourcePath);

String messageBody = camelMessage.getBody(String.class);

Map<Object, Object> context = new HashMap<>();
context.put("camelMessage", messageBody);

javascriptService.handleRequest(path.getSegments()[0], path.constructPathFrom(1), null, context, false);
try (var runner = new DirigibleJavascriptCodeRunner()) {
var module = runner.run(Path.of(resourcePath));
runner.runMethod(module, "onMessage", messageBody);
}
}

@CalledFromJS
public Object invokeRoute(String routeId, Object payload, Map<String, Object> headers) {
return processor.invokeRoute(routeId, payload, headers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible
* contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.engine.camel.processor;

import org.apache.camel.component.platform.http.HttpEndpointModel;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.component.platform.http.springboot.CamelRequestHandlerMapping;

public class CamelDirigibleRequestHandlerMapping extends CamelRequestHandlerMapping {

public CamelDirigibleRequestHandlerMapping(PlatformHttpComponent component, PlatformHttpEngine engine) {
super(component, engine);
}

@Override
public void registerHttpEndpoint(HttpEndpointModel model) {
var patchedModel = DirigibleHttpEndpointModel.from(model);
super.registerHttpEndpoint(patchedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@

import org.apache.camel.CamelContext;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.component.platform.http.springboot.CamelRequestHandlerMapping;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;

import org.apache.camel.spring.boot.SpringBootCamelContext;
import org.apache.camel.support.ResourceHelper;
import org.eclipse.dirigible.components.engine.camel.domain.Camel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CamelProcessor {
private final SpringBootCamelContext context;

private final RequestMappingHandlerMapping camelMapping;
private final CamelRequestHandlerMapping camelRequestHandlerMapping;
private final Map<String, DataSource> datasources;

private final RoutesLoader loader;

private final Map<Long, Resource> camels = new HashMap<>();

@Autowired
public CamelProcessor(CamelContext context, @Qualifier("platformHttpEngineRequestMapping") RequestMappingHandlerMapping camelMapping) {
public CamelProcessor(CamelContext context, CamelRequestHandlerMapping camelRequestHandlerMapping,
Map<String, DataSource> datasources) {
this.context = context.adapt(SpringBootCamelContext.class);
this.camelMapping = camelMapping;
this.camelRequestHandlerMapping = camelRequestHandlerMapping;
this.datasources = datasources;
loader = this.context.getRoutesLoader();
}

Expand Down Expand Up @@ -75,20 +73,13 @@ private void removeAllRoutes() {
try {
context.stopAllRoutes();
context.removeAllRoutes();
unregisterEndpoints();

camelRequestHandlerMapping.getHandlerMethods()
.forEach((info, method) -> camelRequestHandlerMapping.unregisterMapping(info));
} catch (Exception e) {
throw new CamelProcessorException(e);
}
}

private void unregisterEndpoints() {
List<RequestMappingInfo> mappingsToRemove = new ArrayList<>();
camelMapping.getHandlerMethods()
.forEach((info, method) -> mappingsToRemove.add(info));
mappingsToRemove.forEach(info -> camelMapping.unregisterMapping(info));
}

public Object invokeRoute(String routeId, Object payload, Map<String, Object> headers) {
try (FluentProducerTemplate producer = context.createFluentProducerTemplate();) {
return producer.withHeaders(headers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible
* contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.engine.camel.processor;

import org.apache.camel.Consumer;
import org.apache.camel.component.platform.http.HttpEndpointModel;

public class DirigibleHttpEndpointModel extends HttpEndpointModel {

private DirigibleHttpEndpointModel(String uri, String verbs, Consumer consumer) {
super(patchUri(uri), verbs, consumer);
}

public static DirigibleHttpEndpointModel from(HttpEndpointModel model) {
return new DirigibleHttpEndpointModel(model.getUri(), model.getVerbs(), model.getConsumer());
}

private static String patchUri(String uri) {
String base = "/services/integrations";
if (uri == null || uri.isEmpty()) {
return base;
}
return base + (uri.startsWith("/") ? uri : "/" + uri);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading