maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mic...@apache.org
Subject cvs commit: maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers Md5SumObserver.java
Date Thu, 20 May 2004 15:41:38 GMT
michal      2004/05/20 08:41:38

  Modified:    wagon-api/src/main/java/org/apache/maven/wagon Wagon.java
                        LazyFileOutputStream.java
               wagon-api/src/main/java/org/apache/maven/wagon/events
                        TransferEvent.java
               wagon-api/src/main/java/org/apache/maven/wagon/observers
                        Md5SumObserver.java
  Added:       wagon-api/src/main/java/org/apache/maven/wagon
                        WagonUtils.java
  Log:
  Stream based API is exposed in Wagon. Every Wagon implements it alredy and it is quite useful
for in some cases
  
  Revision  Changes    Path
  1.4       +20 -0     maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/Wagon.java
  
  Index: Wagon.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/Wagon.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Wagon.java	26 Apr 2004 19:23:43 -0000	1.3
  +++ Wagon.java	20 May 2004 15:41:37 -0000	1.4
  @@ -24,6 +24,8 @@
   import org.apache.maven.wagon.repository.Repository;
   
   import java.io.File;
  +import java.io.InputStream;
  +import java.io.OutputStream;
   
   public interface Wagon
   {
  @@ -48,6 +50,24 @@
   
       void put( File source, Artifact artifact )
           throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException;
  +
  +
  +    // ----------------------------------------------------------------------
  +    // /Stream handling
  +    // ----------------------------------------------------------------------
  +
  +    /**
  +     *  
  +     */
  +    InputStream getInputStream( String source )
  +        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException;
  +
  +    /**
  +     *  
  +     */
  +    OutputStream getOutputStream( String source )
  +        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException;
  +    
   
       Repository getRepository();
   
  
  
  
  1.2       +3 -7      maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/LazyFileOutputStream.java
  
  Index: LazyFileOutputStream.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/LazyFileOutputStream.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LazyFileOutputStream.java	11 May 2004 19:11:09 -0000	1.1
  +++ LazyFileOutputStream.java	20 May 2004 15:41:37 -0000	1.2
  @@ -118,11 +118,7 @@
        * 
        */
       private void initialize() throws FileNotFoundException
  -    {
  -        
  -        System.out.println( "LazyFileOutputStream initialized."   );
  -        
  -        delegee = new FileOutputStream( file );  
  -        
  +    {        
  +        delegee = new FileOutputStream( file );         
       }
   }
  
  
  
  1.1                  maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/WagonUtils.java
  
  Index: WagonUtils.java
  ===================================================================
  package org.apache.maven.wagon;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import org.apache.maven.wagon.authorization.AuthorizationException;
  import org.codehaus.plexus.util.IOUtil;
  
  /**
   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a> 
   * @version $Id: WagonUtils.java,v 1.1 2004/05/20 15:41:37 michal Exp $ 
   */
  public class WagonUtils
  {
  
      public static byte[] toByteArray( String resource, Wagon wagon  ) throws IOException,
TransferFailedException, ResourceDoesNotExistException, AuthorizationException
      {
          InputStream inputStream = null;
         
          try
          {        
               inputStream = wagon.getInputStream( resource );
               
               byte[] retValue = IOUtil.toByteArray( inputStream );
               
               return retValue;
          }        
          finally
          {
               IOUtil.close( inputStream );
          }
          
           
      }
      
      public static void fromByteArray( String resource, Wagon wagon, byte[] buffer  ) throws
IOException, TransferFailedException, ResourceDoesNotExistException, AuthorizationException
      {
          OutputStream outputStream = null;
         
          try
          {        
               outputStream = wagon.getOutputStream( resource );
               
               IOUtil.copy( buffer, outputStream );
               
               
          }        
          finally
          {
               IOUtil.close( outputStream );
          }
          
           
      }
      
      
      
      public static String toString( String resource, Wagon wagon  ) throws IOException, TransferFailedException,
ResourceDoesNotExistException, AuthorizationException
      {
      
          byte[] buffer = toByteArray( resource, wagon );
          
          String retValue = new String( buffer );
          
          return retValue;
           
      }
      
      public static void fromString( String resource, Wagon wagon, String  str ) throws IOException,
TransferFailedException, ResourceDoesNotExistException, AuthorizationException
      {
      
          byte[] buffer = str.getBytes();
          
          fromByteArray( resource, wagon, buffer  );
          
          
           
      }
      
      
      
  }
  
  
  
  1.3       +10 -2     maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/events/TransferEvent.java
  
  Index: TransferEvent.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/events/TransferEvent.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TransferEvent.java	26 Apr 2004 19:23:43 -0000	1.2
  +++ TransferEvent.java	20 May 2004 15:41:37 -0000	1.3
  @@ -46,13 +46,14 @@
       /** Indicates PUT transfer (to the repository)*/
       public final static int REQUEST_PUT = 6;
   
  -
       private String resource;
   
       private int eventType;
  +    
       private int requestType;
   
       private byte[] data;
  +    
       private int dataLength;
   
       private Exception exception;
  @@ -66,8 +67,11 @@
               final int requestType )
       {
           super( wagon );
  +     
           this.resource = resource;
  +        
           setEventType( eventType );
  +        
           setRequestType( requestType );
   
       }
  @@ -78,8 +82,11 @@
               final Exception exception )
       {
           super( wagon );
  +        
           this.resource = resource;
  +        
           setEventType( TRANSFER_ERROR );
  +        
           this.exception = exception;
   
       }
  @@ -214,6 +221,7 @@
       public void setData( final byte[] data, final int dataLength )
       {
           this.data = data;
  +        
           this.dataLength = dataLength;
       }
   
  
  
  
  1.3       +96 -17    maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers/Md5SumObserver.java
  
  Index: Md5SumObserver.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers/Md5SumObserver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Md5SumObserver.java	16 May 2004 17:16:18 -0000	1.2
  +++ Md5SumObserver.java	20 May 2004 15:41:37 -0000	1.3
  @@ -1,10 +1,15 @@
   package org.apache.maven.wagon.observers;
   
  +import java.io.InputStream;
   import java.security.MessageDigest;
   import java.security.NoSuchAlgorithmException;
   
  +import org.apache.maven.wagon.Wagon;
  +import org.apache.maven.wagon.WagonUtils;
   import org.apache.maven.wagon.events.TransferEvent;
   import org.apache.maven.wagon.events.TransferListener;
  +import org.codehaus.plexus.util.IOUtil;
  +
   
   /**
    * 
  @@ -17,6 +22,10 @@
   {
       private MessageDigest md5Digester;
       
  +    private String expectedMd5;
  +    
  +    private String actualMd5;
  +    
       /**
        * @see org.apache.maven.wagon.events.TransferListener#transferStarted(org.apache.maven.wagon.events.TransferEvent)
        */
  @@ -42,7 +51,9 @@
           {
              byte[] data = transferEvent.getData();
              
  -           md5Digester.update( data );
  +           int len = transferEvent.getDataLength();
  +                      
  +           md5Digester.update( data, 0, len );
              
           }                
           
  @@ -51,6 +62,49 @@
       public void transferCompleted( TransferEvent transferEvent )
       {
           
  +        if ( md5Digester == null )
  +        {
  +            return;
  +        }
  +        actualMd5 = encode ( md5Digester.digest() );
  +        
  +        InputStream inputStream = null;
  +        
  +        try
  +        {
  +            int type = transferEvent.getRequestType();
  +            
  +            Wagon wagon = transferEvent.getWagon();
  +            
  +            String resource = transferEvent.getResource();
  +            
  +            if ( type  == TransferEvent.REQUEST_GET )
  +            {
  +                                            
  +               expectedMd5 = WagonUtils.toString( resource + ".md5 ", wagon ).trim(); 
             
  +            }
  +            else
  +            {
  +                //It's PUT put request
  +                
  +                WagonUtils.fromString( resource + ".md5 ", wagon, actualMd5 );
  +                
  +            }            
  +            
  +        }
  +        catch ( Exception e )
  +        {
  +            // ignore it. Observers should not throw any exceptions
  +        }    
  +        finally
  +        {
  +            if ( inputStream != null )
  +            {
  +                 IOUtil.close( inputStream );
  +            }
  +        }
  +        
  +            
       }
   
       public void transferError( TransferEvent transferEvent )
  @@ -65,18 +119,28 @@
           
       }
       
  -    public String getMd5Sum() 
  -    {
  -       String retValue = null;
  -       
  -       if ( md5Digester != null )
  -       {
  -           retValue = encode ( md5Digester.digest() );
  -       }
  -       
  -       return retValue;
  -       
  +    
  +    
  +    /**
  +     * Returns the md5 checksum downloaded from the server
  +     *   
  +     * @return
  +     */
  +    public String getExpectedMd5Sum() 
  +    {       
  +       return expectedMd5;
       }
  +   
  +    
  +    /**
  +     * Returns md5 checksum which was computed during transfer
  +     * @return
  +     */
  +    public String getActualMd5Sum() 
  +    {       
  +       return actualMd5;
  +    }
  +    
       
       /**
        * Encodes a 128 bit (16 bytes) byte array into a 32 character String.
  @@ -92,7 +156,7 @@
               return null;
           }
   
  -        String result = "";
  +        String retValue = "";
   
           for ( int i = 0; i < 16; i++ )
           {
  @@ -100,16 +164,31 @@
   
               if ( t.length() == 1 )
               {
  -                result += ( "0" + t );
  +                retValue += ( "0" + t );
               }
               else
               {
  -                result += t;
  +                retValue += t;
               }
           }
   
  -        return result;
  +        return retValue.trim();
       }
  +    
  +    
  +    public boolean cheksumIsValid()
  +    {
  +        boolean retValue = false;
  +        
  +        if ( actualMd5 != null && expectedMd5 !=null )
  +        {
  +             retValue = actualMd5.equals( expectedMd5 );
  +        }
  +        
  +        return retValue;
  +    }
  +    
  +    
       
       
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org


Mime
View raw message