Java Code Examples for org.apache.logging.log4j.core.util.Throwables#rethrow()

The following examples show how to use org.apache.logging.log4j.core.util.Throwables#rethrow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    this.thread = Thread.currentThread();
    final byte[] bytes = new byte[4096];
    final DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
    try {
        while (!shutdown) {
            latch.countDown();
            sock.receive(packet);
            ++count;
            final LogEvent event = objectMapper.readValue(packet.getData(), Log4jLogEvent.class);
            queue.add(event);
        }
    } catch (final Throwable e) {
        e.printStackTrace();
        if (!shutdown) {
            Throwables.rethrow(e);
        }
    }
}
 
Example 2
Source File: SocketAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        try (final Socket socket = serverSocket.accept()) {
            if (socket != null) {
                final InputStream is = socket.getInputStream();
                while (!shutdown) {
                    final MappingIterator<LogEvent> mappingIterator = objectMapper.readerFor(Log4jLogEvent.class).readValues(is);
                    while (mappingIterator.hasNextValue()) {
                        queue.add(mappingIterator.nextValue());
                        ++count;
                    }
                }
            }
        }
    } catch (final EOFException eof) {
        // Socket is closed.
    } catch (final Exception e) {
        if (!shutdown) {
            Throwables.rethrow(e);
        }
    }
}
 
Example 3
Source File: TestClassLoader.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
    final String path = name.replace('.', '/').concat(".class");
    final URL resource = super.getResource(path);
    if (resource == null) {
        throw new ClassNotFoundException(name);
    }
    try {
        final URLConnection uc = resource.openConnection();
        final int len = uc.getContentLength();
        final InputStream in = new BufferedInputStream(uc.getInputStream());
        final byte[] bytecode = new byte[len];
        try {
            IOUtils.readFully(in, bytecode);
        } finally {
            Closer.closeSilently(in);
        }
        return defineClass(name, bytecode, 0, bytecode.length);
    } catch (final IOException e) {
        Throwables.rethrow(e);
        return null; // unreachable
    }
}
 
Example 4
Source File: MockUdpSyslogServer.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    System.out.println("Log4j UDP Server started.");
    this.thread = Thread.currentThread();
    final byte[] bytes = new byte[4096];
    final DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
    try {
        while (!shutdown) {
            socket.receive(packet);
            final String str = new String(packet.getData(), 0, packet.getLength());
            messageList.add(str);
        }
    } catch (final Exception e) {
        if (!shutdown) {
            Throwables.rethrow(e);
        }
    }
    System.out.println("Log4j UDP server stopped.");
}
 
Example 5
Source File: FastDatePrinter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Performs the formatting by applying the rules to the
 * specified calendar.</p>
 *
 * @param calendar  the calendar to format
 * @param buf  the buffer to format into
 * @param <B> the Appendable class type, usually StringBuilder or StringBuffer.
 * @return the specified string buffer
 */
private <B extends Appendable> B applyRules(final Calendar calendar, final B buf) {
    try {
        for (final Rule rule : mRules) {
            rule.appendTo(buf, calendar);
        }
    } catch (final IOException ioe) {
        Throwables.rethrow(ioe);
    }
    return buf;
}
 
Example 6
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void writeXmlConfiguration(final OutputStream output) throws IOException {
    try {
        final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output);
        writeXmlConfiguration(xmlWriter);
        xmlWriter.close();
    } catch (final XMLStreamException e) {
        if (e.getNestedException() instanceof IOException) {
            throw (IOException)e.getNestedException();
        }
        Throwables.rethrow(e);
    }
}
 
Example 7
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public String toXmlConfiguration() {
    final StringWriter sw = new StringWriter();
    try {
        final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
        writeXmlConfiguration(xmlWriter);
        xmlWriter.close();
    } catch (final XMLStreamException e) {
        Throwables.rethrow(e);
    }
    return sw.toString();
}
 
Example 8
Source File: CompositeConfigurationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void runTest(final LoggerContextRule rule, final Statement statement) {
    try {
        rule.apply(statement, Description
                .createTestDescription(getClass(), Thread.currentThread().getStackTrace()[1].getMethodName()))
                .evaluate();
    } catch (final Throwable e) {
        Throwables.rethrow(e);
    }
}
 
Example 9
Source File: FileAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    final Thread thread = Thread.currentThread();

    try {
        writer(lock, logEventCount, thread.getName(), createOnDemand, true);
    } catch (final Exception e) {
        exceptionRef[0] = e;
        Throwables.rethrow(e);
    }
}
 
Example 10
Source File: FileAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {

            if (args.length != 3) {
                System.out.println("Required arguments 'id', 'count' and 'lock' not provided");
                System.exit(-1);
            }
            final String id = args[0];

            final int count = Integer.parseInt(args[1]);

            if (count <= 0) {
                System.out.println("Invalid count value: " + args[1]);
                System.exit(-1);
            }
            final boolean lock = Boolean.parseBoolean(args[2]);

            final boolean createOnDemand = Boolean.parseBoolean(args[2]);

            // System.out.println("Got arguments " + id + ", " + count + ", " + lock);

            try {
                writer(lock, count, id, createOnDemand, true);
                // thread.sleep(50);

            } catch (final Exception e) {
                Throwables.rethrow(e);
            }

        }
 
Example 11
Source File: AbstractJdbcAppenderDataSourceTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private DataSource createMockDataSource() {
    try {
        final DataSource dataSource = mock(DataSource.class);
        given(dataSource.getConnection()).willAnswer(new Answer<Connection>() {
            @Override
            public Connection answer(final InvocationOnMock invocation) throws Throwable {
                return jdbcRule.getConnectionSource().getConnection();
            }
        });
        return dataSource;
    } catch (final SQLException e) {
        Throwables.rethrow(e);
        throw new InternalError("unreachable");
    }
}
 
Example 12
Source File: JdbcAppenderMapMessageDataSourceTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private DataSource createMockDataSource() {
    try {
        final DataSource dataSource = mock(DataSource.class);
        given(dataSource.getConnection()).willAnswer(new Answer<Connection>() {
            @Override
            public Connection answer(final InvocationOnMock invocation) throws Throwable {
                return jdbcRule.getConnectionSource().getConnection();
            }
        });
        return dataSource;
    } catch (final SQLException e) {
        Throwables.rethrow(e);
        throw new InternalError("unreachable");
    }
}