jvanzyl     02/03/16 09:22:09

  Modified:    src/java/org/apache/maven UpdateResources.java
  Log:
  Non distributable jars are once again handled by implementing jeff's idea
  of putting the list of non distributable JARs on the server.
  
  So now the list of non-distributable JARs (non-distributable-jars.list)
  is pulled down and placed in lib.repo automatically and then the file
  is processed and the JARs being requested are checked again the non-dist
  list and if there is a match at the end of the update the user is
  warned that they must download the given resource on their own and
  the location of where to find the resource is provided.
  
  Revision  Changes    Path
  1.8       +203 -145  
jakarta-turbine-maven/src/java/org/apache/maven/UpdateResources.java
  
  Index: UpdateResources.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/UpdateResources.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- UpdateResources.java      16 Mar 2002 14:02:01 -0000      1.7
  +++ UpdateResources.java      16 Mar 2002 17:22:09 -0000      1.8
  @@ -58,6 +58,7 @@
   import java.io.FileOutputStream;
   import java.io.InputStream;
   import java.io.IOException;
  +
   import java.net.URL;
   import java.net.URLConnection;
   import java.net.HttpURLConnection;
  @@ -66,9 +67,13 @@
   import java.util.List;
   import java.util.ArrayList;
   import java.util.Iterator;
  +import java.util.Map;
  +import java.util.HashMap;
   import java.io.BufferedReader;
   import java.io.FileReader;
   
  +import org.apache.commons.lang.Strings;
  +
   import org.apache.maven.project.Dependency;
   import org.apache.maven.executor.ProjectExecutor;
   
  @@ -85,14 +90,13 @@
   public class UpdateResources
        extends ProjectExecutor
   {
  -    private URL source;// required
  -    private File dest;// required
  +    private File dest;
       private boolean verbose = false;
       private boolean useTimestamp = true;//on by default
       private boolean ignoreErrors = true;
       private String uname = null;
       private String pword = null;
  -
  +    private static final String NON_DIST_JAR_LIST = "non-distributable-jars.list";
       private String baseUrl;
       private StringBuffer warnings = new StringBuffer();
   
  @@ -144,171 +148,234 @@
               throw new Exception("Can't write to " + dest.getAbsolutePath());
           }
   
  +        // Get the non-distributable-jars.list and place it in
  +        // the lib.repo directory so it can be referenced when updating
  +        // a project's resources.
  +        URL nonDistJarsUrl = new URL(baseUrl + NON_DIST_JAR_LIST);
  +        File f = new File(dest, NON_DIST_JAR_LIST);        
  +        getFile(nonDistJarsUrl, f, NON_DIST_JAR_LIST);
  +        Map nonDistMap = getNonDistMap(f);
  +        
           for (Iterator j = mavenProject.getDependencies().iterator(); j.hasNext(); )
           {
               String file = ((Dependency) j.next()).getJar();
               File destinationFile = new File(dest, file);
  +            URL source = new URL(baseUrl + file);
   
  -            try
  +            // The last parameter here (file) is used for error
  +            // reporting purposes so that the user can see the name
  +            // of the file that's missing if the resource can't be
  +            // found.
  +            
  +            if (nonDistMap.containsKey(file))
               {
  +                
warnings.append("-------------------------------------------------\n");
  +                warnings.append("W A R N I N G\n");
  +                
warnings.append("------------------------------------------------\n");
  +                warnings.append("The following JAR must be downloaded manually:\n");
  +                warnings.append(file + "\n");
  +                warnings.append("\n");
  +                warnings.append("You can find the JAR here:\n");
  +                warnings.append(nonDistMap.get(file) + "\n");
  +                warnings.append("\n");
  +                continue;
  +            }
  +            
  +            getFile(source, destinationFile, file);
  +        }
   
  -                URL source = new URL(baseUrl + file);
  -                log("Getting: " + source);
  +        if (warnings.length() > 1)
  +        {
  +            log("\n" + warnings.toString());
  +        }
  +    }
   
  -                //set the timestamp to the file date.
  -                long timestamp = 0;
  -                boolean hasTimestamp = false;
  -                if (useTimestamp && destinationFile.exists())
  +    public Map getNonDistMap(File f)
  +    {
  +        Map nonDistMap = new HashMap();
  +        String line;
  +        
  +        try
  +        {
  +            BufferedReader in = new BufferedReader(new FileReader(f));
  +            while ((line=in.readLine()) != null)
  +            {
  +                line = line.trim();
  +                
  +                // Allow comments to be placed in the payload
  +                // descriptor.
  +                if (line.startsWith("#") || line.startsWith("--") || line.length() 
< 1)
                   {
  -                    timestamp = destinationFile.lastModified();
  -                    if (verbose)
  -                    {
  -                        Date t = new Date(timestamp);
  -                        log("local file date : " + t.toString());
  -                    }
  -
  -                    hasTimestamp = true;
  +                    continue;
                   }
  +                
  +                String[] entry = Strings.split(line,"|");
  +                
  +                // The name of the non-dist JAR is the key, and the value is the
  +                // location where the user can find it.
  +                nonDistMap.put(entry[0], entry[1]);
  +            }
  +        }
  +        catch (Exception e)
  +        {
  +        }
  +        
  +        return nonDistMap;
  +    }
   
  -                //set up the URL connection
  -                URLConnection connection = source.openConnection();
  -                //modify the headers
  -                //NB: things like user authentication could go in here too.
  -                if (useTimestamp && hasTimestamp)
  +    public void getFile(URL source, File destinationFile, String file)
  +    {
  +        try
  +        {
  +            log("Getting: " + source);
  +
  +            //set the timestamp to the file date.
  +            long timestamp = 0;
  +            boolean hasTimestamp = false;
  +            if (useTimestamp && destinationFile.exists())
  +            {
  +                timestamp = destinationFile.lastModified();
  +                if (verbose)
                   {
  -                    connection.setIfModifiedSince(timestamp);
  +                    Date t = new Date(timestamp);
  +                    log("local file date : " + t.toString());
                   }
  -                // prepare Java 1.1 style credentials
  -                if (uname != null || pword != null)
  -                {
  -                    String up = uname + ":" + pword;
  -                    String encoding;
  -                    // check to see if sun's Base64 encoder is available.
  -                    try
  -                    {
  -                        sun.misc.BASE64Encoder encoder =
  -                            (sun.misc.BASE64Encoder) Class.forName(
  -                            "sun.misc.BASE64Encoder").newInstance();
  -
  -                        encoding = encoder.encode(up.getBytes());
  -                    }
  -                    catch (Exception ex)
  -                    {// sun's base64 encoder isn't available
  -                        Base64Converter encoder = new Base64Converter();
  -                        encoding = encoder.encode(up.getBytes());
  -                    }
  -                    connection.setRequestProperty("Authorization", "Basic " + 
encoding);
  -                }
  -
  -                //connect to the remote site (may take some time)
  -                connection.connect();
  -                //next test for a 304 result (HTTP only)
  -                if (connection instanceof HttpURLConnection)
  -                {
  -                    HttpURLConnection httpConnection = (HttpURLConnection) 
connection;
  -                    if (httpConnection.getResponseCode() == 
HttpURLConnection.HTTP_NOT_MODIFIED)
  -                    {
  -                        //not modified so no file download. just return instead
  -                        //and trace out something so the user doesn't think that the
  -                        //download happened when it didnt
  -                        log("Not modified - so not downloaded");
  -                        continue;
  -                    }
  -                    // test for 401 result (HTTP only)
  -                    if (httpConnection.getResponseCode() == 
HttpURLConnection.HTTP_UNAUTHORIZED)
  -                    {
  -                        log("Not authorized - check " + destinationFile + " for 
details");
  -                        continue;
  -                    }
  -                }
  -
  -                //REVISIT: at this point even non HTTP connections may support the 
if-modified-since
  -                //behaviour -we just check the date of the content and skip the 
write if it is not
  -                //newer. Some protocols (FTP) dont include dates, of course.
   
  -                FileOutputStream fos = new FileOutputStream(destinationFile);
  -                log("Writing " + destinationFile);
  +                hasTimestamp = true;
  +            }
   
  -                InputStream is = null;
  -                for (int i = 0; i < 3; i++)
  +            //set up the URL connection
  +            URLConnection connection = source.openConnection();
  +            //modify the headers
  +            //NB: things like user authentication could go in here too.
  +            if (useTimestamp && hasTimestamp)
  +            {
  +                connection.setIfModifiedSince(timestamp);
  +            }
  +            // prepare Java 1.1 style credentials
  +            if (uname != null || pword != null)
  +            {
  +                String up = uname + ":" + pword;
  +                String encoding;
  +                // check to see if sun's Base64 encoder is available.
  +                try
                   {
  -                    try
  -                    {
  -                        is = connection.getInputStream();
  -                        break;
  -                    }
  -                    catch (IOException ex)
  -                    {
  -                        log("Error opening connection " + ex);
  -                    }
  +                    sun.misc.BASE64Encoder encoder =
  +                        (sun.misc.BASE64Encoder) Class.forName(
  +                        "sun.misc.BASE64Encoder").newInstance();
  +
  +                    encoding = encoder.encode(up.getBytes());
                   }
  -                if (is == null)
  -                {
  -                    log("Can't get " + file + " to " + destinationFile);
  -                    if (ignoreErrors)
  -                    {
  -                        continue;
  -                    }
  -                    throw new Exception(
  -                        "Can't get " + file + " to " + destinationFile);
  +                catch (Exception ex)
  +                {// sun's base64 encoder isn't available
  +                    Base64Converter encoder = new Base64Converter();
  +                    encoding = encoder.encode(up.getBytes());
                   }
  +                connection.setRequestProperty("Authorization", "Basic " + encoding);
  +            }
   
  -                byte[] buffer = new byte[100 * 1024];
  -                int length;
  -
  -                while ((length = is.read(buffer)) >= 0)
  +            //connect to the remote site (may take some time)
  +            connection.connect();
  +            //next test for a 304 result (HTTP only)
  +            if (connection instanceof HttpURLConnection)
  +            {
  +                HttpURLConnection httpConnection = (HttpURLConnection) connection;
  +                if (httpConnection.getResponseCode() == 
HttpURLConnection.HTTP_NOT_MODIFIED)
                   {
  -                    fos.write(buffer, 0, length);
  -                    if (verbose)
  -                    {
  -                        System.out.print(".");
  -                    }
  +                    //not modified so no file download. just return instead
  +                    //and trace out something so the user doesn't think that the
  +                    //download happened when it didnt
  +                    log("Not modified - so not downloaded");
  +                    return;
                   }
  -                if (verbose)
  +                // test for 401 result (HTTP only)
  +                if (httpConnection.getResponseCode() == 
HttpURLConnection.HTTP_UNAUTHORIZED)
                   {
  -                    System.out.println();
  +                    log("Not authorized - check " + destinationFile + " for 
details");
  +                    return;
                   }
  -                fos.close();
  -                is.close();
  +            }
  +
  +            //REVISIT: at this point even non HTTP connections may support the 
if-modified-since
  +            //behaviour -we just check the date of the content and skip the write 
if it is not
  +            //newer. Some protocols (FTP) dont include dates, of course.
  +
  +            FileOutputStream fos = new FileOutputStream(destinationFile);
  +            log("Writing " + destinationFile);
   
  -                //if (and only if) the use file time option is set, then the
  -                //saved file now has its timestamp set to that of the downloaded 
file
  -                if (useTimestamp)
  +            InputStream is = null;
  +            for (int i = 0; i < 3; i++)
  +            {
  +                try
  +                {
  +                    is = connection.getInputStream();
  +                    break;
  +                }
  +                catch (IOException ex)
                   {
  -                    long remoteTimestamp = connection.getLastModified();
  -                    if (verbose)
  -                    {
  -                        Date t = new Date(remoteTimestamp);
  -                        log("last modified = " + t.toString() +
  -                            ((remoteTimestamp == 0) ? " - using current time 
instead" : ""));
  -                    }
  -                    if (remoteTimestamp != 0)
  -                    {
  -                        touchFile(destinationFile, remoteTimestamp);
  -                    }
  +                    log("Error opening connection " + ex);
                   }
               }
  -            catch (IOException ioe)
  +            if (is == null)
               {
  -                log("Error getting " + file + " to " + destinationFile);
  +                log("Can't get " + file + " to " + destinationFile);
                   if (ignoreErrors)
                   {
  -                    continue;
  +                    return;
                   }
  -                throw new Exception(ioe.getMessage());
  +                throw new Exception(
  +                    "Can't get " + file + " to " + destinationFile);
               }
  -            catch (Exception e)
  +
  +            byte[] buffer = new byte[100 * 1024];
  +            int length;
  +
  +            while ((length = is.read(buffer)) >= 0)
               {
  -                e.printStackTrace();
  +                fos.write(buffer, 0, length);
  +                System.out.print(".");
  +            }
  +            
  +            System.out.println();
  +            fos.close();
  +            is.close();
  +
  +            //if (and only if) the use file time option is set, then the
  +            //saved file now has its timestamp set to that of the downloaded file
  +            if (useTimestamp)
  +            {
  +                long remoteTimestamp = connection.getLastModified();
  +                if (verbose)
  +                {
  +                    Date t = new Date(remoteTimestamp);
  +                    log("last modified = " + t.toString() +
  +                        ((remoteTimestamp == 0) ? " - using current time instead" : 
""));
  +                }
  +                if (remoteTimestamp != 0)
  +                {
  +                    touchFile(destinationFile, remoteTimestamp);
  +                }
               }
           }
  +        catch (IOException ioe)
  +        {
  +            log("Error getting " + file + " to " + destinationFile);
  +            if (ignoreErrors)
  +            {
  +                return;
  +            }
   
  -        if (warnings.length() > 1)
  +            // We don't ever really want to throw an exception
  +            // just want to truck along.
  +            //throw new Exception(ioe.getMessage());
  +        }
  +        catch (Exception e)
           {
  -            log("\n" + warnings.toString());
  +            e.printStackTrace();
           }
       }
   
  +
       /**
        * set the timestamp of a named file to a specified time.
        *
  @@ -323,34 +390,25 @@
           throws Exception
       {
           long modifiedTime;
  -        
  -        if (timemillis < 0) 
  +
  +        if (timemillis < 0)
           {
               modifiedTime = System.currentTimeMillis();
  -        } 
  -        else 
  +        }
  +        else
           {
               modifiedTime = timemillis;
           }
  -        
  +
           file.setLastModified(modifiedTime);
           return true;
       }
   
       /**
  -     * Set the URL.
  -     *
  -     * @param u URL for the file.
  -     */
  -    public void setSrc(URL u)
  -    {
  -        this.source = u;
  -    }
  -
  -    /**
  -     * Where to copy the source file.
  +     * This is the directory where all the requested
  +     * files go.
        *
  -     * @param dest Path to file.
  +     * @param dest Destination directory for requested files.
        */
       public void setDest(File dest)
       {
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to