Package org.apache.activemq.transport.amqp.joram

Source Code of org.apache.activemq.transport.amqp.joram.ActiveMQSSLAdmin

/**
* 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.activemq.transport.amqp.joram;

import java.io.File;
import java.security.SecureRandom;

import javax.jms.ConnectionFactory;
import javax.naming.NamingException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.spring.SpringSslContext;
import org.apache.activemq.transport.amqp.DefaultTrustManager;
import org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*/
public class ActiveMQSSLAdmin extends ActiveMQAdmin {

    private static final String AMQP_SSL_URI = "amqp+ssl://localhost:0";
    protected static final Logger LOG = LoggerFactory.getLogger(ActiveMQSSLAdmin.class);

    @Override
    public void startServer() throws Exception {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom());
        SSLContext.setDefault(ctx);

        // Setup SSL context...
        final File classesDir = new File(ActiveMQSSLAdmin.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        File keystore = new File(classesDir, "../../src/test/resources/keystore");
        final SpringSslContext sslContext = new SpringSslContext();
        sslContext.setKeyStore(keystore.getCanonicalPath());
        sslContext.setKeyStorePassword("password");
        sslContext.setTrustStore(keystore.getCanonicalPath());
        sslContext.setTrustStorePassword("password");
        sslContext.afterPropertiesSet();

        if (broker != null) {
            stopServer();
        }
        if (System.getProperty("basedir") == null) {
            File file = new File(".");
            System.setProperty("basedir", file.getAbsolutePath());
        }
        broker = createBroker();
        broker.setSslContext(sslContext);

        String connectorURI = getConnectorURI();
        TransportConnector connector = broker.addConnector(connectorURI);
        port = connector.getConnectUri().getPort();
        LOG.info("ssl port is {}", port);

        broker.start();
        //broker.
        //super.startServer();
    }

    @Override
    protected String getConnectorURI() {
        return AMQP_SSL_URI;
    }

    @Override
    public void createConnectionFactory(String name) {
        try {
            LOG.debug("Creating a connection factory using port {}", port);
            final ConnectionFactory factory = new ConnectionFactoryImpl("localhost", port, null, null, null, true);
            context.bind(name, factory);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
    }
}
TOP

Related Classes of org.apache.activemq.transport.amqp.joram.ActiveMQSSLAdmin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.