org.apache.sling.discovery.commons.providers.spi.base.DiscoveryLiteDescriptor Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.discovery.commons.providers.spi.base;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Session;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReaderFactory;
import org.apache.sling.api.resource.ResourceResolver;
/**
* Simplifies access to the underlying JSON-backed oak discovery-lite descriptor
*/
public class DiscoveryLiteDescriptor {
/** TODO: avoid hardcoding the constant here but use an Oak constant class instead if possible */
public static final String OAK_DISCOVERYLITE_CLUSTERVIEW = "oak.discoverylite.clusterview";
private static final JsonReaderFactory jsonReaderFactory;
static {
Map config = new HashMap();
config.put("org.apache.johnzon.supports-comments", true);
jsonReaderFactory = Json.createReaderFactory(config);
}
/**
* {"seq":8,"final":true,"id":"aae34e9a-b08d-409e-be10-9ff4106e5387","me":4,"active":[4],"deactivating":[],"inactive":[1,2,3]}
*/
public static DiscoveryLiteDescriptor getDescriptorFrom(ResourceResolver resourceResolver) throws Exception {
Session session = resourceResolver.adaptTo(Session.class);
if (session == null) {
throw new Exception("Could not adapt resourceResolver to session: "+resourceResolver);
}
String descriptorStr = session.getRepository().getDescriptor(DiscoveryLiteDescriptor.OAK_DISCOVERYLITE_CLUSTERVIEW);
if (descriptorStr == null) {
throw new Exception("No value available for descriptor " + OAK_DISCOVERYLITE_CLUSTERVIEW);
}
JsonObject descriptor = jsonReaderFactory.createReader(new StringReader(descriptorStr)).readObject();
return new DiscoveryLiteDescriptor(descriptor);
}
/** the actual descriptor **/
private final JsonObject descriptor;
DiscoveryLiteDescriptor(JsonObject descriptor) {
this.descriptor = descriptor;
}
/**
* Returns the 'me' field of the discovery-lite descriptor
* @return the 'me' field of the discovery-lite descriptor
* @throws Exception if anything in the descriptor is wrongly formatted
*/
public int getMyId() throws Exception {
return descriptor.getInt("me");
}
private int[] getArray(String name) throws Exception {
JsonArray deactivating = descriptor.getJsonArray(name);
int[] result = new int[deactivating.size()];
for(int i=0; i