Search in sources :

Example 96 with UncheckedIOException

use of java.io.UncheckedIOException in project LogHub by fbacchella.

the class ElasticSearch method checkTemplate.

private Boolean checkTemplate(int major) {
    if (templatePath == null) {
        templatePath = getClass().getResource("/estemplate." + major + ".json");
    }
    // Lets check for a template
    Map<Object, Object> wantedtemplate;
    try {
        wantedtemplate = Stream.of(templatePath).map(i -> {
            try {
                return i.openStream();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }).map(i -> new InputStreamReader(i, CharsetUtil.UTF_8)).map(i -> {
            try {
                @SuppressWarnings("unchecked") Map<Object, Object> localtemplate = json.get().readValue(i, Map.class);
                return localtemplate;
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }).findFirst().orElseGet(() -> null);
    } catch (UncheckedIOException e) {
        logger.error("Can't load template definition: {}", e.getMessage());
        logger.catching(Level.DEBUG, e);
        return false;
    }
    if (wantedtemplate == null) {
        return false;
    }
    int wantedVersion = wantedtemplate.toString().hashCode();
    wantedtemplate.put("version", wantedVersion);
    Function<JsonNode, Boolean> checkTemplate = node -> {
        try {
            Map<?, ?> foundTemplate = json.get().treeToValue(node, Map.class);
            Map<?, ?> templateMap = (Map<?, ?>) foundTemplate.get(templateName);
            Optional<Integer> opt = Optional.ofNullable((Integer) templateMap.get("version"));
            return opt.map(i -> i != wantedVersion).orElseGet(() -> true);
        } catch (JsonProcessingException e) {
            throw new UncheckedIOException(e);
        }
    };
    Boolean needsrefresh = doquery(null, "/_template/" + templateName, checkTemplate, Collections.singletonMap(404, node -> true), null);
    if (needsrefresh == null) {
        return false;
    } else if (needsrefresh) {
        HttpRequest puttemplate = new HttpRequest();
        puttemplate.setVerb("PUT");
        try {
            String jsonbody = json.get().writeValueAsString(wantedtemplate);
            puttemplate.setTypeAndContent("application/json", CharsetUtil.UTF_8, jsonbody.getBytes(CharsetUtil.UTF_8));
        } catch (IOException e) {
            logger.fatal("Can't build buffer: {}", e);
            return false;
        }
        return doquery(puttemplate, "/_template/" + templateName, node -> true, Collections.emptyMap(), false);
    } else {
        return true;
    }
}
Also used : URL(java.net.URL) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SimpleDateFormat(java.text.SimpleDateFormat) Level(org.apache.logging.log4j.Level) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Map(java.util.Map) CharsetUtil(io.netty.util.CharsetUtil) JsonNode(com.fasterxml.jackson.databind.JsonNode) DateFormat(java.text.DateFormat) MalformedURLException(java.net.MalformedURLException) TimeZone(java.util.TimeZone) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InputStreamReader(java.io.InputStreamReader) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Paths(java.nio.file.Paths) Optional(java.util.Optional) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Collections(java.util.Collections) Event(loghub.Event) Properties(loghub.configuration.Properties) InputStreamReader(java.io.InputStreamReader) Optional(java.util.Optional) UncheckedIOException(java.io.UncheckedIOException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 97 with UncheckedIOException

use of java.io.UncheckedIOException in project providence by morimekta.

the class MessageStreamsTest method testFileCollector.

@Test
public void testFileCollector() throws IOException {
    File file = tmp.newFile("tmp");
    Collector<CompactFields, OutputStream, Integer> collector = MessageCollectors.toFile(file, new PrettySerializer().config());
    OutputStream out = mock(OutputStream.class);
    doThrow(new IOException("oops")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
    doThrow(new IOException("close")).when(out).close();
    assertThat(collector.combiner().apply(out, out), is(sameInstance(out)));
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to write to tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("oops"));
    }
    try {
        collector.finisher().apply(out);
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to close tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("close"));
    }
    Serializer ms = mock(Serializer.class);
    doThrow(new SerializerException("oops2")).when(ms).serialize(out, list.get(0));
    collector = MessageCollectors.toFile(file, ms);
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Bad data"));
        assertThat(e.getCause(), is(instanceOf(SerializerException.class)));
        assertThat(e.getCause().getMessage(), is("oops2"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 98 with UncheckedIOException

use of java.io.UncheckedIOException in project providence by morimekta.

the class ConstProvider method get.

@Override
public Object get() {
    if (parsedValue == null) {
        ConstParser parser = new ConstParser(registry, programContext, startLineNo, startLinePos);
        @SuppressWarnings("unchecked") PDescriptor type = registry.getProvider(typeName, programContext, Collections.EMPTY_MAP).descriptor();
        try (ByteArrayInputStream in = new ByteArrayInputStream(constantString.getBytes(StandardCharsets.UTF_8))) {
            parsedValue = parser.parse(in, type);
        } catch (IOException e) {
            throw new UncheckedIOException(e.getMessage(), e);
        }
    }
    return parsedValue;
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ConstParser(net.morimekta.providence.reflect.parser.internal.ConstParser) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 99 with UncheckedIOException

use of java.io.UncheckedIOException in project providence by morimekta.

the class SocketServer method process.

@SuppressWarnings("unchecked")
private void process(long startTime, Socket socket) {
    try (Socket ignore = socket;
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
        IOMessageReader reader = new IOMessageReader(in, serializer);
        IOMessageWriter writer = new IOMessageWriter(out, serializer)) {
        while (socket.isConnected()) {
            AtomicReference<PServiceCall> callRef = new AtomicReference<>();
            AtomicReference<PServiceCall> responseRef = new AtomicReference<>();
            try {
                DefaultProcessorHandler handler = new DefaultProcessorHandler(new WrappedProcessor(processor, (c, p) -> {
                    callRef.set(c);
                    responseRef.set(p.handleCall(c));
                    return responseRef.get();
                }));
                handler.process(reader, writer);
                out.flush();
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onComplete(duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
            } catch (IOException e) {
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
                throw new UncheckedIOException(e.getMessage(), e);
            }
            in.mark(1);
            if (in.read() < 0) {
                return;
            }
            in.reset();
            startTime = System.nanoTime();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e.getMessage(), e);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Socket(java.net.Socket) BufferedInputStream(java.io.BufferedInputStream) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) LoggerFactory(org.slf4j.LoggerFactory) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedOutputStream(java.io.BufferedOutputStream) ServerSocket(java.net.ServerSocket) PProcessor(net.morimekta.providence.PProcessor) SocketTimeoutException(java.net.SocketTimeoutException) NS_IN_MILLIS(net.morimekta.providence.util.ServiceCallInstrumentation.NS_IN_MILLIS) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) ThreadFactory(java.util.concurrent.ThreadFactory) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Serializer(net.morimekta.providence.serializer.Serializer) Logger(org.slf4j.Logger) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) ServiceCallInstrumentation(net.morimekta.providence.util.ServiceCallInstrumentation) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) PServiceCall(net.morimekta.providence.PServiceCall) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedInputStream(java.io.BufferedInputStream) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) PServiceCall(net.morimekta.providence.PServiceCall) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 100 with UncheckedIOException

use of java.io.UncheckedIOException in project providence by morimekta.

the class DirectoryMessageListStore method putAll.

@Override
@Nonnull
public Map<K, List<M>> putAll(@Nonnull Map<K, List<M>> values) {
    return mutex.lockForWriting(() -> {
        Map<K, List<M>> out = new HashMap<>();
        values.forEach((key, value) -> {
            try {
                value = ImmutableList.copyOf(value);
                write(key, value);
                cache.put(key, value);
                keyset.add(key);
            } catch (IOException e) {
                throw new UncheckedIOException(e.getMessage(), e);
            }
        });
        return out;
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Nonnull(javax.annotation.Nonnull)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)826 IOException (java.io.IOException)786 File (java.io.File)109 Path (java.nio.file.Path)106 ArrayList (java.util.ArrayList)70 InputStream (java.io.InputStream)58 Map (java.util.Map)58 List (java.util.List)55 HashMap (java.util.HashMap)44 Test (org.junit.Test)38 Files (java.nio.file.Files)37 Collectors (java.util.stream.Collectors)37 Stream (java.util.stream.Stream)31 URL (java.net.URL)29 StringWriter (java.io.StringWriter)27 CursorContext (org.neo4j.io.pagecache.context.CursorContext)25 FileInputStream (java.io.FileInputStream)23 HashSet (java.util.HashSet)23 PageCursor (org.neo4j.io.pagecache.PageCursor)22 Optional (java.util.Optional)21