dslinux/user/perl/jpl/get_jdk README get_jdk.pl jdk_hosts

cayenne dslinux_cayenne at user.in-berlin.de
Mon Dec 4 18:00:09 CET 2006


Update of /cvsroot/dslinux/dslinux/user/perl/jpl/get_jdk
In directory antilope:/tmp/cvs-serv17422/jpl/get_jdk

Added Files:
	README get_jdk.pl jdk_hosts 
Log Message:
Adding fresh perl source to HEAD to branch from

--- NEW FILE: jdk_hosts ---
solaris => ftp://ftp.javasoft.com/pub/jdk1.1/jdk1.1.3-solaris2-sparc.bin
linux	=> ftp://ftp.infomagic.com/pub/mirrors/linux/Java/JDK-1.1.3/linux-jdk.1.1.3-v2.tar.gz
linux	=> ftp://ftp.connectnet.com/pub/java/JDK-1.1.3/linux-jdk.1.1.3-v2.tar.gz
freebsd	=> http://www.csi.uottawa.ca/~kwhite/jdkbinaries/jdk1.1-FreeBSD.tar.gz

--- NEW FILE: README ---

This archive contains the following files:
README - the README file which explains how to use this program (this file)
get_jdk.pl - the program to download JDK
jdk_hosts - the descriptor file required by the program

Nate Patwardhan (nvp at oreilly.com) wrote get_jdk.pl to automate the
download of JDK (Java Development Kit) from a distribution site based
on your Unix flavor.  This program is based on some of the examples
found in the LWP cookbook that was included with your LWP distribution.

Current Unix flavors that appear in the descriptor file (more
suggestions from Beta testers will be welcomed):
	Solaris
	Linux
	FreeBSD

To use get_jdk.pl properly, you *must* have LWP (libwww) and its
dependencies installed.  Once you've installed LWP, you should be able
to use this module without any problems on any Unix flavor.

By default, get_jdk.pl uses #!/usr/local/bin/perl in its shebang path,
so you may have to execute get_jdk.pl like:

	perl get_jdk.pl

-OR-

	perl5 get_jdk.pl

based on your site's Perl installation.

get_jdk.pl reads the $^O to determine what Unix flavor you're using,
and compares the value of $^O to the first field shown in the
descriptor file, jdk_hosts.  For example, $^O for Solaris versions of
Perl is: 'solaris'; Solaris is represented in the descriptor file
like:

	solaris=>ftp://ftp.javasoft.com/pub/jdk1.1/jdk1.1.3-solaris2-sparc.bin

When get_jdk.pl reads the descriptor file, it splits the fields on
'=>', and reads them into a hash, %HOSTS.  get_jdk.pl then compares
the value of $^O to $HOSTS{'osname'}, and returns the address of the
JDK distribution site if $^O eq $HOSTS{'osname'}.  If there is not a
match, get_jdk.pl fails.

get_jdk.pl represents the hostname of distribution sites in URL
format: protocol://hostname.some.com/path/filename.extension  
When a URL is found, get_jdk.pl parses out the filename; this is
significant, because the output from the remote host is directed to
the file parsed from the URL.

When you execute get_jdk.pl, you'll know it's working correctly if it
outputs something like:

	A JDK port for your OS has been found.
	Contacting:
	ftp://ftp.javasoft.com/pub/jdk1.1/jdk1.1.3-solaris2-sparc.bin
	Attempting to download: jdk1.1.3-solaris2-sparc.bin
	0% - 1460 bytes received
	0% - 4380 bytes received
	0% - 7300 bytes received
	0% - 8192 bytes received
	[etc etc etc until you reach 100%]

Future (PRK release) versions of get_jdk.pl will allow the user to
update the descriptor file from the ora.com (oreilly.com) FTP/WWW
site.  This version does not support the -update flag.

Happy JDK'ing!  :-)

--
Nate Patwardhan
nvp at oreilly.com

--- NEW FILE: get_jdk.pl ---
#!/usr/bin/perl -w

# Based on an ftp client found in the LWP Cookbook and
# revised by Nathan V. Patwardhan <nvp at ora.com>.

# Copyright 1997 O'Reilly and Associates
# This package may be copied under the same terms as Perl itself.
#
# Code appears in the Unix version of the Perl Resource Kit

use LWP::UserAgent;
use URI::URL;

my $ua = new LWP::UserAgent;

# check to see if a JDK port exists for the OS.  i'd say
# that we should use solaris by default, but a 9meg tarfile
# is a hard pill to swallow if it won't work for somebody.  :-)
my $os_type = $^O; my $URL = lookup_jdk_port($os_type);
die("No JDK port found.  Contact your vendor for details.  Exiting.\n")
    if $URL eq '';

print "A JDK port for your OS has been found.\nContacting: ".$URL."\n";

# Now, parse the URL using URI::URL
my($jdk_file) = (url($URL)->crack)[5]; 
$jdk_file =~ /(.+)\/(.+)/; $jdk_file = $2;

print "Attempting to download: $jdk_file\n";

my $expected_length;
my $bytes_received = 0;

open(OUT, ">".$jdk_file) or die("Can't open $jdk_file: $!");
$ua->request(HTTP::Request->new('GET', $URL),
	     sub {
		 my($chunk, $res) = @_;

		 $bytes_received += length($chunk);
		 unless (defined $expected_length) {
		     $expected_length = $res->content_length || 0;
		 }
		 if ($expected_length) {
		     printf STDERR "%d%% - ",
		     100 * $bytes_received / $expected_length;
		 }
		 print STDERR "$bytes_received bytes received\n";

		 print OUT $chunk;
	     }
);
close(OUT);

sub lookup_jdk_port {
    my($port_os) = @_;
    my $jdk_hosts = 'jdk_hosts';
    my %HOSTS = ();

    open(CFG, $jdk_hosts) or die("hosts error: $!");
    while(<CFG>) {
	chop;
	($os, $host) = split(/\s*=>\s*/, $_);
	next unless $os eq $port_os;
	push(@HOSTS, $host);
    }
    close(CFG);

    return "" unless @HOSTS;
    return $HOSTS[rand @HOSTS];		# Pick one at random.
}





More information about the dslinux-commit mailing list