All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.linkare.zas.aspectj.utils.ZasXmlFileRequirementProcessor Maven / Gradle / Ivy
package com.linkare.zas.aspectj.utils;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.linkare.zas.LoggerSingleton;
import com.linkare.zas.api.Decider;
import com.linkare.zas.api.JoinPointKind;
import com.linkare.zas.api.UnauthorizedAccessImplementor;
import com.linkare.zas.config.Configuration;
import com.linkare.zas.metainfo.ZasBaseRequirement;
/**
* @author Paulo Zenida - Linkare TI
*
*/
public class ZasXmlFileRequirementProcessor {
private static final String YOU_HAVE_NO_ACCESS_TO_THE_POLICY_FILE_DOCUMENT = "You have no access to the policy file document.";
private static final String PROCESSING_XML_FILE = "Processing XML file...";
public static final String PROTECTED_OBJECT = "protected-object";
public static final String NAME = "name";
public static final String TYPE = "type";
private static ZasXmlFileRequirementProcessor instance = new ZasXmlFileRequirementProcessor();
private static long lastModifiedDate = Long.valueOf(0);
private static Document doc;
private static Map externalRequirementsSpecification = new LinkedHashMap();
private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
private static final String ZAS_PERMISSIONS_SCHEMA = "http://zas.cvs.sourceforge.net/*checkout*/zas/zas-revisited/zas-acr.xsd";
private static final String[] schemas = { ZAS_PERMISSIONS_SCHEMA };
private static String protectedObjectName;
private static String abstractAccessModes;
private static Class extends UnauthorizedAccessImplementor> unauthorizedAccessImplementorClass;
private static JoinPointKind protectedObjectKind;
@SuppressWarnings("unchecked")
private static List> deciderClasses = new ArrayList>();
private static boolean isAccessControlInherited;
private static boolean isForced;
private static Set setOfInvokers = new HashSet();
private static boolean isInvokersInherited;
@SuppressWarnings("unchecked")
private static List trustedClasses = new ArrayList();
private static Boolean isShallow;
/**
*
* @param fileLastModifiedDate
* The last modified date of the Z�s access control requirements XML file.
* @return Returns true if the Z�s access control requirements XML file has not been read yet or it has changed since the last time it was read. It returns
* false otherwise.
*/
private boolean isInitialTimeOrFileHasChanged(final long fileLastModifiedDate) {
return (lastModifiedDate == 0 || lastModifiedDate != fileLastModifiedDate);
}
/**
*
* @param filename
* The file name to be loaded.
* @return Returns the file that was loaded from the filename
passed in as argument.
*/
private File loadFile(final String filename) {
try {
final URL url = this.getClass().getClassLoader().getResource(Configuration.getAccessControlRequirementsFilename()) != null ? this.getClass()
.getClassLoader()
.getResource(Configuration.getAccessControlRequirementsFilename())
: this.getClass().getClassLoader().getResource(filename);
if (url.getProtocol().equals("jar")) {
throw new UnsupportedOperationException("zas-acr.in.jar.not.supported.yet");
} else {
return new File(url.toURI());
}
} catch (URISyntaxException e) {
LoggerSingleton.LOGGER.error("URI Systax when loading file " + filename, e);
} catch (Exception e) {
LoggerSingleton.LOGGER.error("It was nos possible to load the XML file " + filename, e);
}
return null;
}
/**
*
* @param filename
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws ClassNotFoundException
*/
private void loadExternalRequirementsSpecification(final String filename) throws ParserConfigurationException, SAXException, IOException,
ClassNotFoundException {
final File file = loadFile(filename);
if (file != null && file.exists()) {
if (isInitialTimeOrFileHasChanged(file.lastModified())) {
lastModifiedDate = file.lastModified();
LoggerSingleton.LOGGER.debug(PROCESSING_XML_FILE);
final BufferedReader br = new BufferedReader(new FileReader(file));
if (br == null) {
LoggerSingleton.LOGGER.error(YOU_HAVE_NO_ACCESS_TO_THE_POLICY_FILE_DOCUMENT);
} else {
final StringBuffer content = new StringBuffer("");
String line = null;
while ((line = br.readLine()) != null) {
content.append(line);
}
parseDocument(new ByteArrayInputStream(content.toString().getBytes()));
}
}
} else {
LoggerSingleton.LOGGER.debug("The xml permissions file has not been (re)loaded");
}
}
private static void parseDocument(final InputStream inputStream) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
try {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, schemas);
final DocumentBuilder documentBuilder = factory.newDocumentBuilder();
doc = documentBuilder.parse(inputStream);
Node protectedObject = doc.getDocumentElement().getFirstChild();
// all resources nodes:
while (protectedObject != null) {
if (protectedObject.getLocalName() != null && protectedObject.getLocalName().equals(PROTECTED_OBJECT)) {
if (protectedObject.getAttributes() != null) {
protectedObjectName = protectedObject.getAttributes().getNamedItem(NAME).getNodeValue();
protectedObjectKind = JoinPointKind.fromDescription(protectedObject.getAttributes().getNamedItem(TYPE).getNodeValue());
}
// process resource node elements:
final NodeList resourceChildNodes = protectedObject.getChildNodes();
for (int i = 0; i != resourceChildNodes.getLength(); i++) {
processEntryLine(resourceChildNodes.item(i));
}
if (protectedObjectName.length() > 0) {
addEntryToAccessControlRequirements(protectedObjectName, abstractAccessModes, unauthorizedAccessImplementorClass, deciderClasses,
trustedClasses, isAccessControlInherited, isForced, protectedObjectKind, setOfInvokers,
isInvokersInherited, isShallow);
}
}
protectedObject = protectedObject.getNextSibling();
}
} catch (IllegalArgumentException x) {
// Happens if the parser does not support JAXP 1.2
} catch (RuntimeException e) {
LoggerSingleton.LOGGER.error("It was not possible to process the xml file.", e);
}
}
/**
*
* @param filename
*/
public static void load(final String filename) {
try {
instance.loadExternalRequirementsSpecification(filename);
//BaseZas.registerExternalRequirements(externalRequirementsSpecification);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param node
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
private static void processEntryLine(final Node node) throws ClassNotFoundException {
if (node.getLocalName() != null) {
final ZasXmlFileRequirementElement element = ZasXmlFileRequirementElement.convertToEnum(node.getNodeName());
final String nodeValue = node.getTextContent() != null ? node.getTextContent().trim() : node.getTextContent();
switch (element) {
case ABSTRACT_ACCESS_MODES:
abstractAccessModes = nodeValue;
break;
case UNAUTHORIZED_ACCESS_IMPLEMENTOR_CLASS:
unauthorizedAccessImplementorClass = (Class extends UnauthorizedAccessImplementor>) Class.forName(nodeValue);
break;
case DECIDER_CLASS:
final Class extends Decider> deciderClass = (Class extends Decider>) Class.forName(nodeValue);
deciderClasses.add(deciderClass);
break;
case DECIDER_CLASSES: {
final NodeList protectedObjectChildNodes = node.getChildNodes();
for (int i = 0; i != protectedObjectChildNodes.getLength(); i++) {
processEntryLine(protectedObjectChildNodes.item(i));
}
}
break;
case TRUSTS: {
final NodeList protectedObjectChildNodes = node.getChildNodes();
for (int i = 0; i != protectedObjectChildNodes.getLength(); i++) {
processEntryLine(protectedObjectChildNodes.item(i));
}
}
break;
case TRUSTED_CLASS:
trustedClasses.add(Class.forName(nodeValue));
break;
case ACCESS_CONTROL_INHERITED:
isAccessControlInherited = Boolean.valueOf(nodeValue);
break;
case FORCED:
isForced = Boolean.valueOf(nodeValue);
break;
case INVOKERS: {
final NodeList protectedObjectChildNodes = node.getChildNodes();
for (int i = 0; i != protectedObjectChildNodes.getLength(); i++) {
processEntryLine(protectedObjectChildNodes.item(i));
}
}
break;
case INVOKER:
setOfInvokers.add(nodeValue);
break;
case INVOKERS_INHERITED:
isInvokersInherited = Boolean.valueOf(nodeValue);
break;
case DEPTH:
isShallow = nodeValue.equals("shallow") ? true : false;
break;
default:
break;
}
}
}
/**
*
* @param protectedObject
* @param abstractAccessMode
* @param unauthorizedAccessImplementorClass
* @param deciderClasses
* @param trustedClasses
* @param isAccessControlInherited
* @param isForced
* @param protectedObjectKind
* @param setOfInvokers
* @param isInvokersInherited
* @param isShallow
*/
@SuppressWarnings("unchecked")
private static void addEntryToAccessControlRequirements(final String protectedObject, final String abstractAccessMode,
final Class extends UnauthorizedAccessImplementor> unauthorizedAccessImplementorClass,
final List> deciderClasses, final List trustedClasses, final boolean isAccessControlInherited,
final boolean isForced, final JoinPointKind protectedObjectKind, final Set setOfInvokers, final boolean isInvokersInherited,
final Boolean isShallow) {
final ZasBaseRequirement accessControlRequirements = new ZasBaseRequirement(abstractAccessMode, unauthorizedAccessImplementorClass, deciderClasses,
trustedClasses, isAccessControlInherited, isForced, protectedObjectKind,
setOfInvokers, isInvokersInherited, isShallow);
externalRequirementsSpecification.put(protectedObject, accessControlRequirements);
resetAccessControlRequirementsValues();
}
/**
*
*
*/
@SuppressWarnings("unchecked")
private static void resetAccessControlRequirementsValues() {
protectedObjectName = "";
abstractAccessModes = "";
protectedObjectKind = null;
deciderClasses = new ArrayList>();
isAccessControlInherited = false;
isForced = false;
trustedClasses = new ArrayList();
setOfInvokers = new HashSet();
isInvokersInherited = false;
isShallow = null;
}
/**
* @return Returns the externalRequirementsSpecification.
*/
public static Map getExternalRequirementsSpecification() {
return externalRequirementsSpecification;
}
/**
* @param externalRequirementsSpecification
* The externalRequirementsSpecification to set.
*/
public static void setExternalRequirementsSpecification(Map externalRequirementsSpecification) {
ZasXmlFileRequirementProcessor.externalRequirementsSpecification = externalRequirementsSpecification;
}
/**
* @return Returns the doc.
*/
public static Document getDoc() {
return ZasXmlFileRequirementProcessor.doc;
}
public static String printRequirements() {
String result = "";
for (final String key : externalRequirementsSpecification.keySet()) {
result += "object " + key + "'s requirements:" + externalRequirementsSpecification.get(key) + "\n";
}
return result.trim();
}
}