com.crosstreelabs.cognitio.communicator.amqp.provider.BrokerProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.crosstreelabs.cognitio.communicator.amqp.provider.BrokerProvider.java

Source

/*
 * Copyright 2015 Crosstree Labs.
 *
 * 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.crosstreelabs.cognitio.communicator.amqp.provider;

import com.crosstreelabs.cognitio.communicator.amqp.config.AMQPConfiguration;
import java.io.File;
import java.io.FileOutputStream;
import javax.inject.Provider;
import org.apache.commons.io.IOUtils;
import org.apache.qpid.server.Broker;
import org.apache.qpid.server.BrokerOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BrokerProvider implements Provider<Broker> {
    private static final Logger LOGGER = LoggerFactory.getLogger(BrokerProvider.class);
    private final AMQPConfiguration config;

    public BrokerProvider(final AMQPConfiguration config) {
        this.config = config;
    }

    @Override
    public Broker get() {
        if (config.amqLocalBroker || config.amqGeneralConnection == null) {
            try {
                return buildBroker();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return null;
    }

    protected Broker buildBroker() throws Exception {
        Broker broker = new Broker();
        BrokerOptions options = new BrokerOptions();
        options.setManagementMode(false);

        File workDir = getWorkDir();
        File configFile = new File(workDir, "config.json");
        File passwdFile = new File(workDir, "passwd");
        workDir.mkdirs();

        if (!configFile.exists() || config.amqCleanWorkDir) {
            configFile.createNewFile();
            IOUtils.copy(BrokerProvider.class.getResourceAsStream("/qpid-config.json"),
                    new FileOutputStream(configFile));
        }
        if (!passwdFile.exists() || config.amqCleanWorkDir) {
            passwdFile.createNewFile();
            IOUtils.copy(BrokerProvider.class.getResourceAsStream("/qpid-passwd"),
                    new FileOutputStream(passwdFile));
        }

        options.setConfigProperty("qpid.work_dir", workDir.getAbsolutePath());
        options.setConfigProperty("qpid.amqp_port", "" + config.amqPort);
        options.setConfigProperty("qpid.http_port", "" + config.amqHttpPort);
        options.setConfigProperty("qpid.home_dir", workDir.getAbsolutePath());
        options.setInitialConfigurationLocation(configFile.getAbsolutePath());

        broker.startup(options);
        LOGGER.info("Broker started");
        return broker;
    }

    private File getWorkDir() {
        if (config.amqWorkDir != null) {
            return config.amqWorkDir;
        }
        return new File("target/amq_work");
    }
}