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.7 by williamc, Mon Aug 28 07:03:29 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 >        my $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
83 <          $oref=eval{$self->{urlmodules}{$type}}->new();
84 <          push @{$self->{urlostack}{$type}}, $oref;
85 <          $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 ne "" ) {
92 >         if ( $file && $nocache) {
93 >          #$self->{cache}->store($url->url(), $location);
94 >          $self->{cache}->store($url->url(), $file);
95 >         }
96          }
97 +        return ($url->url(), $file, $urltypehandler->error());
98 + }
99 +
100 + sub expandurl {
101 +        my $self=shift;
102 +        my $urlstring=shift;
103 +
104 +        my $url=URL::URLclass->new($urlstring);
105 +        my $type=$url->type();
106 +        $url->merge($self->currentbase($type));
107 +        return $url;
108 + }
109 +
110 + sub setbase {
111 +        my $self=shift;
112 +        my $partialurl=shift;
113 +
114 +        my $base=URL::URLclass->new($partialurl);
115 +        my $type=$base->type();
116 +        $self->checktype($type);
117 +        # make a new base-url object
118 +        push @{$self->{"basestack"}{$type}}, $base;
119 +        return $base;
120   }
121  
122   sub unsetbase {
# Line 106 | Line 124 | sub unsetbase {
124          my $type=shift;
125          my $oref;
126  
127 <        # Check type is supported
127 >        $self->checktype($type);
128 >        # pop off the stack and call the unset base method
129 >        if ( $#{$self->{basestack}{$type}} >=0 ) {
130 >           my $base=pop @{$self->{basestack}{$type}};
131 >           undef $base;
132 >        }
133 >        else {
134 >           die "URLhandler error: Unable to unset type $type\n";
135 >        }
136 >        # remove the stack if its empty
137 >        if ( $#{$self->{basestack}{$type}} == -1 ) {
138 >          delete $self->{basestack}{$type};
139 >        }
140 > }
141 >
142 > sub currentbase {
143 >        my $self=shift;
144 >        my $type=shift;
145 >        my $rv;
146 >
147 >        if ( exists $self->{basestack}{$type} ) {
148 >          $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
149 >        }
150 >        else {
151 >          $rv=undef;
152 >        }
153 >        return $rv;
154 > }
155 >
156 > sub checktype($type) {
157 >        my $self=shift;
158 >        my $type=shift;
159 >
160 >        # Check type is supported
161          if ( ! exists $self->{urlmodules}{$type} ) {
162 <          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 <          }
162 >          die "URLhandler error: Unsupported type $type\n";
163          }
164   }
165  
166 < # ------------------------ General Support Routines ----------------------------
166 > sub _typehandler {
167 >        my $self=shift;
168 >        my $type=shift;
169 >
170 >        $self->checktype($type);
171  
172 < sub cachefilename {
173 <         my $self=shift;
174 <             use File::Basename;
175 <             use Utilities::AddDir;
176 <             my $filebase=dirname($rest);
177 <             $cache="/tmp/williamc/urlhandler$$";
178 <             adddir($cache);
137 <             $filename=$cache."/".$filebase;
172 >        # instantiate only if it dosnt already exist;
173 >        if ( exists $self->{'urlobjs'}{$type} ) {
174 >                $self->{'urlobjs'}{$type};
175 >        }      
176 >        else {
177 >                $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
178 >        }
179   }
139

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines