Merge pull request #5 from jdhrh/target_1_8

target java 1.8
This commit is contained in:
Vincent Batts 2020-02-04 13:05:39 -05:00 committed by GitHub
commit c0b2a5e0dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 132 additions and 183 deletions

2
.gitignore vendored
View file

@ -8,3 +8,5 @@ target/
.project
.classpath
.settings/
.idea/
*.iml

71
pom.xml
View file

@ -3,24 +3,20 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.redhat.trie</groupId>
<artifactId>PathPacker</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
<packaging>bundle</packaging>
<prerequisites>
<maven>3.0</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.64</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
@ -28,51 +24,44 @@
<version>2.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
<!--
log4j's metadata pulls in broken and useless deps:
http://stackoverflow.com/questions/2310633/maven-dependency-log4j-error
-->
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.0</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<tags>
<tag>
@ -117,7 +106,7 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<version>4.2.1</version>
<extensions>true</extensions>
<configuration>
<instructions>

View file

@ -146,26 +146,22 @@ public class HuffNode {
* get a String of the bits, that map to Object need
*/
private String getBitPath(HuffNode trie, Object need) {
HuffNode left = trie.getLeft();
HuffNode right = trie.getRight();
if (left != null && left.getValue() != null) {
if (need.equals(left.getValue())) {
return "0";
}
HuffNode leftNode = trie.getLeft();
HuffNode rightNode = trie.getRight();
if (leftNode != null && leftNode.getValue() != null && need.equals(leftNode.getValue())) {
return "0";
}
if (right != null && right.getValue() != null) {
if (need.equals(right.getValue())) {
return "1";
}
if (rightNode != null && rightNode.getValue() != null && need.equals(rightNode.getValue())) {
return "1";
}
if (left != null) {
String leftPath = getBitPath(left, need);
if (leftNode != null) {
String leftPath = getBitPath(leftNode, need);
if (leftPath.length() > 0) {
return "0" + leftPath;
}
}
if (right != null) {
String rightPath = getBitPath(right, need);
if (rightNode != null) {
String rightPath = getBitPath(rightNode, need);
if (rightPath.length() > 0) {
return "1" + rightPath;
}

View file

@ -56,7 +56,7 @@ public class NodePair implements Comparable {
* @return <tt>true</tt> if it has at least one connection and <tt>false</tt> otherwise.
*/
public boolean hasNoChildren() {
return getConnection().getChildren().size() == 0;
return getConnection().getChildren().isEmpty();
}
}

View file

@ -16,12 +16,12 @@
package com.redhat.trie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import org.apache.log4j.Logger;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* PathNode is the relationship to an item in the path tree.
@ -31,7 +31,7 @@ import org.apache.log4j.Logger;
* The Name of a given PathNode, is inferred by the NodePair that regards this PathNode as its "connection"
*/
public class PathNode {
private static org.apache.log4j.Logger log = Logger.getLogger(PathTree.class);
private static Logger log = Logger.getLogger(PathTree.class.getCanonicalName());
private long id = 0;
private List<NodePair> children = new ArrayList<NodePair>();
private List<PathNode> parents = new ArrayList<PathNode>();
@ -161,14 +161,11 @@ public class PathNode {
* Traverse up the tree, and get the highest ancestor PathNode, for node.
*/
public PathNode getStartNode(PathNode node) {
if (node.getParents().size() == 0) {
if (node.getParents().isEmpty()) {
return node; // this is the end!
} else {
return node.getStartNode(node.getParents().get(0));
}
for (PathNode parent : node.getParents()) {
return node.getStartNode(parent);
}
return node; // when in doubt, return yourself
}
/**
@ -182,13 +179,11 @@ public class PathNode {
* Traverse down the tree, and get the "endMarker" child, for node.
*/
public PathNode getEndNode(PathNode node) {
if (node.getChildren().size() == 0) {
if (node.getChildren().isEmpty()) {
return node; // this is the end!
} else {
return node.getEndNode(node.getChildren().get(0).getConnection());
}
for (NodePair child : node.getChildren()) {
return node.getEndNode(child.getConnection());
}
return node; // when in doubt, return yourself
}
/**
@ -233,7 +228,7 @@ public class PathNode {
public boolean includes(PathNode that) {
// if we are at the end of the tree we're checking against,
// then it includes everything up to this point.
if (this.getChildren().size() == 0 || that.getChildren().size() == 0) {
if (this.getChildren().isEmpty() || that.getChildren().isEmpty()) {
return true;
}
@ -248,8 +243,8 @@ public class PathNode {
thisnp.getName().startsWith("$") ||
thisnp.getName().equals(thatnp.getName()) ) {
result = thisnp.getConnection().includes(thatnp.getConnection());
found.add(new Boolean(result).booleanValue());
log.debug("includes: this: " + thisnp.getName() + " == that:" + thatnp.getName());
found.add(result);
log.log(Level.FINE, () -> "includes: this: " + thisnp.getName() + " == that:" + thatnp.getName());
}
}
}
@ -262,12 +257,11 @@ public class PathNode {
* pretty information
*/
public String toString() {
String parentList = "";
StringBuilder parentList = new StringBuilder();
for (PathNode parent : parents) {
parentList += ": " + parent.getId();
parentList.append(": " + parent.getId());
}
parentList += "";
return "ID: " + id + ", Name: " + this.getName() + ", Parents" + parentList + ", Children: " + children;
return "ID: " + id + ", Name: " + this.getName() + ", Parents" + parentList.toString() + ", Children: " + children;
}
}

View file

@ -15,26 +15,23 @@
package com.redhat.trie;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.DataFormatException;
import org.apache.log4j.Logger;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
/**
* PathTree
@ -42,10 +39,10 @@ import org.apache.log4j.Logger;
* An efficient means by which to check the content sets.
*/
public class PathTree {
private static org.apache.log4j.Logger log = Logger.getLogger(PathTree.class);
private static Logger log = Logger.getLogger(PathTree.class.getCanonicalName());
private List<HuffNode> nodeDictionary;
private List<HuffNode> pathDictionary;
private StringBuffer nodeBits;
private StringBuilder nodeBits;
private byte[] payload;
@ -95,11 +92,7 @@ public class PathTree {
* Constructor using the list of content sets.
*/
public PathTree(List<String> contentSets) throws PayloadException {
try {
setContentSets(contentSets);
} catch (PayloadException ex) {
throw ex;
}
setContentSets(contentSets);
}
/**
@ -150,13 +143,13 @@ public class PathTree {
/**
* the buffer of significant bits, with regard to how many nodes there are.
*
* @return StringBuffer of
* @return StringBuilder of
*/
private StringBuffer getNodeBits() {
private StringBuilder getNodeBits() {
return this.nodeBits;
}
private void setNodeBits(StringBuffer nodeBits) {
private void setNodeBits(StringBuilder nodeBits) {
this.nodeBits = nodeBits;
}
@ -211,11 +204,11 @@ public class PathTree {
}
if (this.modified || this.pathDictionary == null || this.nodeDictionary == null) {
this.nodeDictionary = new ArrayList<HuffNode>();
this.setNodeBits(new StringBuffer());
this.setNodeBits(new StringBuilder());
ByteArrayInputStream bais = new ByteArrayInputStream(this.getPayload(),
(new Long(this.getDictOffset())).intValue(),
(new Long(this.getPayload().length - this.getDictOffset()).intValue()));
((int)(this.getDictOffset())),
((int) (this.getPayload().length - this.getDictOffset())));
int value = bais.read();
// check for size bits
this.setNodeCount(value);
@ -261,11 +254,7 @@ public class PathTree {
* @throws PayloadException if the newly read PathNode dictionary can not be read from the payload
*/
private HuffNode getPathTrie() throws PayloadException {
try {
return makeTrie(getPathDictionary());
} catch (PayloadException ex) {
throw ex;
}
return makeTrie(getPathDictionary());
}
/**
@ -275,11 +264,7 @@ public class PathTree {
* @throws PayloadException if the newly read Node name dictionary can not be read from the payload
*/
private HuffNode getNodeTrie() throws PayloadException {
try {
return makeTrie(getNodeDictionary());
} catch (PayloadException ex) {
throw ex;
}
return makeTrie(getNodeDictionary());
}
/**
@ -287,17 +272,12 @@ public class PathTree {
*/
public PathNode getRootPathNode() throws PayloadException {
// populate the PathNodes so we can rebuild the cool url tree
Set<PathNode> pathNodes;
try {
pathNodes = populatePathNodes(getNodeDictionary(),
Set<PathNode> pathNodes = populatePathNodes(getNodeDictionary(),
getPathTrie(), getNodeTrie(), getNodeBits());
} catch (PayloadException ex) {
throw ex;
}
// find the root, he has no parents
PathNode root = null;
for (PathNode pn : pathNodes) {
if (pn.getParents().size() == 0) {
if (pn.getParents().isEmpty()) {
root = pn;
break;
}
@ -316,7 +296,7 @@ public class PathTree {
try {
rootPathNode = getRootPathNode();
} catch(PayloadException pe) {
log.error(pe);
log.log(Level.SEVERE,"Payload Exception", pe);
return false;
}
return test(contentPath, rootPathNode);
@ -336,7 +316,7 @@ public class PathTree {
/* Request is of the form "/content/rc/rhel/7/..."
* Grab the next element.
*/
log.debug("test(" + request + ")");
log.log(Level.FINE, () -> "test(" + request + ")");
StringTokenizer tokenizer = new StringTokenizer(request, PATH_DELIMITER);
if(tokenizer.countTokens() == 0) {
return false;
@ -344,12 +324,11 @@ public class PathTree {
String currentToken = tokenizer.nextToken();
for(NodePair nodePair: tree.getChildren()) {
String nodePairName = nodePair.getName();
log.debug("Current token: [" + currentToken + "] =??= NodePair name: [" + nodePairName + "]");
log.log(Level.FINE, () -> "Current token: [" + currentToken + "] =??= NodePair name: [" + nodePairName + "]");
if(currentToken.equals(nodePairName) || nodePairName.startsWith(CONTENT_PATH_VARIABLE_PREFIX)) {
if(nodePair.hasNoChildren()) {
return true;
} else {
String s = PATH_DELIMITER + currentToken;
boolean retval = test(request.substring(currentToken.length()+1), nodePair.getConnection());
if(retval) {
return true;
@ -377,7 +356,7 @@ public class PathTree {
PathNode treeRoot = PathTree.makePathTree(contentSets, new PathNode());
List<String> nodeStrings = orderStrings(treeRoot);
if (nodeStrings.size() == 0) {
if (nodeStrings.isEmpty()) {
this.payload = new byte[0];
return;
}
@ -429,14 +408,14 @@ public class PathTree {
private List<String> byteArrayToStringList(byte[] ba) {
List<String> strings = new ArrayList<String>();
String str = "";
StringBuilder str = new StringBuilder();
for (byte b : ba) {
if (b == '\0') {
strings.add(str);
str = "";
strings.add(str.toString());
str.setLength(0);
} else {
str += (char) b;
str.append((char) b);
}
}
return strings;
@ -475,9 +454,9 @@ public class PathTree {
* @return the Set of weighted PathNode
*/
private Set<PathNode> populatePathNodes(List<HuffNode> thisNodeDictionary,
HuffNode pathTrie, HuffNode nodeTrie, StringBuffer theseNodeBits) {
HuffNode pathTrie, HuffNode nodeTrie, StringBuilder theseNodeBits) {
Set<PathNode> pathNodes = new HashSet<PathNode>();
StringBuffer myNodeBits = new StringBuffer(theseNodeBits.toString());
StringBuilder myNodeBits = new StringBuilder(theseNodeBits.toString());
for (HuffNode node : thisNodeDictionary) {
pathNodes.add((PathNode) node.getValue());
boolean stillNode = true;
@ -485,7 +464,7 @@ public class PathTree {
// get first child name
// if its HuffNode.END_NODE we are done
String nameValue = null;
StringBuffer nameBits = new StringBuffer();
StringBuilder nameBits = new StringBuilder();
while (nameValue == null && stillNode) {
nameBits.append(myNodeBits.charAt(0));
myNodeBits.deleteCharAt(0);
@ -503,7 +482,7 @@ public class PathTree {
}
PathNode nodeValue = null;
StringBuffer pathBits = new StringBuffer();
StringBuilder pathBits = new StringBuilder();
while (nodeValue == null && stillNode) {
pathBits.append(myNodeBits.charAt(0));
myNodeBits.deleteCharAt(0);
@ -530,7 +509,7 @@ public class PathTree {
*/
public List<String> toList() {
List<String> urls = new ArrayList<String>();
StringBuffer aPath = new StringBuffer();
StringBuilder aPath = new StringBuilder();
try {
makeURLs(getRootPathNode(), urls, aPath);
} catch (PayloadException ex) {
@ -539,12 +518,12 @@ public class PathTree {
return urls;
}
private void makeURLs(PathNode root, List<String> urls, StringBuffer aPath) {
if (root.getChildren().size() == 0) {
private void makeURLs(PathNode root, List<String> urls, StringBuilder aPath) {
if (root.getChildren().isEmpty()) {
urls.add(aPath.toString());
}
for (NodePair child : root.getChildren()) {
StringBuffer childPath = new StringBuffer(aPath.substring(0));
StringBuilder childPath = new StringBuilder(aPath.substring(0));
childPath.append("/");
childPath.append(child.getName());
makeURLs(child.getConnection(), urls, childPath);
@ -572,9 +551,8 @@ public class PathTree {
private HuffNode mergeNodes(HuffNode node1, HuffNode node2) {
HuffNode left = node1;
HuffNode right = node2;
HuffNode parent = new HuffNode(getHuffNodeContext(),
return new HuffNode(getHuffNodeContext(),
null, left.getWeight() + right.getWeight(), left, right);
return parent;
}
private List<HuffNode> getStringNodeList(List<String> pathStrings) {
@ -603,13 +581,13 @@ public class PathTree {
* @return deflated byte array
*/
private byte[] byteProcess(List<String> entries)
throws IOException, UnsupportedEncodingException {
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos,
new Deflater(Deflater.BEST_COMPRESSION));
for (String segment : entries) {
dos.write(segment.getBytes("UTF-8"));
dos.write("\0".getBytes("UTF-8"));
dos.write(segment.getBytes(StandardCharsets.UTF_8));
dos.write("\0".getBytes(StandardCharsets.UTF_8));
}
dos.finish();
dos.close();
@ -622,11 +600,11 @@ public class PathTree {
Map<String, Integer> segments = new HashMap<String, Integer>();
Set<PathNode> nodes = new HashSet<PathNode>();
buildSegments(segments, nodes, parent);
for (String part : segments.keySet()) {
if (!part.equals("")) {
int count = segments.get(part);
if (parts.size() == 0) {
parts.add(part);
for (Map.Entry<String, Integer> part : segments.entrySet()) {
if (!part.getKey().equals("")) {
int count = part.getValue();
if (parts.isEmpty()) {
parts.add(part.getKey());
}
else {
int pos = parts.size();
@ -636,7 +614,7 @@ public class PathTree {
break;
}
}
parts.add(pos, part);
parts.add(pos, part.getKey());
}
}
}
@ -654,10 +632,7 @@ public class PathTree {
boolean start = false;
/* TODO ??? */
for (byte b : toByteArray(nodeSize)) {
if (b == 0 && !start) {
continue;
}
else {
if (!(b == 0 && !start)) {
countBaos.write(b);
start = true;
}
@ -673,7 +648,7 @@ public class PathTree {
else {
baos.write(nodeSize);
}
StringBuffer bits = new StringBuffer();
StringBuilder bits = new StringBuilder();
String endNodeLocationBitPath = stringParent.getBitPath(HuffNode.END_NODE);
for (PathNode pn : pathNodes) {
for (NodePair np : pn.getChildren()) {
@ -726,7 +701,7 @@ public class PathTree {
Set<PathNode> nodes = treeRoot.getAllNodes();
for (PathNode pn : nodes) {
int count = pn.getParents().size();
if (nodes.size() == 0) {
if (nodes.isEmpty()) {
nodes.add(pn);
}
else {
@ -786,7 +761,7 @@ public class PathTree {
for (NodePair np : parent.getChildren()) {
Integer count = segments.get(np.getName());
if (count == null) {
count = new Integer(0);
count = 0;
}
segments.put(np.getName(), ++count);
buildSegments(segments, nodes, np.getConnection());
@ -814,7 +789,6 @@ public class PathTree {
// "equivalent" parents are merged
List<PathNode> parentResult = new ArrayList<PathNode>();
parentResult.addAll(location.getParents());
//log.debug(location);
for (PathNode parent1 : location.getParents()) {
if (!parentResult.contains(parent1)) {
continue;

View file

@ -15,8 +15,6 @@
package com.redhat.trie;
import java.lang.Exception;
public class PayloadException extends Exception {
}

View file

@ -1,35 +1,33 @@
package com.redhat.trie;
import java.io.InputStreamReader;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.apache.log4j.Logger;
import static org.junit.Assert.fail;
/**
* This class is just to provide helpers for the other tests
*/
public class TestHelpers {
private static org.apache.log4j.Logger log = Logger.getLogger(TestHelpers.class);
private static Logger log = Logger.getLogger(TestHelpers.class.getCanonicalName());
/**
* junit requires at least one runnable test
*/
@Test
public void testDeadVicker() {
assertNotNull(new String("What's its diocese?"));
assertNotNull("What's its diocese?");
}
// Helpers
@ -98,7 +96,7 @@ public class TestHelpers {
br = new BufferedReader(new InputStreamReader(in));
} catch(NullPointerException npe) {
// can happen in the case of an empty content set list
log.warn(">>>>>>> Empty content set <<<<<");
log.fine(">>>>>>> Empty content set <<<<<");
return contentList;
}

View file

@ -1,19 +1,17 @@
package com.redhat.trie;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Tests of whether a URL should be granted access based on a given content set