ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.7
Committed: Thu Nov 4 10:08:33 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.6: +79 -54 lines
Log Message:
New interface and use of cache

File Contents

# Content
1 # url handler -> returns the location of the file
2 # Interface
3 # ---------
4 # new(cache) : A new urlhandler with a defined default cahce directory
5 # get(url) : download from the specified url to the default cache
6 # getto(url,dirlocation): download to the specified directory - no cache registry
7 # setbase(type,variablehash) : set the url defaults for a specific url type
8 # arguments are dependent on type
9 # unsetbase(type) : deactivate a previously set base
10 #
11 # ----------------------------------------------------------------------
12 # returns file location - or crashes out
13 # can pass a file name for the item to be stored as
14 # if not then stores in a default cache.
15
16 package URL::URLhandler;
17 require 5.004;
18 use Utilities::AddDir;
19 use URL::URLUtilities;
20 use URL::URL_base;
21 use Carp;
22
23 sub new {
24 my $class=shift;
25 my $cache=shift;
26 $self={};
27 bless $self, $class;
28 $self->init($cache);
29 return $self;
30 }
31
32 sub init {
33 use URL::URLcache;
34 use Utilities::AddDir;
35 my $self=shift;
36 my $cache=shift;
37 $self->{cache}=$cache;
38 $self->{cachestore}=$self->{cache}->filestore();
39 use URL::URL_cvs;
40 use URL::URL_cvsfile;
41 use URL::URL_file;
42 use URL::URL_filed;
43 $self->{urlmodules}={
44 'cvsfile' => 'URL::URL_cvsfile',
45 'cvs' => 'URL::URL_cvs',
46 'file' => 'URL::URL_file',
47 'filed' => 'URL::URL_filed'
48 };
49 $self->{filebase}="";
50 $self->setbase("file", {}); # Base file as default
51 $self->setbase("filed", {}); # Base file as default
52 }
53
54 #
55 sub get {
56 my $self=shift;
57 my $url=shift;
58
59
60 my ($urlstring, $type, $urltypehandler, $base, $version)
61 =$self->_parseurl($url); # get a unique url string
62 my $rv="";
63 $rv=$self->getto($url,$self->{cache}->filename($urlstring));
64
65 # now register it in the cache if successful
66 if ( $rv ne "" ) {
67 print "Storing ".$urlstring.",".$rv."\n";
68 $self->{cache}->store($urlstring, $rv);
69 }
70 return $rv;
71 }
72
73 sub getto ($@) {
74 my $self=shift;
75 my $origurl=shift;
76 my $dirname=shift;
77
78 my $rest;
79 my $type;
80 my $version;
81 my $rv="";
82
83 # Process the URL string
84 my ($urlstring, $type, $urltypehandler, $base, $version)
85 =$self->_parseurl($origurl);
86
87 if ( defined $urltypehandler ) {
88 # Call the download module
89 $rv=eval{$urltypehandler->get($base.(($version ne "")?"\?".$version:""), $dirname); };
90 }
91
92 # Check the return type
93 if ( defined $rv ) {
94 # if ( $rv!~/^\// ) {
95 # $rv=$dirname."/".$rv;
96 # }
97 }
98 return $rv;
99 }
100
101 sub setbase {
102 my $self=shift;
103 my $type=shift;
104 my @args=@_;
105 my $oref;
106
107 # Check type is supported
108 if ( ! exists $self->{urlmodules}{$type} ) {
109 print "URLhandler error: Unsupported type $type\n";
110 return 1;
111 }
112 else {
113 # A new URL object - pushed onto the stack
114 $oref=eval{$self->{urlmodules}{$type}}->new();
115 push @{$self->{urlostack}{$type}}, $oref;
116 $oref->setbase(@args);
117 }
118 }
119
120 sub unsetbase {
121 my $self=shift;
122 my $type=shift;
123 my $oref;
124
125 # Check type is supported
126 if ( ! exists $self->{urlmodules}{$type} ) {
127 print "URLhandler error: Unsupported type $type\n";
128 return 1;
129 }
130 else {
131 # pop off the stack and call the unset base method
132 if ( $#{$self->{urlostack}{$type}} >=0 ) {
133 $oref=pop @{$self->{urlostack}{$type}};
134 $oref->unsetbase();
135 undef $oref;
136 }
137 else {
138 print "URLhandler error: Unable to unset type $type\n";
139 return 1;
140 }
141 }
142 }
143
144 # -------------------- Support Routines (private Methods) -------------
145
146 #
147 # Process the URL string into its component parts
148 #
149 sub _parseurl {
150 my $self=shift;
151 my $origurl=shift;
152 chomp $origurl;
153
154 my ($type,$rest,$cmdstring)=URLUtilities::parseURL($origurl);
155 my $urlstring="";
156 my $url;
157
158 $base="";
159
160 # check type are supported
161 if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
162 print "URLhandler: Unsupported type $type\n";
163 die;
164 }
165 else {
166 if ( $#{$self->{urlostack}{$type}} < 0 ) {
167 print "URLhandler : base not set for type $type \n";
168 die;
169 }
170 else {
171 # ---- type is supported
172 $urltypehandler=
173 ${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}];
174 $base=$urltypehandler->baseurl();
175 }
176 }
177
178 my $urlbase=($base ne ""?$base."/":"").$rest;
179 $urlstring=$type.":".$urlbase.($cmdstring ne ""?"?".$cmdstring:"");
180 return $urlstring, $type, $urltypehandler, $urlbase, $cmdstring;
181 }