Merge "Refactor frontend JS"
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / SecureMessageReadWriteService.java
index b41156147f7d3ea04066903e4794820437ab9099..bb8ba04fb8cab97ede7cd7d03d3c4aa25a4800fc 100644 (file)
@@ -10,6 +10,8 @@
 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
 
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SelectionKey;
@@ -50,14 +52,20 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
     private ByteBuffer myNetData;                      // encrypted message to be sent
     private ByteBuffer peerAppData;                            // clear text message received from the switch
     private ByteBuffer peerNetData;                    // encrypted message from the switch
+    private FileInputStream kfd = null, tfd = null;
 
     public SecureMessageReadWriteService(SocketChannel socket, Selector selector) throws Exception {
        this.socket = socket;
        this.selector = selector;
        this.factory = new BasicFactory();
 
-       createSecureChannel(socket);
-       createBuffers(sslEngine);
+       try {
+               createSecureChannel(socket);
+               createBuffers(sslEngine);
+       } catch (Exception e) {
+               stop();
+               throw e;
+       }
     }
 
        /**
@@ -71,13 +79,40 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
        String keyStorePassword = System.getProperty("controllerKeyStorePassword");
        String trustStoreFile = System.getProperty("controllerTrustStore");
        String trustStorePassword = System.getProperty("controllerTrustStorePassword");
-
+       
+       if (keyStoreFile != null) {
+               keyStoreFile = keyStoreFile.trim();
+       }
+       if ((keyStoreFile == null) || keyStoreFile.isEmpty()) {
+               throw new FileNotFoundException("controllerKeyStore not specified in ./configuration/config.ini");
+       }
+       if (keyStorePassword != null) {
+               keyStorePassword = keyStorePassword.trim();
+       } 
+       if ((keyStorePassword == null) || keyStorePassword.isEmpty()) {
+               throw new FileNotFoundException("controllerKeyStorePassword not specified in ./configuration/config.ini");
+       }
+       if (trustStoreFile != null) {
+               trustStoreFile = trustStoreFile.trim();
+       }
+       if ((trustStoreFile == null) || trustStoreFile.isEmpty()) {     
+               throw new FileNotFoundException("controllerTrustStore not specified in ./configuration/config.ini");
+       }
+       if (trustStorePassword != null) {
+               trustStorePassword = trustStorePassword.trim();
+       }
+        if ((trustStorePassword == null) || trustStorePassword.isEmpty()) {
+               throw new FileNotFoundException("controllerTrustStorePassword not specified in ./configuration/config.ini");
+       }
+       
         KeyStore ks = KeyStore.getInstance("JKS");
         KeyStore ts = KeyStore.getInstance("JKS");
         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
-        ks.load(new FileInputStream(keyStoreFile), keyStorePassword.toCharArray());
-        ts.load(new FileInputStream(trustStoreFile), trustStorePassword.toCharArray());
+        kfd = new FileInputStream(keyStoreFile);
+        tfd = new FileInputStream(trustStoreFile);
+        ks.load(kfd, keyStorePassword.toCharArray());
+        ts.load(tfd, trustStorePassword.toCharArray());
         kmf.init(ks, keyStorePassword.toCharArray());
         tmf.init(ts);
 
@@ -117,6 +152,8 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
                        newBuffer.put(myAppData);
                        myAppData = newBuffer;
                }
+       }
+       synchronized (myAppData) {
                msg.writeTo(myAppData);
                myAppData.flip();
                sslEngineResult = sslEngine.wrap(myAppData, myNetData);
@@ -344,4 +381,23 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
        this.myNetData = ByteBuffer.allocate(session.getPacketBufferSize());
        this.peerNetData = ByteBuffer.allocate(session.getPacketBufferSize());
     }
+
+       @Override
+       public void stop() throws IOException {
+               this.sslEngine = null;
+               this.sslEngineResult = null;
+               this.myAppData = null;
+               this.myNetData = null;
+               this.peerAppData = null;
+               this.peerNetData = null;
+           
+           if (this.kfd != null) {
+               this.kfd.close();
+               this.kfd = null;
+               }
+               if (this.tfd != null) {
+                       this.tfd.close();
+                       this.tfd = null;
+               }
+       }
 }