|
|
|
package com.fr.plugin.db.redis.core;
|
|
|
|
|
|
|
|
import cpm.fanruan.api.conf.HolderKit;
|
|
|
|
import com.fanruan.api.conf.impl.ColConf;
|
|
|
|
import com.fr.data.impl.AbstractDatabaseConnection;
|
|
|
|
import com.fr.data.impl.Connection;
|
|
|
|
import com.fanruan.api.i18n.I18nKit;
|
|
|
|
import com.fr.plugin.db.redis.core.emb.Redis;
|
|
|
|
import com.fanruan.api.security.SecurityKit;
|
|
|
|
import com.fanruan.api.util.ArrayKit;
|
|
|
|
import com.fanruan.api.util.TypeKit;
|
|
|
|
import com.fanruan.api.util.StringKit;
|
|
|
|
import com.fr.stable.xml.XMLPrintWriter;
|
|
|
|
import com.fr.stable.xml.XMLableReader;
|
|
|
|
import com.fr.third.redis.clients.jedis.Jedis;
|
|
|
|
import com.fr.config.holder.Conf;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class RedisDatabaseConnection extends AbstractDatabaseConnection {
|
|
|
|
|
|
|
|
private static final int DEFAULT_REDIS_PORT = 6379;
|
|
|
|
|
|
|
|
private Conf<String> host = HolderKit.simple(StringKit.EMPTY);
|
|
|
|
private Conf<Integer> port = HolderKit.simple(DEFAULT_REDIS_PORT);
|
|
|
|
private Conf<String> password = HolderKit.simple(StringKit.EMPTY);
|
|
|
|
|
|
|
|
public RedisDatabaseConnection() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHost() {
|
|
|
|
return host.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setHost(String host) {
|
|
|
|
this.host.set(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
return port.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPort(int port) {
|
|
|
|
this.port.set(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return password.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPassword(String password) {
|
|
|
|
this.password.set(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void testConnection() throws Exception {
|
|
|
|
Redis client = createRedisClient();
|
|
|
|
try {
|
|
|
|
String text = client.getClient().ping();
|
|
|
|
if (!"pong".equalsIgnoreCase(text)) {
|
|
|
|
throw new Exception(text);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Redis createRedisClient() {
|
|
|
|
return RedisPool.getPool().getResource(getHost(), getPort(), getPassword());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public java.sql.Connection createConnection() throws Exception {
|
|
|
|
return Connection.IGNORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] summary(String... args) {
|
|
|
|
if (ArrayKit.isEmpty(args)) {
|
|
|
|
return ArrayKit.EMPTY_STRING_ARRAY;
|
|
|
|
} else {
|
|
|
|
Redis redis = createRedisClient();
|
|
|
|
Set<String> keys = redis.getClient().keys(args[0]);
|
|
|
|
String[] array = keys.toArray(new String[0]);
|
|
|
|
redis.close();
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String connectMessage(boolean status) {
|
|
|
|
if (status) {
|
|
|
|
return I18nKit.getLocText("Plugin-Redis_Connection_Successfully") + "!";
|
|
|
|
} else {
|
|
|
|
return I18nKit.getLocText("Plugin-Redis_Connection_Failed") + "!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addConnection(List<String> list, String connectionName, Class<? extends Connection>[] acceptTypes) {
|
|
|
|
for (Class<? extends com.fr.data.impl.Connection> accept : acceptTypes) {
|
|
|
|
if (TypeKit.classInstanceOf(getClass(), accept)) {
|
|
|
|
list.add(connectionName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDriver() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getOriginalCharsetName() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOriginalCharsetName(String s) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNewCharsetName() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setNewCharsetName(String s) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readXML(XMLableReader reader) {
|
|
|
|
super.readXML(reader);
|
|
|
|
if (reader.isChildNode()) {
|
|
|
|
String tagName = reader.getTagName();
|
|
|
|
if ("Attr".equals(tagName)) {
|
|
|
|
setHost(reader.getAttrAsString("host", StringKit.EMPTY));
|
|
|
|
setPort(reader.getAttrAsInt("port", DEFAULT_REDIS_PORT));
|
|
|
|
String pwd = reader.getAttrAsString("password", StringKit.EMPTY);
|
|
|
|
if (StringKit.isNotEmpty(pwd)) {
|
|
|
|
setPassword(SecurityKit.encrypt(pwd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeXML(XMLPrintWriter writer) {
|
|
|
|
super.writeXML(writer);
|
|
|
|
writer.startTAG("Attr");
|
|
|
|
writer.attr("host", getHost());
|
|
|
|
writer.attr("port", getPort());
|
|
|
|
if (StringKit.isNotEmpty(getPassword())) {
|
|
|
|
writer.attr("password", SecurityKit.decrypt(getPassword()));
|
|
|
|
}
|
|
|
|
writer.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object clone() throws CloneNotSupportedException {
|
|
|
|
com.fr.plugin.db.redis.core.RedisDatabaseConnection cloned = (com.fr.plugin.db.redis.core.RedisDatabaseConnection) super.clone();
|
|
|
|
cloned.host = (Conf<String>) host.clone();
|
|
|
|
cloned.port = (Conf<Integer>) port.clone();
|
|
|
|
cloned.password = (Conf<String>) password.clone();
|
|
|
|
return cloned;
|
|
|
|
}
|
|
|
|
}
|