ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.15.2.1
Committed: Fri Aug 4 08:21:26 2000 UTC (24 years, 9 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.15: +0 -0 lines
Log Message:
Add to branch

File Contents

# Content
1 # Interface
2 # ---------
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
9 # unsetbase(type) : deactivate a previously set base
10 # currentbase(type) : return the current base for the given type
11 #
12 # ----------------------------------------------------------------------
13
14 package URL::URLhandler;
15 require 5.004;
16 use Utilities::AddDir;
17 use URL::URLcache;
18 use URL::URLclass;
19 use Carp;
20
21 sub new {
22 my $class=shift;
23 my $cache=shift;
24 $self={};
25 bless $self, $class;
26 $self->init($cache);
27 return $self;
28 }
29
30 sub init {
31 use Utilities::AddDir;
32 my $self=shift;
33 my $cache=shift;
34 $self->{cache}=$cache;
35 $self->{cachestore}=$self->{cache}->filestore();
36 use URL::URL_cvs;
37 use URL::URL_file;
38 use URL::URL_test;
39 $self->{urlmodules}={
40 'cvs' => 'URL::URL_cvs',
41 'file' => 'URL::URL_file',
42 'test' => 'URL::URL_test'
43 };
44 }
45
46 sub get {
47 my $self=shift;
48 my $origurl=shift;
49 my $file="";
50
51 my $url=URL::URLclass->new($origurl);
52 my $type=$url->type();
53 $url->merge($self->currentbase($type));
54 my $fullurl=$url->url();
55
56 $file=$self->{cache}->file($fullurl);
57 if ( $file eq "" ) {
58 ($fullurl,$file)=$self->download($origurl, @_);
59 }
60 return ($fullurl, $file);
61 }
62
63 sub download {
64 my $self=shift;
65 my $origurl=shift;
66
67 # Process the URL string
68 my $url=URL::URLclass->new($origurl);
69 my $type=$url->type();
70 $urltypehandler=$self->_typehandler($type);
71 $url->merge($self->currentbase($type));
72
73 # Generate a location name if not provided
74 $nocache=1;
75 if ( @_ ) {
76 $location=shift;
77 $nocache=0; # dont cache if downloaded to an external location
78 }
79 else {
80 $location=$self->{cache}->filename($url->url());
81 }
82 # -- get the file from the appropriate handler
83 if ( defined $urltypehandler ) {
84 # Call the download module
85 $file=eval{$urltypehandler->get($url, $location)};
86 }
87
88 # now register it in the cache if successful
89 if ( $file && $nocache) {
90 $self->{cache}->store($url->url(), $location);
91 }
92 return ($url->url(), $file);
93 }
94
95 sub setbase {
96 my $self=shift;
97 my $partialurl=shift;
98
99 my $base=URL::URLclass->new($partialurl);
100 my $type=$base->type();
101 $self->checktype($type);
102 # make a new base-url object
103 push @{$self->{"basestack"}{$type}}, $base;
104 }
105
106 sub unsetbase {
107 my $self=shift;
108 my $type=shift;
109 my $oref;
110
111 $self->checktype($type);
112 # pop off the stack and call the unset base method
113 if ( $#{$self->{basestack}{$type}} >=0 ) {
114 my $base=pop @{$self->{basestack}{$type}};
115 undef $base;
116 }
117 else {
118 die "URLhandler error: Unable to unset type $type\n";
119 }
120 # remove the stack if its empty
121 if ( $#{$self->{basestack}{$type}} == -1 ) {
122 delete $self->{basestack}{$type};
123 }
124 }
125
126 sub currentbase {
127 my $self=shift;
128 my $type=shift;
129 my $rv;
130
131 if ( exists $self->{basestack}{$type} ) {
132 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
133 }
134 else {
135 $rv=undef;
136 }
137 return $rv;
138 }
139
140 sub checktype($type) {
141 my $self=shift;
142 my $type=shift;
143
144 # Check type is supported
145 if ( ! exists $self->{urlmodules}{$type} ) {
146 die "URLhandler error: Unsupported type $type\n";
147 }
148 }
149
150 sub _typehandler {
151 my $self=shift;
152 my $type=shift;
153
154 $self->checktype($type);
155
156 # instantiate only if it dosnt already exist;
157 if ( exists $self->{'urlobjs'}{$type} ) {
158 $self->{'urlobjs'}{$type};
159 }
160 else {
161 $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
162 }
163 }