ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
(Generate patch)

Comparing COMP/SCRAM/src/URL/URLhandler.pm (file contents):
Revision 1.6 by williamc, Wed Sep 29 08:56:39 1999 UTC vs.
Revision 1.12 by williamc, Wed Mar 1 11:28:53 2000 UTC

# Line 1 | Line 1
1 < # url handler -> returns the location of thefilu&# Interface
1 > # url handler -> returns the location of the file
2 > # Interface
3   # ---------
4 < # new() :
5 < # new(cachedir)       : A new urlhandler with a defined default cahce directory
6 < # get(url)            : download from the specified url to the default cache
7 < # get(url,dirlocation)   : download to the specified directory
4 > # new(cache)       : A new urlhandler with a defined default cahce directory
5 > # download(url,[location]) : as get but always download
6 > # get(url,[location]) : download from the specified url to cache or location
7 > #                       return the full url path name incl. any base expansion
8 > #                       and the filename downloaded to
9   # setbase(type,variablehash) : set the url defaults for a specific url type
10 < #                              arguments are dependenton type
10 > #                              arguments are dependent on type
11   # unsetbase(type)  : deactivate a previously set base
12 < # setcache(dir)    : set the default cache location
12 > # currentbase(type) : return the current base for the given type
13   #
14   # ----------------------------------------------------------------------
13 # returns file location - or crashes out
14 # can pass a file name for the item to be stored as
15 # if not then stores in a default cache.
15  
16   package URL::URLhandler;
17   require 5.004;
18   use Utilities::AddDir;
19 < use URL::URL_base;
19 > use URL::URLcache;
20 > use URL::URLclass;
21 > use URL::URLbase;
22   use Carp;
23  
24   sub new {
# Line 33 | Line 34 | sub init {
34          use Utilities::AddDir;
35          my $self=shift;
36          my $cache=shift;
36        if (! defined $cache  ) { $cache="/tmp/SCRAMcache" }; # default cache
37        AddDir::adddir($cache);
37          $self->{cache}=$cache;
38 +        $self->{dummybase}=URL::URLbase->new({});
39 +        $self->{cachestore}=$self->{cache}->filestore();
40          use URL::URL_cvs;
41          use URL::URL_cvsfile;
42          use URL::URL_file;
43 +        use URL::URL_filed;
44          $self->{urlmodules}={
45                          'cvsfile' => 'URL::URL_cvsfile',
46                          'cvs' => 'URL::URL_cvs',
47 <                        'file' => 'URL::URL_file'
47 >                        'file' => 'URL::URL_file',
48 >                        'filed' => 'URL::URL_filed'
49                  };
47        $self->{filebase}="";
48        $self->setbase("file", {}); # Base file as default
50   }
51  
52 < sub setcache {
52 > sub get {
53          my $self=shift;
54 <        my $cache=shift;
54 >        my $origurl=shift;
55 >        my $file="";
56  
57 <        $self->{cache}=$cache;
57 >        my $url=URL::URLclass->new($origurl);
58 >        my $type=$url->type();
59 >        $url->expandurl($self->currentbase($type));
60 >        my $fullurl=$url->url();
61 >
62 >        $file=$self->{cache}->file($fullurl);
63 >        if ( $file eq "" ) {
64 >          ($fullurl,$file)=$self->download($origurl, @_);
65 >        }
66 >        return ($fullurl, $file);
67   }
68  
69 < sub get ($@) {
69 > sub download {
70          my $self=shift;
71 <        my $origurl=shift;
72 <        my $dirname=shift;
73 <        my $rest;
74 <        my $type;
75 <        my $url;
76 <        my $version;
77 <        my $rv="";
78 <
79 <        if ( ! defined $dirname ) {
80 <          $dirname=$self->{cache};
81 <        }
82 <        chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
83 <        chomp $origurl;
73 <        # get our version info from the url (after last ??)
74 <        ( $url, $version) = split /\?\?/, $origurl;
75 <        if ( $url=~/:/ ) {
76 <          ($type,$rest) = split /:/, $url;
77 <        }
78 <        else {
79 <           $type='label';
80 <           $rest=$url.":".$version;
81 <        }
82 <        if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
83 <           print "URLhandler: Unsupported type $type\n";
84 <           carp;
85 <        }
86 <        else {
87 <           if ( $#{$self->{urlostack}{$type}} < 0 ) {
88 <                print "URLhandler : base not set for type $type \n";
89 <           }
90 <           else {
91 <             $rv=
92 <             eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}->
93 <                                get($rest, $dirname);
94 <           }
71 >        my $origurl=shift;
72 >
73 >        # Process the URL string
74 >        my $url=URL::URLclass->new($origurl);
75 >        my $type=$url->type();
76 >        $urltypehandler=$self->_typehandler($type);
77 >        $url->expandurl($self->currentbase($type));
78 >
79 >        # Generate a location name if not provided
80 >        $nocache=1;
81 >        if ( @_ ) {
82 >           $location=shift;
83 >           $nocache=0; # dont cache if downloaded to an external location
84          }
85 <        if ( $rv ne "" ) {
86 <                $rv=$dirname."/".$rv;
85 >        else {
86 >           $location=$self->{cache}->filename($url->url());
87          }
88 <        return $rv;
88 >        # -- get the file from the appropriate handler
89 >        if ( defined $urltypehandler ) {
90 >             # Call the download module
91 >             $file=eval{$urltypehandler->get($url, $location)};
92 >        }
93 >
94 >        # now register it in the cache if successful
95 >        if ( $file && $nocache) {
96 >          $self->{cache}->store($url->url(), $location);
97 >        }
98 >        return ($url->url(), $file);
99   }
100  
101   sub setbase {
# Line 105 | Line 104 | sub setbase {
104          my @args=@_;
105          my $oref;
106  
107 <        # Check type is supported
108 <        if ( ! exists $self->{urlmodules}{$type} ) {
109 <          print "URLhandler error: Unsupported type $type\n";
110 <          return 1;
112 <        }
113 <        else {
114 <          # A new URL object - pushed onto the stack
115 <          $oref=eval{$self->{urlmodules}{$type}}->new();
116 <          push @{$self->{urlostack}{$type}}, $oref;
117 <          $oref->setbase(@args);
118 <        }
107 >        $self->checktype($type);
108 >        # make a new base object
109 >        my $base=URL::URLbase->new(@_);
110 >        push @{$self->{"basestack"}{$type}}, $base;
111   }
112  
113   sub unsetbase {
# Line 123 | Line 115 | sub unsetbase {
115          my $type=shift;
116          my $oref;
117  
118 <        # Check type is supported
118 >        $self->checktype($type);
119 >        # pop off the stack and call the unset base method
120 >        if ( $#{$self->{basestack}{$type}} >=0 ) {
121 >           my $base=pop @{$self->{basestack}{$type}};
122 >           undef $base;
123 >        }
124 >        else {
125 >           die "URLhandler error: Unable to unset type $type\n";
126 >        }
127 >        # remove the stack if its empty
128 >        if ( $#{$self->{basestack}{$type}} == -1 ) {
129 >          delete $self->{basestack}{$type};
130 >        }
131 > }
132 >
133 > sub currentbase {
134 >        my $self=shift;
135 >        my $type=shift;
136 >        my $rv;
137 >
138 >        if ( exists $self->{basestack}{$type} ) {
139 >          $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
140 >        }
141 >        else {
142 >          $rv=$self->{dummybase};
143 >        }
144 >        return $rv;
145 > }
146 >
147 > sub checktype($type) {
148 >        my $self=shift;
149 >        my $type=shift;
150 >
151 >        # Check type is supported
152          if ( ! exists $self->{urlmodules}{$type} ) {
153 <          print "URLhandler error: Unsupported type $type\n";
129 <          return 1;
130 <        }
131 <        else {
132 <          # pop off the stack and call the unset base method
133 <          if ( $#{$self->{urlostack}{$type}} >=0 ) {
134 <            $oref=pop @{$self->{urlostack}{$type}};
135 <            $oref->unsetbase();
136 <            undef $oref;
137 <          }
138 <          else {
139 <           print "URLhandler error: Unable to unset type $type\n";
140 <           return 1;
141 <          }
153 >          die "URLhandler error: Unsupported type $type\n";
154          }
155   }
156  
157 < # ------------------------ General Support Routines ----------------------------
157 > sub _typehandler {
158 >        my $self=shift;
159 >        my $type=shift;
160 >
161 >        $self->checktype($type);
162  
163 < sub cachefilename {
164 <         my $self=shift;
165 <             use File::Basename;
166 <             use Utilities::AddDir;
167 <             my $filebase=dirname($rest);
168 <             $cache="/tmp/williamc/urlhandler$$";
169 <             adddir($cache);
154 <             $filename=$cache."/".$filebase;
163 >        # instantiate only if it dosnt already exist;
164 >        if ( exists $self->{'urlobjs'}{$type} ) {
165 >                $self->{'urlobjs'}{$type};
166 >        }      
167 >        else {
168 >                $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
169 >        }
170   }
156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines