You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
5.1 KiB
178 lines
5.1 KiB
package com.fr.plugin.db.redis.core; |
|
|
|
import com.fanruan.api.conf.HolderKit; |
|
import com.fanruan.api.data.open.BaseConnection; |
|
import com.fanruan.api.i18n.I18nKit; |
|
import com.fanruan.api.security.SecurityKit; |
|
import com.fanruan.api.util.ArrayKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fanruan.api.util.TypeKit; |
|
import com.fr.config.holder.Conf; |
|
import com.fr.data.impl.Connection; |
|
import com.fr.plugin.db.redis.core.accessor.EmbedRedis; |
|
import com.fr.plugin.db.redis.core.emb.Redis; |
|
import com.fr.stable.xml.XMLPrintWriter; |
|
import com.fr.stable.xml.XMLableReader; |
|
import com.fr.third.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
|
|
import java.util.List; |
|
import java.util.Set; |
|
|
|
@JsonIgnoreProperties({"id", "data", "classInfo", "nameSpace", "driver"}) |
|
public class RedisDatabaseConnection extends BaseConnection { |
|
|
|
private static final int DEFAULT_REDIS_PORT = 6379; |
|
|
|
private Conf<String> host = HolderKit.simple(StringKit.EMPTY); |
|
private Conf<String> port = HolderKit.simple(String.valueOf(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 String getPort() { |
|
return port.get(); |
|
} |
|
|
|
public void setPort(String 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.getFinal(getHost(), getPort(), getPassword()); |
|
} |
|
|
|
@Override |
|
public String[] summary(String... args) { |
|
if (ArrayKit.isEmpty(args)) { |
|
return ArrayKit.EMPTY_STRING_ARRAY; |
|
} else { |
|
Redis redis = createRedisClient(); |
|
try { |
|
EmbedRedis embedRedis = redis.getClient(); |
|
int len = ArrayKit.getLength(args); |
|
if (len > 1) { |
|
int dbIndex = Integer.parseInt(args[1]); |
|
embedRedis.select(dbIndex); |
|
} |
|
Set<String> keys = embedRedis.keys(args[0]); |
|
return keys.toArray(new String[0]); |
|
} finally { |
|
redis.close(); |
|
} |
|
} |
|
} |
|
|
|
@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.getAttrAsString("port", String.valueOf(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<String>) port.clone(); |
|
cloned.password = (Conf<String>) password.clone(); |
|
return cloned; |
|
} |
|
} |