001package org.eclipse.aether.named.providers;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.io.UncheckedIOException;
024import java.nio.channels.FileChannel;
025import java.nio.file.AccessDeniedException;
026import java.nio.file.Files;
027import java.nio.file.Path;
028import java.nio.file.Paths;
029import java.nio.file.StandardOpenOption;
030import java.util.concurrent.ConcurrentHashMap;
031import java.util.concurrent.ConcurrentMap;
032
033import javax.inject.Named;
034import javax.inject.Singleton;
035
036import org.eclipse.aether.named.support.FileLockNamedLock;
037import org.eclipse.aether.named.support.NamedLockFactorySupport;
038import org.eclipse.aether.named.support.NamedLockSupport;
039
040import static org.eclipse.aether.named.support.Retry.retry;
041
042/**
043 * Named locks factory of {@link FileLockNamedLock}s. This is a bit special implementation, as it
044 * expects locks names to be fully qualified absolute file system paths.
045 *
046 * @since 1.7.3
047 */
048@Singleton
049@Named( FileLockNamedLockFactory.NAME )
050public class FileLockNamedLockFactory
051    extends NamedLockFactorySupport
052{
053    public static final String NAME = "file-lock";
054
055    /**
056     * Tweak: on Windows, the presence of {@link StandardOpenOption#DELETE_ON_CLOSE} causes concurrency issues. This
057     * flag allows to have it removed from effective flags, at the cost that lockfile directory becomes crowded
058     * with 0 byte sized lock files that are never cleaned up. Default value is {@code true}.
059     *
060     * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
061     */
062    private static final boolean DELETE_LOCK_FILES = Boolean.parseBoolean(
063            System.getProperty( "aether.named.file-lock.deleteLockFiles", Boolean.TRUE.toString() ) );
064
065    /**
066     * Tweak: on Windows, the presence of {@link StandardOpenOption#DELETE_ON_CLOSE} causes concurrency issues. This
067     * flag allows to implement similar fix as referenced JDK bug report: retry and hope the best. Default value is
068     * 5 attempts (will retry 4 times).
069     *
070     * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
071     */
072    private static final int ATTEMPTS = Integer.parseInt(
073            System.getProperty( "aether.named.file-lock.attempts", "5" ) );
074
075    /**
076     * Tweak: When {@link #ATTEMPTS} used, the amount of milliseconds to sleep between subsequent retries. Default
077     * value is 50 milliseconds.
078     */
079    private static final long SLEEP_MILLIS = Long.parseLong(
080            System.getProperty( "aether.named.file-lock.sleepMillis", "50" ) );
081
082    private final ConcurrentMap<String, FileChannel> fileChannels;
083
084    public FileLockNamedLockFactory()
085    {
086        this.fileChannels = new ConcurrentHashMap<>();
087    }
088
089    @Override
090    protected NamedLockSupport createLock( final String name )
091    {
092        Path path = Paths.get( name );
093        FileChannel fileChannel = fileChannels.computeIfAbsent( name, k ->
094        {
095            try
096            {
097                Files.createDirectories( path.getParent() );
098                FileChannel channel = retry( ATTEMPTS, SLEEP_MILLIS, () ->
099                {
100                    try
101                    {
102                        if ( DELETE_LOCK_FILES )
103                        {
104                            return FileChannel.open(
105                                    path,
106                                    StandardOpenOption.READ, StandardOpenOption.WRITE,
107                                    StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE
108                            );
109                        }
110                        else
111                        {
112                            return FileChannel.open(
113                                    path,
114                                    StandardOpenOption.READ, StandardOpenOption.WRITE,
115                                    StandardOpenOption.CREATE
116                            );
117                        }
118                    }
119                    catch ( AccessDeniedException e )
120                    {
121                        return null;
122                    }
123                }, null, null );
124
125                if ( channel == null )
126                {
127                    throw new IllegalStateException( "Could not open file channel for '"
128                            + name + "' after " + ATTEMPTS + " attempts; giving up" );
129                }
130                return channel;
131            }
132            catch ( InterruptedException e )
133            {
134                Thread.currentThread().interrupt();
135                throw new RuntimeException( "Interrupted while opening file channel for '"
136                        + name + "'", e );
137            }
138            catch ( IOException e )
139            {
140                throw new UncheckedIOException( "Failed to open file channel for '"
141                    + name + "'", e );
142            }
143        } );
144        return new FileLockNamedLock( name, fileChannel, this );
145    }
146
147    @Override
148    protected void destroyLock( final String name )
149    {
150        FileChannel fileChannel = fileChannels.remove( name );
151        if ( fileChannel == null )
152        {
153            throw new IllegalStateException( "File channel expected, but does not exist: " + name );
154        }
155
156        try
157        {
158            fileChannel.close();
159        }
160        catch ( IOException e )
161        {
162            throw new UncheckedIOException( "Failed to close file channel for '"
163                    + name + "'", e );
164        }
165    }
166}