123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package org.apache.archiva.common.filelock;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import java.io.Closeable;
- import java.io.IOException;
- import java.nio.channels.FileChannel;
- import java.nio.channels.FileLock;
- import java.nio.file.Path;
- import java.nio.file.StandardOpenOption;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicBoolean;
- import java.util.concurrent.atomic.AtomicInteger;
-
- /**
- * @author Olivier Lamy
- * @since 2.0.0
- */
- public class Lock implements Closeable
- {
- private Path file;
-
- private AtomicBoolean write;
-
- private final Map<Thread, AtomicInteger> fileClients = new HashMap<>();
-
- private FileLock fileLock;
-
- private FileChannel fileChannel;
-
- public Lock( Path file )
- {
- this.file = file;
- }
-
- public Lock( Path file, boolean write )
- throws IOException
- {
- this.file = file;
- this.write = new AtomicBoolean( write );
- fileChannel = write ? FileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.READ) : FileChannel.open(file, StandardOpenOption.READ);
- }
-
- public Path getFile()
- {
- return file;
- }
-
- public AtomicBoolean isWrite()
- {
- return write;
- }
-
- public void setFile( Path file )
- {
- this.file = file;
- }
-
- public void setWrite( boolean write )
- {
- this.write.set( write );
- }
-
- public boolean isShared()
- {
- return this.fileLock.isValid() && this.fileLock.isShared();
- }
-
- public boolean isValid()
- {
- return this.fileLock!=null && this.fileLock.isValid();
- }
-
- public Map<Thread, AtomicInteger> getFileClients()
- {
- return fileClients;
- }
-
- public void addFileClient( Thread thread )
- {
- this.fileClients.put( thread, new AtomicInteger( 1 ) );
- }
-
- public boolean removeFileClient( Thread thread )
- {
- return this.fileClients.remove( thread ) != null;
- }
-
- public void close()
- throws IOException
- {
- IOException ioException = null;
- try
- {
- if (this.fileLock!=null) {
- this.fileLock.release();
- }
- }
- catch ( IOException e )
- {
- ioException = e;
- } finally {
- closeQuietly( fileChannel );
- fileClients.remove( Thread.currentThread() );
- }
-
- if ( ioException != null )
- {
- throw ioException;
- }
-
- }
-
- protected void openLock( boolean write, boolean timeout )
- throws IOException
- {
- fileClients.put( Thread.currentThread(), new AtomicInteger( 1 ) );
-
- this.fileLock = timeout
- ? fileChannel.tryLock( 0L, Long.MAX_VALUE, write ? false : true )
- : fileChannel.lock( 0L, Long.MAX_VALUE, write ? false : true );
-
- }
-
-
-
- private void closeQuietly( Closeable closeable )
- {
- try
- {
- closeable.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
-
-
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder( "Lock{" );
- sb.append( "file=" ).append( file );
- sb.append( '}' );
- return sb.toString();
- }
- }
|