Example usage for io.vertx.core.json JsonArray JsonArray

List of usage examples for io.vertx.core.json JsonArray JsonArray

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Create an empty instance

Usage

From source file:com.deblox.releaseboard.ReleaseBoardVerticle.java

License:Apache License

public void saveState() {
    logger.info("saving state");
    JsonObject db = new JsonObject();
    JsonArray releases = new JsonArray();

    Iterator<Map.Entry<String, JsonObject>> iter = releasesData.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, JsonObject> entry = iter.next();
        JsonObject rel = new JsonObject();
        rel.put("id", entry.getKey());
        rel.put("data", entry.getValue());
        releases.add(rel);//w  w  w . j a  v  a2  s.c o m
    }

    db.put("releases", releases);

    vertx.fileSystem().exists(stateFile, te -> {
        if (te.succeeded()) {
            if (te.result().booleanValue()) {
                vertx.fileSystem().deleteBlocking(stateFile);
                vertx.fileSystem().createFileBlocking(stateFile);
            } else {
                vertx.fileSystem().createFileBlocking(stateFile);
            }

        } else {
            logger.warn("unable to check if file exists: " + stateFile);
        }

        vertx.fileSystem().open(stateFile, new OpenOptions().setCreate(true).setWrite(true), r -> {
            if (r.succeeded()) {
                AsyncFile file = r.result();
                file.write(Buffer.buffer(db.toString()));
                file.close();
            } else {
                logger.warn(r.cause());
            }
        });

    });

}

From source file:com.emikra.vertx.oak.OakServiceVertxProxyHandler.java

License:Apache License

private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) {
    return res -> {
        if (res.failed()) {
            msg.fail(-1, res.cause().getMessage());
        } else {/* w w w  .  jav  a  2s .c o  m*/
            JsonArray arr = new JsonArray();
            for (Character chr : res.result()) {
                arr.add((int) chr);
            }
            msg.reply(arr);
        }
    };
}

From source file:com.emikra.vertx.oak.OakServiceVertxProxyHandler.java

License:Apache License

private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
    return res -> {
        if (res.failed()) {
            msg.fail(-1, res.cause().getMessage());
        } else {/*from ww w.j  a  va  2s .  c o  m*/
            JsonArray arr = new JsonArray();
            for (Character chr : res.result()) {
                arr.add((int) chr);
            }
            msg.reply(arr);
        }
    };
}

From source file:com.englishtown.vertx.guice.GuiceVerticleLoader.java

License:Open Source License

private Verticle createRealVerticle(Class<?> clazz) throws Exception {

    JsonObject config = context.config();
    Object field = config.getValue(CONFIG_BOOTSTRAP_BINDER_NAME);
    JsonArray bootstrapNames;//from w ww.  j  a  v a2  s  .c om
    List<Module> bootstraps = new ArrayList<>();

    if (field instanceof JsonArray) {
        bootstrapNames = (JsonArray) field;
    } else {
        bootstrapNames = new JsonArray().add((field == null ? BOOTSTRAP_BINDER_NAME : field));
    }

    for (int i = 0; i < bootstrapNames.size(); i++) {
        String bootstrapName = bootstrapNames.getString(i);
        try {
            Class bootstrapClass = classLoader.loadClass(bootstrapName);
            Object obj = bootstrapClass.newInstance();

            if (obj instanceof Module) {
                bootstraps.add((Module) obj);
            } else {
                logger.error("Class " + bootstrapName + " does not implement Module.");
            }
        } catch (ClassNotFoundException e) {
            logger.error("Guice bootstrap binder class " + bootstrapName
                    + " was not found.  Are you missing injection bindings?");
        }
    }

    // Add vert.x binder
    bootstraps.add(new GuiceVertxBinder(vertx));

    // Each verticle factory will have it's own injector instance
    Injector injector = Guice.createInjector(bootstraps);
    return (Verticle) injector.getInstance(clazz);
}

From source file:com.englishtown.vertx.hk2.HK2VerticleLoader.java

License:Open Source License

