001package org.eclipse.aether;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.Closeable;
023import java.util.Collection;
024
025import org.eclipse.aether.artifact.Artifact;
026import org.eclipse.aether.metadata.Metadata;
027
028/**
029 * A synchronization context used to coordinate concurrent access to artifacts or metadatas. The typical usage of a
030 * synchronization context looks like this:
031 * 
032 * <pre>
033 * SyncContext syncContext = repositorySystem.newSyncContext( ... );
034 * try {
035 *     syncContext.acquire( artifacts, metadatas );
036 *     // work with the artifacts and metadatas
037 * } finally {
038 *     syncContext.close();
039 * }
040 * </pre>
041 * 
042 * Within one thread, synchronization contexts may be nested which can naturally happen in a hierarchy of method calls.
043 * The nested synchronization contexts may also acquire overlapping sets of artifacts/metadatas as long as the following
044 * conditions are met. If the outer-most context holding a particular resource is exclusive, that resource can be
045 * reacquired in any nested context. If however the outer-most context is shared, the resource may only be reacquired by
046 * nested contexts if these are also shared.
047 * <p>
048 * A synchronization context is meant to be utilized by only one thread and as such is not thread-safe.
049 * <p>
050 * Note that the level of actual synchronization is subject to the implementation and might range from OS-wide to none.
051 * 
052 * @see RepositorySystem#newSyncContext(RepositorySystemSession, boolean)
053 */
054public interface SyncContext
055    extends Closeable
056{
057
058    /**
059     * Acquires synchronized access to the specified artifacts and metadatas. The invocation will potentially block
060     * until all requested resources can be acquired by the calling thread. Acquiring resources that are already
061     * acquired by this synchronization context has no effect. Please also see the class-level documentation for
062     * information regarding reentrancy. The method may be invoked multiple times on a synchronization context until all
063     * desired resources have been acquired.
064     * 
065     * @param artifacts The artifacts to acquire, may be {@code null} or empty if none.
066     * @param metadatas The metadatas to acquire, may be {@code null} or empty if none.
067     */
068    void acquire( Collection<? extends Artifact> artifacts, Collection<? extends Metadata> metadatas );
069
070    /**
071     * Releases all previously acquired artifacts/metadatas. If no resources have been acquired before or if this
072     * synchronization context has already been closed, this method does nothing.
073     */
074    void close();
075
076}