@ Override public void close ( ) { if ( ! this . closed ) { try { Future < String > closeTask = threadpool . submit ( ( ) -> { commandWriter . println ( "exit" ) ; p . waitFor ( ) ; return "OK" ; } ) ; if ( ! closeAndWait ( closeTask ) && this . pid > 0 ) { //If it can be closed, force kill the process Logger . getLogger ( PowerShell . class . getName ( ) ) . log ( Level . INFO , "Forcing PowerShell to close. PID: " + this . pid ) ; try { Runtime . getRuntime ( ) . exec ( "taskkill.exe /PID " + pid + " /F /T" ) ; this . closed = true ; } catch ( IOException e ) { Logger . getLogger ( PowerShell . class . getName ( ) ) . log ( Level . SEVERE , "Unexpected error while killing powershell process" , e ) ; } } } catch ( InterruptedException | ExecutionException ex ) { logger . log ( Level . SEVERE , "Unexpected error when when closing PowerShell" , ex ) ; } finally { commandWriter . close ( ) ; try { if ( p . isAlive ( ) ) { p . getInputStream ( ) . close ( ) ; } } catch ( IOException ex ) { logger . log ( Level . SEVERE , "Unexpected error when when closing streams" , ex ) ; } if ( this . threadpool != null ) { try { this . threadpool . shutdownNow ( ) ; this . threadpool . awaitTermination ( 5 , TimeUnit . SECONDS ) ; } catch ( InterruptedException ex ) { logger . log ( Level . SEVERE , "Unexpected error when when shutting down thread pool" , ex ) ; } } this . closed = true ; } } }
Closes all the resources used to maintain the PowerShell context