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.nio.channels.FileChannel;
27  import java.nio.file.AccessDeniedException;
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.support.FileLockNamedLock;
36  import org.eclipse.aether.named.support.NamedLockFactorySupport;
37  import org.eclipse.aether.named.support.NamedLockSupport;
38  
39  import static org.eclipse.aether.named.support.Retry.retry;
40  
41  /**
42   * Named locks factory of {@link FileLockNamedLock}s. This is a bit special implementation, as it
43   * expects locks names to be fully qualified absolute file system paths.
44   *
45   * @since 1.7.3
46   */
47  @Singleton
48  @Named(FileLockNamedLockFactory.NAME)
49  public class FileLockNamedLockFactory extends NamedLockFactorySupport {
50      public static final String NAME = "file-lock";
51  
52      /**
53       * Tweak: on Windows, the presence of {@link StandardOpenOption#DELETE_ON_CLOSE} causes concurrency issues. This
54       * flag allows to have it removed from effective flags, at the cost that lockfile directory becomes crowded
55       * with 0 byte sized lock files that are never cleaned up. Default value is {@code true}.
56       *
57       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
58       * @configurationSource {@link System#getProperty(String, String)}
59       * @configurationType {@link java.lang.Boolean}
60       * @configurationDefaultValue true
61       */
62      public static final String SYSTEM_PROP_DELETE_LOCK_FILES = "aether.named.file-lock.deleteLockFiles";
63  
64      private static final boolean DELETE_LOCK_FILES =
65              Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DELETE_LOCK_FILES, Boolean.TRUE.toString()));
66  
67      /**
68       * Tweak: on Windows, the presence of {@link StandardOpenOption#DELETE_ON_CLOSE} causes concurrency issues. This
69       * flag allows to implement similar fix as referenced JDK bug report: retry and hope the best. Default value is
70       * 5 attempts (will retry 4 times).
71       *
72       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
73       * @configurationSource {@link System#getProperty(String, String)}
74       * @configurationType {@link java.lang.Integer}
75       * @configurationDefaultValue 5
76       */
77      public static final String SYSTEM_PROP_ATTEMPTS = "aether.named.file-lock.attempts";
78  
79      private static final int ATTEMPTS = Integer.parseInt(System.getProperty(SYSTEM_PROP_ATTEMPTS, "5"));
80  
81      /**
82       * Tweak: When {@link #SYSTEM_PROP_ATTEMPTS} used, the amount of milliseconds to sleep between subsequent retries. Default
83       * value is 50 milliseconds.
84       *
85       * @configurationSource {@link System#getProperty(String, String)}
86       * @configurationType {@link java.lang.Long}
87       * @configurationDefaultValue 50
88       */
89      public static final String SYSTEM_PROP_SLEEP_MILLIS = "aether.named.file-lock.sleepMillis";
90  
91      private static final long SLEEP_MILLIS = Long.parseLong(System.getProperty(SYSTEM_PROP_SLEEP_MILLIS, "50"));
92  
93      private final ConcurrentMap<String, FileChannel> fileChannels;
94  
95      public FileLockNamedLockFactory() {
96          this.fileChannels = new ConcurrentHashMap<>();
97      }
98  
99      @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 }