Skip to content

Commit

Permalink
Change xpath parser to use Document and not reread the file each time…
Browse files Browse the repository at this point in the history
…. For 1000 evalutes saw time drop from 9s to 7s. Not great, but it's an improvement.
  • Loading branch information
noahcampbell authored and gschueler committed Nov 19, 2010
1 parent a275674 commit ccc480b
Show file tree
Hide file tree
Showing 6 changed files with 730 additions and 18 deletions.
28 changes: 28 additions & 0 deletions core/.classpath
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/java"/>
<classpathentry kind="src" path="src/test"/>
<classpathentry kind="lib" path="target/rundeck-core-1.0.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/ant-1.8.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/ant-jsch-1.8.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/ant-launcher-1.8.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-beanutils-1.8.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-cli-1.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-codec-1.3.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-httpclient-3.0.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-lang-2.4.jar"/>
<classpathentry kind="lib" path="target/tools/lib/commons-logging-1.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/jaxen-1.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/jruby-1.3.1.jar"/>
<classpathentry kind="lib" path="target/tools/lib/jsch-0.1.42.jar"/>
<classpathentry kind="lib" path="target/tools/lib/jython-2.5.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="target/tools/lib/rundeck-core-1.0.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/xerces-2.6.0.jar"/>
<classpathentry kind="lib" path="target/tools/lib/xml-apis-2.6.0.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
17 changes: 17 additions & 0 deletions core/.project
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>core</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
331 changes: 331 additions & 0 deletions core/junit-1591694651.properties

Large diffs are not rendered by default.

326 changes: 326 additions & 0 deletions core/junit860154675.properties

Large diffs are not rendered by default.

Expand Up @@ -45,6 +45,7 @@
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -69,6 +70,7 @@ public class Policies {
private static final String NS_LDAP = "http://dtolabs.com/rundeck/ldap";
private final XPath xpath = XPathFactory.newInstance().newXPath();
private final List<File> policyFiles = new ArrayList<File>();
private final List<Document> aclpolicies = new ArrayList<Document>();

public Policies() {
xpath.setNamespaceContext(new NamespaceContext() {
Expand All @@ -95,7 +97,7 @@ public void add(File file) throws SAXException, IOException, ParserConfiguration
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
builder.parse(file);
aclpolicies.add(builder.parse(file));

this.policyFiles.add(file);
}
Expand All @@ -106,15 +108,13 @@ public void remove(File file) {

public int count() {
int count = 0;
for(File f : policyFiles) {
for(Document f : aclpolicies) {
try {
Double n = (Double)xpath.evaluate("count(//policy)", new InputSource(new FileReader(f)), XPathConstants.NUMBER);
Double n = (Double)xpath.evaluate("count(//policy)", f, XPathConstants.NUMBER);
count += n;
} catch (XPathExpressionException e) {
// TODO squash
} catch (FileNotFoundException e) {
// TODO squash
}
}
}
return count;
}
Expand Down Expand Up @@ -148,9 +148,9 @@ public boolean accept(File dir, String name) {
public List<Context> narrowContext(Subject subject, Set<Attribute> environment) {

List<Context> matchedContexts = new ArrayList<Context>();
for(File f : policyFiles) {
for(Document f : aclpolicies) {
try {
NodeList policiesToEvaluate = (NodeList)xpath.evaluate("//policy", new InputSource(new FileReader(f)), XPathConstants.NODESET);
NodeList policiesToEvaluate = (NodeList)xpath.evaluate("//policy", f, XPathConstants.NODESET);
for(int i = 0; i < policiesToEvaluate.getLength(); i++) {

Node policy = policiesToEvaluate.item(i);
Expand Down Expand Up @@ -233,10 +233,7 @@ public List<Context> narrowContext(Subject subject, Set<Attribute> environment)
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return matchedContexts;
}
Expand Down Expand Up @@ -472,11 +469,11 @@ public String toString() {
@Deprecated
public List<String> listAllRoles() {
List<String> results = new ArrayList<String>();
for(File f: policyFiles) {
for(Document f: aclpolicies) {
try {

NodeList groups = (NodeList) xpath.evaluate("//by/group/@ldap:name | //by/group/@name",
new InputSource(new FileReader(f)), XPathConstants.NODESET);
f, XPathConstants.NODESET);

for(int i = 0; i < groups.getLength(); i++) {

Expand All @@ -487,10 +484,7 @@ public List<String> listAllRoles() {
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return results;
Expand Down
Expand Up @@ -196,6 +196,22 @@ public void testActionAuthorization() throws Exception {
"execute", null);
assertFalse("foobar/moduleName was granted authorization when it shouldn't.", decision.isAuthorized());

Set<Map<String,String>> resources = new HashSet<Map<String,String>>();
final int resourcesCount = 100;
final int actionsCount = 10;
for(int i = 0; i < resourcesCount; i++) {
resources.add(declareScript(Integer.toString(i), "big/test/" + Integer.toString(i)));
}
Set<String> actions = new HashSet<String>();
for(int i = 0; i < actionsCount; i++) {
actions.add("Action" + Integer.toString(i));
}
long start = System.currentTimeMillis();
authorization.evaluate(resources, subject, actions, environment);
long end = System.currentTimeMillis() - start;
System.out.println("Took " + end + "ms for " + resourcesCount + " resources and " + actionsCount + " actions.");


}

public void off_testProjectEnvironment() throws Exception {
Expand Down

0 comments on commit ccc480b

Please sign in to comment.