ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.21
Committed: Fri Jan 14 17:36:43 2011 UTC (14 years, 3 months ago) by muzaffar
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.20: +0 -0 lines
State: FILE REMOVED
Log Message:
merged SCRAM_V2 branch in to head

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
38 use URL::URL_cvs;
39 use URL::URL_file;
40 use URL::URL_http;
41 use URL::URL_svn;
42
43 $self->{urlmodules}={
44 'cvs' => 'URL::URL_cvs',
45 'file' => 'URL::URL_file',
46 'http' => 'URL::URL_http',
47 'svn' => 'URL::URL_svn'
48 };
49 }
50
51 sub get
52 {
53 my $self=shift;
54 my $origurl=shift;
55 my $file="";
56
57 my $url=URL::URLclass->new($origurl);
58 my $type=$url->type();
59 $url->merge($self->currentbase($type));
60 my $fullurl=$url->url();
61
62 $file=$self->{cache}->file($fullurl);
63 if ( $file eq "" )
64 {
65 ($fullurl,$file)=$self->download($origurl, @_);
66 if (-f $file)
67 {
68 my $filemode = 0644; chmod $filemode, $file;
69 }
70 }
71 return ($fullurl, $file);
72 }
73
74 sub download {
75 my $self=shift;
76 my $origurl=shift;
77
78 # Process the URL string
79 my $url=URL::URLclass->new($origurl);
80 my $type=$url->type();
81 $urltypehandler=$self->_typehandler($type);
82 $url->merge($self->currentbase($type));
83
84 # Generate a location name if not provided
85 my $nocache=1;
86 if ( @_ ) {
87 $location=shift;
88 $nocache=0; # dont cache if downloaded to an external location
89 }
90 else {
91 $location=$self->{cache}->filename($url->url());
92 }
93 # -- get the file from the appropriate handler
94 if ( defined $urltypehandler ) {
95 # Call the download module
96 $file=eval{$urltypehandler->get($url, $location)};
97 }
98
99 # now register it in the cache if successful
100 if ( $file ne "" ) {
101 if ( $file && $nocache) {
102 $self->{cache}->store($url->url(), $file);
103 }
104 }
105 return ($url->url(), $file, $urltypehandler->error());
106 }
107
108 sub expandurl {
109 my $self=shift;
110 my $urlstring=shift;
111
112 my $url=URL::URLclass->new($urlstring);
113 my $type=$url->type();
114 $url->merge($self->currentbase($type));
115 return $url;
116 }
117
118 sub setbase {
119 my $self=shift;
120 my $partialurl=shift;
121
122 my $base=URL::URLclass->new($partialurl);
123 my $type=$base->type();
124 $self->checktype($type);
125 # make a new base-url object
126 push @{$self->{"basestack"}{$type}}, $base;
127 return $base;
128 }
129
130 sub unsetbase {
131 my $self=shift;
132 my $type=shift;
133 my $oref;
134
135 $self->checktype($type);
136 # pop off the stack and call the unset base method
137 if ( $#{$self->{basestack}{$type}} >=0 ) {
138 my $base=pop @{$self->{basestack}{$type}};
139 undef $base;
140 }
141 else {
142 die "URLhandler error: Unable to unset type $type\n";
143 }
144 # remove the stack if its empty
145 if ( $#{$self->{basestack}{$type}} == -1 ) {
146 delete $self->{basestack}{$type};
147 }
148 }
149
150 sub currentbase {
151 my $self=shift;
152 my $type=shift;
153 my $rv;
154
155 if ( exists $self->{basestack}{$type} ) {
156 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
157 }
158 else {
159 $rv=undef;
160 }
161 return $rv;
162 }
163
164 sub checktype($type) {
165 my $self=shift;
166 my $type=shift;
167
168 # Check type is supported
169 if ( ! exists $self->{urlmodules}{$type} ) {
170 die "URLhandler error: Unsupported type $type\n";
171 }
172 }
173
174 sub _typehandler {
175 my $self=shift;
176 my $type=shift;
177
178 $self->checktype($type);
179
180 # instantiate only if it doesnt already exist;
181 if ( exists $self->{'urlobjs'}{$type} ) {
182 $self->{'urlobjs'}{$type};
183 }
184 else {
185 $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
186 }
187 }