Search in sources :

Example 16 with JsonReader

use of javax.json.JsonReader in project sling by apache.

the class SimpleDistributionQueueProvider method enableQueueProcessing.

public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {
    if (checkpoint) {
        // recover from checkpoints
        log.debug("recovering from checkpoints if needed");
        for (final String queueName : queueNames) {
            log.debug("recovering for queue {}", queueName);
            DistributionQueue queue = getQueue(queueName);
            FilenameFilter filenameFilter = new FilenameFilter() {

                @Override
                public boolean accept(File file, String name) {
                    return name.equals(queueName + "-checkpoint");
                }
            };
            for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
                log.info("recovering from checkpoint {}", qf);
                try {
                    LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
                    while (lineIterator.hasNext()) {
                        String s = lineIterator.nextLine();
                        String[] split = s.split(" ");
                        String id = split[0];
                        String infoString = split[1];
                        Map<String, Object> info = new HashMap<String, Object>();
                        JsonReader reader = Json.createReader(new StringReader(infoString));
                        JsonObject jsonObject = reader.readObject();
                        for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
                            if (entry.getValue().getValueType().equals(JsonValue.ValueType.ARRAY)) {
                                JsonArray value = jsonObject.getJsonArray(entry.getKey());
                                String[] a = new String[value.size()];
                                for (int i = 0; i < a.length; i++) {
                                    a[i] = value.getString(i);
                                }
                                info.put(entry.getKey(), a);
                            } else if (JsonValue.NULL.equals(entry.getValue())) {
                                info.put(entry.getKey(), null);
                            } else {
                                info.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
                            }
                        }
                        queue.add(new DistributionQueueItem(id, info));
                    }
                    log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
                } catch (FileNotFoundException e) {
                    log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
                } catch (JsonException e) {
                    log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
                }
            }
        }
        // enable checkpointing
        for (String queueName : queueNames) {
            ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false).name(getJobName(queueName + "-checkpoint"));
            scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory), options);
        }
    }
    // enable processing
    for (String queueName : queueNames) {
        ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
        scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileNotFoundException(java.io.FileNotFoundException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LineIterator(org.apache.commons.io.LineIterator) FilenameFilter(java.io.FilenameFilter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) FileReader(java.io.FileReader) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JsonValue(javax.json.JsonValue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) JsonArray(javax.json.JsonArray) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 17 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromReaderTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    JsonReader jsonReader = Json.createReader(new StringReader("{" + "  \"title\":\"The Matrix\"," + "  \"year\":1999," + "  \"cast\":[" + "    \"Keanu Reaves\"," + "    \"Laurence Fishburne\"," + "    \"Carrie-Anne Moss\"" + "  ]" + "}"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("title"));
    assertEquals("The Matrix", json.getString("title"));
    assertTrue(json.containsKey("year"));
    assertEquals(1999, json.getInt("year"));
    assertTrue(json.containsKey("cast"));
    JsonArray jsonArr = json.getJsonArray("cast");
    assertNotNull(jsonArr);
    assertEquals(3, jsonArr.size());
    JSONAssert.assertEquals("[" + "    \"Keanu Reaves\"," + "    \"Laurence Fishburne\"," + "    \"Carrie-Anne Moss\"" + "  ]", jsonArr.toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 18 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromStreamTest method testSimpleObjectWithTwoElements.

@Test
public void testSimpleObjectWithTwoElements() throws JSONException {
    JsonReader jsonReader = Json.createReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("/2.json"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("apple"));
    assertEquals("red", json.getString("apple"));
    assertTrue(json.containsKey("banana"));
    assertEquals("yellow", json.getString("banana"));
}
Also used : JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 19 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromStreamTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    JsonReader jsonReader = Json.createReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("/4.json"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("title"));
    assertEquals("The Matrix", json.getString("title"));
    assertTrue(json.containsKey("year"));
    assertEquals(1999, json.getInt("year"));
    assertTrue(json.containsKey("cast"));
    JsonArray jsonArr = json.getJsonArray("cast");
    assertNotNull(jsonArr);
    assertEquals(3, jsonArr.size());
    JSONAssert.assertEquals("[" + "    \"Keanu Reaves\"," + "    \"Laurence Fishburne\"," + "    \"Carrie-Anne Moss\"" + "  ]", jsonArr.toString(), JSONCompareMode.STRICT);
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 20 with JsonReader

use of javax.json.JsonReader in project javaee7-samples by javaee-samples.

the class JsonReaderFromStreamTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    JsonReader jsonReader = Json.createReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("/1.json"));
    JsonObject json = jsonReader.readObject();
    assertNotNull(json);
    assertTrue(json.isEmpty());
}
Also used : JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonReader (javax.json.JsonReader)30 JsonObject (javax.json.JsonObject)25 StringReader (java.io.StringReader)22 Test (org.junit.Test)18 JsonArray (javax.json.JsonArray)14 JsonString (javax.json.JsonString)12 File (java.io.File)3 JsonValue (javax.json.JsonValue)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonException (javax.json.JsonException)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)1 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)1 AttributeReference (com.torodb.core.language.AttributeReference)1 DefaultNamedToroIndex (com.torodb.core.model.DefaultNamedToroIndex)1 IndexedAttributes (com.torodb.core.model.IndexedAttributes)1