Search in sources :

Example 26 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class LocalFileCodec method deserialize.

@Override
public LocalFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    URI uri = URI.create(jsonObj.get("uri").getAsString());
    long lastModified = jsonObj.get("lastModified").getAsLong();
    long size = jsonObj.get("size").getAsLong();
    boolean archive = jsonObj.get("archive").getAsBoolean();
    JsonElement pattern = jsonObj.get("pattern");
    return new DefaultLocalFile(name, uri, lastModified, size, archive, (pattern == null || pattern.isJsonNull()) ? null : pattern.getAsString());
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) DefaultLocalFile(com.continuuity.weave.internal.DefaultLocalFile) URI(java.net.URI)

Example 27 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class ResourceReportCodec method serialize.

@Override
public JsonElement serialize(ResourceReport src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("appMasterId", src.getApplicationId());
    json.add("appMasterResources", context.serialize(src.getAppMasterResources(), new TypeToken<WeaveRunResources>() {
    }.getType()));
    json.add("runnableResources", context.serialize(src.getResources(), new TypeToken<Map<String, Collection<WeaveRunResources>>>() {
    }.getType()));
    return json;
}
Also used : JsonObject(com.google.gson.JsonObject) WeaveRunResources(com.continuuity.weave.api.WeaveRunResources) Map(java.util.Map)

Example 28 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class ZKServiceDecoratorTest method testStateTransition.

@Test
public void testStateTransition() throws InterruptedException, ExecutionException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    try {
        final String namespace = Joiner.on('/').join("/weave", RunIds.generate(), "runnables", "Runner1");
        final ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClient.startAndWait();
        zkClient.create(namespace, null, CreateMode.PERSISTENT).get();
        try {
            JsonObject content = new JsonObject();
            content.addProperty("containerId", "container-123");
            content.addProperty("host", "localhost");
            RunId runId = RunIds.generate();
            final Semaphore semaphore = new Semaphore(0);
            ZKServiceDecorator service = new ZKServiceDecorator(ZKClients.namespace(zkClient, namespace), runId, Suppliers.ofInstance(content), new AbstractIdleService() {

                @Override
                protected void startUp() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to start");
                }

                @Override
                protected void shutDown() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to stop");
                }
            });
            final String runnablePath = namespace + "/" + runId.getId();
            final AtomicReference<String> stateMatch = new AtomicReference<String>("STARTING");
            watchDataChange(zkClient, runnablePath + "/state", semaphore, stateMatch);
            Assert.assertEquals(Service.State.RUNNING, service.start().get(5, TimeUnit.SECONDS));
            stateMatch.set("STOPPING");
            Assert.assertEquals(Service.State.TERMINATED, service.stop().get(5, TimeUnit.SECONDS));
        } finally {
            zkClient.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKServiceDecorator(com.continuuity.weave.internal.ZKServiceDecorator) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RunId(com.continuuity.weave.api.RunId) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 29 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class RuntimeSpecificationCodec method serialize.

@Override
public JsonElement serialize(RuntimeSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    json.add("runnable", context.serialize(src.getRunnableSpecification(), WeaveRunnableSpecification.class));
    json.add("resources", context.serialize(src.getResourceSpecification(), ResourceSpecification.class));
    json.add("files", context.serialize(src.getLocalFiles(), new TypeToken<Collection<LocalFile>>() {
    }.getType()));
    return json;
}
Also used : JsonObject(com.google.gson.JsonObject) Collection(java.util.Collection) ResourceSpecification(com.continuuity.weave.api.ResourceSpecification) WeaveRunnableSpecification(com.continuuity.weave.api.WeaveRunnableSpecification)

Example 30 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class RuntimeSpecificationCodec method deserialize.

@Override
public RuntimeSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    WeaveRunnableSpecification runnable = context.deserialize(jsonObj.get("runnable"), WeaveRunnableSpecification.class);
    ResourceSpecification resources = context.deserialize(jsonObj.get("resources"), ResourceSpecification.class);
    Collection<LocalFile> files = context.deserialize(jsonObj.get("files"), new TypeToken<Collection<LocalFile>>() {
    }.getType());
    return new DefaultRuntimeSpecification(name, runnable, resources, files);
}
Also used : LocalFile(com.continuuity.weave.api.LocalFile) DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) TypeToken(com.google.common.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) ResourceSpecification(com.continuuity.weave.api.ResourceSpecification) WeaveRunnableSpecification(com.continuuity.weave.api.WeaveRunnableSpecification)

Aggregations

JsonObject (com.google.gson.JsonObject)1124 JsonParser (com.google.gson.JsonParser)263 JsonElement (com.google.gson.JsonElement)228 JsonArray (com.google.gson.JsonArray)226 JsonPrimitive (com.google.gson.JsonPrimitive)78 HashMap (java.util.HashMap)78 Test (org.junit.Test)77 Gson (com.google.gson.Gson)70 Map (java.util.Map)66 ArrayList (java.util.ArrayList)63 Test (org.testng.annotations.Test)61 IOException (java.io.IOException)53 InputStreamReader (java.io.InputStreamReader)27 JsonParseException (com.google.gson.JsonParseException)24 File (java.io.File)21 HttpResponse (org.apache.http.HttpResponse)21 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 JsonReader (com.google.gson.stream.JsonReader)17 InputStream (java.io.InputStream)17 URL (java.net.URL)15