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.internal.impl.synccontext.named;
020
021import java.util.ArrayDeque;
022import java.util.Collection;
023import java.util.Deque;
024import java.util.Objects;
025import java.util.concurrent.TimeUnit;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.SyncContext;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.metadata.Metadata;
031import org.eclipse.aether.named.NamedLock;
032import org.eclipse.aether.named.NamedLockFactory;
033import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
034import org.eclipse.aether.util.ConfigUtils;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * Adapter to adapt {@link NamedLockFactory} and {@link NamedLock} to {@link SyncContext}.
040 */
041public final class NamedLockFactoryAdapter {
042    public static final String TIME_KEY = "aether.syncContext.named.time";
043
044    public static final long DEFAULT_TIME = 30L;
045
046    public static final String TIME_UNIT_KEY = "aether.syncContext.named.time.unit";
047
048    public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
049
050    private final NameMapper nameMapper;
051
052    private final NamedLockFactory namedLockFactory;
053
054    public NamedLockFactoryAdapter(final NameMapper nameMapper, final NamedLockFactory namedLockFactory) {
055        this.nameMapper = Objects.requireNonNull(nameMapper);
056        this.namedLockFactory = Objects.requireNonNull(namedLockFactory);
057        // TODO: this is ad-hoc "validation", experimental and likely to change
058        if (this.namedLockFactory instanceof FileLockNamedLockFactory && !this.nameMapper.isFileSystemFriendly()) {
059            throw new IllegalArgumentException(
060                    "Misconfiguration: FileLockNamedLockFactory lock factory requires FS friendly NameMapper");
061        }
062    }
063
064    public SyncContext newInstance(final RepositorySystemSession session, final boolean shared) {
065        return new AdaptedLockSyncContext(session, shared, nameMapper, namedLockFactory);
066    }
067
068    /**
069     * @since 1.9.1
070     */
071    public NameMapper getNameMapper() {
072        return nameMapper;
073    }
074
075    /**
076     * @since 1.9.1
077     */
078    public NamedLockFactory getNamedLockFactory() {
079        return namedLockFactory;
080    }
081
082    public String toString() {
083        return getClass().getSimpleName()
084                + "(nameMapper=" + nameMapper
085                + ", namedLockFactory=" + namedLockFactory
086                + ")";
087    }
088
089    private static class AdaptedLockSyncContext implements SyncContext {
090        private static final Logger LOGGER = LoggerFactory.getLogger(AdaptedLockSyncContext.class);
091
092        private final RepositorySystemSession session;
093
094        private final boolean shared;
095
096        private final NameMapper lockNaming;
097
098        private final NamedLockFactory namedLockFactory;
099
100        private final long time;
101
102        private final TimeUnit timeUnit;
103
104        private final Deque<NamedLock> locks;
105
106        private AdaptedLockSyncContext(
107                final RepositorySystemSession session,
108                final boolean shared,
109                final NameMapper lockNaming,
110                final NamedLockFactory namedLockFactory) {
111            this.session = session;
112            this.shared = shared;
113            this.lockNaming = lockNaming;
114            this.namedLockFactory = namedLockFactory;
115            this.time = getTime(session);
116            this.timeUnit = getTimeUnit(session);
117            this.locks = new ArrayDeque<>();
118
119            if (time < 0L) {
120                throw new IllegalArgumentException("time cannot be negative");
121            }
122        }
123
124        private long getTime(final RepositorySystemSession session) {
125            return ConfigUtils.getLong(session, DEFAULT_TIME, TIME_KEY);
126        }
127
128        private TimeUnit getTimeUnit(final RepositorySystemSession session) {
129            return TimeUnit.valueOf(ConfigUtils.getString(session, DEFAULT_TIME_UNIT.name(), TIME_UNIT_KEY));
130        }
131
132        @Override
133        public void acquire(Collection<? extends Artifact> artifacts, Collection<? extends Metadata> metadatas) {
134            Collection<String> keys = lockNaming.nameLocks(session, artifacts, metadatas);
135            if (keys.isEmpty()) {
136                return;
137            }
138
139            LOGGER.trace("Need {} {} lock(s) for {}", keys.size(), shared ? "read" : "write", keys);
140            int acquiredLockCount = 0;
141            for (String key : keys) {
142                NamedLock namedLock = namedLockFactory.getLock(key);
143                try {
144                    LOGGER.trace("Acquiring {} lock for '{}'", shared ? "read" : "write", key);
145
146                    boolean locked;
147                    if (shared) {
148                        locked = namedLock.lockShared(time, timeUnit);
149                    } else {
150                        locked = namedLock.lockExclusively(time, timeUnit);
151                    }
152
153                    if (!locked) {
154                        LOGGER.trace("Failed to acquire {} lock for '{}'", shared ? "read" : "write", key);
155
156                        namedLock.close();
157                        throw new IllegalStateException("Could not acquire " + (shared ? "read" : "write")
158                                + " lock for '" + namedLock.name() + "'");
159                    }
160
161                    locks.push(namedLock);
162                    acquiredLockCount++;
163                } catch (InterruptedException e) {
164                    Thread.currentThread().interrupt();
165                    throw new RuntimeException(e);
166                }
167            }
168            LOGGER.trace("Total locks acquired: {}", acquiredLockCount);
169        }
170
171        @Override
172        public void close() {
173            if (locks.isEmpty()) {
174                return;
175            }
176
177            // Release locks in reverse insertion order
178            int released = 0;
179            while (!locks.isEmpty()) {
180                try (NamedLock namedLock = locks.pop()) {
181                    LOGGER.trace("Releasing {} lock for '{}'", shared ? "read" : "write", namedLock.name());
182                    namedLock.unlock();
183                    released++;
184                }
185            }
186            LOGGER.trace("Total locks released: {}", released);
187        }
188    }
189}