mirror of
https://gitee.com/devlive-community/datacap.git
synced 2024-12-05 05:27:52 +08:00
[Plugin] Support redis for native (#160)
This commit is contained in:
commit
16dc1b73c3
19
plugin/native/pom.xml
Normal file
19
plugin/native/pom.xml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>datacap-plugin</artifactId>
|
||||
<groupId>io.edurt.datacap.plugin</groupId>
|
||||
<version>1.3.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>datacap-plugin-native</artifactId>
|
||||
<groupId>io.edurt.datacap.plugin.natived</groupId>
|
||||
<name>DataCap plugin for (Native)</name>
|
||||
|
||||
<modules>
|
||||
<module>redis</module>
|
||||
</modules>
|
||||
</project>
|
61
plugin/native/redis/pom.xml
Normal file
61
plugin/native/redis/pom.xml
Normal file
@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>datacap-plugin-native</artifactId>
|
||||
<groupId>io.edurt.datacap.plugin.natived</groupId>
|
||||
<version>1.3.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>datacap-plugin-native-redis</artifactId>
|
||||
<name>DataCap plugin for native (Redis)</name>
|
||||
|
||||
<properties>
|
||||
<redis.version>3.6.3</redis.version>
|
||||
<plugin.name>native-redis</plugin.name>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.edurt.datacap</groupId>
|
||||
<artifactId>datacap-spi</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>${redis.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>${assembly-plugin.version}</version>
|
||||
<configuration>
|
||||
<finalName>${plugin.name}</finalName>
|
||||
<descriptors>
|
||||
<descriptor>../../../configure/assembly/assembly-plugin.xml</descriptor>
|
||||
</descriptors>
|
||||
<outputDirectory>../../../dist/plugins/${plugin.name}</outputDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,81 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import io.edurt.datacap.spi.adapter.NativeAdapter;
|
||||
import io.edurt.datacap.spi.model.Configure;
|
||||
import io.edurt.datacap.spi.model.Response;
|
||||
import io.edurt.datacap.spi.model.Time;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
import redis.clients.jedis.Client;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@SuppressFBWarnings(value = {"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"},
|
||||
justification = "I prefer to suppress these FindBugs warnings")
|
||||
public class RedisAdapter
|
||||
extends NativeAdapter
|
||||
{
|
||||
protected RedisConnection redisConnection;
|
||||
|
||||
public RedisAdapter(RedisConnection redisConnection)
|
||||
{
|
||||
super(redisConnection);
|
||||
this.redisConnection = redisConnection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response handlerExecute(String content)
|
||||
{
|
||||
Time processorTime = new Time();
|
||||
processorTime.setStart(new Date().getTime());
|
||||
Response response = this.redisConnection.getResponse();
|
||||
Configure configure = this.redisConnection.getConfigure();
|
||||
if (response.getIsConnected()) {
|
||||
List<String> headers = new ArrayList<>();
|
||||
List<String> types = new ArrayList<>();
|
||||
List<Object> columns = new ArrayList<>();
|
||||
try {
|
||||
String[] commands = content.split(" ", 2);
|
||||
Protocol.Command cmd = Protocol.Command.valueOf(commands[0].toUpperCase());
|
||||
Method method = MethodUtils.getMatchingMethod(Client.class, "sendCommand", Protocol.Command.class, String[].class);
|
||||
method.setAccessible(true);
|
||||
Client client = this.redisConnection.getJedis().getClient();
|
||||
method.invoke(client, cmd, new String[] {commands[1]});
|
||||
Object body = client.getOne();
|
||||
headers.add(commands[1]);
|
||||
types.add("String");
|
||||
if (body instanceof List) {
|
||||
List<Object> bodySplit = ((List) body);
|
||||
for (Object obj : bodySplit) {
|
||||
columns.add(handlerFormatter(configure.getFormat(), headers, Arrays.asList(new String((byte[]) obj, Charset.forName("UTF-8")))));
|
||||
}
|
||||
}
|
||||
else {
|
||||
columns.add(handlerFormatter(configure.getFormat(), headers, Arrays.asList(new String((byte[]) body, Charset.forName("UTF-8")))));
|
||||
}
|
||||
response.setIsSuccessful(Boolean.TRUE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
log.error("Execute content failed content {} exception ", content, ex);
|
||||
response.setIsSuccessful(Boolean.FALSE);
|
||||
response.setMessage(ex.getMessage());
|
||||
}
|
||||
finally {
|
||||
response.setHeaders(headers);
|
||||
response.setTypes(types);
|
||||
response.setColumns(columns);
|
||||
}
|
||||
}
|
||||
processorTime.setEnd(new Date().getTime());
|
||||
response.setProcessor(processorTime);
|
||||
return response;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import io.edurt.datacap.spi.connection.Connection;
|
||||
import io.edurt.datacap.spi.model.Configure;
|
||||
import io.edurt.datacap.spi.model.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
@Slf4j
|
||||
public class RedisConnection
|
||||
extends Connection
|
||||
{
|
||||
private Configure configure;
|
||||
private Response response;
|
||||
private Jedis jedis;
|
||||
|
||||
public RedisConnection(Configure configure, Response response)
|
||||
{
|
||||
super(configure, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String formatJdbcUrl()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected java.sql.Connection openConnection()
|
||||
{
|
||||
try {
|
||||
this.configure = getConfigure();
|
||||
this.response = getResponse();
|
||||
log.info("Connection url {}", formatJdbcUrl());
|
||||
this.jedis = new Jedis(this.configure.getHost(), this.configure.getPort());
|
||||
this.jedis.clientId();
|
||||
response.setIsConnected(Boolean.TRUE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
log.error("Connection failed ", ex);
|
||||
response.setIsConnected(Boolean.FALSE);
|
||||
response.setMessage(ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
this.jedis.close();
|
||||
log.info("Connection close successful");
|
||||
}
|
||||
|
||||
public Jedis getJedis()
|
||||
{
|
||||
return jedis;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import io.edurt.datacap.spi.Plugin;
|
||||
import io.edurt.datacap.spi.PluginType;
|
||||
import io.edurt.datacap.spi.adapter.Adapter;
|
||||
import io.edurt.datacap.spi.connection.JdbcConfigure;
|
||||
import io.edurt.datacap.spi.model.Configure;
|
||||
import io.edurt.datacap.spi.model.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
||||
@Slf4j
|
||||
public class RedisPlugin
|
||||
implements Plugin
|
||||
{
|
||||
private Configure configure;
|
||||
private RedisConnection connection;
|
||||
private Response response;
|
||||
|
||||
@Override
|
||||
public String validator()
|
||||
{
|
||||
return "PING Redis";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name()
|
||||
{
|
||||
return "Redis";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description()
|
||||
{
|
||||
return "Integrate Redis data sources";
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginType type()
|
||||
{
|
||||
return PluginType.NATIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(Configure configure)
|
||||
{
|
||||
try {
|
||||
this.response = new Response();
|
||||
this.configure = new JdbcConfigure();
|
||||
BeanUtils.copyProperties(this.configure, configure);
|
||||
this.connection = new RedisConnection(this.configure, this.response);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
this.response.setIsConnected(Boolean.FALSE);
|
||||
this.response.setMessage(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(String content)
|
||||
{
|
||||
if (ObjectUtils.isNotEmpty(this.connection)) {
|
||||
log.info("Execute redis plugin logic started");
|
||||
this.response = this.connection.getResponse();
|
||||
Adapter processor = new RedisAdapter(this.connection);
|
||||
this.response = processor.handlerExecute(content);
|
||||
log.info("Execute redis plugin logic end");
|
||||
}
|
||||
return this.response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
if (ObjectUtils.isNotEmpty(this.connection)) {
|
||||
this.connection.destroy();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import com.google.inject.multibindings.Multibinder;
|
||||
import io.edurt.datacap.spi.AbstractPluginModule;
|
||||
import io.edurt.datacap.spi.Plugin;
|
||||
import io.edurt.datacap.spi.PluginModule;
|
||||
import io.edurt.datacap.spi.PluginType;
|
||||
|
||||
public class RedisPluginModule
|
||||
extends AbstractPluginModule
|
||||
implements PluginModule
|
||||
{
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "Redis";
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginType getType()
|
||||
{
|
||||
return PluginType.NATIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractPluginModule get()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void configure()
|
||||
{
|
||||
Multibinder<String> module = Multibinder.newSetBinder(this.binder(), String.class);
|
||||
module.addBinding().toInstance(this.getClass().getSimpleName());
|
||||
Multibinder<Plugin> plugin = Multibinder.newSetBinder(this.binder(), Plugin.class);
|
||||
plugin.addBinding().to(RedisPlugin.class);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
io.edurt.datacap.plugin.natived.redis.RedisPluginModule
|
@ -0,0 +1,16 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import io.edurt.datacap.spi.adapter.Adapter;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RedisAdapterTest
|
||||
extends RedisBaseTest
|
||||
{
|
||||
@Test
|
||||
public void handlerExecute()
|
||||
{
|
||||
RedisConnection connection = new RedisConnection(this.configure, this.response);
|
||||
Adapter adapter = new RedisAdapter(connection);
|
||||
adapter.handlerExecute("GET key");
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import io.edurt.datacap.spi.model.Configure;
|
||||
import io.edurt.datacap.spi.model.Response;
|
||||
import org.junit.Before;
|
||||
|
||||
public class RedisBaseTest
|
||||
{
|
||||
protected Configure configure;
|
||||
protected Response response;
|
||||
|
||||
@Before
|
||||
public void before()
|
||||
{
|
||||
this.configure = new Configure();
|
||||
this.configure.setHost("127.0.0.1");
|
||||
this.configure.setPort(6379);
|
||||
this.response = new Response();
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RedisConnectionTest
|
||||
extends RedisBaseTest
|
||||
{
|
||||
@Test
|
||||
public void openConnection()
|
||||
{
|
||||
RedisConnection connection = new RedisConnection(this.configure, this.response);
|
||||
Assert.assertNotNull(connection.getResponse().getConnection());
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import io.edurt.datacap.spi.Plugin;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class RedisPluginModuleTest
|
||||
{
|
||||
private Injector injector;
|
||||
|
||||
@Before
|
||||
public void before()
|
||||
{
|
||||
this.injector = Guice.createInjector(new RedisPluginModule());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test()
|
||||
{
|
||||
Set<Plugin> plugins = injector.getInstance(Key.get(new TypeLiteral<Set<Plugin>>() {}));
|
||||
Assert.assertTrue(plugins.size() > 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package io.edurt.datacap.plugin.natived.redis;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import io.edurt.datacap.spi.Plugin;
|
||||
import io.edurt.datacap.spi.model.Configure;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class RedisPluginTest
|
||||
{
|
||||
private Injector injector;
|
||||
private Configure configure;
|
||||
|
||||
@Before
|
||||
public void before()
|
||||
{
|
||||
injector = Guice.createInjector(new RedisPluginModule());
|
||||
configure = new Configure();
|
||||
configure.setHost("127.0.0.1");
|
||||
configure.setPort(6379);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test()
|
||||
{
|
||||
Set<Plugin> plugins = injector.getInstance(Key.get(new TypeLiteral<Set<Plugin>>() {}));
|
||||
Optional<Plugin> pluginOptional = plugins.stream()
|
||||
.filter(v -> v.name().equalsIgnoreCase("Redis"))
|
||||
.findFirst();
|
||||
if (pluginOptional.isPresent()) {
|
||||
Plugin plugin = pluginOptional.get();
|
||||
plugin.connect(configure);
|
||||
Assert.assertNotNull(plugin.execute(plugin.validator()).getConnection());
|
||||
plugin.destroy();
|
||||
}
|
||||
}
|
||||
}
|
@ -18,5 +18,6 @@
|
||||
<module>example</module>
|
||||
<module>jdbc</module>
|
||||
<module>http</module>
|
||||
<module>native</module>
|
||||
</modules>
|
||||
</project>
|
@ -224,6 +224,11 @@
|
||||
<artifactId>datacap-plugin-jdbc-oceanbase</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.edurt.datacap.plugin.natived</groupId>
|
||||
<artifactId>datacap-plugin-native-redis</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -3,5 +3,6 @@ package io.edurt.datacap.spi;
|
||||
public enum PluginType
|
||||
{
|
||||
JDBC,
|
||||
HTTP
|
||||
HTTP,
|
||||
NATIVE
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package io.edurt.datacap.spi.adapter;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import io.edurt.datacap.spi.FormatType;
|
||||
import io.edurt.datacap.spi.connection.Connection;
|
||||
import io.edurt.datacap.spi.formatter.FormatterFactory;
|
||||
import io.edurt.datacap.spi.model.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@SuppressFBWarnings(value = {"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"},
|
||||
justification = "I prefer to suppress these FindBugs warnings")
|
||||
public class NativeAdapter
|
||||
implements Adapter
|
||||
{
|
||||
protected Connection connection;
|
||||
|
||||
public NativeAdapter(Connection connection)
|
||||
{
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
protected Object handlerFormatter(FormatType format, List<String> headers, List<Object> columns)
|
||||
{
|
||||
return FormatterFactory.createFormatter(format, headers, columns).formatter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response handlerExecute(String content)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user