View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.concurrency;
    20
    21  import java.io.File;
    22  import java.io.IOException;
    23  import java.util.logging.Level;
    24  import java.util.logging.Logger;
    25
    26  /**
    27   * A simple task that obtains a lock on a directory. This is used in testing of
    28   * the shared and exclusive locks.
    29   *
    30   * @author Jeremy Long (jeremy.long@owasp.org)
    31   */
    32  public class SpinLockTask implements Runnable {
    33
    34      DirectorySpinLock lock = null;
    35      int holdLockFor;
    36      long maxWait;
    37      boolean shared;
    38      private Exception exception = null;
    39
    40      /**
    41       * Get the value of exception
    42       *
    43       * @return the value of exception
    44       */
    45      public Exception getException() {
    46          return exception;
    47      }
    48
    49      /**
    50       * Set the value of exception
    51       *
    52       * @param exception new value of exception
    53       */
    54      public void setException(Exception exception) {
    55          this.exception = exception;
    56      }
    57
    58      public SpinLockTask(File directory, int holdLockFor, boolean shared, long maxWait) throws InvalidDirectoryException, DirectoryLockException {
    59          this.holdLockFor = holdLockFor;
    60          this.shared = shared;
    61          this.maxWait = maxWait;
    62          lock = new DirectorySpinLock(directory);
    63      }
    64
    65      @Override
    66      public void run() {
    67          try {
    68              lock.obtainLock(shared, maxWait);
    69              Thread.sleep(holdLockFor);
    70          } catch (DirectoryLockException ex) {
    71              exception = ex;
    72          } catch (InterruptedException ex) {
    73              exception = ex;
    74          } finally {
    75              if (lock != null) {
    76                  try {
    77                      lock.close();
    78                  } catch (IOException ex) {
    79                      exception = ex;
    80                  }
    81              }
    82          }
    83      }
    84  }