| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # Java::SJ::Classpath | ||
| 4 | # | ||
| 5 | # $Id: Classpath.pm,v 1.3 2003/07/20 18:52:21 wiggly Exp $ | ||
| 6 | # | ||
| 7 | # $Author: wiggly $ | ||
| 8 | # | ||
| 9 | # $DateTime$ | ||
| 10 | # | ||
| 11 | # $Revision: 1.3 $ | ||
| 12 | # | ||
| 13 | ########################################################################### | ||
| 14 | |||
| 15 | package Java::SJ::Classpath; | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use English; | ||
| 19 | use Data::Dumper; | ||
| 20 | use File::Glob qw( :glob ); | ||
| 21 | |||
| 22 | our $VERSION = '0.01'; | ||
| 23 | |||
| 24 | ########################################################################### | ||
| 25 | # | ||
| 26 | # Constructor | ||
| 27 | # | ||
| 28 | ########################################################################### | ||
| 29 | sub new | ||
| 30 | { | ||
| 31 | my $class = shift; | ||
| 32 | my $self = {}; | ||
| 33 | bless $self, $class; | ||
| 34 | |||
| 35 | $self->{'dir'} = []; | ||
| 36 | $self->{'jar'} = []; | ||
| 37 | |||
| 38 | #print STDERR "[DEBUG] CLASSPATH\n" . Dumper( $self ) . "\n\n"; | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | ########################################################################### | ||
| 43 | # | ||
| 44 | # add_dir | ||
| 45 | # | ||
| 46 | ########################################################################### | ||
| 47 | sub add_dir | ||
| 48 | { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | my $path = shift; | ||
| 52 | |||
| 53 | push @{$self->{'dir'}}, $path; | ||
| 54 | |||
| 55 | 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | ########################################################################### | ||
| 59 | # | ||
| 60 | # add_jar | ||
| 61 | # | ||
| 62 | ########################################################################### | ||
| 63 | sub add_jar | ||
| 64 | { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my %hash = @_; | ||
| 68 | |||
| 69 | push @{$self->{'jar'}}, \%hash; | ||
| 70 | |||
| 71 | 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | ########################################################################### | ||
| 75 | # | ||
| 76 | # generate_classpath | ||
| 77 | # | ||
| 78 | # This is where much of the SJ magic goes on. | ||
| 79 | # | ||
| 80 | # Given the base SJ directory where classes are stored we go off and figure | ||
| 81 | # out if the Jars we want exist, if so we figure out their full paths. We | ||
| 82 | # use these and any fully specified jar filenames and classpath directories | ||
| 83 | # to build a full classpath. | ||
| 84 | # | ||
| 85 | ########################################################################### | ||
| 86 | sub generate_classpath | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | my $base = shift; | ||
| 91 | |||
| 92 | my $jar = undef; | ||
| 93 | |||
| 94 | my $dir = undef; | ||
| 95 | |||
| 96 | my $file = ''; | ||
| 97 | |||
| 98 | my @classpath = (); | ||
| 99 | |||
| 100 | #print STDERR "[INFO] generate_classpath\n"; | ||
| 101 | #print STDERR "[INFO] SJ Jar File Directory : $base\n"; | ||
| 102 | |||
| 103 | foreach $jar ( @{$self->{'jar'}} ) | ||
| 104 | { | ||
| 105 | if( $jar->{'file'} !~ /^$/ ) | ||
| 106 | { | ||
| 107 | push @classpath, $jar->{'file'}; | ||
| 108 | } | ||
| 109 | else | ||
| 110 | { | ||
| 111 | # | ||
| 112 | # To figure out which JAR file to use we do the following; | ||
| 113 | # | ||
| 114 | # 1) If both a name and version are specified then we build up the | ||
| 115 | # path by concatenating the base lib dir, a path seperator token, the | ||
| 116 | # name, a hyphen, the version and '.jar'. | ||
| 117 | # | ||
| 118 | # 2) If only a name is supplied then we look for all of the filenames | ||
| 119 | # in the base lib dir that start with the name specified, sort them | ||
| 120 | # lexicographically and take the last one in the list. | ||
| 121 | # | ||
| 122 | # We take the last because it should be the highest version number for | ||
| 123 | # that JAR file. | ||
| 124 | # | ||
| 125 | # | ||
| 126 | if( $jar->{'version'} !~ /^$/ ) | ||
| 127 | { | ||
| 128 | # construct jar pathname | ||
| 129 | $file = $base . '/' . $jar->{'name'} . '-' . $jar->{'version'} . '.jar'; | ||
| 130 | |||
| 131 | # add it to our list | ||
| 132 | push @classpath, $file; | ||
| 133 | |||
| 134 | $file = ''; | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | # construct base pathname | ||
| 139 | $file = $base . '/' . $jar->{'name'} . '-*'; | ||
| 140 | |||
| 141 | # BSD glob will sort them into ASCII order for us | ||
| 142 | @files = bsd_glob( $file ); | ||
| 143 | |||
| 144 | # we want the last one | ||
| 145 | $file = $files[-1]; | ||
| 146 | |||
| 147 | # add it to our list | ||
| 148 | push @classpath, $file; | ||
| 149 | |||
| 150 | $file = ''; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | return join( ':', ( @classpath, @{$self->{'dir'}} ) ); | ||
| 156 | } | ||
| 157 | |||
| 158 | ########################################################################### | ||
| 159 | 1; | ||
| 160 | |||
| 161 | =pod | ||
| 162 | |||
| 163 | =head1 NAME | ||
| 164 | |||
| 165 | Java::SJ::Classpath - Java classpath generator | ||
| 166 | |||
| 167 | =head1 DESCRIPTION | ||
| 168 | |||
| 169 | This module is used by L<Java::SJ::Config> when generating complete | ||
| 170 | classpaths. | ||
| 171 | |||
| 172 | =head1 TODO | ||
| 173 | |||
| 174 | Test, test, test. | ||
| 175 | |||
| 176 | =head1 BUGS | ||
| 177 | |||
| 178 | None known so far. Please report any and all to Nigel Rantor <F<wiggly@wiggly.org>> | ||
| 179 | |||
| 180 | =head1 SUPPORT / WARRANTY | ||
| 181 | |||
| 182 | This module is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. | ||
| 183 | |||
| 184 | =head1 LICENSE | ||
| 185 | |||
| 186 | The Java::SJ::Classpath module is Copyright (c) 2003 Nigel Rantor. England. | ||
| 187 | All rights reserved. | ||
| 188 | |||
| 189 | You may distribute under the terms of either the GNU General Public License | ||
| 190 | or the Artistic License, as specified in the Perl README file. | ||
| 191 | |||
| 192 | =head1 AUTHORS | ||
| 193 | |||
| 194 | Nigel Rantor <F<wiggly@wiggly.org>> | ||
| 195 | |||
| 196 | =head1 SEE ALSO | ||
| 197 | |||
| 198 | L<Java::SJ>. L<Java::SJ::Config>. | ||
| 199 | |||
| 200 | =cut |