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;
020
021import java.io.Closeable;
022import java.util.Collection;
023
024import org.eclipse.aether.artifact.Artifact;
025import org.eclipse.aether.metadata.Metadata;
026
027/**
028 * A synchronization context used to coordinate concurrent access to artifacts or metadatas. The typical usage of a
029 * synchronization context looks like this:
030 *
031 * <pre>
032 * SyncContext syncContext = repositorySystem.newSyncContext( ... );
033 * try {
034 *     syncContext.acquire( artifacts, metadatas );
035 *     // work with the artifacts and metadatas
036 * } finally {
037 *     syncContext.close();
038 * }
039 * </pre>
040 *
041 * Within one thread, synchronization contexts may be nested which can naturally happen in a hierarchy of method calls.
042 * The nested synchronization contexts may also acquire overlapping sets of artifacts/metadatas as long as the following
043 * conditions are met. If the outer-most context holding a particular resource is exclusive, that resource can be
044 * reacquired in any nested context. If however the outer-most context is shared, the resource may only be reacquired by
045 * nested contexts if these are also shared.
046 * <p>
047 * A synchronization context is meant to be utilized by only one thread and as such is not thread-safe.
048 * <p>
049 * Note that the level of actual synchronization is subject to the implementation and might range from OS-wide to none.
050 *
051 * @see RepositorySystem#newSyncContext(RepositorySystemSession, boolean)
052 */
053public interface SyncContext extends Closeable {
054
055    /**
056     * Acquires synchronized access to the specified artifacts and metadatas. The invocation will potentially block
057     * until all requested resources can be acquired by the calling thread. Acquiring resources that are already
058     * acquired by this synchronization context has no effect. Please also see the class-level documentation for
059     * information regarding reentrancy. The method may be invoked multiple times on a synchronization context until all
060     * desired resources have been acquired.
061     *
062     * @param artifacts The artifacts to acquire, may be {@code null} or empty if none.
063     * @param metadatas The metadatas to acquire, may be {@code null} or empty if none.
064     */
065    void acquire(Collection<? extends Artifact> artifacts, Collection<? extends Metadata> metadatas);
066
067    /**
068     * Releases all previously acquired artifacts/metadatas. If no resources have been acquired before or if this
069     * synchronization context has already been closed, this method does nothing.
070     */
071    void close();
072}