📄 modulerightsconfig.java
字号:
package com.easyjf.news.business;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import com.easyjf.dbo.config.IConfigFactory;
import com.easyjf.dbo.config.XMLConfigFactory;
public class ModuleRightsConfig {
private Document doc;
private static final String configFile="/easyjf-rights.xml";
private static final Logger logger = (Logger) Logger.getLogger(IConfigFactory.class.getName());
private final Map map = new HashMap();
private final Map rights = new HashMap();
private static ModuleRightsConfig singlton;
public ModuleRightsConfig(InputStream in) {
try {
//SAXReader reader = new SAXReader();
byte[] bs=new byte[in.available()];
in.read(bs);
String xml=new String(bs,"utf-8");
//doc = reader.read(in);
//System.out.println(xml);
//System.out.println(xml.charAt(0));
doc =DocumentHelper.parseText(xml.charAt(0)!='<'?xml.substring(1):xml);
init();
} catch (Exception e) {
logger.warn("配置文件错误!" + e);
}
}
public static ModuleRightsConfig getInstance() {
if(singlton==null){
synchronized (ModuleRightsConfig.class) {
InputStream in = null;
try {
logger.debug("从easyjf-rights.xml文件初始化数据源!");
in = ModuleRights.class.getResourceAsStream(configFile);
} catch (Exception e) {
logger.warn("查找权限配置文件文件错误!" + e);
}
singlton=new ModuleRightsConfig(in);
}
}
return singlton;
}
public void init() {
if (doc == null)
return;
synchronized (ModuleRightsConfig.class) {
if (!map.isEmpty())
map.clear();
if (!rights.isEmpty())
rights.clear();
List nodes = doc.selectNodes("/easyjf-rights/common-rights/ref");
if (nodes != null) {
for (int i = 0; i < nodes.size(); i++) {
Element e = (Element) nodes.get(i);
String name = e.attributeValue("name");
if (name != null && (!"".equals(name))) {
String sn = e.attributeValue("sn");
String title = e.attributeValue("title");
String value = e.attributeValue("value");
String intro = e.attributeValue("intro");
String validateClass = e.attributeValue("class");
SystemRights r = new SystemRights(name, sn, title,value, intro,validateClass);
r.setTypes(1);
rights.put(name, r);
//System.out.println(r);
}
}
}
nodes = doc.selectNodes("/easyjf-rights/module");
if (nodes != null) {
for (int i = 0; i < nodes.size(); i++) {
Element e = (Element) nodes.get(i);
String name = e.attributeValue("name");
ModuleRights mr = new ModuleRights();
mr.setModuleName(name);
List actions = e.selectNodes("action");
if (actions != null) {
for (int j = 0; j < actions.size(); j++) {
Element a = (Element) actions.get(j);
Element node = (Element) a.selectSingleNode("ref");
if (node != null) {
String rName = node.attributeValue("name");
SystemRights r = (SystemRights) rights.get(rName);
if (r != null)
mr.addRights(a.attributeValue("name"),r);
} else {
String rName = a.attributeValue("name");
if (rName != null && (!"".equals(rName))) {
String sn = a.attributeValue("sn");
String title = a.attributeValue("title");
String value = a.attributeValue("value");
String intro =a.attributeValue("intro");
String validateClass = a.attributeValue("class");
SystemRights r = new SystemRights(name, sn, title,
value, intro,validateClass);
mr.addRights(rName,r);
}
}
}
}
map.put(mr.getModuleName(),mr);
}
}
}
}
public Map getModuleRights()
{
return map;
}
public ModuleRights getModuleRights(String moduleName) {
return (ModuleRights) map.get(moduleName);
}
public Map getRights() {
return rights;
}
public boolean addModuleRights(ModuleRights mr)
{
boolean ret=false;
this.map.put(mr.getModuleName(),mr);
Node node=doc.selectSingleNode("/easyjf-rights/module[@name='"+mr.getModuleName()+"']");
if(node==null)
{
Element module=DocumentHelper.createElement("module");
module.addAttribute("name",mr.getModuleName());
Iterator it=mr.getRights().entrySet().iterator();
while(it.hasNext())
{
Map.Entry en=(Map.Entry)it.next();
String actionName=(String)en.getKey();
SystemRights r=(SystemRights)en.getValue();
Element action= DocumentHelper.createElement("action");
action.addAttribute("name",actionName);
if(r.getTypes()>0 && (this.rights.get(r.getName())!=null))
{
Element ref=DocumentHelper.createElement("ref");
ref.addAttribute("name",r.getName());
action.add(ref);
}
else{
action.addAttribute("sn",r.getSn());
action.addAttribute("title",r.getTitle());
action.addAttribute("value",r.getValue());
action.addAttribute("intro",r.getIntro());
action.addAttribute("class",r.getValidateClass());
}
module.add(action);
}
doc.getRootElement().add(module);
ret=true;
}
String fileName=XMLConfigFactory.class.getResource(configFile).getPath();
ret=ret&&write(fileName,doc);
return ret;
}
public boolean delModuleRights(ModuleRights mr)
{
boolean ret=false;
this.map.remove(mr.getModuleName());
Node node=doc.selectSingleNode("/easyjf-rights/module[@name='"+mr.getModuleName()+"']");
if(node!=null)
{
node.detach();
ret=true;
}
String fileName=XMLConfigFactory.class.getResource(configFile).getPath();
ret=ret&&write(fileName,doc);
return ret;
}
public boolean updateModuleRights(ModuleRights mr)
{
boolean ret=false;
ret=delModuleRights(mr)&&addModuleRights(mr);
return ret;
}
public boolean addRights(SystemRights r)
{
boolean ret=false;
this.rights.put(r.getName(),r);
Node node=doc.selectSingleNode("/easyjf-rights/common-rights/ref[@name='"+r.getName()+"']");
if(node==null)
{
Element e=DocumentHelper.createElement("ref");
e.addAttribute("name",r.getName());
e.addAttribute("sn",r.getSn());
e.addAttribute("title",r.getTitle()==null?"":r.getTitle());
e.addAttribute("value",r.getValue()==null?"":r.getValue());
e.addAttribute("class",r.getValidateClass()==null?"":r.getValidateClass());
e.addAttribute("intro",r.getIntro()==null?"":r.getIntro());
Element common=(Element)doc.selectSingleNode("/easyjf-rights/common-rights");
if(common!=null)
{common.add(e);
ret=true;
}
}
String fileName=XMLConfigFactory.class.getResource(configFile).getPath();
ret=ret&&write(fileName,doc);
return ret;
}
public boolean delRights(SystemRights r)
{
boolean ret=false;
this.rights.remove(r.getName());
Node node=doc.selectSingleNode("/easyjf-rights/common-rights/ref[@name='"+r.getName()+"']");
if(node!=null)
{node.detach();
ret=true;
}
String fileName=XMLConfigFactory.class.getResource(configFile).getPath();
ret=ret&&write(fileName,doc);
return ret;
}
public boolean updateRights(SystemRights r)
{
boolean ret=false;
//this.rights.remove(r.getName());
Element e=(Element)doc.selectSingleNode("/easyjf-rights/common-rights/ref[@name='"+r.getName()+"']");
if(e!=null)
{
if(r.getTitle()!=null)e.attribute("title").setValue(r.getTitle());
if(r.getSn()!=null)e.attribute("sn").setValue(r.getSn());
if(r.getValue()!=null)e.attribute("value").setValue(r.getValue());
if(r.getIntro()!=null)e.attribute("intro").setValue(r.getIntro());
if(r.getValidateClass()!=null) e.attribute("class").setValue(r.getValidateClass());
this.rights.put(r.getName(),r);
ret=true;
}
String fileName=XMLConfigFactory.class.getResource(configFile).getPath();
ret=ret&&write(fileName,doc);
return ret;
}
private synchronized boolean write(String fileName,Document doc)
{
boolean ret=false;
try{
// Writer writer=new FileWriter(fileName);
OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream(new File(fileName)),"utf-8");
// doc.setXMLEncoding("utf-8");
// System.out.println(doc.asXML());
doc.write(out);
// writer.close();
out.close();
ret=true;
}
catch(Exception e)
{
e.printStackTrace();
logger.error("写文件错误!"+e);
}
return ret;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("完成!");
ModuleRightsConfig config=ModuleRightsConfig.getInstance();
config.init();
//config.addRights(new SystemRights("fkkkddk","dsfd","dfsd","dfsdf","",""));
System.out.println(config.getModuleRights("/test"));
System.out.println("完成!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -