ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.15.2.7
Committed: Mon Aug 28 07:03:29 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
CVS Tags: BuildSystemProto1, V0_18_0, V0_18_0model, V0_17_1, V0_18_0alpha, V0_17_0, V0_16_4, V0_16_3, V0_16_2, V0_16_1, V0_16_0, V0_15_1, V0_15_0, V0_15_0beta
Branch point for: V0_17branch, V0_16branch, V0_15branch
Changes since 1.15.2.6: +5 -3 lines
Log Message:
Update error handling URL

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 - 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 # ----------------------------------------------------------------------
14
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 {
23 my $class=shift;
24 my $cache=shift;
25 $self={};
26 bless $self, $class;
27 $self->init($cache);
28 return $self;
29 }
30
31 sub init {
32 use Utilities::AddDir;
33 my $self=shift;
34 my $cache=shift;
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::URL_cvs',
42 'file' => 'URL::URL_file',
43 'test' => 'URL::URL_test'
44 };
45 }
46
47 sub get {
48 my $self=shift;
49 my $origurl=shift;
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 download {
65 my $self=shift;
66 my $origurl=shift;
67
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 $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 {
123 my $self=shift;
124 my $type=shift;
125 my $oref;
126
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 die "URLhandler error: Unsupported type $type\n";
163 }
164 }
165
166 sub _typehandler {
167 my $self=shift;
168 my $type=shift;
169
170 $self->checktype($type);
171
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 }