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.ipc;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.Collection;
28  import java.util.stream.Collectors;
29  
30  import org.eclipse.aether.named.NamedLock;
31  import org.eclipse.aether.named.NamedLockKey;
32  import org.eclipse.aether.named.support.NamedLockFactorySupport;
33  import org.eclipse.aether.named.support.NamedLockSupport;
34  import org.eclipse.aether.util.StringDigestUtil;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   * IPC named locks factory.
40   *
41   * @since 2.0.1
42   */
43  @Singleton
44  @Named(IpcNamedLockFactory.NAME)
45  public class IpcNamedLockFactory extends NamedLockFactorySupport {
46      public static final String NAME = "ipc";
47  
48      protected final IpcClient client;
49  
50      @Inject
51      public IpcNamedLockFactory() {
52          this(Paths.get(System.getProperty("user.home")).resolve(".ipc-sync"));
53      }
54  
55      public IpcNamedLockFactory(Path ipcHome) {
56          requireNonNull(ipcHome);
57          Path repository = ipcHome.resolve("repository");
58          Path logPath = ipcHome.resolve("log");
59          Path syncPath = null;
60          this.client = new IpcClient(repository, logPath, syncPath);
61      }
62  
63      @Override
64      protected NamedLock doGetLock(Collection<NamedLockKey> keys) {
65          StringDigestUtil sha1 = StringDigestUtil.sha1();
66          keys.forEach(k -> sha1.update(k.name()));
67          NamedLockKey key = NamedLockKey.of(
68                  sha1.digest(),
69                  keys.stream()
70                          .map(NamedLockKey::resources)
71                          .flatMap(Collection::stream)
72                          .collect(Collectors.toList()));
73          return getLockAndRefTrack(
74                  key,
75                  () -> new IpcNamedLock(
76                          key, this, client, keys.stream().map(NamedLockKey::name).collect(Collectors.toList())));
77      }
78  
79      @Override
80      protected NamedLockSupport createLock(NamedLockKey key) {
81          throw new IllegalStateException("should not get here");
82      }
83  
84      @Override
85      protected void doShutdown() {
86          client.close();
87      }
88  }