http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java deleted file mode 100644 index 56951fa..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java +++ /dev/null @@ -1,663 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Set; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.aether.RepositoryEvent; -import org.eclipse.aether.RepositoryEvent.EventType; -import org.eclipse.aether.RepositoryException; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.RequestTrace; -import org.eclipse.aether.SyncContext; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.deployment.DeployRequest; -import org.eclipse.aether.deployment.DeployResult; -import org.eclipse.aether.deployment.DeploymentException; -import org.eclipse.aether.impl.Deployer; -import org.eclipse.aether.impl.MetadataGenerator; -import org.eclipse.aether.impl.MetadataGeneratorFactory; -import org.eclipse.aether.impl.OfflineController; -import org.eclipse.aether.impl.RemoteRepositoryManager; -import org.eclipse.aether.impl.RepositoryConnectorProvider; -import org.eclipse.aether.impl.RepositoryEventDispatcher; -import org.eclipse.aether.impl.SyncContextFactory; -import org.eclipse.aether.impl.UpdateCheck; -import org.eclipse.aether.impl.UpdateCheckManager; -import org.eclipse.aether.metadata.MergeableMetadata; -import org.eclipse.aether.metadata.Metadata; -import org.eclipse.aether.repository.LocalRepositoryManager; -import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.repository.RepositoryPolicy; -import org.eclipse.aether.spi.connector.ArtifactUpload; -import org.eclipse.aether.spi.connector.MetadataDownload; -import org.eclipse.aether.spi.connector.MetadataUpload; -import org.eclipse.aether.spi.connector.RepositoryConnector; -import org.eclipse.aether.spi.io.FileProcessor; -import org.eclipse.aether.spi.locator.Service; -import org.eclipse.aether.spi.locator.ServiceLocator; -import org.eclipse.aether.spi.log.Logger; -import org.eclipse.aether.spi.log.LoggerFactory; -import org.eclipse.aether.spi.log.NullLoggerFactory; -import org.eclipse.aether.transfer.ArtifactTransferException; -import org.eclipse.aether.transfer.MetadataNotFoundException; -import org.eclipse.aether.transfer.MetadataTransferException; -import org.eclipse.aether.transfer.NoRepositoryConnectorException; -import org.eclipse.aether.transfer.RepositoryOfflineException; -import org.eclipse.aether.transfer.TransferCancelledException; -import org.eclipse.aether.transfer.TransferEvent; - -/** - */ -@Named -public class DefaultDeployer - implements Deployer, Service -{ - - private Logger logger = NullLoggerFactory.LOGGER; - - private FileProcessor fileProcessor; - - private RepositoryEventDispatcher repositoryEventDispatcher; - - private RepositoryConnectorProvider repositoryConnectorProvider; - - private RemoteRepositoryManager remoteRepositoryManager; - - private UpdateCheckManager updateCheckManager; - - private Collection<MetadataGeneratorFactory> metadataFactories = new ArrayList<MetadataGeneratorFactory>(); - - private SyncContextFactory syncContextFactory; - - private OfflineController offlineController; - - public DefaultDeployer() - { - // enables default constructor - } - - @Inject - DefaultDeployer( FileProcessor fileProcessor, RepositoryEventDispatcher repositoryEventDispatcher, - RepositoryConnectorProvider repositoryConnectorProvider, - RemoteRepositoryManager remoteRepositoryManager, UpdateCheckManager updateCheckManager, - Set<MetadataGeneratorFactory> metadataFactories, SyncContextFactory syncContextFactory, - OfflineController offlineController, LoggerFactory loggerFactory ) - { - setFileProcessor( fileProcessor ); - setRepositoryEventDispatcher( repositoryEventDispatcher ); - setRepositoryConnectorProvider( repositoryConnectorProvider ); - setRemoteRepositoryManager( remoteRepositoryManager ); - setUpdateCheckManager( updateCheckManager ); - setMetadataGeneratorFactories( metadataFactories ); - setSyncContextFactory( syncContextFactory ); - setLoggerFactory( loggerFactory ); - setOfflineController( offlineController ); - } - - public void initService( ServiceLocator locator ) - { - setLoggerFactory( locator.getService( LoggerFactory.class ) ); - setFileProcessor( locator.getService( FileProcessor.class ) ); - setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); - setRepositoryConnectorProvider( locator.getService( RepositoryConnectorProvider.class ) ); - setRemoteRepositoryManager( locator.getService( RemoteRepositoryManager.class ) ); - setUpdateCheckManager( locator.getService( UpdateCheckManager.class ) ); - setMetadataGeneratorFactories( locator.getServices( MetadataGeneratorFactory.class ) ); - setSyncContextFactory( locator.getService( SyncContextFactory.class ) ); - setOfflineController( locator.getService( OfflineController.class ) ); - } - - public DefaultDeployer setLoggerFactory( LoggerFactory loggerFactory ) - { - this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); - return this; - } - - public DefaultDeployer setFileProcessor( FileProcessor fileProcessor ) - { - if ( fileProcessor == null ) - { - throw new IllegalArgumentException( "file processor has not been specified" ); - } - this.fileProcessor = fileProcessor; - return this; - } - - public DefaultDeployer setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher ) - { - if ( repositoryEventDispatcher == null ) - { - throw new IllegalArgumentException( "repository event dispatcher has not been specified" ); - } - this.repositoryEventDispatcher = repositoryEventDispatcher; - return this; - } - - public DefaultDeployer setRepositoryConnectorProvider( RepositoryConnectorProvider repositoryConnectorProvider ) - { - if ( repositoryConnectorProvider == null ) - { - throw new IllegalArgumentException( "repository connector provider has not been specified" ); - } - this.repositoryConnectorProvider = repositoryConnectorProvider; - return this; - } - - public DefaultDeployer setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ) - { - if ( remoteRepositoryManager == null ) - { - throw new IllegalArgumentException( "remote repository manager has not been specified" ); - } - this.remoteRepositoryManager = remoteRepositoryManager; - return this; - } - - public DefaultDeployer setUpdateCheckManager( UpdateCheckManager updateCheckManager ) - { - if ( updateCheckManager == null ) - { - throw new IllegalArgumentException( "update check manager has not been specified" ); - } - this.updateCheckManager = updateCheckManager; - return this; - } - - public DefaultDeployer addMetadataGeneratorFactory( MetadataGeneratorFactory factory ) - { - if ( factory == null ) - { - throw new IllegalArgumentException( "metadata generator factory has not been specified" ); - } - metadataFactories.add( factory ); - return this; - } - - public DefaultDeployer setMetadataGeneratorFactories( Collection<MetadataGeneratorFactory> metadataFactories ) - { - if ( metadataFactories == null ) - { - this.metadataFactories = new ArrayList<MetadataGeneratorFactory>(); - } - else - { - this.metadataFactories = metadataFactories; - } - return this; - } - - public DefaultDeployer setSyncContextFactory( SyncContextFactory syncContextFactory ) - { - if ( syncContextFactory == null ) - { - throw new IllegalArgumentException( "sync context factory has not been specified" ); - } - this.syncContextFactory = syncContextFactory; - return this; - } - - public DefaultDeployer setOfflineController( OfflineController offlineController ) - { - if ( offlineController == null ) - { - throw new IllegalArgumentException( "offline controller has not been specified" ); - } - this.offlineController = offlineController; - return this; - } - - public DeployResult deploy( RepositorySystemSession session, DeployRequest request ) - throws DeploymentException - { - try - { - Utils.checkOffline( session, offlineController, request.getRepository() ); - } - catch ( RepositoryOfflineException e ) - { - throw new DeploymentException( "Cannot deploy while " + request.getRepository().getId() + " (" - + request.getRepository().getUrl() + ") is in offline mode", e ); - } - - SyncContext syncContext = syncContextFactory.newInstance( session, false ); - - try - { - return deploy( syncContext, session, request ); - } - finally - { - syncContext.close(); - } - } - - private DeployResult deploy( SyncContext syncContext, RepositorySystemSession session, DeployRequest request ) - throws DeploymentException - { - DeployResult result = new DeployResult( request ); - - RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); - - RemoteRepository repository = request.getRepository(); - - RepositoryConnector connector; - try - { - connector = repositoryConnectorProvider.newRepositoryConnector( session, repository ); - } - catch ( NoRepositoryConnectorException e ) - { - throw new DeploymentException( "Failed to deploy artifacts/metadata: " + e.getMessage(), e ); - } - - try - { - List<? extends MetadataGenerator> generators = getMetadataGenerators( session, request ); - - List<ArtifactUpload> artifactUploads = new ArrayList<ArtifactUpload>(); - List<MetadataUpload> metadataUploads = new ArrayList<MetadataUpload>(); - IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<Metadata, Object>(); - - EventCatapult catapult = new EventCatapult( session, trace, repository, repositoryEventDispatcher ); - - List<Artifact> artifacts = new ArrayList<Artifact>( request.getArtifacts() ); - - List<Metadata> metadatas = Utils.prepareMetadata( generators, artifacts ); - - syncContext.acquire( artifacts, Utils.combine( request.getMetadata(), metadatas ) ); - - for ( Metadata metadata : metadatas ) - { - upload( metadataUploads, session, metadata, repository, connector, catapult ); - processedMetadata.put( metadata, null ); - } - - for ( int i = 0; i < artifacts.size(); i++ ) - { - Artifact artifact = artifacts.get( i ); - - for ( MetadataGenerator generator : generators ) - { - artifact = generator.transformArtifact( artifact ); - } - - artifacts.set( i, artifact ); - - ArtifactUpload upload = new ArtifactUpload( artifact, artifact.getFile() ); - upload.setTrace( trace ); - upload.setListener( new ArtifactUploadListener( catapult, upload, logger ) ); - artifactUploads.add( upload ); - } - - connector.put( artifactUploads, null ); - - for ( ArtifactUpload upload : artifactUploads ) - { - if ( upload.getException() != null ) - { - throw new DeploymentException( "Failed to deploy artifacts: " + upload.getException().getMessage(), - upload.getException() ); - } - result.addArtifact( upload.getArtifact() ); - } - - metadatas = Utils.finishMetadata( generators, artifacts ); - - syncContext.acquire( null, metadatas ); - - for ( Metadata metadata : metadatas ) - { - upload( metadataUploads, session, metadata, repository, connector, catapult ); - processedMetadata.put( metadata, null ); - } - - for ( Metadata metadata : request.getMetadata() ) - { - if ( !processedMetadata.containsKey( metadata ) ) - { - upload( metadataUploads, session, metadata, repository, connector, catapult ); - processedMetadata.put( metadata, null ); - } - } - - connector.put( null, metadataUploads ); - - for ( MetadataUpload upload : metadataUploads ) - { - if ( upload.getException() != null ) - { - throw new DeploymentException( "Failed to deploy metadata: " + upload.getException().getMessage(), - upload.getException() ); - } - result.addMetadata( upload.getMetadata() ); - } - } - finally - { - connector.close(); - } - - return result; - } - - private List<? extends MetadataGenerator> getMetadataGenerators( RepositorySystemSession session, - DeployRequest request ) - { - PrioritizedComponents<MetadataGeneratorFactory> factories = - Utils.sortMetadataGeneratorFactories( session, this.metadataFactories ); - - List<MetadataGenerator> generators = new ArrayList<MetadataGenerator>(); - - for ( PrioritizedComponent<MetadataGeneratorFactory> factory : factories.getEnabled() ) - { - MetadataGenerator generator = factory.getComponent().newInstance( session, request ); - if ( generator != null ) - { - generators.add( generator ); - } - } - - return generators; - } - - private void upload( Collection<MetadataUpload> metadataUploads, RepositorySystemSession session, - Metadata metadata, RemoteRepository repository, RepositoryConnector connector, - EventCatapult catapult ) - throws DeploymentException - { - LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - File basedir = lrm.getRepository().getBasedir(); - - File dstFile = new File( basedir, lrm.getPathForRemoteMetadata( metadata, repository, "" ) ); - - if ( metadata instanceof MergeableMetadata ) - { - if ( !( (MergeableMetadata) metadata ).isMerged() ) - { - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_RESOLVING ); - event.setTrace( catapult.getTrace() ); - event.setMetadata( metadata ); - event.setRepository( repository ); - repositoryEventDispatcher.dispatch( event.build() ); - - event = new RepositoryEvent.Builder( session, EventType.METADATA_DOWNLOADING ); - event.setTrace( catapult.getTrace() ); - event.setMetadata( metadata ); - event.setRepository( repository ); - repositoryEventDispatcher.dispatch( event.build() ); - } - - RepositoryPolicy policy = getPolicy( session, repository, metadata.getNature() ); - MetadataDownload download = new MetadataDownload(); - download.setMetadata( metadata ); - download.setFile( dstFile ); - download.setChecksumPolicy( policy.getChecksumPolicy() ); - download.setListener( SafeTransferListener.wrap( session, logger ) ); - download.setTrace( catapult.getTrace() ); - connector.get( null, Arrays.asList( download ) ); - - Exception error = download.getException(); - - if ( error instanceof MetadataNotFoundException ) - { - dstFile.delete(); - } - - { - RepositoryEvent.Builder event = - new RepositoryEvent.Builder( session, EventType.METADATA_DOWNLOADED ); - event.setTrace( catapult.getTrace() ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setException( error ); - event.setFile( dstFile ); - repositoryEventDispatcher.dispatch( event.build() ); - - event = new RepositoryEvent.Builder( session, EventType.METADATA_RESOLVED ); - event.setTrace( catapult.getTrace() ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setException( error ); - event.setFile( dstFile ); - repositoryEventDispatcher.dispatch( event.build() ); - } - - if ( error != null && !( error instanceof MetadataNotFoundException ) ) - { - throw new DeploymentException( "Failed to retrieve remote metadata " + metadata + ": " - + error.getMessage(), error ); - } - } - - try - { - ( (MergeableMetadata) metadata ).merge( dstFile, dstFile ); - } - catch ( RepositoryException e ) - { - throw new DeploymentException( "Failed to update metadata " + metadata + ": " + e.getMessage(), e ); - } - } - else - { - if ( metadata.getFile() == null ) - { - throw new DeploymentException( "Failed to update metadata " + metadata + ": No file attached." ); - } - try - { - fileProcessor.copy( metadata.getFile(), dstFile ); - } - catch ( IOException e ) - { - throw new DeploymentException( "Failed to update metadata " + metadata + ": " + e.getMessage(), e ); - } - } - - UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<Metadata, MetadataTransferException>(); - check.setItem( metadata ); - check.setFile( dstFile ); - check.setRepository( repository ); - check.setAuthoritativeRepository( repository ); - updateCheckManager.touchMetadata( session, check ); - - MetadataUpload upload = new MetadataUpload( metadata, dstFile ); - upload.setTrace( catapult.getTrace() ); - upload.setListener( new MetadataUploadListener( catapult, upload, logger ) ); - metadataUploads.add( upload ); - } - - private RepositoryPolicy getPolicy( RepositorySystemSession session, RemoteRepository repository, - Metadata.Nature nature ) - { - boolean releases = !Metadata.Nature.SNAPSHOT.equals( nature ); - boolean snapshots = !Metadata.Nature.RELEASE.equals( nature ); - return remoteRepositoryManager.getPolicy( session, repository, releases, snapshots ); - } - - static final class EventCatapult - { - - private final RepositorySystemSession session; - - private final RequestTrace trace; - - private final RemoteRepository repository; - - private final RepositoryEventDispatcher dispatcher; - - public EventCatapult( RepositorySystemSession session, RequestTrace trace, RemoteRepository repository, - RepositoryEventDispatcher dispatcher ) - { - this.session = session; - this.trace = trace; - this.repository = repository; - this.dispatcher = dispatcher; - } - - public RepositorySystemSession getSession() - { - return session; - } - - public RequestTrace getTrace() - { - return trace; - } - - public void artifactDeploying( Artifact artifact, File file ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DEPLOYING ); - event.setTrace( trace ); - event.setArtifact( artifact ); - event.setRepository( repository ); - event.setFile( file ); - - dispatcher.dispatch( event.build() ); - } - - public void artifactDeployed( Artifact artifact, File file, ArtifactTransferException exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DEPLOYED ); - event.setTrace( trace ); - event.setArtifact( artifact ); - event.setRepository( repository ); - event.setFile( file ); - event.setException( exception ); - - dispatcher.dispatch( event.build() ); - } - - public void metadataDeploying( Metadata metadata, File file ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_DEPLOYING ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setFile( file ); - - dispatcher.dispatch( event.build() ); - } - - public void metadataDeployed( Metadata metadata, File file, Exception exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_DEPLOYED ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setFile( file ); - event.setException( exception ); - - dispatcher.dispatch( event.build() ); - } - - } - - static final class ArtifactUploadListener - extends SafeTransferListener - { - - private final EventCatapult catapult; - - private final ArtifactUpload transfer; - - public ArtifactUploadListener( EventCatapult catapult, ArtifactUpload transfer, Logger logger ) - { - super( catapult.getSession(), logger ); - this.catapult = catapult; - this.transfer = transfer; - } - - @Override - public void transferInitiated( TransferEvent event ) - throws TransferCancelledException - { - super.transferInitiated( event ); - catapult.artifactDeploying( transfer.getArtifact(), transfer.getFile() ); - } - - @Override - public void transferFailed( TransferEvent event ) - { - super.transferFailed( event ); - catapult.artifactDeployed( transfer.getArtifact(), transfer.getFile(), transfer.getException() ); - } - - @Override - public void transferSucceeded( TransferEvent event ) - { - super.transferSucceeded( event ); - catapult.artifactDeployed( transfer.getArtifact(), transfer.getFile(), null ); - } - - } - - static final class MetadataUploadListener - extends SafeTransferListener - { - - private final EventCatapult catapult; - - private final MetadataUpload transfer; - - public MetadataUploadListener( EventCatapult catapult, MetadataUpload transfer, Logger logger ) - { - super( catapult.getSession(), logger ); - this.catapult = catapult; - this.transfer = transfer; - } - - @Override - public void transferInitiated( TransferEvent event ) - throws TransferCancelledException - { - super.transferInitiated( event ); - catapult.metadataDeploying( transfer.getMetadata(), transfer.getFile() ); - } - - @Override - public void transferFailed( TransferEvent event ) - { - super.transferFailed( event ); - catapult.metadataDeployed( transfer.getMetadata(), transfer.getFile(), transfer.getException() ); - } - - @Override - public void transferSucceeded( TransferEvent event ) - { - super.transferSucceeded( event ); - catapult.metadataDeployed( transfer.getMetadata(), transfer.getFile(), null ); - } - - } - -}
http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java deleted file mode 100644 index cfeac98..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java +++ /dev/null @@ -1,228 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.Closeable; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.ByteBuffer; - -import javax.inject.Named; - -import org.eclipse.aether.spi.io.FileProcessor; - -/** - * A utility class helping with file-based operations. - */ -@Named -public class DefaultFileProcessor - implements FileProcessor -{ - - private static void close( Closeable closeable ) - { - if ( closeable != null ) - { - try - { - closeable.close(); - } - catch ( IOException e ) - { - // too bad but who cares - } - } - } - - /** - * Thread-safe variant of {@link File#mkdirs()}. Creates the directory named by the given abstract pathname, - * including any necessary but nonexistent parent directories. Note that if this operation fails it may have - * succeeded in creating some of the necessary parent directories. - * - * @param directory The directory to create, may be {@code null}. - * @return {@code true} if and only if the directory was created, along with all necessary parent directories; - * {@code false} otherwise - */ - public boolean mkdirs( File directory ) - { - if ( directory == null ) - { - return false; - } - - if ( directory.exists() ) - { - return false; - } - if ( directory.mkdir() ) - { - return true; - } - - File canonDir; - try - { - canonDir = directory.getCanonicalFile(); - } - catch ( IOException e ) - { - return false; - } - - File parentDir = canonDir.getParentFile(); - return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() ); - } - - public void write( File target, String data ) - throws IOException - { - mkdirs( target.getAbsoluteFile().getParentFile() ); - - OutputStream fos = null; - try - { - fos = new FileOutputStream( target ); - - if ( data != null ) - { - fos.write( data.getBytes( "UTF-8" ) ); - } - - // allow output to report any flush/close errors - fos.close(); - } - finally - { - close( fos ); - } - } - - public void write( File target, InputStream source ) - throws IOException - { - mkdirs( target.getAbsoluteFile().getParentFile() ); - - OutputStream fos = null; - try - { - fos = new FileOutputStream( target ); - - copy( fos, source, null ); - - // allow output to report any flush/close errors - fos.close(); - } - finally - { - close( fos ); - } - } - - public void copy( File source, File target ) - throws IOException - { - copy( source, target, null ); - } - - public long copy( File source, File target, ProgressListener listener ) - throws IOException - { - long total = 0; - - InputStream fis = null; - OutputStream fos = null; - try - { - fis = new FileInputStream( source ); - - mkdirs( target.getAbsoluteFile().getParentFile() ); - - fos = new FileOutputStream( target ); - - total = copy( fos, fis, listener ); - - // allow output to report any flush/close errors - fos.close(); - } - finally - { - close( fis ); - close( fos ); - } - - return total; - } - - private long copy( OutputStream os, InputStream is, ProgressListener listener ) - throws IOException - { - long total = 0; - - ByteBuffer buffer = ByteBuffer.allocate( 1024 * 32 ); - byte[] array = buffer.array(); - - while ( true ) - { - int bytes = is.read( array ); - if ( bytes < 0 ) - { - break; - } - - os.write( array, 0, bytes ); - - total += bytes; - - if ( listener != null && bytes > 0 ) - { - try - { - buffer.rewind(); - buffer.limit( bytes ); - listener.progressed( buffer ); - } - catch ( Exception e ) - { - // too bad - } - } - } - - return total; - } - - public void move( File source, File target ) - throws IOException - { - if ( !source.renameTo( target ) ) - { - copy( source, target ); - - target.setLastModified( source.lastModified() ); - - source.delete(); - } - } - -} http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java deleted file mode 100644 index ea95371..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java +++ /dev/null @@ -1,391 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Set; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.aether.RepositoryEvent; -import org.eclipse.aether.RepositoryEvent.EventType; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.RequestTrace; -import org.eclipse.aether.SyncContext; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.impl.Installer; -import org.eclipse.aether.impl.MetadataGenerator; -import org.eclipse.aether.impl.MetadataGeneratorFactory; -import org.eclipse.aether.impl.RepositoryEventDispatcher; -import org.eclipse.aether.impl.SyncContextFactory; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.installation.InstallResult; -import org.eclipse.aether.installation.InstallationException; -import org.eclipse.aether.metadata.MergeableMetadata; -import org.eclipse.aether.metadata.Metadata; -import org.eclipse.aether.repository.LocalArtifactRegistration; -import org.eclipse.aether.repository.LocalMetadataRegistration; -import org.eclipse.aether.repository.LocalRepositoryManager; -import org.eclipse.aether.spi.io.FileProcessor; -import org.eclipse.aether.spi.locator.Service; -import org.eclipse.aether.spi.locator.ServiceLocator; -import org.eclipse.aether.spi.log.Logger; -import org.eclipse.aether.spi.log.LoggerFactory; -import org.eclipse.aether.spi.log.NullLoggerFactory; - -/** - */ -@Named -public class DefaultInstaller - implements Installer, Service -{ - - private Logger logger = NullLoggerFactory.LOGGER; - - private FileProcessor fileProcessor; - - private RepositoryEventDispatcher repositoryEventDispatcher; - - private Collection<MetadataGeneratorFactory> metadataFactories = new ArrayList<MetadataGeneratorFactory>(); - - private SyncContextFactory syncContextFactory; - - public DefaultInstaller() - { - // enables default constructor - } - - @Inject - DefaultInstaller( FileProcessor fileProcessor, RepositoryEventDispatcher repositoryEventDispatcher, - Set<MetadataGeneratorFactory> metadataFactories, SyncContextFactory syncContextFactory, - LoggerFactory loggerFactory ) - { - setFileProcessor( fileProcessor ); - setRepositoryEventDispatcher( repositoryEventDispatcher ); - setMetadataGeneratorFactories( metadataFactories ); - setSyncContextFactory( syncContextFactory ); - setLoggerFactory( loggerFactory ); - } - - public void initService( ServiceLocator locator ) - { - setLoggerFactory( locator.getService( LoggerFactory.class ) ); - setFileProcessor( locator.getService( FileProcessor.class ) ); - setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); - setMetadataGeneratorFactories( locator.getServices( MetadataGeneratorFactory.class ) ); - setSyncContextFactory( locator.getService( SyncContextFactory.class ) ); - } - - public DefaultInstaller setLoggerFactory( LoggerFactory loggerFactory ) - { - this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); - return this; - } - - public DefaultInstaller setFileProcessor( FileProcessor fileProcessor ) - { - if ( fileProcessor == null ) - { - throw new IllegalArgumentException( "file processor has not been specified" ); - } - this.fileProcessor = fileProcessor; - return this; - } - - public DefaultInstaller setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher ) - { - if ( repositoryEventDispatcher == null ) - { - throw new IllegalArgumentException( "repository event dispatcher has not been specified" ); - } - this.repositoryEventDispatcher = repositoryEventDispatcher; - return this; - } - - public DefaultInstaller addMetadataGeneratorFactory( MetadataGeneratorFactory factory ) - { - if ( factory == null ) - { - throw new IllegalArgumentException( "metadata generator factory has not been specified" ); - } - metadataFactories.add( factory ); - return this; - } - - public DefaultInstaller setMetadataGeneratorFactories( Collection<MetadataGeneratorFactory> metadataFactories ) - { - if ( metadataFactories == null ) - { - this.metadataFactories = new ArrayList<MetadataGeneratorFactory>(); - } - else - { - this.metadataFactories = metadataFactories; - } - return this; - } - - public DefaultInstaller setSyncContextFactory( SyncContextFactory syncContextFactory ) - { - if ( syncContextFactory == null ) - { - throw new IllegalArgumentException( "sync context factory has not been specified" ); - } - this.syncContextFactory = syncContextFactory; - return this; - } - - public InstallResult install( RepositorySystemSession session, InstallRequest request ) - throws InstallationException - { - SyncContext syncContext = syncContextFactory.newInstance( session, false ); - - try - { - return install( syncContext, session, request ); - } - finally - { - syncContext.close(); - } - } - - private InstallResult install( SyncContext syncContext, RepositorySystemSession session, InstallRequest request ) - throws InstallationException - { - InstallResult result = new InstallResult( request ); - - RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); - - List<? extends MetadataGenerator> generators = getMetadataGenerators( session, request ); - - List<Artifact> artifacts = new ArrayList<Artifact>( request.getArtifacts() ); - - IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<Metadata, Object>(); - - List<Metadata> metadatas = Utils.prepareMetadata( generators, artifacts ); - - syncContext.acquire( artifacts, Utils.combine( request.getMetadata(), metadatas ) ); - - for ( Metadata metadata : metadatas ) - { - install( session, trace, metadata ); - processedMetadata.put( metadata, null ); - result.addMetadata( metadata ); - } - - for ( int i = 0; i < artifacts.size(); i++ ) - { - Artifact artifact = artifacts.get( i ); - - for ( MetadataGenerator generator : generators ) - { - artifact = generator.transformArtifact( artifact ); - } - - artifacts.set( i, artifact ); - - install( session, trace, artifact ); - result.addArtifact( artifact ); - } - - metadatas = Utils.finishMetadata( generators, artifacts ); - - syncContext.acquire( null, metadatas ); - - for ( Metadata metadata : metadatas ) - { - install( session, trace, metadata ); - processedMetadata.put( metadata, null ); - result.addMetadata( metadata ); - } - - for ( Metadata metadata : request.getMetadata() ) - { - if ( !processedMetadata.containsKey( metadata ) ) - { - install( session, trace, metadata ); - result.addMetadata( metadata ); - } - } - - return result; - } - - private List<? extends MetadataGenerator> getMetadataGenerators( RepositorySystemSession session, - InstallRequest request ) - { - PrioritizedComponents<MetadataGeneratorFactory> factories = - Utils.sortMetadataGeneratorFactories( session, this.metadataFactories ); - - List<MetadataGenerator> generators = new ArrayList<MetadataGenerator>(); - - for ( PrioritizedComponent<MetadataGeneratorFactory> factory : factories.getEnabled() ) - { - MetadataGenerator generator = factory.getComponent().newInstance( session, request ); - if ( generator != null ) - { - generators.add( generator ); - } - } - - return generators; - } - - private void install( RepositorySystemSession session, RequestTrace trace, Artifact artifact ) - throws InstallationException - { - LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - - File srcFile = artifact.getFile(); - - File dstFile = new File( lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact( artifact ) ); - - artifactInstalling( session, trace, artifact, dstFile ); - - Exception exception = null; - try - { - if ( dstFile.equals( srcFile ) ) - { - throw new IllegalArgumentException( "Cannot install " + dstFile + " to same path" ); - } - - boolean copy = - "pom".equals( artifact.getExtension() ) || srcFile.lastModified() != dstFile.lastModified() - || srcFile.length() != dstFile.length() || !srcFile.exists(); - - if ( copy ) - { - fileProcessor.copy( srcFile, dstFile ); - dstFile.setLastModified( srcFile.lastModified() ); - } - else - { - logger.debug( "Skipped re-installing " + srcFile + " to " + dstFile + ", seems unchanged" ); - } - - lrm.add( session, new LocalArtifactRegistration( artifact ) ); - } - catch ( Exception e ) - { - exception = e; - throw new InstallationException( "Failed to install artifact " + artifact + ": " + e.getMessage(), e ); - } - finally - { - artifactInstalled( session, trace, artifact, dstFile, exception ); - } - } - - private void install( RepositorySystemSession session, RequestTrace trace, Metadata metadata ) - throws InstallationException - { - LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - - File dstFile = new File( lrm.getRepository().getBasedir(), lrm.getPathForLocalMetadata( metadata ) ); - - metadataInstalling( session, trace, metadata, dstFile ); - - Exception exception = null; - try - { - if ( metadata instanceof MergeableMetadata ) - { - ( (MergeableMetadata) metadata ).merge( dstFile, dstFile ); - } - else - { - if ( dstFile.equals( metadata.getFile() ) ) - { - throw new IllegalArgumentException( "Cannot install " + dstFile + " to same path" ); - } - fileProcessor.copy( metadata.getFile(), dstFile ); - } - - lrm.add( session, new LocalMetadataRegistration( metadata ) ); - } - catch ( Exception e ) - { - exception = e; - throw new InstallationException( "Failed to install metadata " + metadata + ": " + e.getMessage(), e ); - } - finally - { - metadataInstalled( session, trace, metadata, dstFile, exception ); - } - } - - private void artifactInstalling( RepositorySystemSession session, RequestTrace trace, Artifact artifact, - File dstFile ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_INSTALLING ); - event.setTrace( trace ); - event.setArtifact( artifact ); - event.setRepository( session.getLocalRepositoryManager().getRepository() ); - event.setFile( dstFile ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void artifactInstalled( RepositorySystemSession session, RequestTrace trace, Artifact artifact, - File dstFile, Exception exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_INSTALLED ); - event.setTrace( trace ); - event.setArtifact( artifact ); - event.setRepository( session.getLocalRepositoryManager().getRepository() ); - event.setFile( dstFile ); - event.setException( exception ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void metadataInstalling( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - File dstFile ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_INSTALLING ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( session.getLocalRepositoryManager().getRepository() ); - event.setFile( dstFile ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void metadataInstalled( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - File dstFile, Exception exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_INSTALLED ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( session.getLocalRepositoryManager().getRepository() ); - event.setFile( dstFile ); - event.setException( exception ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - -} http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java deleted file mode 100644 index 1f0597a..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java +++ /dev/null @@ -1,162 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.impl.LocalRepositoryProvider; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.LocalRepositoryManager; -import org.eclipse.aether.repository.NoLocalRepositoryManagerException; -import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; -import org.eclipse.aether.spi.locator.Service; -import org.eclipse.aether.spi.locator.ServiceLocator; -import org.eclipse.aether.spi.log.Logger; -import org.eclipse.aether.spi.log.LoggerFactory; -import org.eclipse.aether.spi.log.NullLoggerFactory; - -/** - */ -@Named -public class DefaultLocalRepositoryProvider - implements LocalRepositoryProvider, Service -{ - - private Logger logger = NullLoggerFactory.LOGGER; - - private Collection<LocalRepositoryManagerFactory> managerFactories = new ArrayList<LocalRepositoryManagerFactory>(); - - public DefaultLocalRepositoryProvider() - { - // enables default constructor - } - - @Inject - DefaultLocalRepositoryProvider( Set<LocalRepositoryManagerFactory> factories, LoggerFactory loggerFactory ) - { - setLocalRepositoryManagerFactories( factories ); - setLoggerFactory( loggerFactory ); - } - - public void initService( ServiceLocator locator ) - { - setLoggerFactory( locator.getService( LoggerFactory.class ) ); - setLocalRepositoryManagerFactories( locator.getServices( LocalRepositoryManagerFactory.class ) ); - } - - public DefaultLocalRepositoryProvider setLoggerFactory( LoggerFactory loggerFactory ) - { - this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); - return this; - } - - public DefaultLocalRepositoryProvider addLocalRepositoryManagerFactory( LocalRepositoryManagerFactory factory ) - { - if ( factory == null ) - { - throw new IllegalArgumentException( "Local repository manager factory has not been specified." ); - } - managerFactories.add( factory ); - return this; - } - - public DefaultLocalRepositoryProvider setLocalRepositoryManagerFactories( Collection<LocalRepositoryManagerFactory> factories ) - { - if ( factories == null ) - { - managerFactories = new ArrayList<LocalRepositoryManagerFactory>( 2 ); - } - else - { - managerFactories = factories; - } - return this; - } - - public LocalRepositoryManager newLocalRepositoryManager( RepositorySystemSession session, LocalRepository repository ) - throws NoLocalRepositoryManagerException - { - PrioritizedComponents<LocalRepositoryManagerFactory> factories = - new PrioritizedComponents<LocalRepositoryManagerFactory>( session ); - for ( LocalRepositoryManagerFactory factory : this.managerFactories ) - { - factories.add( factory, factory.getPriority() ); - } - - List<NoLocalRepositoryManagerException> errors = new ArrayList<NoLocalRepositoryManagerException>(); - for ( PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled() ) - { - try - { - LocalRepositoryManager manager = factory.getComponent().newInstance( session, repository ); - - if ( logger.isDebugEnabled() ) - { - StringBuilder buffer = new StringBuilder( 256 ); - buffer.append( "Using manager " ).append( manager.getClass().getSimpleName() ); - Utils.appendClassLoader( buffer, manager ); - buffer.append( " with priority " ).append( factory.getPriority() ); - buffer.append( " for " ).append( repository.getBasedir() ); - - logger.debug( buffer.toString() ); - } - - return manager; - } - catch ( NoLocalRepositoryManagerException e ) - { - // continue and try next factory - errors.add( e ); - } - } - if ( logger.isDebugEnabled() && errors.size() > 1 ) - { - String msg = "Could not obtain local repository manager for " + repository; - for ( Exception e : errors ) - { - logger.debug( msg, e ); - } - } - - StringBuilder buffer = new StringBuilder( 256 ); - if ( factories.isEmpty() ) - { - buffer.append( "No local repository managers registered" ); - } - else - { - buffer.append( "Cannot access " ).append( repository.getBasedir() ); - buffer.append( " with type " ).append( repository.getContentType() ); - buffer.append( " using the available factories " ); - factories.list( buffer ); - } - - throw new NoLocalRepositoryManagerException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) - : null ); - } - -} http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java deleted file mode 100644 index c751112..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java +++ /dev/null @@ -1,658 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.aether.RepositoryEvent; -import org.eclipse.aether.RepositoryEvent.EventType; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.RequestTrace; -import org.eclipse.aether.SyncContext; -import org.eclipse.aether.impl.MetadataResolver; -import org.eclipse.aether.impl.OfflineController; -import org.eclipse.aether.impl.RemoteRepositoryManager; -import org.eclipse.aether.impl.RepositoryConnectorProvider; -import org.eclipse.aether.impl.RepositoryEventDispatcher; -import org.eclipse.aether.impl.SyncContextFactory; -import org.eclipse.aether.impl.UpdateCheck; -import org.eclipse.aether.impl.UpdateCheckManager; -import org.eclipse.aether.metadata.Metadata; -import org.eclipse.aether.repository.ArtifactRepository; -import org.eclipse.aether.repository.LocalMetadataRegistration; -import org.eclipse.aether.repository.LocalMetadataRequest; -import org.eclipse.aether.repository.LocalMetadataResult; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.LocalRepositoryManager; -import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.repository.RepositoryPolicy; -import org.eclipse.aether.resolution.MetadataRequest; -import org.eclipse.aether.resolution.MetadataResult; -import org.eclipse.aether.spi.connector.MetadataDownload; -import org.eclipse.aether.spi.connector.RepositoryConnector; -import org.eclipse.aether.spi.locator.Service; -import org.eclipse.aether.spi.locator.ServiceLocator; -import org.eclipse.aether.spi.log.Logger; -import org.eclipse.aether.spi.log.LoggerFactory; -import org.eclipse.aether.spi.log.NullLoggerFactory; -import org.eclipse.aether.transfer.MetadataNotFoundException; -import org.eclipse.aether.transfer.MetadataTransferException; -import org.eclipse.aether.transfer.NoRepositoryConnectorException; -import org.eclipse.aether.transfer.RepositoryOfflineException; -import org.eclipse.aether.util.ConfigUtils; -import org.eclipse.aether.util.concurrency.RunnableErrorForwarder; -import org.eclipse.aether.util.concurrency.WorkerThreadFactory; - -/** - */ -@Named -public class DefaultMetadataResolver - implements MetadataResolver, Service -{ - - private static final String CONFIG_PROP_THREADS = "aether.metadataResolver.threads"; - - private Logger logger = NullLoggerFactory.LOGGER; - - private RepositoryEventDispatcher repositoryEventDispatcher; - - private UpdateCheckManager updateCheckManager; - - private RepositoryConnectorProvider repositoryConnectorProvider; - - private RemoteRepositoryManager remoteRepositoryManager; - - private SyncContextFactory syncContextFactory; - - private OfflineController offlineController; - - public DefaultMetadataResolver() - { - // enables default constructor - } - - @Inject - DefaultMetadataResolver( RepositoryEventDispatcher repositoryEventDispatcher, - UpdateCheckManager updateCheckManager, - RepositoryConnectorProvider repositoryConnectorProvider, - RemoteRepositoryManager remoteRepositoryManager, SyncContextFactory syncContextFactory, - OfflineController offlineController, LoggerFactory loggerFactory ) - { - setRepositoryEventDispatcher( repositoryEventDispatcher ); - setUpdateCheckManager( updateCheckManager ); - setRepositoryConnectorProvider( repositoryConnectorProvider ); - setRemoteRepositoryManager( remoteRepositoryManager ); - setSyncContextFactory( syncContextFactory ); - setOfflineController( offlineController ); - setLoggerFactory( loggerFactory ); - } - - public void initService( ServiceLocator locator ) - { - setLoggerFactory( locator.getService( LoggerFactory.class ) ); - setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); - setUpdateCheckManager( locator.getService( UpdateCheckManager.class ) ); - setRepositoryConnectorProvider( locator.getService( RepositoryConnectorProvider.class ) ); - setRemoteRepositoryManager( locator.getService( RemoteRepositoryManager.class ) ); - setSyncContextFactory( locator.getService( SyncContextFactory.class ) ); - setOfflineController( locator.getService( OfflineController.class ) ); - } - - public DefaultMetadataResolver setLoggerFactory( LoggerFactory loggerFactory ) - { - this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); - return this; - } - - public DefaultMetadataResolver setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher ) - { - if ( repositoryEventDispatcher == null ) - { - throw new IllegalArgumentException( "repository event dispatcher has not been specified" ); - } - this.repositoryEventDispatcher = repositoryEventDispatcher; - return this; - } - - public DefaultMetadataResolver setUpdateCheckManager( UpdateCheckManager updateCheckManager ) - { - if ( updateCheckManager == null ) - { - throw new IllegalArgumentException( "update check manager has not been specified" ); - } - this.updateCheckManager = updateCheckManager; - return this; - } - - public DefaultMetadataResolver setRepositoryConnectorProvider( RepositoryConnectorProvider repositoryConnectorProvider ) - { - if ( repositoryConnectorProvider == null ) - { - throw new IllegalArgumentException( "repository connector provider has not been specified" ); - } - this.repositoryConnectorProvider = repositoryConnectorProvider; - return this; - } - - public DefaultMetadataResolver setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ) - { - if ( remoteRepositoryManager == null ) - { - throw new IllegalArgumentException( "remote repository manager has not been specified" ); - } - this.remoteRepositoryManager = remoteRepositoryManager; - return this; - } - - public DefaultMetadataResolver setSyncContextFactory( SyncContextFactory syncContextFactory ) - { - if ( syncContextFactory == null ) - { - throw new IllegalArgumentException( "sync context factory has not been specified" ); - } - this.syncContextFactory = syncContextFactory; - return this; - } - - public DefaultMetadataResolver setOfflineController( OfflineController offlineController ) - { - if ( offlineController == null ) - { - throw new IllegalArgumentException( "offline controller has not been specified" ); - } - this.offlineController = offlineController; - return this; - } - - public List<MetadataResult> resolveMetadata( RepositorySystemSession session, - Collection<? extends MetadataRequest> requests ) - { - SyncContext syncContext = syncContextFactory.newInstance( session, false ); - - try - { - Collection<Metadata> metadata = new ArrayList<Metadata>( requests.size() ); - for ( MetadataRequest request : requests ) - { - metadata.add( request.getMetadata() ); - } - - syncContext.acquire( null, metadata ); - - return resolve( session, requests ); - } - finally - { - syncContext.close(); - } - } - - private List<MetadataResult> resolve( RepositorySystemSession session, - Collection<? extends MetadataRequest> requests ) - { - List<MetadataResult> results = new ArrayList<MetadataResult>( requests.size() ); - - List<ResolveTask> tasks = new ArrayList<ResolveTask>( requests.size() ); - - Map<File, Long> localLastUpdates = new HashMap<File, Long>(); - - for ( MetadataRequest request : requests ) - { - RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); - - MetadataResult result = new MetadataResult( request ); - results.add( result ); - - Metadata metadata = request.getMetadata(); - RemoteRepository repository = request.getRepository(); - - if ( repository == null ) - { - LocalRepository localRepo = session.getLocalRepositoryManager().getRepository(); - - metadataResolving( session, trace, metadata, localRepo ); - - File localFile = getLocalFile( session, metadata ); - - if ( localFile != null ) - { - metadata = metadata.setFile( localFile ); - result.setMetadata( metadata ); - } - else - { - result.setException( new MetadataNotFoundException( metadata, localRepo ) ); - } - - metadataResolved( session, trace, metadata, localRepo, result.getException() ); - continue; - } - - List<RemoteRepository> repositories = getEnabledSourceRepositories( repository, metadata.getNature() ); - - if ( repositories.isEmpty() ) - { - continue; - } - - metadataResolving( session, trace, metadata, repository ); - LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - LocalMetadataRequest localRequest = - new LocalMetadataRequest( metadata, repository, request.getRequestContext() ); - LocalMetadataResult lrmResult = lrm.find( session, localRequest ); - - File metadataFile = lrmResult.getFile(); - - try - { - Utils.checkOffline( session, offlineController, repository ); - } - catch ( RepositoryOfflineException e ) - { - if ( metadataFile != null ) - { - metadata = metadata.setFile( metadataFile ); - result.setMetadata( metadata ); - } - else - { - String msg = - "Cannot access " + repository.getId() + " (" + repository.getUrl() - + ") in offline mode and the metadata " + metadata - + " has not been downloaded from it before"; - result.setException( new MetadataNotFoundException( metadata, repository, msg, e ) ); - } - - metadataResolved( session, trace, metadata, repository, result.getException() ); - continue; - } - - Long localLastUpdate = null; - if ( request.isFavorLocalRepository() ) - { - File localFile = getLocalFile( session, metadata ); - localLastUpdate = localLastUpdates.get( localFile ); - if ( localLastUpdate == null ) - { - localLastUpdate = localFile != null ? localFile.lastModified() : 0; - localLastUpdates.put( localFile, localLastUpdate ); - } - } - - List<UpdateCheck<Metadata, MetadataTransferException>> checks = - new ArrayList<UpdateCheck<Metadata, MetadataTransferException>>(); - Exception exception = null; - for ( RemoteRepository repo : repositories ) - { - UpdateCheck<Metadata, MetadataTransferException> check = - new UpdateCheck<Metadata, MetadataTransferException>(); - check.setLocalLastUpdated( ( localLastUpdate != null ) ? localLastUpdate : 0 ); - check.setItem( metadata ); - - // use 'main' installation file for the check (-> use requested repository) - File checkFile = - new File( - session.getLocalRepository().getBasedir(), - session.getLocalRepositoryManager().getPathForRemoteMetadata( metadata, repository, - request.getRequestContext() ) ); - check.setFile( checkFile ); - check.setRepository( repository ); - check.setAuthoritativeRepository( repo ); - check.setPolicy( getPolicy( session, repo, metadata.getNature() ).getUpdatePolicy() ); - - if ( lrmResult.isStale() ) - { - checks.add( check ); - } - else - { - updateCheckManager.checkMetadata( session, check ); - if ( check.isRequired() ) - { - checks.add( check ); - } - else if ( exception == null ) - { - exception = check.getException(); - } - } - } - - if ( !checks.isEmpty() ) - { - RepositoryPolicy policy = getPolicy( session, repository, metadata.getNature() ); - - // install path may be different from lookup path - File installFile = - new File( - session.getLocalRepository().getBasedir(), - session.getLocalRepositoryManager().getPathForRemoteMetadata( metadata, - request.getRepository(), - request.getRequestContext() ) ); - - ResolveTask task = - new ResolveTask( session, trace, result, installFile, checks, policy.getChecksumPolicy() ); - tasks.add( task ); - } - else - { - result.setException( exception ); - if ( metadataFile != null ) - { - metadata = metadata.setFile( metadataFile ); - result.setMetadata( metadata ); - } - metadataResolved( session, trace, metadata, repository, result.getException() ); - } - } - - if ( !tasks.isEmpty() ) - { - int threads = ConfigUtils.getInteger( session, 4, CONFIG_PROP_THREADS ); - Executor executor = getExecutor( Math.min( tasks.size(), threads ) ); - try - { - RunnableErrorForwarder errorForwarder = new RunnableErrorForwarder(); - - for ( ResolveTask task : tasks ) - { - executor.execute( errorForwarder.wrap( task ) ); - } - - errorForwarder.await(); - - for ( ResolveTask task : tasks ) - { - task.result.setException( task.exception ); - } - } - finally - { - shutdown( executor ); - } - for ( ResolveTask task : tasks ) - { - Metadata metadata = task.request.getMetadata(); - // re-lookup metadata for resolve - LocalMetadataRequest localRequest = - new LocalMetadataRequest( metadata, task.request.getRepository(), task.request.getRequestContext() ); - File metadataFile = session.getLocalRepositoryManager().find( session, localRequest ).getFile(); - if ( metadataFile != null ) - { - metadata = metadata.setFile( metadataFile ); - task.result.setMetadata( metadata ); - } - if ( task.result.getException() == null ) - { - task.result.setUpdated( true ); - } - metadataResolved( session, task.trace, metadata, task.request.getRepository(), - task.result.getException() ); - } - } - - return results; - } - - private File getLocalFile( RepositorySystemSession session, Metadata metadata ) - { - LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - LocalMetadataResult localResult = lrm.find( session, new LocalMetadataRequest( metadata, null, null ) ); - File localFile = localResult.getFile(); - return localFile; - } - - private List<RemoteRepository> getEnabledSourceRepositories( RemoteRepository repository, Metadata.Nature nature ) - { - List<RemoteRepository> repositories = new ArrayList<RemoteRepository>(); - - if ( repository.isRepositoryManager() ) - { - for ( RemoteRepository repo : repository.getMirroredRepositories() ) - { - if ( isEnabled( repo, nature ) ) - { - repositories.add( repo ); - } - } - } - else if ( isEnabled( repository, nature ) ) - { - repositories.add( repository ); - } - - return repositories; - } - - private boolean isEnabled( RemoteRepository repository, Metadata.Nature nature ) - { - if ( !Metadata.Nature.SNAPSHOT.equals( nature ) && repository.getPolicy( false ).isEnabled() ) - { - return true; - } - if ( !Metadata.Nature.RELEASE.equals( nature ) && repository.getPolicy( true ).isEnabled() ) - { - return true; - } - return false; - } - - private RepositoryPolicy getPolicy( RepositorySystemSession session, RemoteRepository repository, - Metadata.Nature nature ) - { - boolean releases = !Metadata.Nature.SNAPSHOT.equals( nature ); - boolean snapshots = !Metadata.Nature.RELEASE.equals( nature ); - return remoteRepositoryManager.getPolicy( session, repository, releases, snapshots ); - } - - private void metadataResolving( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - ArtifactRepository repository ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_RESOLVING ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void metadataResolved( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - ArtifactRepository repository, Exception exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_RESOLVED ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setException( exception ); - event.setFile( metadata.getFile() ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void metadataDownloading( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - ArtifactRepository repository ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_DOWNLOADING ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private void metadataDownloaded( RepositorySystemSession session, RequestTrace trace, Metadata metadata, - ArtifactRepository repository, File file, Exception exception ) - { - RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_DOWNLOADED ); - event.setTrace( trace ); - event.setMetadata( metadata ); - event.setRepository( repository ); - event.setException( exception ); - event.setFile( file ); - - repositoryEventDispatcher.dispatch( event.build() ); - } - - private Executor getExecutor( int threads ) - { - if ( threads <= 1 ) - { - return new Executor() - { - public void execute( Runnable command ) - { - command.run(); - } - }; - } - else - { - return new ThreadPoolExecutor( threads, threads, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), - new WorkerThreadFactory( null ) ); - } - } - - private void shutdown( Executor executor ) - { - if ( executor instanceof ExecutorService ) - { - ( (ExecutorService) executor ).shutdown(); - } - } - - class ResolveTask - implements Runnable - { - - final RepositorySystemSession session; - - final RequestTrace trace; - - final MetadataResult result; - - final MetadataRequest request; - - final File metadataFile; - - final String policy; - - final List<UpdateCheck<Metadata, MetadataTransferException>> checks; - - volatile MetadataTransferException exception; - - public ResolveTask( RepositorySystemSession session, RequestTrace trace, MetadataResult result, - File metadataFile, List<UpdateCheck<Metadata, MetadataTransferException>> checks, - String policy ) - { - this.session = session; - this.trace = trace; - this.result = result; - this.request = result.getRequest(); - this.metadataFile = metadataFile; - this.policy = policy; - this.checks = checks; - } - - public void run() - { - Metadata metadata = request.getMetadata(); - RemoteRepository requestRepository = request.getRepository(); - - metadataDownloading( session, trace, metadata, requestRepository ); - - try - { - List<RemoteRepository> repositories = new ArrayList<RemoteRepository>(); - for ( UpdateCheck<Metadata, MetadataTransferException> check : checks ) - { - repositories.add( check.getAuthoritativeRepository() ); - } - - MetadataDownload download = new MetadataDownload(); - download.setMetadata( metadata ); - download.setRequestContext( request.getRequestContext() ); - download.setFile( metadataFile ); - download.setChecksumPolicy( policy ); - download.setRepositories( repositories ); - download.setListener( SafeTransferListener.wrap( session, logger ) ); - download.setTrace( trace ); - - RepositoryConnector connector = - repositoryConnectorProvider.newRepositoryConnector( session, requestRepository ); - try - { - connector.get( null, Arrays.asList( download ) ); - } - finally - { - connector.close(); - } - - exception = download.getException(); - - if ( exception == null ) - { - - List<String> contexts = Collections.singletonList( request.getRequestContext() ); - LocalMetadataRegistration registration = - new LocalMetadataRegistration( metadata, requestRepository, contexts ); - - session.getLocalRepositoryManager().add( session, registration ); - } - else if ( request.isDeleteLocalCopyIfMissing() && exception instanceof MetadataNotFoundException ) - { - download.getFile().delete(); - } - } - catch ( NoRepositoryConnectorException e ) - { - exception = new MetadataTransferException( metadata, requestRepository, e ); - } - - /* - * NOTE: Touch after registration with local repo to ensure concurrent resolution is not rejected with - * "already updated" via session data when actual update to local repo is still pending. - */ - for ( UpdateCheck<Metadata, MetadataTransferException> check : checks ) - { - updateCheckManager.touchMetadata( session, check.setException( exception ) ); - } - - metadataDownloaded( session, trace, metadata, requestRepository, metadataFile, exception ); - } - - } - -} http://git-wip-us.apache.org/repos/asf/maven-resolver/blob/3a1b8ae0/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultOfflineController.java ---------------------------------------------------------------------- diff --git a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultOfflineController.java b/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultOfflineController.java deleted file mode 100644 index 938a9e1..0000000 --- a/aether-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultOfflineController.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.eclipse.aether.internal.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.regex.Pattern; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.impl.OfflineController; -import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.spi.locator.Service; -import org.eclipse.aether.spi.locator.ServiceLocator; -import org.eclipse.aether.spi.log.Logger; -import org.eclipse.aether.spi.log.LoggerFactory; -import org.eclipse.aether.spi.log.NullLoggerFactory; -import org.eclipse.aether.transfer.RepositoryOfflineException; -import org.eclipse.aether.util.ConfigUtils; - -/** - * - */ -@Named -public class DefaultOfflineController - implements OfflineController, Service -{ - - static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols"; - - static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts"; - - private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" ); - - private Logger logger = NullLoggerFactory.LOGGER; - - public DefaultOfflineController() - { - // enables default constructor - } - - @Inject - DefaultOfflineController( LoggerFactory loggerFactory ) - { - setLoggerFactory( loggerFactory ); - } - - public void initService( ServiceLocator locator ) - { - setLoggerFactory( locator.getService( LoggerFactory.class ) ); - } - - public DefaultOfflineController setLoggerFactory( LoggerFactory loggerFactory ) - { - this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); - return this; - } - - public void checkOffline( RepositorySystemSession session, RemoteRepository repository ) - throws RepositoryOfflineException - { - if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) ) - { - return; - } - - throw new RepositoryOfflineException( repository ); - } - - private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository ) - { - String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS ); - if ( protocols != null ) - { - String protocol = repository.getProtocol(); - if ( protocol.length() > 0 ) - { - for ( String p : protocols ) - { - if ( p.equalsIgnoreCase( protocol ) ) - { - return true; - } - } - } - } - return false; - } - - private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository ) - { - String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS ); - if ( hosts != null ) - { - String host = repository.getHost(); - if ( host.length() > 0 ) - { - for ( String h : hosts ) - { - if ( h.equalsIgnoreCase( host ) ) - { - return true; - } - } - } - } - return false; - } - - private String[] getConfig( RepositorySystemSession session, String key ) - { - String value = ConfigUtils.getString( session, "", key ).trim(); - if ( value.length() <= 0 ) - { - return null; - } - return SEP.split( value ); - } - -}