ProcTools.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. package cern.accsoft.steering.jmad.util;

  23. public final class ProcTools {

  24.     private ProcTools() {
  25.         /* only static methods */
  26.     }

  27.     /**
  28.      * @param process the process to test.
  29.      * @return true, if the thread was started before, false otherwise
  30.      */
  31.     public static boolean isRunning(Process process) {
  32.         if (process == null) {
  33.             return false;
  34.         }
  35.         /*
  36.          * XXX some dirty trick: Tries to get the exit value. If this is possible, then the thread already terminated.
  37.          * If its not possible, then the process is still running. TODO better solution.
  38.          */
  39.         try {
  40.             process.exitValue();
  41.             return false;
  42.         } catch (IllegalThreadStateException e) {
  43.             return true;
  44.         }
  45.     }
  46. }