use of javax.json.JsonArray 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);
}
}
use of javax.json.JsonArray in project sling by apache.
the class DistributionUtils method getQueues.
public static Map<String, Map<String, Object>> getQueues(SlingInstance instance, String agentName) throws IOException, JsonException {
Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
JsonObject json = getResource(instance, queueUrl(agentName) + ".infinity");
JsonArray items = json.getJsonArray("items");
for (int i = 0; i < items.size(); i++) {
String queueName = items.getString(i);
Map<String, Object> queueProperties = new HashMap<String, Object>();
JsonObject queue = json.getJsonObject(queueName);
queueProperties.put("empty", queue.getBoolean("empty"));
queueProperties.put("itemsCount", queue.getInt("itemsCount"));
result.put(queueName, queueProperties);
}
return result;
}
use of javax.json.JsonArray in project sling by apache.
the class JsonRenderer method prettyPrint.
/**
* Make a prettyprinted JSON text of this JSONObject.
* <p>
* Warning: This method assumes that the data structure is acyclical.
* @return a printable, displayable, transmittable
* representation of the object, beginning
* with <code>{</code> <small>(left brace)</small> and ending
* with <code>}</code> <small>(right brace)</small>.
* @throws IllegalArgumentException If the object contains an invalid number.
*/
public String prettyPrint(JsonObject jo, Options opt) {
int n = jo.size();
if (n == 0) {
return "{}";
}
final JsonArrayBuilder children = Json.createArrayBuilder();
Iterator<String> keys = jo.keySet().iterator();
StringBuilder sb = new StringBuilder("{");
int newindent = opt.initialIndent + opt.indent;
String o;
if (n == 1) {
o = keys.next();
final Object v = jo.get(o);
if (!skipChildObject(children, opt, o, v)) {
sb.append(quote(o));
sb.append(": ");
sb.append(valueToString(v, opt));
}
} else {
while (keys.hasNext()) {
o = keys.next();
final Object v = jo.get(o);
if (skipChildObject(children, opt, o, v)) {
continue;
}
if (sb.length() > 1) {
sb.append(",\n");
} else {
sb.append('\n');
}
indent(sb, newindent);
sb.append(quote(o.toString()));
sb.append(": ");
sb.append(valueToString(v, options().withIndent(opt.indent).withInitialIndent(newindent)));
}
if (sb.length() > 1) {
sb.append('\n');
indent(sb, newindent);
}
}
/** Render children if any were skipped (in "children in arrays" mode) */
JsonArray childrenArray = children.build();
if (childrenArray.size() > 0) {
if (sb.length() > 1) {
sb.append(",\n");
} else {
sb.append('\n');
}
final Options childOpt = new Options(opt);
childOpt.withInitialIndent(childOpt.initialIndent + newindent);
indent(sb, childOpt.initialIndent);
sb.append(quote(opt.childrenKey)).append(":");
sb.append(prettyPrint(childrenArray, childOpt));
}
sb.append('}');
return sb.toString();
}
use of javax.json.JsonArray in project sling by apache.
the class JsonResponseTest method testOnChange.
public void testOnChange() throws Exception {
res.onChange("modified", "argument1", "argument2");
Object prop = res.getProperty("changes");
JsonArray changes = assertInstanceOf(prop, JsonArray.class);
assertEquals(1, changes.size());
Object obj = changes.getJsonObject(0);
JsonObject change = assertInstanceOf(obj, JsonObject.class);
assertProperty(change, JSONResponse.PROP_TYPE, "modified");
JsonArray arguments = change.getJsonArray(JSONResponse.PROP_ARGUMENT);
assertEquals(2, arguments.size());
}
use of javax.json.JsonArray in project sling by apache.
the class FsMountHelper method getCurrentConfigurations.
/**
* Return all file provider configs for this project
* @param targetUrl The targetUrl of the webconsole
* @return A map (may be empty) with the pids as keys and the configurations as values
* @throws MojoExecutionException
*/
public Map<String, FsResourceConfiguration> getCurrentConfigurations(final String targetUrl) throws MojoExecutionException {
log.debug("Getting current file provider configurations.");
final Map<String, FsResourceConfiguration> result = new HashMap<>();
final String getUrl = targetUrl + "/configMgr/(service.factoryPid=" + FS_FACTORY + ").json";
final GetMethod get = new GetMethod(getUrl);
try {
final int status = httpClient.executeMethod(get);
if (status == 200) {
String contentType = get.getResponseHeader(HEADER_CONTENT_TYPE).getValue();
int pos = contentType.indexOf(';');
if (pos != -1) {
contentType = contentType.substring(0, pos);
}
if (!JsonSupport.JSON_MIME_TYPE.equals(contentType)) {
log.debug("Response type from web console is not JSON, but " + contentType);
throw new MojoExecutionException("The Apache Felix Web Console is too old to mount " + "the initial content through file system provider configs. " + "Either upgrade the web console or disable this feature.");
}
final String jsonText;
try (InputStream jsonResponse = get.getResponseBodyAsStream()) {
jsonText = IOUtils.toString(jsonResponse, CharEncoding.UTF_8);
}
try {
JsonArray array = JsonSupport.parseArray(jsonText);
for (int i = 0; i < array.size(); i++) {
final JsonObject obj = array.getJsonObject(i);
final String pid = obj.getString("pid");
final JsonObject properties = obj.getJsonObject("properties");
final String fsmode = getConfigPropertyValue(properties, PROPERTY_FSMODE);
final String path = getConfigPropertyValue(properties, PROPERTY_PATH);
final String initialContentImportOptions = getConfigPropertyValue(properties, PROPERTY_INITIAL_CONTENT_IMPORT_OPTIONS);
final String fileVaultFilterXml = getConfigPropertyValue(properties, PROPERTY_FILEVAULT_FILTER_XML);
String root = getConfigPropertyValue(properties, PROPERTY_ROOTS);
if (root == null) {
root = getConfigPropertyValue(properties, PROPERTY_ROOT);
}
if (path != null && path.startsWith(this.project.getBasedir().getAbsolutePath()) && root != null) {
FsResourceConfiguration cfg = new FsResourceConfiguration().fsMode(fsmode).providerRootPath(path).contentRootDir(root).initialContentImportOptions(initialContentImportOptions).fileVaultFilterXml(fileVaultFilterXml);
log.debug("Found configuration with pid: " + pid + ", " + cfg);
result.put(pid, cfg);
}
}
} catch (JsonException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
}
}
} catch (HttpException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
} catch (IOException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
} finally {
get.releaseConnection();
}
return result;
}
Aggregations