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