Search in sources :

Example 6 with ExceptionListener

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;
}
Also used : IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BufferedInputStream(java.io.BufferedInputStream) TreeSet(java.util.TreeSet) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) HashSet(java.util.HashSet)

Example 7 with ExceptionListener

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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PatternSyntaxException(java.util.regex.PatternSyntaxException) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 8 with ExceptionListener

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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 9 with ExceptionListener

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URLClassLoader(java.net.URLClassLoader) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) URL(java.net.URL)

Example 10 with ExceptionListener

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;
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

ExceptionListener (java.beans.ExceptionListener)15 XMLDecoder (java.beans.XMLDecoder)12 LinkedList (java.util.LinkedList)10 XMLEncoder (java.beans.XMLEncoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 AssertionFailedError (junit.framework.AssertionFailedError)4 Test (org.junit.Test)4 Test (org.junit.jupiter.api.Test)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 TreeSet (java.util.TreeSet)2 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (io.siddhi.core.SiddhiManager)1 Event (io.siddhi.core.event.Event)1