org.coos.messaging.transport.SecureTCPTransportManager Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging.transport;
import java.io.FileInputStream;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.security.KeyStore;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.net.ServerSocketFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import org.coos.messaging.LinkManager;
import org.coos.messaging.Transport;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
/**
*
* @author sverre A manager for secure transport
*/
public class SecureTCPTransportManager extends TCPTransportManager {
private static final Log logger = LogFactory.getLog(TCPTransportManager.class.getName());
// variables to point to keystore and its authentication.
String keystore = "serverkeys";
char[] keystorepass; // = "hellothere".toCharArray();
char[] keypassword; // = "hiagain".toCharArray();
private int listenPort;
private SSLServerSocket serverSocket;
private Set transports = Collections.synchronizedSet(new HashSet());
private boolean running;
Thread thread;
boolean stopping = false;
/**
* Empty constructor provided for dynamic classloading
* */
public SecureTCPTransportManager() {
}
/**
* constructor
*
* @param listnerPort
* - Integer
indicating the portnumber for the
* listner socket.
* @param linkManager
* - LinkManager
*
* */
public SecureTCPTransportManager(int listenPort, LinkManager linkManager) {
this.listenPort = listenPort;
setLinkManager(linkManager);
}
/**
* Creates a serversocket, and configure it with SSL attributes
*
* @return ServerSocket
configured with SSL
* */
public SSLServerSocket getServer() throws Exception {
// fetch the values to load the keystore from the configuration
keystore = ((String) properties.get("keystore"));
keystorepass = ((String) properties.get("keystorepass")).toCharArray();
keypassword = ((String) properties.get("keypassword")).toCharArray();
// Fetch the keystore
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), keystorepass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypassword);
// set the context to SSL 3
SSLContext sslcontext = SSLContext.getInstance("SSLv3");
sslcontext.init(kmf.getKeyManagers(), null, null);
// create the socket
ServerSocketFactory ssf = sslcontext.getServerSocketFactory();
SSLServerSocket serversocket = (SSLServerSocket) ssf.createServerSocket(listenPort);
// set whether or not the client's certificate is required
serversocket.setNeedClientAuth(properties.get("needClientAuth").equals("true")); //
return serversocket;
}
/**
* provides the listnersockets portnumber
*
* @return Integer
indicating the listnersockets portnumber
* */
public int getListenPort() {
return listenPort;
}
/**
*
* Starts the secureTCPTransportManager. I.E. creates the SSL listner
* socket, and starts listening
*
* */
public void start() throws Exception {
if (properties.get(PROPERTY_LISTEN_PORT) != null) {
listenPort = Integer.valueOf((String) properties.get(PROPERTY_LISTEN_PORT));
}
// initialize the socket
serverSocket = getServer();
listenPort = serverSocket.getLocalPort();
logger.info(" Listening on port " + listenPort);
serverSocket.setSoTimeout(500);
// set the running flag
running = true;
// create the thread that executes the server
thread = new Thread(new Runnable() {
public void run() {
try {
while (running) {
try {
// listen for client queries
SSLSocket client = (SSLSocket) serverSocket.accept();
// create a new transport on another port
SecureTCPTransport transport = new SecureTCPTransport(client, SecureTCPTransportManager.this);
initializeChannel(transport);
// start the transport
transport.start();
// add the transport to the open connections
synchronized (transports) {
transports.add(transport);
}
} catch (SocketTimeoutException e) {
if (!running) {
if ((serverSocket != null) && !serverSocket.isClosed()) {
serverSocket.close();
}
return;
}
} catch (SocketException e) {
if (e.getMessage().equals("socket closed")) {
logger.info("Server connection closing");
running = false;
}
}
}
} catch (Exception e1) {
logger.warn("Exception ignored", e1);
}
}
});
// start the server
thread.start();
}
/**
* Stops listening
* */
public void stop() throws Exception {
if (running) {
running = false;
serverSocket.close();
thread.interrupt();
stopTransports();
}
}
public void stopTransports() throws Exception {
synchronized (transports) {
stopping = true;
for (Transport transport : transports) {
// close all open connections
transport.stop();
}
transports.clear();
stopping = false;
}
}
protected void disconnect(SecureTCPTransport transport) {
if (stopping)
return;
synchronized (transports) {
transports.remove(transport);
}
}
}