View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named.providers;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.IOException;
25  import java.io.UncheckedIOException;
26  import java.net.URI;
27  import java.nio.channels.FileChannel;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.nio.file.StandardOpenOption;
32  import java.util.concurrent.ConcurrentHashMap;
33  import java.util.concurrent.ConcurrentMap;
34  
35  import org.eclipse.aether.named.NamedLock;
36  import org.eclipse.aether.named.NamedLockKey;
37  import org.eclipse.aether.named.support.FileLockNamedLock;
38  import org.eclipse.aether.named.support.NamedLockFactorySupport;
39  import org.eclipse.aether.named.support.NamedLockSupport;
40  
41  import static org.eclipse.aether.named.support.Retry.retry;
42  
43  /**
44   * Named locks factory of {@link FileLockNamedLock}s. This is a bit of special implementation, as it
45   * expects locks names to be proper URI string representations (use {@code file:} protocol for default
46   * file system).
47   *
48   * @since 1.7.3
49   */
50  @Singleton
51  @Named(FileLockNamedLockFactory.NAME)
52  public class FileLockNamedLockFactory extends NamedLockFactorySupport {
53      public static final String NAME = "file-lock";
54  
55      // Logic borrowed from Commons-Lang3: we really need only this, to decide do we "delete on close" or not
56      private static final boolean IS_WINDOWS =
57              System.getProperty("os.name", "unknown").startsWith("Windows");
58  
59      /**
60       * Tweak: on Windows, the presence of <em>StandardOpenOption#DELETE_ON_CLOSE</em> causes concurrency issues. This
61       * flag allows to have it removed from effective flags, at the cost that lockfile directory becomes crowded
62       * with 0 byte sized lock files that are never cleaned up. Default value is {@code true} on non-Windows OS.
63       * See <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a> for Windows related bug. Users
64       * on Windows can still force "delete on close" by explicitly setting this property to {@code true}.
65       *
66       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
67       * @configurationSource {@link System#getProperty(String, String)}
68       * @configurationType {@link java.lang.Boolean}
69       * @configurationDefaultValue true
70       */
71      public static final String SYSTEM_PROP_DELETE_LOCK_FILES = "aether.named.file-lock.deleteLockFiles";
72  
73      private static final boolean DELETE_LOCK_FILES =
74              Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DELETE_LOCK_FILES, Boolean.toString(!IS_WINDOWS)));
75  
76      /**
77       * Tweak: on Windows, the presence of <em>StandardOpenOption#DELETE_ON_CLOSE</em> causes concurrency issues. This
78       * flag allows to implement similar fix as referenced JDK bug report: retry and hope the best. Default value is
79       * 5 attempts (will retry 4 times).
80       *
81       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
82       * @configurationSource {@link System#getProperty(String, String)}
83       * @configurationType {@link java.lang.Integer}
84       * @configurationDefaultValue 5
85       */
86      public static final String SYSTEM_PROP_ATTEMPTS = "aether.named.file-lock.attempts";
87  
88      private static final int ATTEMPTS = Integer.parseInt(System.getProperty(SYSTEM_PROP_ATTEMPTS, "5"));
89  
90      /**
91       * Tweak: When {@link #SYSTEM_PROP_ATTEMPTS} used, the amount of milliseconds to sleep between subsequent retries. Default
92       * value is 50 milliseconds.
93       *
94       * @configurationSource {@link System#getProperty(String, String)}
95       * @configurationType {@link java.lang.Long}
96       * @configurationDefaultValue 50
97       */
98      public static final String SYSTEM_PROP_SLEEP_MILLIS = "aether.named.file-lock.sleepMillis";
99  
100     private static final long SLEEP_MILLIS = Long.parseLong(System.getProperty(SYSTEM_PROP_SLEEP_MILLIS, "50"));
101 
102     private final ConcurrentMap<NamedLockKey, FileChannel> fileChannels;
103 
104     public FileLockNamedLockFactory() {
105         this.fileChannels = new ConcurrentHashMap<>();
106     }
107 
108     @Override
109     protected NamedLockSupport createLock(final NamedLockKey key) {
110         Path path = Paths.get(URI.create(key.name()));
111         FileChannel fileChannel = fileChannels.computeIfAbsent(key, k -> {
112             try {
113                 Files.createDirectories(path.getParent());
114                 FileChannel channel = retry(
115                         ATTEMPTS,
116                         SLEEP_MILLIS,
117                         () -> {
118                             if (DELETE_LOCK_FILES) {
119                                 return FileChannel.open(
120                                         path,
121                                         StandardOpenOption.READ,
122                                         StandardOpenOption.WRITE,
123                                         StandardOpenOption.CREATE,
124                                         StandardOpenOption.DELETE_ON_CLOSE);
125                             } else {
126                                 return FileChannel.open(
127                                         path,
128                                         StandardOpenOption.READ,
129                                         StandardOpenOption.WRITE,
130                                         StandardOpenOption.CREATE);
131                             }
132                         },
133                         null,
134                         null);
135 
136                 if (channel == null) {
137                     throw new IllegalStateException(
138                             "Could not open file channel for '" + key + "' after " + ATTEMPTS + " attempts; giving up");
139                 }
140                 return channel;
141             } catch (InterruptedException e) {
142                 Thread.currentThread().interrupt();
143                 throw new RuntimeException("Interrupted while opening file channel for '" + key + "'", e);
144             } catch (IOException e) {
145                 throw new UncheckedIOException("Failed to open file channel for '" + key + "'", e);
146             }
147         });
148         return new FileLockNamedLock(key, fileChannel, this);
149     }
150 
151     @Override
152     protected void destroyLock(final NamedLock namedLock) {
153         if (namedLock instanceof FileLockNamedLock) {
154             final NamedLockKey key = namedLock.key();
155             FileChannel fileChannel = fileChannels.remove(key);
156             if (fileChannel == null) {
157                 throw new IllegalStateException("File channel expected, but does not exist: " + key);
158             }
159 
160             try {
161                 fileChannel.close();
162             } catch (IOException e) {
163                 throw new UncheckedIOException("Failed to close file channel for '" + key + "'", e);
164             }
165         }
166     }
167 }