jvanzyl 02/01/15 20:59:32
Added: src/tdk/task/org/apache/tdk Get.java Touch.java
Log:
- adding a modifed <get> task that will take a baseUrl and a file
that lists a set of explicit dependencies and will compare timestamps
of jars in your local ${lib.repo} and the jars in the main archive.
i'm using the alexandria descriptors to glean most of the dep info though
i had to modify the list by hand (have to start somewhere).
The task looks something like this:
<httpget
baseUrl="http://jakarta.apache.org/turbine/jars/
dest="${lib.repo}"
dependencyFile="deps.list"
/>
Right now things seem to work if you're in the same time zone, so
I'm not sure if I messed something up while modifying the original
source or if there's something wrong with original code. I will
look into it. But I successfully built stratum starting with
nothing, downloaded the jars and built the project.
Revision Changes Path
1.1 jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/Get.java
Index: Get.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.tdk.task;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.util.Date;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* Get a particular file from a URL source.
* Options include verbose reporting, timestamp based fetches and controlling
* actions on failures. NB: access through a firewall only works if the whole
* Java runtime is correctly configured.
*
* @author [EMAIL PROTECTED]
* @author [EMAIL PROTECTED] (Added Java 1.1 style HTTP basic auth)
*/
public class Get extends Task {
private URL source; // required
private File dest; // required
private boolean verbose = false;
private boolean useTimestamp = true; //on by default
private boolean ignoreErrors = false;
private String uname = null;
private String pword = null;
private String baseUrl;
private File dependencyFile;
private List fileLinesToList(File f)
{
ArrayList l = new ArrayList();
String line;
try
{
BufferedReader in = new BufferedReader(new FileReader(f));
while ((line=in.readLine()) != null)
{
line = line.trim();
l.add(line);
}
}
catch (Exception e)
{
}
return l;
}
public void setDependencyFile(File dependencyFile)
{
this.dependencyFile = dependencyFile;
}
public void setBaseUrl(String baseUrl)
{
this.baseUrl = baseUrl;
}
/**
* Does the work.
*
* @exception BuildException Thrown in unrecoverable error.
*/
public void execute() throws BuildException {
if (baseUrl == null)
{
throw new BuildException("baseUrl attribute is required", location);
}
if (dependencyFile == null)
{
throw new BuildException("dependencyFile attribute is required",
location);
}
if (dest == null)
{
throw new BuildException("dest attribute is required", location);
}
if (dest.exists() && !dest.canWrite())
{
throw new BuildException("Can't write to " + dest.getAbsolutePath(),
location);
}
// Get list of files that might need to be retrieved.
List l = fileLinesToList(dependencyFile);
for (Iterator j = l.iterator(); j.hasNext();)
{
String file = (String) j.next();
File destinationFile = new File(dest,file);
try
{
URL source = new URL(baseUrl + file);
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)
{
Date t=new Date(timestamp);
log("local file date : "+t.toString());
}
hasTimestamp=true;
}
//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
{
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);
InputStream is=null;
for( int i=0; i< 3 ; i++ )
{
try
{
is = connection.getInputStream();
break;
}
catch( IOException ex )
{
log( "Error opening connection " + ex );
}
}
if( is==null )
{
log( "Can't get " + source + " to " + destinationFile);
if(ignoreErrors)
{
return;
}
throw new BuildException(
"Can't get " + source + " to " + destinationFile,location);
}
byte[] buffer = new byte[100 * 1024];
int length;
while ((length = is.read(buffer)) >= 0)
{
fos.write(buffer, 0, length);
if (verbose)
{
System.out.print(".");
}
}
if(verbose)
{
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 " + source + " to " + destinationFile );
if(ignoreErrors)
{
return;
}
throw new BuildException(ioe, location);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* set the timestamp of a named file to a specified time.
*
* @param filename
* @param time in milliseconds since the start of the era
* @return true if it succeeded. False means that this is a
* java1.1 system and that file times can not be set
* @exception BuildException Thrown in unrecoverable error. Likely
* this comes from file access failures.
*/
protected boolean touchFile(File file, long timemillis)
throws BuildException
{
if (project.getJavaVersion() != Project.JAVA_1_1)
{
Touch touch = (Touch) project.createTask("touch");
touch.setOwningTarget(target);
touch.setTaskName(getTaskName());
touch.setLocation(getLocation());
touch.setFile(file);
touch.setMillis(timemillis);
touch.touch();
return true;
}
else
{
return false;
}
}
/**
* Set the URL.
*
* @param u URL for the file.
*/
public void setSrc(URL u)
{
this.source = u;
}
/**
* Where to copy the source file.
*
* @param dest Path to file.
*/
public void setDest(File dest)
{
this.dest = dest;
}
/**
* Be verbose, if set to "<CODE>true</CODE>".
*
* @param v if "true" then be verbose
*/
public void setVerbose(boolean v)
{
verbose = v;
}
/**
* Don't stop if get fails if set to "<CODE>true</CODE>".
*
* @param v if "true" then don't report download errors up to ant
*/
public void setIgnoreErrors(boolean v)
{
ignoreErrors = v;
}
/**
* Use timestamps, if set to "<CODE>true</CODE>".
*
* <p>In this situation, the if-modified-since header is set so that the file is
* only fetched if it is newer than the local file (or there is no local file)
* This flag is only valid on HTTP connections, it is ignored in other cases.
* When the flag is set, the local copy of the downloaded file will also
* have its timestamp set to the remote file time.
* <br>
* Note that remote files of date 1/1/1970 (GMT) are treated as 'no timestamp',
and
* web servers often serve files with a timestamp in the future by replacing
their timestamp
* with that of the current time. Also, inter-computer clock differences can
cause no end of
* grief.
* @param v "true" to enable file time fetching
*/
public void setUseTimestamp(boolean v)
{
if (project.getJavaVersion() != Project.JAVA_1_1)
{
useTimestamp = v;
}
}
/**
* Username for basic auth.
*
* @param u username for authentication
*/
public void setUsername(String u)
{
this.uname = u;
}
/**
* password for the basic auth.
*
* @param p password for authentication
*/
public void setPassword(String p)
{
this.pword = p;
}
/*********************************************************************
* BASE 64 encoding of a String or an array of bytes.
*
* Based on RFC 1421.
*
* @author
* Unknown
* @author
* <a HREF="[EMAIL PROTECTED]">Gautam Guliani</a>
*********************************************************************/
class Base64Converter
{
public final char [ ] alphabet = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
public String encode ( String s )
{
return encode ( s.getBytes ( ) );
}
public String encode ( byte [ ] octetString )
{
int bits24;
int bits6;
char [ ] out
= new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
int outIndex = 0;
int i = 0;
while ( ( i + 3 ) <= octetString.length ) {
// store the octets
bits24=( octetString [ i++ ] & 0xFF ) << 16;
bits24 |=( octetString [ i++ ] & 0xFF ) << 8;
bits6=( bits24 & 0x00FC0000 )>> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0000003F );
out [ outIndex++ ] = alphabet [ bits6 ];
}
if ( octetString.length - i == 2 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits24 |=( octetString [ i + 1 ] & 0xFF ) << 8;
bits6=( bits24 & 0x00FC0000 )>> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
}
else if ( octetString.length - i == 1 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits6=( bits24 & 0x00FC0000 )>> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
out [ outIndex++ ] = '=';
}
return new String ( out );
}
}
}
1.1 jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/Touch.java
Index: Touch.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.tdk.task;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.util.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.Vector;
/**
* Touch a file and/or fileset(s) -- corresponds to the Unix touch command.
*
* <p>If the file to touch doesn't exist, an empty one is
* created. </p>
*
* <p>Note: Setting the modification time of files is not supported in
* JDK 1.1.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Michael J. Sikorsky</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Robert Shaw</a>
*/
public class Touch extends Task {
private File file; // required
private long millis = -1;
private String dateTime;
private Vector filesets = new Vector();
private FileUtils fileUtils;
public Touch() {
fileUtils = FileUtils.newFileUtils();
}
/**
* Sets a single source file to touch. If the file does not exist
* an empty file will be created.
*/
public void setFile(File file) {
this.file = file;
}
/**
* Milliseconds since 01/01/1970 00:00 am.
*/
public void setMillis(long millis) {
this.millis = millis;
}
/**
* Date in the format MM/DD/YYYY HH:MM AM_PM.
*/
public void setDatetime(String dateTime) {
this.dateTime = dateTime;
}
/**
* Adds a set of files (nested fileset attribute).
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
}
/**
* Execute the touch operation.
*/
public void execute() throws BuildException {
if (file == null && filesets.size() == 0) {
throw
new BuildException("Specify at least one source - a file or a
fileset.");
}
if (file != null && file.exists() && file.isDirectory()) {
throw new BuildException("Use a fileset to touch directories.");
}
if (dateTime != null) {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT,
Locale.US);
try {
setMillis(df.parse(dateTime).getTime());
if (millis < 0) {
throw new BuildException("Date of " + dateTime
+ " results in negative milliseconds
value relative to epoch (January 1, 1970, 00:00:00 GMT).");
}
} catch (ParseException pe) {
throw new BuildException(pe.getMessage(), pe, location);
}
}
touch();
}
/**
* Does the actual work. Entry point for Untar and Expand as well.
*/
protected void touch() throws BuildException {
if (file != null) {
if (!file.exists()) {
log("Creating "+file, Project.MSG_INFO);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(new byte[0]);
fos.close();
} catch (IOException ioe) {
throw new BuildException("Could not create "+file, ioe,
location);
}
}
}
if (millis >= 0 && project.getJavaVersion() == Project.JAVA_1_1) {
log("modification time of files cannot be set in JDK 1.1",
Project.MSG_WARN);
return;
}
boolean resetMillis = false;
if (millis < 0) {
resetMillis = true;
millis = System.currentTimeMillis();
}
if (file != null) {
touch(file);
}
// deal with the filesets
for (int i=0; i < filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);
File fromDir = fs.getDir(project);
String[] srcFiles = ds.getIncludedFiles();
String[] srcDirs = ds.getIncludedDirectories();
for(int j=0; j < srcFiles.length ; j++) {
touch(new File(fromDir, srcFiles[j]));
}
for(int j=0; j < srcDirs.length ; j++) {
touch(new File(fromDir, srcDirs[j]));
}
}
if (resetMillis) {
millis = -1;
}
}
protected void touch(File file) throws BuildException {
if (!file.canWrite()) {
throw new BuildException("Can not change modification date of read-only
file " + file);
}
if (project.getJavaVersion() == Project.JAVA_1_1) {
return;
}
fileUtils.setFileLastModified(file, millis);
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>