private Verticle createRealVerticle(Class<?> clazz) throws Exception {

    JsonObject config = config();/*from   w w w. j  a va 2  s.  c o m*/
    Object field = config.getValue(CONFIG_BOOTSTRAP_BINDER_NAME);
    JsonArray bootstrapNames;
    List<Binder> bootstraps = new ArrayList<>();

    if (field instanceof JsonArray) {
        bootstrapNames = (JsonArray) field;
    } else {
        bootstrapNames = new JsonArray().add((field == null ? BOOTSTRAP_BINDER_NAME : field));
    }

    for (int i = 0; i < bootstrapNames.size(); i++) {
        String bootstrapName = bootstrapNames.getString(i);
        try {
            Class bootstrapClass = classLoader.loadClass(bootstrapName);
            Object obj = bootstrapClass.newInstance();

            if (obj instanceof Binder) {
                bootstraps.add((Binder) obj);
            } else {
                logger.error("Class " + bootstrapName + " does not implement Binder.");
            }
        } catch (ClassNotFoundException e) {
            logger.error("HK2 bootstrap binder class " + bootstrapName
                    + " was not found.  Are you missing injection bindings?");
        }
    }

    // Each verticle factory will have it's own service locator instance
    // Passing a null name will not cache the locator in the factory
    locator = ServiceLocatorFactory.getInstance().create(null, parent);

    bootstraps.add(0, new HK2VertxBinder(vertx));
    ServiceLocatorUtilities.bind(locator, bootstraps.toArray(new Binder[bootstraps.size()]));

    return (Verticle) locator.createAndInitialize(clazz);
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {/*from   w w w . ja va  2  s  .c  o m*/
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                jrs.put("data", r.result().getRows().stream().map(Driver::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Optional<Driver>> getOne(String id) {
    Future<Optional<Driver>> result = Future.future();
    client.getConnection(connHandler(result, connection -> {
        connection.queryWithParams(SQL_QUERY, new JsonArray().add(id), r -> {
            if (r.failed()) {
                result.fail(r.cause());/* w  w w . j  ava2  s .com*/
            } else {
                List<JsonObject> list = r.result().getRows();
                if (list == null || list.isEmpty()) {
                    result.complete(Optional.empty());
                } else {
                    result.complete(Optional.of(new Driver(list.get(0))));
                }
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Optional<Driver>> insert(JsonObject json) {
    Future<Optional<Driver>> result = Future.future();
    JsonArray params = new JsonArray();
    //username,email,phone,driverlicense,memo,status
    params.add(json.getString("username"));
    params.add(json.getString("email"));
    params.add(json.getString("phone"));
    params.add(json.getString("driverlicense"));
    params.add(json.getString("memo"));
    params.add(json.getString("status"));
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(SQL_INSERT, params, r -> {
            if (r.failed()) {
                result.fail(r.cause());/*from   w w w  .j  ava2 s.com*/
            } else {
                UpdateResult urs = r.result();
                params.clear();
                params.add(urs.getKeys().getInteger(0));
                log.info(urs.getKeys().getInteger(0));
                connection.queryWithParams(SQL_QUERY, params, rs -> {
                    if (rs.failed()) {
                        result.fail(rs.cause());
                        log.info(rs.cause());
                    } else {
                        List<JsonObject> list = rs.result().getRows();
                        if (list == null || list.isEmpty()) {
                            result.complete(Optional.empty());
                        } else {
                            result.complete(Optional.of(new Driver(list.get(0))));
                        }
                    }
                });
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Boolean> update(JsonObject params) {
    Future<Boolean> result = Future.future();
    int id = params.getInteger("id");
    JsonArray setparams = new JsonArray();
    StringBuffer sqlSet = new StringBuffer();
    params.forEach(r -> {//from w  ww. j  a  va 2  s  .  c  om
        if (!r.getKey().equals("id")) {
            sqlSet.append(r.getKey()).append("=?,");
            setparams.add(r.getValue());
        }
    });
    if (sqlSet.length() > 0) {
        sqlSet.deleteCharAt(sqlSet.length() - 1);
    } else {
        sqlSet.append("status=abs(status-1)");
    }
    sqlSet.append(" where id = ?");
    setparams.add(id);
    String sql = SQL_UPDATE + sqlSet.toString();
    log.info(sql);
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(sql, setparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                result.complete(true);
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Boolean> delete(String id) {
    return deleteProcess(SQL_DELETE, new JsonArray().add(id));
}