com.cloudbees.clickstack.vertx.VertxConfigurationBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudbees.clickstack.vertx.VertxConfigurationBuilder.java

Source

/*
 * Copyright 2010-2013, CloudBees Inc.
 *
 * Licensed 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 com.cloudbees.clickstack.vertx;

import com.cloudbees.clickstack.domain.metadata.Database;
import com.cloudbees.clickstack.domain.metadata.Email;
import com.cloudbees.clickstack.domain.metadata.Metadata;
import com.cloudbees.clickstack.domain.metadata.Resource;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

/**
 * @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
 */
public class VertxConfigurationBuilder {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public void fillVertxModuleConfiguration(Path vertxHome, Metadata metadata) throws IOException {
        Path jsonConfPath = vertxHome.resolve("conf/configuration.json");
        Preconditions.checkState(Files.exists(jsonConfPath), "Expected conf file file %s does not exist",
                jsonConfPath);
        ObjectMapper mapper = new ObjectMapper();
        JsonNodeFactory nodeFactory = mapper.getNodeFactory();
        ObjectNode conf = (ObjectNode) mapper.readValue(jsonConfPath.toFile(), JsonNode.class);

        new VertxConfigurationBuilder().fillVertxModuleConfiguration(metadata, nodeFactory, conf);

        mapper.writerWithDefaultPrettyPrinter().writeValue(jsonConfPath.toFile(), conf);
    }

    public void fillVertxModuleConfiguration(Metadata metadata, JsonNodeFactory nodeFactory, ObjectNode conf) {
        for (Resource resource : metadata.getResources().values()) {
            if (resource instanceof Database) {
                Database database = (Database) resource;

                ObjectNode dbNode = nodeFactory.objectNode();
                conf.put("database." + database.getName(), dbNode);

                // see https://github.com/vert-x/mod-mysql-postgresql
                DatabaseUrl dbUrl = new DatabaseUrl(database.getUrl(), database.getUsername(),
                        database.getPassword());
                dbNode.put("address", "vertx.database." + database.getName());

                // for https://github.com/vert-x/mod-mysql-postgresql
                dbNode.put("connection", dbUrl.connection);
                dbNode.put("host", dbUrl.host);
                if (dbUrl.port != null)
                    dbNode.put("port", dbUrl.port);
                dbNode.put("username", dbUrl.username);
                dbNode.put("password", dbUrl.password);
                dbNode.put("database", dbUrl.database);

                // fields for https://github.com/timyates/mod-jdbc-persistor
                dbNode.put("driver", database.getJavaDriver());
                dbNode.put("url", "jdbc:" + database.getUrl());

                dbNode.put("maxpool", 20);

                for (Map.Entry<String, String> entry : database.getProperties().entrySet()) {
                    dbNode.put(entry.getKey(), entry.getValue());
                }

            } else if (resource instanceof Email) {

                Email email = (Email) resource;
                ObjectNode mailerNode = nodeFactory.objectNode();

                // https://github.com/vert-x/mod-mailer
                mailerNode.put("address", "vertx.mailer");
                conf.put("mailer." + email.getName(), mailerNode);
                mailerNode.put("host", email.getHost());
                if (email.getUsername() != null) {
                    mailerNode.put("auth", true);
                    mailerNode.put("username", email.getUsername());
                    mailerNode.put("password", email.getPassword());
                }
                for (Map.Entry<String, String> entry : email.getProperties().entrySet()) {
                    mailerNode.put(entry.getKey(), entry.getValue());
                }
            } else {
                logger.warn("Silently ignore resource {}", resource);
            }
        }
    }

}