From ae93f02c8d905173a0095ecef2912f4ddda9bffc Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Mon, 20 Jun 2005 15:21:50 +0000 Subject: [PATCH] PR: MNG-418 use a silent transfer monitor in batch mode git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@191492 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/cli/BatchModeDownloadMonitor.java | 79 +++++++++++++++++++ .../maven/cli/ConsoleDownloadMonitor.java | 22 ++++-- .../java/org/apache/maven/cli/MavenCli.java | 13 ++- 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/cli/BatchModeDownloadMonitor.java diff --git a/maven-core/src/main/java/org/apache/maven/cli/BatchModeDownloadMonitor.java b/maven-core/src/main/java/org/apache/maven/cli/BatchModeDownloadMonitor.java new file mode 100644 index 0000000000..8f13092360 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/cli/BatchModeDownloadMonitor.java @@ -0,0 +1,79 @@ +package org.apache.maven.cli; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.wagon.WagonConstants; +import org.apache.maven.wagon.events.TransferEvent; +import org.apache.maven.wagon.events.TransferListener; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +/** + * Console download progress meter. + * + * @author Brett Porter + * @version $Id: ConsoleDownloadMonitor.java 169548 2005-05-11 01:04:50Z brett $ + */ +public class BatchModeDownloadMonitor + extends AbstractLogEnabled + implements TransferListener +{ + public void transferInitiated( TransferEvent transferEvent ) + { + String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading"; + + String url = transferEvent.getWagon().getRepository().getUrl(); + + // TODO: can't use getLogger() because this isn't currently instantiated as a component + System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() ); + } + + public void transferStarted( TransferEvent transferEvent ) + { + // This space left intentionally blank + } + + public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) + { + // This space left intentionally blank + } + + public void transferCompleted( TransferEvent transferEvent ) + { + long contentLength = transferEvent.getResource().getContentLength(); + if ( contentLength != WagonConstants.UNKNOWN_LENGTH ) + { + String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" ); + String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b"; + System.out.println( l + " " + type ); + } + } + + public void transferError( TransferEvent transferEvent ) + { + // TODO: can't use getLogger() because this isn't currently instantiated as a component + transferEvent.getException().printStackTrace(); + } + + public void debug( String message ) + { + // TODO: can't use getLogger() because this isn't currently instantiated as a component +// getLogger().debug( message ); + } +} + + + diff --git a/maven-core/src/main/java/org/apache/maven/cli/ConsoleDownloadMonitor.java b/maven-core/src/main/java/org/apache/maven/cli/ConsoleDownloadMonitor.java index 8886fff330..cc4d49042d 100644 --- a/maven-core/src/main/java/org/apache/maven/cli/ConsoleDownloadMonitor.java +++ b/maven-core/src/main/java/org/apache/maven/cli/ConsoleDownloadMonitor.java @@ -55,15 +55,27 @@ public class ConsoleDownloadMonitor long total = transferEvent.getResource().getContentLength(); complete += length; // TODO [BP]: Sys.out may no longer be appropriate, but will \r work with getLogger()? - System.out.print( ( complete / 1024 ) + "/" + - ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" ) + "\r" ); + if ( total >= 1024 ) + { + System.out.print( + ( complete / 1024 ) + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" ) + + "\r" ); + } + else + { + System.out.print( complete + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" ) + "\r" ); + } } public void transferCompleted( TransferEvent transferEvent ) { - System.out.println( - ( complete / 1024 ) + "K " + - ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" ) ); + long contentLength = transferEvent.getResource().getContentLength(); + if ( contentLength != WagonConstants.UNKNOWN_LENGTH ) + { + String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" ); + String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b"; + System.out.println( l + " " + type ); + } } public void transferError( TransferEvent transferEvent ) diff --git a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java index a13125b646..0ff68c7ac3 100644 --- a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java @@ -224,7 +224,7 @@ public class MavenCli request = createRequest( projectFiles, embedder, commandLine, settings, eventDispatcher, manager ); - maven = createMavenInstance( embedder ); + maven = createMavenInstance( embedder, settings.isInteractiveMode() ); } catch ( ComponentLookupException e ) { @@ -346,12 +346,19 @@ public class MavenCli return files; } - private static Maven createMavenInstance( Embedder embedder ) + private static Maven createMavenInstance( Embedder embedder, boolean interactive ) throws ComponentLookupException { // TODO [BP]: doing this here as it is CLI specific, though it doesn't feel like the right place (likewise logger). WagonManager wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE ); - wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor() ); + if ( interactive ) + { + wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor() ); + } + else + { + wagonManager.setDownloadMonitor( new BatchModeDownloadMonitor() ); + } return (Maven) embedder.lookup( Maven.ROLE ); }