FileMonitor.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. import java.io.File;

  24. import cern.accsoft.steering.jmad.JMadException;

  25. /**
  26.  * polls a file and terminates as soon as it exists.
  27.  *
  28.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  29.  */
  30. public class FileMonitor {
  31.     /** the sleeping interval between the polls. in milliseconds. */
  32.     private static final int POLL_INTERVAL = 10;

  33.     /** The file to wait for */
  34.     private File file = null;

  35.     /** The process to monitor. If this is interrupted, then the waiting throws an exception */
  36.     private Process process = null;

  37.     /**
  38.      * waits forever, until the file exists.
  39.      *
  40.      * @return true, if it exists finally, false, if the monitored process was interrupted
  41.      * @throws WaitingFailedException if something goes wrong during waiting
  42.      */
  43.     public boolean waitForFile() throws JMadException {
  44.         return waitForFile(null);
  45.     }

  46.     /**
  47.      * waits until a file exists.
  48.      *
  49.      * @param timeout the maximum millisecs to wait (if null, then we wait forever ...)
  50.      * @return true, if the file finally exists, false, if the monitored process was interrupted or we timed out.
  51.      * @throws WaitingFailedException if something goes wrong during the waiting
  52.      */
  53.     public synchronized boolean waitForFile(Long timeout) throws JMadException {
  54.         long startTime = System.currentTimeMillis();
  55.         while (!file.exists()) {
  56.             /* interrupt if the process has stopped meanwhile. */
  57.             if ((process != null) && (!ProcTools.isRunning(process))) {
  58.                 throw new ProcessTerminatedUnexpectedlyException("process '" + process.toString()
  59.                         + "' terminated while waiting for file '" + file.getAbsolutePath()
  60.                         + "' - maybe there was some error!");
  61.             }

  62.             /* interrupt if the timeout is reached */
  63.             if (timeout != null) {
  64.                 long delay = timeout - (System.currentTimeMillis() - startTime);
  65.                 if (delay <= 0) {
  66.                     return false;
  67.                 }
  68.             }

  69.             /*
  70.              * If everything is ok so far, we wait a little bit before continuing.
  71.              */
  72.             try {
  73.                 this.wait(POLL_INTERVAL);
  74.             } catch (InterruptedException e) {
  75.                 throw new WaitingFailedException("waiting for file '" + file.getAbsolutePath() + "' was interrupted", e);
  76.             }
  77.         }

  78.         /* the file finally exists */
  79.         return true;
  80.     }

  81.     public FileMonitor(File file) {
  82.         this(file, null);
  83.     }

  84.     public FileMonitor(File file, Process process) {
  85.         this.file = file;
  86.         this.process = process;
  87.     }

  88.     /**
  89.      * Will be thrown, if the waiting fails for some reason.
  90.      *
  91.      * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  92.      */
  93.     public static class WaitingFailedException extends JMadException {
  94.         private static final long serialVersionUID = 1L;

  95.         public WaitingFailedException(String message) {
  96.             super(message);
  97.         }

  98.         public WaitingFailedException(String message, Throwable cause) {
  99.             super(message, cause);
  100.         }

  101.     }

  102.     public static class ProcessTerminatedUnexpectedlyException extends JMadException {
  103.         private static final long serialVersionUID = 1L;

  104.         public ProcessTerminatedUnexpectedlyException(String message) {
  105.             super(message);
  106.         }

  107.         public ProcessTerminatedUnexpectedlyException(String message, Throwable cause) {
  108.             super(message, cause);
  109.         }

  110.     }

  111. }