参考:插件配置示例文档即可。
该对象会作为配置文件类的一个属性,以com.fr.data.imlp.Connection(数据连接)对象为例子,该对象最终是需要保存到数据连接配置中去的,因此需要遵循如下几个原则:
1、需要继承com.fr.config.utils.UniqueKey
2、该对象内部所有需要持久化的属性,都需要用com.fr.config.holder.Conf封装
3、为了设计器可以打开配置修改,需要重写clone方法
具体事例可参考Redis数据连接的实现:
public class RedisDatabaseConnection extends AbstractDatabaseConnection { private static final int DEFAULT_REDIS_PORT = 6379; private Conf<String> host = Holders.simple(StringUtils.EMPTY); private Conf<Integer> port = Holders.simple(DEFAULT_REDIS_PORT); private Conf<String> password = Holders.simple(StringUtils.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 { Jedis client = createRedisClient(); try { String text = client.ping(); if (!"pong".equalsIgnoreCase(text)) { throw new Exception(text); } } finally { client.close(); } } public Jedis createRedisClient() { String myHost = getHost(); int myPort = getPort(); String myPassword = getPassword(); Jedis client = new Jedis(myHost, myPort); if (StringUtils.isNotEmpty(myPassword)) { client.auth(myPassword); } return client; } @Override public java.sql.Connection createConnection() throws Exception { return null; } @Override public String connectMessage(boolean status) { if (status) { return InterProviderFactory.getProvider().getLocText("Plugin-Redis_Connect_Successful") + "!"; } else { return InterProviderFactory.getProvider().getLocText("Plugin-Redis_Connect_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 (StableUtils.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", StringUtils.EMPTY)); setPort(reader.getAttrAsInt("port", DEFAULT_REDIS_PORT)); String pwd = reader.getAttrAsString("password", StringUtils.EMPTY); if (StringUtils.isNotEmpty(pwd)) { setPassword(CodeUtils.passwordDecode(pwd)); } } } } @Override public void writeXML(XMLPrintWriter writer) { super.writeXML(writer); writer.startTAG("Attr"); writer.attr("host", getHost()); writer.attr("port", getPort()); if (StringUtils.isNotEmpty(getPassword())) { writer.attr("password", CodeUtils.passwordEncode(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; } } |
其中的readXML和writeXML方法,如果不是为了兼容,都是可以不用实现的,留空即可。