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;
}
}
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"));
}
}
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;
}
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);
}
}
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;
});
}
Aggregations