Search in sources :

Example 6 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class ResourceConfigSupplier method loadInternal.

private static <Message extends PMessage<Message, Field>, Field extends PField> Message loadInternal(String resourceName, PMessageDescriptor<Message, Field> descriptor) throws ProvidenceConfigException {
    int lastDot = resourceName.lastIndexOf(".");
    if (lastDot < 1) {
        throw new ProvidenceConfigException("No file ending, or no resource file name: " + resourceName);
    }
    int lastSlash = resourceName.lastIndexOf("/");
    String fileName = resourceName;
    if (lastSlash >= 0) {
        fileName = resourceName.substring(lastSlash + 1);
    }
    String suffix = resourceName.substring(lastDot).toLowerCase(Locale.US);
    Serializer serializer;
    switch(suffix) {
        case ".jsn":
        case ".json":
            serializer = new JsonSerializer();
            break;
        case ".cfg":
        case ".cnf":
        case ".config":
        case ".pvd":
        case ".providence":
            serializer = new PrettySerializer().config();
            break;
        // TODO: Add YAML serializer to the file options. Could be a wrapper around SnakeYAML.
        default:
            throw new ProvidenceConfigException(String.format(Locale.US, "Unrecognized resource config type: %s (%s)", suffix, resourceName));
    }
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        in = ResourceConfigSupplier.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new ProvidenceConfigException("No such config resource: " + resourceName);
        }
    }
    try {
        try (InputStream bin = new BufferedInputStream(in)) {
            return serializer.deserialize(bin, descriptor);
        } catch (TokenizerException te) {
            throw new ProvidenceConfigException(te);
        } catch (JsonSerializerException se) {
            throw new ProvidenceConfigException(se);
        } catch (IOException e) {
            throw new ProvidenceConfigException(e, "Unknown serializer exception: " + e.getMessage());
        }
    } catch (ProvidenceConfigException pce) {
        pce.setFile(fileName);
        throw pce;
    }
}
Also used : JsonSerializerException(net.morimekta.providence.serializer.JsonSerializerException) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer)

Example 7 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class NonblockingSocketClientHandlerTest method testSimpleRequest_cannotConnect.

@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure {
    Serializer serializer = new BinarySerializer();
    InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
    try (NonblockingSocketClientHandler handler = new NonblockingSocketClientHandler(serializer, address)) {
        MyService.Iface client = new MyService.Client(handler);
        try {
            client.test(Request.builder().setText("test").build());
            fail("no exception");
        } catch (ConnectException e) {
        // The exception message is entirely localized, so it's impossible to reliably match against.
        // assertThat(e.getMessage(), startsWith("Connection refused"));
        }
        verifyZeroInteractions(impl);
    }
}
Also used : Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) InetSocketAddress(java.net.InetSocketAddress) MyService(net.morimekta.test.providence.thrift.service.MyService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 8 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class SocketClientHandlerTest method testSimpleRequest_cannotConnect.

@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure, InterruptedException {
    Serializer serializer = new BinarySerializer();
    InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
    MyService.Iface client = new MyService.Client(new SocketClientHandler(serializer, address));
    try {
        client.test(new Request(null));
        fail("no exception");
    } catch (ConnectException e) {
        assertThat(e.getMessage(), containsString("Connection refused"));
    }
    Thread.sleep(10L);
    verifyZeroInteractions(impl);
}
Also used : Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) InetSocketAddress(java.net.InetSocketAddress) MyService(net.morimekta.test.providence.thrift.service.MyService) Request(net.morimekta.test.providence.thrift.service.Request) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 9 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class FormatUtils method getInput.

public static <Message extends PMessage<Message, Field>, Field extends PField> Stream<Message> getInput(@Nonnull PMessageDescriptor<Message, Field> descriptor, @Nonnull ConvertStream in, boolean strict) throws IOException {
    Serializer serializer = in.format.createSerializer(strict);
    InputStream is = getInputStream(in);
    // Ensures it's closed at the end.
    return StreamSupport.stream(new MessageSpliterator<>(is, serializer, descriptor, is), false);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Serializer(net.morimekta.providence.serializer.Serializer)

Example 10 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class MessageStreamsTest method testSpliterator.

@Test
public void testSpliterator() throws IOException {
    InputStream in = mock(InputStream.class);
    Serializer serializer = mock(Serializer.class);
    Consumer<CompactFields> consumer = mock(Consumer.class);
    MessageSpliterator<CompactFields, CompactFields._Field> spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
    assertThat(spliterator.trySplit(), is(nullValue()));
    assertThat(spliterator.estimateSize(), is(Long.MAX_VALUE));
    assertThat(spliterator.getExactSizeIfKnown(), is(-1L));
    assertThat(spliterator.characteristics(), is(Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE));
    assertThat(spliterator.getComparator(), is(notNullValue()));
    CompactFields cf = CompactFields.builder().build();
    // -- read
    doReturn(cf).when(serializer).deserialize(in, CompactFields.kDescriptor);
    assertThat(spliterator.tryAdvance(consumer), is(true));
    verify(serializer).deserialize(in, CompactFields.kDescriptor);
    verify(in).markSupported();
    verify(consumer).accept(same(cf));
    reset(in, serializer, consumer);
    doReturn(false).when(serializer).binaryProtocol();
    doReturn(-1).when(in).read();
    assertThat(spliterator.tryAdvance(consumer), is(false));
    verify(in).markSupported();
    verifyNoMoreInteractions(in);
    verifyZeroInteractions(consumer);
    // fails.
    reset(in, serializer, consumer);
    spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
    doReturn(true).when(serializer).binaryProtocol();
    doReturn(true).when(in).markSupported();
    doThrow(new IOException("read")).when(in).read();
    doThrow(new IOException("close")).when(in).close();
    try {
        spliterator.tryAdvance(consumer);
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("read"));
        assertThat(e.getCause(), is(notNullValue()));
        assertThat(e.getCause().getSuppressed().length, is(1));
        assertThat(e.getCause().getSuppressed()[0].getMessage(), is("close"));
    }
    verify(in).markSupported();
    verify(in).mark(2);
    verify(in).read();
    verify(in).close();
    verifyNoMoreInteractions(in);
    verifyZeroInteractions(serializer, consumer);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) 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)

Aggregations

Serializer (net.morimekta.providence.serializer.Serializer)13 IOException (java.io.IOException)7 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 UncheckedIOException (java.io.UncheckedIOException)4 InetSocketAddress (java.net.InetSocketAddress)4 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)4 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 ConnectException (java.net.ConnectException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 MediaType (com.google.common.net.MediaType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OutputStream (java.io.OutputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nonnull (javax.annotation.Nonnull)2 ServiceCallInstrumentation (net.morimekta.providence.util.ServiceCallInstrumentation)2