gt:         if (!requestDir.exists()) {
context: /* $Id$
   * $Revision$
   * $Date$
   * $Author$
   *
   * The Netarchive Suite - Software to harvest and preserve websites
   * Copyright 2004-2011 Det Kongelige Bibliotek and Statsbiblioteket, Denmark
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 
   *  USA
   */
  package dk.netarkivet.harvester.indexserver.distribute;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.util.ArrayList;
  import java.util.EnumMap;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.Timer;
  import java.util.TimerTask;
  import java.util.concurrent.atomic.AtomicBoolean;
  
  import org.apache.commons.io.IOUtils;
  import org.apache.commons.io.LineIterator;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import dk.netarkivet.common.distribute.Channels;
  import dk.netarkivet.common.distribute.JMSConnection;
  import dk.netarkivet.common.distribute.JMSConnectionFactory;
  import dk.netarkivet.common.distribute.RemoteFile;
  import dk.netarkivet.common.distribute.RemoteFileFactory;
  import dk.netarkivet.common.distribute.indexserver.RequestType;
  import dk.netarkivet.common.exceptions.ArgumentNotValid;
  import dk.netarkivet.common.exceptions.IOFailure;
  import dk.netarkivet.common.exceptions.IllegalState;
  import dk.netarkivet.common.exceptions.UnknownID;
  import dk.netarkivet.common.utils.CleanupIF;
  import dk.netarkivet.common.utils.FileUtils;
  import dk.netarkivet.common.utils.Settings;
  import dk.netarkivet.common.utils.StringUtils;
  import dk.netarkivet.harvester.HarvesterSettings;
  import dk.netarkivet.harvester.distribute.HarvesterMessageHandler;
  import dk.netarkivet.harvester.distribute.IndexReadyMessage;
  import dk.netarkivet.harvester.indexserver.FileBasedCache;
  import dk.netarkivet.harvester.indexserver.IndexRequestServerInterface;
  
  /**
   * Index request server singleton.
   *
   * This class contains a singleton that handles requesting an index over JMS.
   *
   * This has two modes. 1) Given a file with a list of jobIDs, it will always return 
   * the same lucene index based on the list of job identifiers in the file regardless 
   * of what kind of index the client is requesting.
   * 2) if setting "settings.harvester.indexserver.alwaysSetIsIndexReadyToFalse" is true
   * it will always return the IndexRequestMessage with isindexready set to false.
   */
  public final class TestIndexRequestServer extends HarvesterMessageHandler
          implements CleanupIF, IndexRequestServerInterface {
      
      /** The default place in classpath where the settings file can be found. */
      private static String defaultSettingsClasspath = "dk/netarkivet/harvester/"
          + "indexserver/distribute/TestIndexRequestServerSettings.xml";
  
      /*
       * The static initialiser is called when the class is loaded.
       * It will add default values for all settings defined in this class, by
       * loading them from a settings.xml file in classpath.
       */
      static {
          Settings.addDefaultClasspathSettings(
                  defaultSettingsClasspath
          );
      }
      
      /** 
       * <b>settings.harvester.indexserver.fileContainingJobsForTestindex<b>:
       * The file containing the list of jobids that the test index uses as 
       * data. The default name of the file is "jobids.txt"
       */
      public static String JOBS_FOR_TESTINDEX 
          = "settings.harvester.indexserver.indexrequestserver.fileContainingJobsForTestindex";
      /** 
       * <b>settings.archive.indexserver.alwaysSetIsIndexReadyToFalse<b>:
       * The default: false.
       * If set to true, the IndexRequestMessage returned has always isindexready = false.
       */
      public static String ALWAYS_SET_ISINDEX_READY_TO_FALSE
              = "settings.harvester.indexserver.indexrequestserver.alwaysSetIsIndexReadyToFalse";
      
      /** The class logger. */
      private static Log log = LogFactory.getLog(TestIndexRequestServer.class);
      /** The unique instance. */
      private static TestIndexRequestServer instance;
      /** The handlers for index request types. */
      private Map<RequestType, FileBasedCache<Set<Long>>> handlers;
      
      /** The connection to the JMSBroker. */
      private static JMSConnection conn;
      /** A set with the current indexing jobs in progress. */
      private static Map<String, IndexRequestMessage> currentJobs;
      /** The max number of concurrent jobs. */
      private static long maxConcurrentJobs;
      /** Are we listening, now. */
      private static AtomicBoolean isListening = new AtomicBoolean();
      
      /** Interval in milliseconds between listening checks. */
      private static long listeningInterval;
      /** The timer that initiates the checkIflisteningTask. */
      private Timer checkIflisteningTimer = new Timer();
      
      /** The File containing the list of jobids, that the default index 
       * consists of.
       */
      private File jobsForDefaultIndex; 
      
      private boolean alwaysReturnFalseMode = false;
      
      /** The set of Jobs ids used for the default index. */
      private Set<Long> defaultIDs;
      
      /**
       * The directory to store backup copies of the currentJobs.
       * In case of the indexserver crashing. 
       */
      private File requestDir;
      /** Initialise index request server with no handlers, listening to the
       * index JMS channel.
       */
      private TestIndexRequestServer() {
          maxConcurrentJobs = Settings.getLong(
                  HarvesterSettings.INDEXSERVER_INDEXING_MAXCLIENTS);
          requestDir = Settings.getFile(
                  HarvesterSettings.INDEXSERVER_INDEXING_REQUESTDIR);
          listeningInterval = Settings.getLong(
                  HarvesterSettings.INDEXSERVER_INDEXING_LISTENING_INTERVAL);
          
          alwaysReturnFalseMode = Settings.getBoolean(ALWAYS_SET_ISINDEX_READY_TO_FALSE);
          if (alwaysReturnFalseMode) {
              log.info("alwaysSetIsIndexReadyToFalse is true");
          } else {
              log.info("alwaysSetIsIndexReadyToFalse is false");
          }
          
          jobsForDefaultIndex = Settings.getFile(JOBS_FOR_TESTINDEX);
          
          if (!jobsForDefaultIndex.exists()) {
              final String msg = "The file '" + jobsForDefaultIndex.getAbsolutePath() 
                      + "' does not exist";
              log.fatal("The file containing job identifiers for default index '" 
                      + jobsForDefaultIndex.getAbsolutePath() 
                      + "' does not exist");
              System.err.println(msg + ". Exiting program");
              System.exit(1);
          }
          defaultIDs = readLongsFromFile(jobsForDefaultIndex);
          currentJobs = new HashMap<String, IndexRequestMessage>();
          handlers = new EnumMap<RequestType, FileBasedCache<Set<Long>>>(
                  RequestType.class);
          conn = JMSConnectionFactory.getInstance();
          checkIflisteningTimer = new Timer();
      }
  
      private Set<Long> readLongsFromFile(File fileWithLongs) {
          Set<Long> resultSet = new HashSet<Long>();
          try {
              LineIterator lineIterator = new LineIterator(new FileReader(fileWithLongs));
              while (lineIterator.hasNext()) {
                  String line = lineIterator.next();
                  resultSet.add(Long.parseLong(line));
              }
          } catch (IOException e) {
              log.fatal("Unable to read from file '" 
                      + fileWithLongs.getAbsolutePath() 
                      + "'. Returns set of size " + resultSet.size());
          }
          
          return resultSet;
      }
  
      /**
       * Restore old requests from requestDir.
       */
      private void restoreRequestsfromRequestDir() {
  