Example usage for io.vertx.core.json JsonObject getJsonObject

List of usage examples for io.vertx.core.json JsonObject getJsonObject

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getJsonObject.

Prototype

public JsonObject getJsonObject(String key) 

Source Link

Document

Get the JsonObject value with the specified key

Usage

From source file:net.kuujo.vertigo.impl.ConfigPortTypeResolver.java

License:Apache License

@Override
public Class<?> resolve(String type) {
    JsonObject config = Configs.load();
    JsonObject ports = Args.checkNotNull(config.getJsonObject("port"));
    JsonObject types = Args.checkNotNull(ports.getJsonObject("type"));
    String typeClass = types.getString(type);
    if (typeClass == null) {
        throw new VertigoException(String.format("Invalid port type %s", type));
    }/*from   w w w  .j a  v a2  s . c om*/
    try {
        return Class.forName(typeClass);
    } catch (ClassNotFoundException e) {
        throw new VertigoException(e);
    }
}

From source file:net.kuujo.vertigo.network.impl.ComponentConfigImpl.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void update(JsonObject component) {
    if (this.name == null) {
        this.name = component.getString(COMPONENT_NAME, UUID.randomUUID().toString());
    }/*  w ww. java 2 s  .com*/
    if (this.identifier == null) {
        this.identifier = component.getString(COMPONENT_IDENTIFIER);
        if (this.identifier == null) {
            throw new NetworkFormatException("Component " + this.name + " did not specify an identifier.");
        }
    }
    if (component.containsKey(COMPONENT_CONFIG)) {
        this.config = component.getJsonObject(COMPONENT_CONFIG);
    }
    if (component.containsKey(COMPONENT_WORKER)) {
        this.worker = component.getBoolean(COMPONENT_WORKER);
    }
    if (component.containsKey(COMPONENT_MULTI_THREADED)) {
        this.multiThreaded = component.getBoolean(COMPONENT_MULTI_THREADED);
    }
    if (component.containsKey(COMPONENT_STATEFUL)) {
        this.stateful = component.getBoolean(COMPONENT_STATEFUL);
    }
    if (component.containsKey(COMPONENT_REPLICAS)) {
        this.replicas = component.getInteger(COMPONENT_REPLICAS, 1);
    }
    if (component.containsKey(COMPONENT_RESOURCES)) {
        this.resources.addAll(component.getJsonArray(COMPONENT_RESOURCES, new JsonArray()).getList());
    }
    JsonObject inputs = component.getJsonObject(COMPONENT_INPUT);
    if (inputs == null) {
        inputs = new JsonObject();
    }
    if (this.input == null) {
        this.input = new InputConfigImpl(inputs).setComponent(this);
    } else {
        this.input.update(inputs);
    }
    JsonObject outputs = component.getJsonObject(COMPONENT_OUTPUT);
    if (outputs == null) {
        outputs = new JsonObject();
    }
    if (this.output == null) {
        this.output = new OutputConfigImpl(outputs).setComponent(this);
    } else {
        this.output.update(outputs);
    }
}

From source file:net.kuujo.vertigo.network.impl.ConnectionConfigImpl.java

License:Apache License

@Override
public void update(JsonObject connection) {
    if (connection.containsKey(CONNECTION_SOURCE)) {
        if (this.source == null) {
            this.source = new SourceConfigImpl(connection.getJsonObject(CONNECTION_SOURCE));
        } else {//from w  w  w. j a v a 2s.  c om
            this.source.update(connection.getJsonObject(CONNECTION_SOURCE));
        }
    }
    if (connection.containsKey(CONNECTION_TARGET)) {
        if (this.target == null) {
            this.target = new TargetConfigImpl(connection.getJsonObject(CONNECTION_TARGET));
        } else {
            this.target.update(connection.getJsonObject(CONNECTION_TARGET));
        }
    }
    if (connection.containsKey(CONNECTION_ORDERED)) {
        this.ordered = connection.getBoolean(CONNECTION_ORDERED);
    }
    if (connection.containsKey(CONNECTION_AT_LEAST_ONCE)) {
        this.atLeastOnce = connection.getBoolean(CONNECTION_AT_LEAST_ONCE);
    }
    if (connection.containsKey(CONNECTION_SEND_TIMEOUT)) {
        this.sendTimeout = connection.getLong(CONNECTION_SEND_TIMEOUT);
    }
}

From source file:net.kuujo.vertigo.network.impl.InputConfigImpl.java

License:Apache License

@Override
public void update(JsonObject output) {
    for (String key : output.fieldNames()) {
        ports.put(key, new InputPortConfigImpl(output.getJsonObject(key)).setName(key));
    }//from  w  w w . j  ava  2  s .  c  om
}

