1 package org.apache.archiva.common.filelock;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.io.Closeable;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.channels.FileChannel;
28 import java.nio.channels.FileLock;
29 import java.util.HashMap;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import java.util.concurrent.atomic.AtomicInteger;
35 * @author Olivier Lamy
42 private AtomicBoolean write;
44 private final Map<Thread, AtomicInteger> fileClients = new HashMap<>();
46 private FileLock fileLock;
48 private RandomAccessFile randomAccessFile;
50 private FileChannel fileChannel;
52 public Lock( File file )
57 public Lock( File file, boolean write )
58 throws FileNotFoundException
61 this.write = new AtomicBoolean( write );
62 randomAccessFile = new RandomAccessFile( file, write ? "rw" : "r" );
63 fileChannel = randomAccessFile.getChannel();
71 public AtomicBoolean isWrite()
76 public void setFile( File file )
81 public void setWrite( boolean write )
83 this.write.set( write );
86 public boolean isShared()
88 return this.fileLock.isValid() && this.fileLock.isShared();
91 public boolean isValid()
93 return this.fileLock.isValid();
96 public Map<Thread, AtomicInteger> getFileClients()
101 public void addFileClient( Thread thread )
103 this.fileClients.put( thread, new AtomicInteger( 1 ) );
106 public boolean removeFileClient( Thread thread )
108 return this.fileClients.remove( thread ) != null;
111 protected void close()
114 IOException ioException = null;
117 this.fileLock.release();
119 catch ( IOException e )
124 closeQuietly( fileChannel );
125 closeQuietly( randomAccessFile );
127 fileClients.remove( Thread.currentThread() );
129 if ( ioException != null )
136 protected void openLock( boolean write, boolean timeout )
139 fileClients.put( Thread.currentThread(), new AtomicInteger( 1 ) );
141 this.fileLock = timeout
142 ? fileChannel.tryLock( 0L, Long.MAX_VALUE, write ? false : true )
143 : fileChannel.lock( 0L, Long.MAX_VALUE, write ? false : true );
147 protected RandomAccessFile getRandomAccessFile()
149 return randomAccessFile;
152 private void closeQuietly( Closeable closeable )
158 catch ( IOException e )
166 public String toString()
168 final StringBuilder sb = new StringBuilder( "Lock{" );
169 sb.append( "file=" ).append( file );
171 return sb.toString();