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.13 by williamc, Wed Mar 29 09:48:33 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_test;
44 +        use URL::URL_filed;
45          $self->{urlmodules}={
46                          'cvsfile' => 'URL::URL_cvsfile',
47                          'cvs' => 'URL::URL_cvs',
48 <                        'file' => 'URL::URL_file'
48 >                        'file' => 'URL::URL_file',
49 >                        'filed' => 'URL::URL_filed',
50 >                        'test' => 'URL::URL_test'
51                  };
47        $self->{filebase}="";
48        $self->setbase("file", {}); # Base file as default
52   }
53  
54 < sub setcache {
54 > sub get {
55          my $self=shift;
56 <        my $cache=shift;
56 >        my $origurl=shift;
57 >        my $file="";
58  
59 <        $self->{cache}=$cache;
59 >        my $url=URL::URLclass->new($origurl);
60 >        my $type=$url->type();
61 >        $url->expandurl($self->currentbase($type));
62 >        my $fullurl=$url->url();
63 >
64 >        $file=$self->{cache}->file($fullurl);
65 >        if ( $file eq "" ) {
66 >          ($fullurl,$file)=$self->download($origurl, @_);
67 >        }
68 >        return ($fullurl, $file);
69   }
70  
71 < sub get ($@) {
71 > sub download {
72          my $self=shift;
73 <        my $origurl=shift;
74 <        my $dirname=shift;
75 <        my $rest;
76 <        my $type;
77 <        my $url;
78 <        my $version;
79 <        my $rv="";
80 <
81 <        if ( ! defined $dirname ) {
82 <          $dirname=$self->{cache};
83 <        }
84 <        chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
85 <        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 <           }
73 >        my $origurl=shift;
74 >
75 >        # Process the URL string
76 >        my $url=URL::URLclass->new($origurl);
77 >        my $type=$url->type();
78 >        $urltypehandler=$self->_typehandler($type);
79 >        $url->expandurl($self->currentbase($type));
80 >
81 >        # Generate a location name if not provided
82 >        $nocache=1;
83 >        if ( @_ ) {
84 >           $location=shift;
85 >           $nocache=0; # dont cache if downloaded to an external location
86          }
87 <        if ( $rv ne "" ) {
88 <                $rv=$dirname."/".$rv;
87 >        else {
88 >           $location=$self->{cache}->filename($url->url());
89          }
90 <        return $rv;
90 >        # -- get the file from the appropriate handler
91 >        if ( defined $urltypehandler ) {
92 >             # Call the download module
93 >             $file=eval{$urltypehandler->get($url, $location)};
94 >        }
95 >
96 >        # now register it in the cache if successful
97 >        if ( $file && $nocache) {
98 >          $self->{cache}->store($url->url(), $location);
99 >        }
100 >        return ($url->url(), $file);
101   }
102  
103   sub setbase {
# Line 105 | Line 106 | sub setbase {
106          my @args=@_;
107          my $oref;
108  
109 <        # Check type is supported
110 <        if ( ! exists $self->{urlmodules}{$type} ) {
111 <          print "URLhandler error: Unsupported type $type\n";
112 <          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 <        }
109 >        $self->checktype($type);
110 >        # make a new base object
111 >        my $base=URL::URLbase->new(@_);
112 >        push @{$self->{"basestack"}{$type}}, $base;
113   }
114  
115   sub unsetbase {
# Line 123 | Line 117 | sub unsetbase {
117          my $type=shift;
118          my $oref;
119  
120 <        # Check type is supported
120 >        $self->checktype($type);
121 >        # pop off the stack and call the unset base method
122 >        if ( $#{$self->{basestack}{$type}} >=0 ) {
123 >           my $base=pop @{$self->{basestack}{$type}};
124 >           undef $base;
125 >        }
126 >        else {
127 >           die "URLhandler error: Unable to unset type $type\n";
128 >        }
129 >        # remove the stack if its empty
130 >        if ( $#{$self->{basestack}{$type}} == -1 ) {
131 >          delete $self->{basestack}{$type};
132 >        }
133 > }
134 >
135 > sub currentbase {
136 >        my $self=shift;
137 >        my $type=shift;
138 >        my $rv;
139 >
140 >        if ( exists $self->{basestack}{$type} ) {
141 >          $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
142 >        }
143 >        else {
144 >          $rv=$self->{dummybase};
145 >        }
146 >        return $rv;
147 > }
148 >
149 > sub checktype($type) {
150 >        my $self=shift;
151 >        my $type=shift;
152 >
153 >        # Check type is supported
154          if ( ! exists $self->{urlmodules}{$type} ) {
155 <          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 <          }
155 >          die "URLhandler error: Unsupported type $type\n";
156          }
157   }
158  
159 < # ------------------------ General Support Routines ----------------------------
159 > sub _typehandler {
160 >        my $self=shift;
161 >        my $type=shift;
162 >
163 >        $self->checktype($type);
164  
165 < sub cachefilename {
166 <         my $self=shift;
167 <             use File::Basename;
168 <             use Utilities::AddDir;
169 <             my $filebase=dirname($rest);
170 <             $cache="/tmp/williamc/urlhandler$$";
171 <             adddir($cache);
154 <             $filename=$cache."/".$filebase;
165 >        # instantiate only if it dosnt already exist;
166 >        if ( exists $self->{'urlobjs'}{$type} ) {
167 >                $self->{'urlobjs'}{$type};
168 >        }      
169 >        else {
170 >                $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
171 >        }
172   }
156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines