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.ipc;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.nio.file.Path;
026import java.nio.file.Paths;
027import java.util.Collection;
028import java.util.stream.Collectors;
029
030import org.eclipse.aether.named.NamedLock;
031import org.eclipse.aether.named.NamedLockKey;
032import org.eclipse.aether.named.support.NamedLockFactorySupport;
033import org.eclipse.aether.named.support.NamedLockSupport;
034import org.eclipse.aether.util.StringDigestUtil;
035
036import static java.util.Objects.requireNonNull;
037
038/**
039 * IPC named locks factory.
040 *
041 * @since 2.0.1
042 */
043@Singleton
044@Named(IpcNamedLockFactory.NAME)
045public class IpcNamedLockFactory extends NamedLockFactorySupport {
046    public static final String NAME = "ipc";
047
048    protected final IpcClient client;
049
050    @Inject
051    public IpcNamedLockFactory() {
052        this(Paths.get(System.getProperty("user.home")).resolve(".ipc-sync"));
053    }
054
055    public IpcNamedLockFactory(Path ipcHome) {
056        requireNonNull(ipcHome);
057        Path repository = ipcHome.resolve("repository");
058        Path logPath = ipcHome.resolve("log");
059        Path syncPath = null;
060        this.client = new IpcClient(repository, logPath, syncPath);
061    }
062
063    @Override
064    protected NamedLock doGetLock(Collection<NamedLockKey> keys) {
065        StringDigestUtil sha1 = StringDigestUtil.sha1();
066        keys.forEach(k -> sha1.update(k.name()));
067        NamedLockKey key = NamedLockKey.of(
068                sha1.digest(),
069                keys.stream()
070                        .map(NamedLockKey::resources)
071                        .flatMap(Collection::stream)
072                        .collect(Collectors.toList()));
073        return getLockAndRefTrack(
074                key,
075                () -> new IpcNamedLock(
076                        key, this, client, keys.stream().map(NamedLockKey::name).collect(Collectors.toList())));
077    }
078
079    @Override
080    protected NamedLockSupport createLock(NamedLockKey key) {
081        throw new IllegalStateException("should not get here");
082    }
083
084    @Override
085    protected void doShutdown() {
086        client.close();
087    }
088}