summaryrefslogtreecommitdiff
path: root/java/broker-plugins/management-jmx/src/main/java/org/apache/qpid/server/jmx/QpidSslRMIServerSocketFactory.java
blob: b0f5abd1a3d6f91b9efbfa384db9fe46ad2ef4c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 *
 * 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.qpid.server.jmx;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class QpidSslRMIServerSocketFactory extends SslRMIServerSocketFactory
{
    private final SSLContext _sslContext;

    /**
     * SslRMIServerSocketFactory which creates the ServerSocket using the
     * supplied SSLContext rather than the system default context normally
     * used by the superclass, allowing us to use a configuration-specified
     * key store.
     *
     * @param sslContext previously created sslContext using the desired key store.
     * @throws NullPointerException if the provided {@link SSLContext} is null.
     */
    public QpidSslRMIServerSocketFactory(SSLContext sslContext) throws NullPointerException
    {
        super();

        if(sslContext == null)
        {
            throw new NullPointerException("The provided SSLContext must not be null");
        }

        _sslContext = sslContext;

        //TODO: settings + implementation for SSL client auth, updating equals and hashCode appropriately.
    }

    @Override
    public ServerSocket createServerSocket(int port) throws IOException
    {
        final SSLSocketFactory factory = _sslContext.getSocketFactory();

        ServerSocket serverSocket = new ServerSocket()
        {
            public Socket accept() throws IOException
            {
                Socket socket = super.accept();

                SSLSocket sslSocket =
                        (SSLSocket) factory.createSocket(socket,
                                                         socket.getInetAddress().getHostName(),
                                                         socket.getPort(),
                                                         true);
                sslSocket.setUseClientMode(false);

                return sslSocket;
            }
        };
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(port));
        return serverSocket;
    }

    /**
     * One QpidSslRMIServerSocketFactory is equal to
     * another if their (non-null) SSLContext are equal.
     */
    @Override
    public boolean equals(Object object)
    {
        if (!(object instanceof QpidSslRMIServerSocketFactory))
        {
            return false;
        }

        QpidSslRMIServerSocketFactory that = (QpidSslRMIServerSocketFactory) object;

        return _sslContext.equals(that._sslContext);
    }

    @Override
    public int hashCode()
    {
        return _sslContext.hashCode();
    }

}