use of java.beans.ExceptionListener in project OpenGrok by OpenGrok.
the class Configuration method decodeObject.
@SuppressWarnings("lgtm[java/unsafe-deserialization]")
private static Configuration decodeObject(InputStream in) throws IOException {
final Object ret;
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, listener, new ConfigurationClassLoader())) {
ret = d.readObject();
}
if (!(ret instanceof Configuration)) {
throw new IOException("Not a valid config file");
}
if (!exceptions.isEmpty()) {
// see addGroup()
if (exceptions.getFirst() instanceof IOException) {
throw (IOException) exceptions.getFirst();
}
throw new IOException(exceptions.getFirst());
}
Configuration conf = ((Configuration) ret);
// Removes all non-root groups.
// This ensures that when the configuration is reloaded then the set
// contains only root groups. Subgroups are discovered again
// as follows below
conf.groups.removeIf(g -> g.getParent() != null);
// Traversing subgroups and checking for duplicates,
// effectively transforms the group tree to a structure (Set)
// supporting an iterator.
TreeSet<Group> copy = new TreeSet<>();
LinkedList<Group> stack = new LinkedList<>(conf.groups);
while (!stack.isEmpty()) {
Group group = stack.pollFirst();
stack.addAll(group.getSubgroups());
if (!copy.add(group)) {
throw new IOException(String.format("Duplicate group name '%s' in configuration.", group.getName()));
}
// populate groups where the current group in in their subtree
Group tmp = group.getParent();
while (tmp != null) {
tmp.addDescendant(group);
tmp = tmp.getParent();
}
}
conf.setGroups(copy);
/*
* Validate any defined canonicalRoot entries, and only include where
* validation succeeds.
*/
if (conf.canonicalRoots != null) {
conf.canonicalRoots = conf.canonicalRoots.stream().filter(s -> {
String problem = CanonicalRootValidator.validate(s, "canonicalRoot element");
if (problem == null) {
return true;
} else {
LOGGER.warning(problem);
return false;
}
}).collect(Collectors.toCollection(HashSet::new));
}
return conf;
}
use of java.beans.ExceptionListener in project OpenGrok by OpenGrok.
the class GroupTest method testEncodeDecode.
/**
* Test that a {@code Group} instance can be encoded and decoded without
* errors.
*/
@Test
public void testEncodeDecode() {
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(out);
enc.setExceptionListener(listener);
Group g1 = new Group();
enc.writeObject(g1);
enc.close();
// verify that the write didn'abcd fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, listener);
Group g2 = (Group) dec.readObject();
assertNotNull(g2);
dec.close();
// verify that the read didn'abcd fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
}
use of java.beans.ExceptionListener in project OpenGrok by OpenGrok.
the class ProjectTest method testEncodeDecode.
/**
* Test that a {@code Project} instance can be encoded and decoded without
* errors. Bug #3077.
*/
@Test
public void testEncodeDecode() {
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(out);
enc.setExceptionListener(listener);
Project p1 = new Project("foo");
enc.writeObject(p1);
enc.close();
// verify that the write didn't fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, listener);
Project p2 = (Project) dec.readObject();
assertNotNull(p2);
dec.close();
// verify that the read didn't fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
}
use of java.beans.ExceptionListener in project jdk8u_jdk by JetBrains.
the class Test4676532 method main.
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = { new URL(sb.toString()) };
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
use of java.beans.ExceptionListener in project OpenGrok by OpenGrok.
the class ProjectTest method testEncodeDecode.
/**
* Test that a {@code Project} instance can be encoded and decoded without
* errors. Bug #3077.
*/
@Test
public void testEncodeDecode() {
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<Exception>();
ExceptionListener listener = new ExceptionListener() {
public void exceptionThrown(Exception e) {
exceptions.addLast(e);
}
};
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(out);
enc.setExceptionListener(listener);
Project p1 = new Project("foo");
enc.writeObject(p1);
enc.close();
// verify that the write didn't fail
if (!exceptions.isEmpty()) {
AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
// Can only chain one of the exceptions. Take the first one.
afe.initCause(exceptions.getFirst());
throw afe;
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, listener);
Project p2 = (Project) dec.readObject();
assertNotNull(p2);
dec.close();
// verify that the read didn't fail
if (!exceptions.isEmpty()) {
AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
// Can only chain one of the exceptions. Take the first one.
afe.initCause(exceptions.getFirst());
throw afe;
}
}
Aggregations