From source file:net.kuujo.vertigo.network.impl.NetworkImpl.java

License:Apache License

@Override
public void update(JsonObject network) {
    this.name = network.getString(NETWORK_NAME);
    if (this.name == null) {
        throw new NetworkFormatException("Network name is mandatory.");
    }//from www .j  a  va2 s  . com
    JsonObject components = network.getJsonObject(NETWORK_COMPONENTS);
    if (components != null) {
        for (String name : components.fieldNames()) {
            this.components.add(
                    NetworkConfig.component(components.getJsonObject(name)).setName(name).setNetwork(this));
        }
    }
    JsonArray connections = network.getJsonArray(NETWORK_CONNECTIONS);
    if (connections != null) {
        for (Object connection : connections) {
            ConnectionConfig connectionConfig = NetworkConfig.connection((JsonObject) connection);
            SourceConfig source = connectionConfig.getSource();
            if (!source.getIsNetwork()) {
                String componentName = connectionConfig.getSource().getComponent();
                if (componentName == null) {
                    throw new NetworkFormatException("A connection source does not specify a component.");
                }
                ComponentConfig component = this.getComponent(componentName);
                if (component == null) {
                    throw new NetworkFormatException("No component with name " + componentName
                            + " was found while trying to create source vertigo connection.");
                }
                OutputConfig output = component.getOutput();
                if (output.getPort(source.getPort()) == null) {
                    output.addPort(source.getPort());
                }
            }
            TargetConfig target = connectionConfig.getTarget();
            if (!target.getIsNetwork()) {
                String componentName = connectionConfig.getTarget().getComponent();
                if (componentName == null) {
                    throw new NetworkFormatException("A connection target does not specify a component.");
                }
                ComponentConfig component = this.getComponent(componentName);
                if (component == null) {
                    throw new NetworkFormatException("No target component with name " + componentName
                            + " was found while trying to create target vertigo connection.");
                }
                InputConfig input = component.getInput();
                if (input.getPort(target.getPort()) == null) {
                    input.addPort(target.getPort());
                }
            }
            this.connections.add(connectionConfig);
        }
    }
}

From source file:net.kuujo.vertigo.network.impl.OutputConfigImpl.java

License:Apache License

@Override
public void update(JsonObject output) {
    for (String key : output.fieldNames()) {
        ports.put(key, new OutputPortConfigImpl(output.getJsonObject(key)).setName(key));
    }/*from  w  ww .j  ava2s. c o m*/
}

From source file:net.sf.sgsimulator.sgsrest.SGSVertxServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    String configFile = null;//from  w w  w. j a va2s . co m
    if (args.length == 0) {
        configFile = "config.json";
    } else {
        configFile = args[0];
    }

    Path configPath = Paths.get(configFile);
    JsonObject config = new JsonObject(new String(Files.readAllBytes(configPath)));

    Random r = new Random(new Date().getTime());
    char c1 = (char) (r.nextInt(26) + 'a');
    char c2 = (char) (r.nextInt(26) + 'a');
    char c3 = (char) (r.nextInt(26) + 'a');
    String admincode = "" + c1 + c2 + c3;
    System.getProperties().put("sgsimulator.admincode", admincode);

    Vertx vertx = Vertx.vertx();
    System.out.println("****************************");
    System.out.println("Starting...");
    System.out.println("To load pages:");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/panel");
    System.out.println("Admin interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println("http://" + config.getString("host") + ":" + config.getInteger("port")
            + "/sg/pages/admin/" + admincode);
    System.out.println("Projector screen interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/screen");

    System.out.println("****************************");

    VertxNubes nubes = new VertxNubes(vertx, config);

    // System.err.println("IP:"+ip.substring(1));

    // config.put("host", ip.substring(1));
    System.getProperties().put("sgsimulator.scenario", config.getJsonObject("gridlab").getString("scenario"));
    System.getProperties().put("sgsimulator.adminip", config.getString("adminip"));
    nubes.bootstrap(res -> {
        if (res.succeeded()) {
            final Router router = res.result();
            final HttpServer server = vertx.createHttpServer(new HttpServerOptions(config));
            server.requestHandler(router::accept);
            server.listen();
            System.out.println("****************************");
            System.out.println("Started...");
            System.out.println("****************************");
        } else {
            res.cause().printStackTrace();
        }
    });
    // nubes.stop((_void) -> {
    // vertx.close();
    // });
}

From source file:net.sf.sgsimulator.sgsrest.vertx.services.GridLabSimulatorService.java

License:Open Source License

