2 * Copyright (C) 2007 uguu at users.sourceforge.jp, All Rights Reserved.
\r
4 * Redistribution and use in source and binary forms, with or without
\r
5 * modification, are permitted provided that the following conditions
\r
8 * 1. Redistributions of source code must retain the above copyright
\r
9 * notice, this list of conditions and the following disclaimer.
\r
11 * 2. Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
15 * 3. Neither the name of Clarkware Consulting, Inc. nor the names of its
\r
16 * contributors may be used to endorse or promote products derived
\r
17 * from this software without prior written permission. For written
\r
18 * permission, please contact clarkware@clarkware.com.
\r
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
\r
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
\r
22 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
\r
23 * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 package jp.gr.java_conf.u6k.filelock;
\r
34 import java.io.File;
\r
35 import java.io.IOException;
\r
36 import java.io.RandomAccessFile;
\r
37 import java.nio.channels.FileChannel;
\r
38 import java.nio.channels.FileLock;
\r
39 import java.util.ArrayList;
\r
40 import java.util.List;
\r
41 import java.util.Map;
\r
42 import java.util.TreeMap;
\r
50 * @version $Rev$ $Date$
\r
52 public final class FileLockUtil {
\r
56 * ロックしたファイルのパスと{@link FileLock}インスタンスのマップ。
\r
59 private Map<String, FileLock> lockMap = new TreeMap<String, FileLock>();
\r
66 private Map<String, FileChannel> channelMap = new TreeMap<String, FileChannel>();
\r
70 * 指定したファイルをロックし、{@link FileLockUtil}インスタンスを初期化します。ディレクトリを指定した場合、子ファイルを再帰的に検索し、ロックします。ロックに失敗しても例外はスローしません。
\r
75 * @throws IOException
\r
78 public FileLockUtil(String[] paths) throws IOException {
\r
79 if (paths == null) {
\r
80 throw new NullPointerException("paths");
\r
83 for (String path : paths) {
\r
84 File file = new File(path).getCanonicalFile();
\r
89 private void lock(File file) throws IOException {
\r
90 if (file.isFile()) {
\r
91 FileChannel fch = new RandomAccessFile(file, "rw").getChannel();
\r
92 this.channelMap.put(file.getAbsolutePath(), fch);
\r
94 FileLock l = fch.tryLock();
\r
95 this.lockMap.put(file.getAbsolutePath(), l);
\r
96 } else if (file.isDirectory()) {
\r
97 for (File child : file.listFiles()) {
\r
105 * ロックに成功したファイルのパスの配列を返します。
\r
108 * @return ロックに成功したファイルのパスの配列。
\r
110 public String[] lockFiles() {
\r
111 List<String> files = new ArrayList<String>();
\r
112 for (String path : this.lockMap.keySet()) {
\r
113 if (this.lockMap.get(path) != null) {
\r
117 return files.toArray(new String[0]);
\r
122 * ロックに失敗したファイルのパスの配列を返します。
\r
125 * @return ロックに失敗したファイルのパスの配列。
\r
127 public String[] lockFailFiles() {
\r
128 List<String> files = new ArrayList<String>();
\r
129 for (String path : this.lockMap.keySet()) {
\r
130 if (this.lockMap.get(path) == null) {
\r
134 return files.toArray(new String[0]);
\r
139 * 保持している全てのロックを解除します。
\r
142 public void release() {
\r
143 for (FileChannel fch : this.channelMap.values()) {
\r
146 } catch (IOException e) {
\r
147 e.printStackTrace();
\r