View Javadoc

    1   //========================================================================
    2   //Copyright 2009 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.util.daemon;
    16
    17  import java.io.File;
    18  import java.io.FilenameFilter;
    19  import java.util.Timer;
    20  import java.util.TimerTask;
    21
    22  public class Sweeper implements Runnable, FilenameFilter
    23  {
    24
    25      public static final long DEFAULT_INTERVAL = 3600000; // 1 hour
    26
    27      public static boolean LOG_TO_CONSOLE = true;
    28
    29      public Sweeper()
    30      {
    31          // managed by a scheduler
    32      }
    33
    34      public Sweeper(long interval)
    35      {
    36          if(interval==0)
    37              run();
    38          else
    39              schedule(new Timer(), interval);
    40      }
    41
    42      public Sweeper(Timer timer, long interval)
    43      {
    44          schedule(timer, interval);
    45      }
    46
    47      private void schedule(Timer timer, long interval)
    48      {
    49          if (LOG_TO_CONSOLE)
    50              System.err.println("Sweeping "+System.getProperty("java.io.tmpdir")+" every "+interval+"ms");
    51          timer.scheduleAtFixedRate(new TimerTask()
    52          {
    53              public void run()
    54              {
    55                  Sweeper.this.run();
    56              }
    57
    58          }, 1000, interval);
    59      }
    60
    61      public void run()
    62      {
    63          File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    64          File[] files = tmpDir.listFiles(this);
    65          for(int i=0; i<files.length; i++)
    66          {
    67              File dir = files[i];
    68              File sentinel = new File(dir, ".active");
    69              if(!sentinel.exists() && delete(dir) && LOG_TO_CONSOLE)
    70                  System.err.println("removed dir: " + dir);
    71          }
    72      }
    73
    74      public static boolean delete(File file)
    75      {
    76          if (file.isDirectory())
    77          {
    78              File[] files = file.listFiles();
    79              for (int i=0; i<files.length; i++)
    80                  delete(files[i]);
    81          }
    82          return file.delete();
    83      }
    84
    85      public boolean accept(File file, String name)
    86      {
    87          return name.startsWith("Jetty_") && file.isDirectory();
    88      }
    89
    90      public static void main(String[] args)
    91      {
    92          new Sweeper(args.length!=0 ? Long.parseLong(args[0]) :
    93              Long.getLong("interval", DEFAULT_INTERVAL).longValue());
    94      }
    95
    96  }