【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:AclImpl.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package jaoso.framework.security.impl;
import jaoso.framework.security.Acl;
import jaoso.framework.security.ProtectedResource;
import jaoso.framework.util.MyUtils;
import jaoso.framework.util.XMLUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* parse XML
*
* @author edgeloner
*/
public class AclImpl implements Acl {
//~ Static fields/initializers =============================================
/** DOCUMENT ME! */
private static Map protectedResourcesMap;
/** DOCUMENT ME! */
private static Collection protectedResources = new ArrayList();
/** DOCUMENT ME! */
private static String fileName;
/** log */
private static Log log = LogFactory.getLog(AclImpl.class);
//~ Constructors ===========================================================
/**
* Constructor, read protected Resource infomation from acl-config.xml, and
* save them to a hashtable
*/
public AclImpl() {
String path = AclImpl.class.getResource(
"/log4j.properties")
.getPath();
path = path.substring(1, path.indexOf("classes"));
//for
linux or unix
if (!path.substring(0, 1)
.equals("/")
&& !path.substring(1, 2)
.equals(":")) {
path = "/" + path;
}
fileName = path + "jaas/acl-config.xml";
log.info("set acl config file : " + fileName);
init();
}
//~ Methods ================================================================
/**
* Check if a uri is a protected Resource
* @param uri url
* @return boolean - if this uri is a protected Resource
*/
public final boolean isProtectedResource(final String uri) {
if (MyUtils.isBlank(uri)) {
return false;
}
return protectedResourcesMap.containsKey(uri);
}
/**
* get ProtectedResources collection
*
* @return ProtectedResources collection
*/
public final Collection getProtectedResources() {
return protectedResources;
}
/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
*/
public static final void main(final String[] args) {
AclImpl acl = new AclImpl();
for (Iterator it = protectedResources.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
/**
* @param map ProtectedResources Map
*/
public final void setProtectedResourcesMap(final Map map) {
protectedResourcesMap = map;
protectedResources = (Collection) map.get("protectedResources");
XMLUtils.write(protectedResources, fileName);
}
/**
* check group has right for the url
* @param url url
* @param group group id
* @return boolean
*/
public final boolean hasRight(final String url, final String group) {
log.info("hasRight: " + url + group);
if (MyUtils.isBlank(url) || MyUtils.isBlank(group)) {
return false;
}
Map groups = (Map) protectedResourcesMap.get(url);
if (groups != null) {
return groups.containsKey(group);
} else {
return false;
}
}
/**
* init
*/
private void init() {
int i;
File xmlFile = new File(fileName);
Document doc = null;
String box = null;
String url = null;
String desc = null;
String property;
String value;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// validate
xml file by parser
// that is: use a dtd file to validate the xml file
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();
NodeList nodes = root.getChildNodes();
for (i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeName()
.equals("#text")) {
continue;
}
if (node.getNodeName()
.equals(ProtectedResource.PROPERTY_PROTECTED_RESOURCE)) {
ProtectedResource pr = parseNode(node);
protectedResources.add(pr);
}
}
} catch (ParserConfigurationException e) {
log.error("parse acl-config.xml: " + e);
} catch (IOException e) {
log.error("parse acl-config.xml: " + e);
} catch (SAXException e) {
log.error("parse acl-config.xml: " + e);
}
}
/**
* Read uri and desc from a protected-resource node
*
* @param protectedResourceNode node of protected resourc