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.2 by williamc, Mon Aug 23 15:54:10 1999 UTC vs.
Revision 1.15.2.4 by williamc, Wed Aug 9 13:48:06 2000 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines