forked from fanruan/demo-tabledata-redis
8 changed files with 130 additions and 1 deletions
@ -0,0 +1,13 @@
|
||||
package com.fr.plugin.db.redis; |
||||
|
||||
import com.fr.web.struct.Component; |
||||
import com.fr.web.struct.browser.RequestClient; |
||||
import com.fr.web.struct.category.ScriptPath; |
||||
|
||||
public class RedisConnectionComponent extends Component { |
||||
public static final RedisConnectionComponent KEY = new RedisConnectionComponent(); |
||||
@Override |
||||
public ScriptPath script(RequestClient req) { |
||||
return ScriptPath.build("com/fr/plugin/db/redis/web/redis.js"); |
||||
} |
||||
} |
@ -0,0 +1,105 @@
|
||||
package com.fr.plugin.db.redis; |
||||
|
||||
import com.fr.decision.fun.impl.AbstractUniversalConnectionProvider; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.utils.WebServiceUtils; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.plugin.db.redis.core.RedisDatabaseConnection; |
||||
import com.fr.plugin.db.redis.core.pool.RedisConnectionPoolConfig; |
||||
import com.fr.plugin.db.redis.core.pool.RedisConnectionProxyConfig; |
||||
import com.fr.stable.AssistUtils; |
||||
import com.fr.transaction.Configurations; |
||||
import com.fr.transaction.WorkerAdaptor; |
||||
import com.fr.web.struct.Atom; |
||||
|
||||
|
||||
public class RedisUniversalConnectionImpl extends AbstractUniversalConnectionProvider<RedisDatabaseConnection> { |
||||
|
||||
private static final String BASIC_INFO = "basicConfig"; |
||||
private static final String POOL_INFO = "poolConfig"; |
||||
private static final String PROXY_INFO = "proxyConfig"; |
||||
|
||||
|
||||
@Override |
||||
public String nameForConnection() { |
||||
return "Redis"; |
||||
} |
||||
|
||||
@Override |
||||
public String iconPathForConnection() { |
||||
return "/com/fr/plugin/db/redis/images/redis.png"; |
||||
} |
||||
|
||||
@Override |
||||
public Class<RedisDatabaseConnection> classForConnection() { |
||||
return RedisDatabaseConnection.class; |
||||
} |
||||
|
||||
@Override |
||||
public Atom client() { |
||||
return RedisConnectionComponent.KEY; |
||||
} |
||||
|
||||
@Override |
||||
public JSONObject serialize(RedisDatabaseConnection connection) { |
||||
JSONObject jsonObject = new JSONObject(); |
||||
try { |
||||
jsonObject.put(BASIC_INFO, JSONObject.mapFrom(connection.clone())); |
||||
//将ConnectionPoolConfig和RedisConnectionProxyConfig也一次塞给前台展示
|
||||
jsonObject.put(POOL_INFO, JSONObject.mapFrom(RedisConnectionPoolConfig.getInstance().clone())); |
||||
JSONObject proxy = JSONObject.mapFrom(RedisConnectionProxyConfig.getInstance().clone()); |
||||
proxy.put("password", DecisionServiceConstants.DEFAULT_PASSWORD); |
||||
jsonObject.put(PROXY_INFO, proxy); |
||||
} catch (CloneNotSupportedException ignore) { |
||||
} |
||||
return jsonObject; |
||||
} |
||||
|
||||
@Override |
||||
public RedisDatabaseConnection deserialize(RedisDatabaseConnection oldConnection, JSONObject pluginData) { |
||||
JSONObject redisDatabaseConnection = getRedisDataConnection(pluginData); |
||||
if (oldConnection != null && DecisionServiceConstants.DEFAULT_PASSWORD.equals(redisDatabaseConnection.getString("password"))) { |
||||
redisDatabaseConnection.put("password", oldConnection.getPassword()); |
||||
} else { |
||||
redisDatabaseConnection.put("password", WebServiceUtils.getBase64DecodeStr(redisDatabaseConnection.getString("password"))); |
||||
} |
||||
return redisDatabaseConnection.mapTo(RedisDatabaseConnection.class); |
||||
} |
||||
|
||||
private JSONObject getRedisDataConnection(JSONObject object) { |
||||
|
||||
final JSONObject pool = object.getJSONObject(POOL_INFO); |
||||
if (pool != null) { |
||||
final RedisConnectionPoolConfig poolConfig = pool.mapTo(RedisConnectionPoolConfig.class); |
||||
Configurations.update(new WorkerAdaptor(RedisConnectionPoolConfig.class) { |
||||
@Override |
||||
public void run() { |
||||
RedisConnectionPoolConfig.getInstance().setBlockWhenExhausted(poolConfig.getBlockWhenExhausted()); |
||||
RedisConnectionPoolConfig.getInstance().setLifo(poolConfig.getLifo()); |
||||
RedisConnectionPoolConfig.getInstance().setMaxIdle(poolConfig.getMaxIdle()); |
||||
RedisConnectionPoolConfig.getInstance().setMaxTotal(poolConfig.getMaxTotal()); |
||||
RedisConnectionPoolConfig.getInstance().setMaxWait(poolConfig.getMaxWait()); |
||||
RedisConnectionPoolConfig.getInstance().setTimeout(poolConfig.getTimeout()); |
||||
} |
||||
}); |
||||
} |
||||
final JSONObject proxy = object.getJSONObject(PROXY_INFO); |
||||
if (proxy != null) { |
||||
final RedisConnectionProxyConfig proxyConfig = proxy.mapTo(RedisConnectionProxyConfig.class); |
||||
Configurations.update(new WorkerAdaptor(RedisConnectionProxyConfig.class) { |
||||
@Override |
||||
public void run() { |
||||
RedisConnectionProxyConfig.getInstance().setHost(proxyConfig.getHost()); |
||||
RedisConnectionProxyConfig.getInstance().setOpen(proxyConfig.isOpen()); |
||||
if (!AssistUtils.equals(proxyConfig.getPassword(), DecisionServiceConstants.DEFAULT_PASSWORD)) { |
||||
RedisConnectionProxyConfig.getInstance().setPassword(WebServiceUtils.getBase64DecodeStr(proxyConfig.getPassword())); |
||||
} |
||||
RedisConnectionProxyConfig.getInstance().setPort(proxyConfig.getPort()); |
||||
RedisConnectionProxyConfig.getInstance().setPrivateKeyPath(proxyConfig.getPrivateKeyPath()); |
||||
RedisConnectionProxyConfig.getInstance().setUsername(proxyConfig.getUsername()); |
||||
} |
||||
}); |
||||
} |
||||
return object.getJSONObject(BASIC_INFO); |
||||
} |
||||
} |
Binary file not shown.
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue