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.14 by williamc, Thu May 18 14:57:30 2000 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines