| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util; |
| 16 |
|
| 17 |
import java.util.List; |
| 18 |
import java.util.Iterator; |
| 19 |
import java.util.LinkedList; |
| 20 |
import java.util.Collections; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.OutputStream; |
| 23 |
import java.io.RandomAccessFile; |
| 24 |
import java.io.File; |
| 25 |
|
| 26 |
import org.apache.commons.logging.Log; |
| 27 |
import org.apache.commons.logging.LogFactory; |
| 28 |
|
| 29 |
public class Tail implements Runnable { |
| 30 |
|
| 31 |
private static final int CHUNK_SIZE = 2048; |
| 32 |
|
| 33 |
private byte [] buffer; |
| 34 |
private boolean _stop = false; |
| 35 |
private List list = null; |
| 36 |
private Thread _thread; |
| 37 |
|
| 38 |
private Log _logger; |
| 39 |
|
| 40 |
public Tail() { |
| 41 |
buffer = new byte[CHUNK_SIZE]; |
| 42 |
list = Collections.synchronizedList(new LinkedList()); |
| 43 |
} |
| 44 |
|
| 45 |
public void setLog(Log logger) { |
| 46 |
_logger = logger; |
| 47 |
} |
| 48 |
|
| 49 |
public void start() { |
| 50 |
_thread = new Thread(this); |
| 51 |
_thread.start(); |
| 52 |
} |
| 53 |
|
| 54 |
class FileWatcher { |
| 55 |
private RandomAccessFile _ras; |
| 56 |
private OutputStream _out; |
| 57 |
private long _pos; |
| 58 |
|
| 59 |
public FileWatcher(File file, OutputStream out, int pos) |
| 60 |
throws IOException { |
| 61 |
_ras = new RandomAccessFile(file, "r"); |
| 62 |
_out = out; |
| 63 |
_pos = pos; |
| 64 |
} |
| 65 |
|
| 66 |
public void init() |
| 67 |
throws IOException { |
| 68 |
_ras.seek(_pos); |
| 69 |
} |
| 70 |
|
| 71 |
public long getDiff() |
| 72 |
throws IOException { |
| 73 |
return _ras.length() - _pos; |
| 74 |
} |
| 75 |
|
| 76 |
public void moveBuffer(byte [] buffer, int size) |
| 77 |
throws IOException { |
| 78 |
_ras.readFully(buffer, 0, size); |
| 79 |
_pos += size; |
| 80 |
|
| 81 |
if (_logger.isDebugEnabled()) { |
| 82 |
_logger.debug("[tail] output size: " + size); |
| 83 |
} |
| 84 |
|
| 85 |
_out.write(buffer, 0, size); |
| 86 |
} |
| 87 |
|
| 88 |
public void close() { |
| 89 |
try { |
| 90 |
_ras.close(); |
| 91 |
} catch(Exception e) {} |
| 92 |
try { |
| 93 |
_out.close(); |
| 94 |
} catch (Exception e) {} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public void join() |
| 99 |
throws InterruptedException { |
| 100 |
_thread.join(); |
| 101 |
} |
| 102 |
|
| 103 |
public void addFile(File file, OutputStream out, int pos) |
| 104 |
throws IOException { |
| 105 |
list.add(new FileWatcher(file, out, pos)); |
| 106 |
} |
| 107 |
|
| 108 |
public void run() { |
| 109 |
|
| 110 |
_logger.debug("[tail] running..."); |
| 111 |
|
| 112 |
long len; |
| 113 |
int size; |
| 114 |
|
| 115 |
Iterator iter = null; |
| 116 |
FileWatcher watcher = null; |
| 117 |
|
| 118 |
try { |
| 119 |
iter = list.iterator(); |
| 120 |
while(iter.hasNext()) { |
| 121 |
watcher = (FileWatcher)iter.next(); |
| 122 |
watcher.init(); |
| 123 |
} |
| 124 |
|
| 125 |
while(!isDone()) { |
| 126 |
|
| 127 |
try { |
| 128 |
Thread.sleep(2000); |
| 129 |
} catch(Exception e) { |
| 130 |
} |
| 131 |
|
| 132 |
iter = list.iterator(); |
| 133 |
while(iter.hasNext()) { |
| 134 |
watcher = (FileWatcher)iter.next(); |
| 135 |
|
| 136 |
len = watcher.getDiff(); |
| 137 |
if (len <= 0) continue; |
| 138 |
|
| 139 |
while(len > 0) { |
| 140 |
size = (len > CHUNK_SIZE) ? CHUNK_SIZE : (int)len; |
| 141 |
watcher.moveBuffer(buffer, size); |
| 142 |
len -= size; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} catch(IOException e) { |
| 147 |
_logger.debug("Unexpected error.", e); |
| 148 |
} finally { |
| 149 |
close(); |
| 150 |
} |
| 151 |
|
| 152 |
_logger.debug("[tail] done."); |
| 153 |
} |
| 154 |
|
| 155 |
private boolean isDone() |
| 156 |
throws IOException { |
| 157 |
if (!_stop) return false; |
| 158 |
Iterator iter = null; |
| 159 |
FileWatcher watcher = null; |
| 160 |
|
| 161 |
iter = list.iterator(); |
| 162 |
while(iter.hasNext()) { |
| 163 |
watcher = (FileWatcher)iter.next(); |
| 164 |
if (watcher.getDiff() > 0) return false; |
| 165 |
} |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
private void close() { |
| 171 |
Iterator iter = null; |
| 172 |
FileWatcher watcher = null; |
| 173 |
iter = list.iterator(); |
| 174 |
while(iter.hasNext()) { |
| 175 |
watcher = (FileWatcher)iter.next(); |
| 176 |
watcher.close(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public void stop() { |
| 181 |
_logger.debug("[tail] stop called"); |
| 182 |
_stop = true; |
| 183 |
} |
| 184 |
|
| 185 |
} |