private void onInit(Vertx vertx, JsonObject config) throws IOException {
    config = config.getJsonObject("gridlab");
    int cycleTimeInMillis = config.getInteger("cycleTimeInMillis");// 60000;
    boolean intelligence = config.getBoolean("intelligence");// true;
    int moments = config.getInteger("moments");// 30;
    Double minRange = config.getDouble("minRange", null);
    Double maxRange = config.getDouble("maxRange", null);
    millisperwebpoll = config.getDouble("millisperwebpoll", null);
    boolean activeClients = config.getBoolean("activeClients");// true;
    // TODO parametrize
    Tariff tariff = new TariffExample();

    JsonArray sdjson = config.getJsonArray("startDate");
    JsonArray edjson = config.getJsonArray("endDate");
    Calendar calendarStart = Calendar.getInstance();
    calendarStart.set(sdjson.getInteger(0), sdjson.getInteger(1), sdjson.getInteger(2), sdjson.getInteger(3),
            sdjson.getInteger(4), sdjson.getInteger(5));

    Calendar calendarEnd = Calendar.getInstance();
    calendarEnd.set(edjson.getInteger(0), edjson.getInteger(1), edjson.getInteger(2), edjson.getInteger(3),
            edjson.getInteger(4), edjson.getInteger(5));

    String configurationFile = config.getString("configurationFile");
    String gridFile = config.getString("gridFile");
    // definition
    String scenarioFile = config.getString("scenarioFile");
    String gridLabFolder = config.getString("gridLabFolder");
    PATHS paths = new PATHS(configurationFile, gridFile, scenarioFile, gridLabFolder);

    Date[] dates = readSimulationTime(paths.getNetXmlSrcFile());
    long diffInMillies = calendarEnd.getTimeInMillis() - calendarStart.getTimeInMillis();

    int hoursOfSimulation = (int) TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS);
    startDate = calendarStart.getTime();
    init(title, startDate, cycleTimeInMillis, paths, intelligence, hoursOfSimulation, tariff, moments, minRange,
            maxRange, activeClients);//from  w ww.j  av a 2  s.  co  m
}

From source file:org.eclipse.hono.deviceregistry.FileBasedRegistrationService.java

License:Open Source License

Future<Void> loadRegistrationData() {
    Future<Void> result = Future.future();

    if (getConfig().getFilename() == null) {
        result.fail(new IllegalStateException("device identity filename is not set"));
    } else {//from w w w. j a  va2  s.  c o  m

        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load device registration information from file {}", getConfig().getFilename());
        if (fs.existsBlocking(getConfig().getFilename())) {
            final AtomicInteger deviceCount = new AtomicInteger();
            fs.readFile(getConfig().getFilename(), readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        log.debug("loading devices for tenant [{}]", tenantId);
                        Map<String, JsonObject> deviceMap = new HashMap<>();
                        for (Object deviceObj : tenant.getJsonArray(ARRAY_DEVICES)) {
                            JsonObject device = (JsonObject) deviceObj;
                            String deviceId = device.getString(FIELD_DEVICE_ID);
                            log.debug("loading device [{}]", deviceId);
                            deviceMap.put(deviceId, device.getJsonObject(FIELD_DATA));
                            deviceCount.incrementAndGet();
                        }
                        identities.put(tenantId, deviceMap);
                    }
                    log.info("successfully loaded {} device identities from file [{}]", deviceCount.get(),
                            getConfig().getFilename());
                    result.complete();
                } else {
                    log.warn("could not load device identities from file [{}]", getConfig().getFilename());
                    result.fail(readAttempt.cause());
                }
            });
        } else {
            log.debug("device identity file [{}] does not exist (yet)", getConfig().getFilename());
            result.complete();
        }
    }
    return result;
}

From source file:org.eclipse.hono.service.registration.impl.FileBasedRegistrationService.java

License:Open Source License

private void loadRegistrationData() {
    if (filename != null) {
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load device registration information from file {}", filename);
        if (fs.existsBlocking(filename)) {
            final AtomicInteger deviceCount = new AtomicInteger();
            fs.readFile(filename, readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        Map<String, JsonObject> deviceMap = new HashMap<>();
                        for (Object deviceObj : tenant.getJsonArray(ARRAY_DEVICES)) {
                            JsonObject device = (JsonObject) deviceObj;
                            deviceMap.put(device.getString(FIELD_HONO_ID), device.getJsonObject(FIELD_DATA));
                            deviceCount.incrementAndGet();
                        }/*from   www . j ava2  s.c o  m*/
                        identities.put(tenantId, deviceMap);
                    }
                    log.info("successfully loaded {} device identities from file [{}]", deviceCount.get(),
                            filename);
                } else {
                    log.warn("could not load device identities from file [{}]", filename, readAttempt.cause());
                }
            });
        } else {
            log.debug("device identity file {} does not exist (yet)", filename);
        }
    }
}