| 1 | #!/usr/bin/perl | ||
| 2 | package JMX::Jmx4Perl::Agent::Jolokia::ArtifactHandler; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | JMX::Jmx4Perl::Agent::ArtifactHandler - Handler for extracting and manipulating | ||
| 7 | Jolokia artifacts | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This module is responsible for mangaging a singe JAR or WAR Archive. It | ||
| 12 | requires L<Archive::Zip> for proper operation. | ||
| 13 | |||
| 14 | I.e. this module can | ||
| 15 | |||
| 16 | =over | ||
| 17 | |||
| 18 | =item * | ||
| 19 | |||
| 20 | Extract jolokia-access.xml and web.xml from WAR/JAR archives | ||
| 21 | |||
| 22 | =item * | ||
| 23 | |||
| 24 | Check for the esistance of jolokia-access.xml | ||
| 25 | |||
| 26 | =item * | ||
| 27 | |||
| 28 | Update web.xml for WAR files | ||
| 29 | |||
| 30 | =back | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use Data::Dumper; | ||
| 35 | use strict; | ||
| 36 | |||
| 37 | use vars qw($HAS_ARCHIVE_ZIP $GLOBAL_ERROR); | ||
| 38 | |||
| 39 | BEGIN { | ||
| 40 | $HAS_ARCHIVE_ZIP = eval "require Archive::Zip; Archive::Zip->import(qw(:ERROR_CODES)); 1"; | ||
| 41 | if ($HAS_ARCHIVE_ZIP) { | ||
| 42 | Archive::Zip::setErrorHandler( sub { | ||
| 43 | $GLOBAL_ERROR = join " ",@_; | ||
| 44 | chomp $GLOBAL_ERROR; | ||
| 45 | } ); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item $handler = JMX::Jmx4Perl::Agent::Jolokia::ArtifactHandler->new(...) | ||
| 54 | |||
| 55 | Create a new handler with the following options: | ||
| 56 | |||
| 57 | file => $file : Path to archive to handle | ||
| 58 | logger => $logger : Logger to use | ||
| 59 | meta => $meta : Jolokia-Meta handler to extract the type of an archive | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my $class = shift; | ||
| 65 | my %args = @_; | ||
| 66 | my $file = $args{file}; | ||
| 67 | my $self = { file => $file, logger => $args{logger}, meta => $args{meta}}; | ||
| 68 | bless $self,(ref($class) || $class); | ||
| 69 | $self->_fatal("No Archive::Zip found. Please install it for handling Jolokia archives.") unless $HAS_ARCHIVE_ZIP; | ||
| 70 | $self->_fatal("No file given") unless $file; | ||
| 71 | $self->_fatal("No such file $file") unless -e $file; | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | =item $info = $handler->info() | ||
| 77 | |||
| 78 | Extract information about an archive. Return value is a has with the following | ||
| 79 | keys: | ||
| 80 | |||
| 81 | "version" Agent's version | ||
| 82 | "type" Agent type (war, osgi, osgi-bundle, mule, jdk6) | ||
| 83 | "artifactId" Maven artifact id | ||
| 84 | "groupId" Maven group Id | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub info { | ||
| 89 | my $self = shift; | ||
| 90 | my $file = $self->{file}; | ||
| 91 | my $jar = $self->_read_archive(); | ||
| 92 | my @props = $jar->membersMatching('META-INF/maven/org.jolokia/.*?/pom.properties'); | ||
| 93 | $self->_fatal("Cannot extract pom.properties from $file") unless @props; | ||
| 94 | for my $prop (@props) { | ||
| 95 | my ($content,$status) = $prop->contents; | ||
| 96 | $self->_fatal("Cannot extract pom.properties: ",$GLOBAL_ERROR) unless $status eq AZ_OK(); | ||
| 97 | my $ret = {}; | ||
| 98 | for my $l (split /\n/,$content) { | ||
| 99 | next if $l =~ /^\s*#/; | ||
| 100 | my ($k,$v) = split /=/,$l,2; | ||
| 101 | $ret->{$k} = $v; | ||
| 102 | } | ||
| 103 | $self->_fatal("$file is not a Jolokia archive") unless $ret->{groupId} eq "org.jolokia" ; | ||
| 104 | my $type; | ||
| 105 | if ($self->{meta}->initialized()) { | ||
| 106 | $type = $self->{meta}->extract_type($ret->{artifactId}); | ||
| 107 | } else { | ||
| 108 | $type = $self->_detect_type_by_heuristic($ret->{artifactId}); | ||
| 109 | } | ||
| 110 | if ($type) { | ||
| 111 | $ret->{type} = $type; | ||
| 112 | return $ret; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return {}; | ||
| 116 | } | ||
| 117 | |||
| 118 | =item $handler->add_policy($policy) | ||
| 119 | |||
| 120 | Add or update the policy given as string to this archive. Dependening on | ||
| 121 | whether it is a WAR or another agent, it is put into the proper place | ||
| 122 | |||
| 123 | For "war" agents, this is F<WEB-INF/classes/jolokia-access.xml>, for all others | ||
| 124 | it is F</jolokia-access.xml> | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub add_policy { | ||
| 129 | my $self = shift; | ||
| 130 | my $policy = shift; | ||
| 131 | my $file = $self->{file}; | ||
| 132 | $self->_fatal("No such file $policy") unless -e $policy; | ||
| 133 | |||
| 134 | my $jar = $self->_read_archive(); | ||
| 135 | my $path = $self->_policy_path; | ||
| 136 | |||
| 137 | my $existing = $jar->removeMember($path); | ||
| 138 | my $res = $jar->addFile($policy,$path); | ||
| 139 | $self->_fatal("Cannot add $policy to $file as ",$path,": ",$GLOBAL_ERROR) unless $res; | ||
| 140 | my $status = $jar->overwrite(); | ||
| 141 | $self->_fatal("Cannot write $file: ",$GLOBAL_ERROR) unless $status eq AZ_OK(); | ||
| 142 | $self->_info($existing ? "Replacing existing policy " : "Adding policy ","[em]",$path,"[/em]",$existing ? " in " : " to ","[em]",$file,"[/em]"); | ||
| 143 | } | ||
| 144 | |||
| 145 | =item $handler->remove_policy() | ||
| 146 | |||
| 147 | Remove a policy file (no-op, when no policy is present) | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub remove_policy { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | my $file = $self->{file}; | ||
| 155 | |||
| 156 | my $jar = $self->_read_archive(); | ||
| 157 | my $path = $self->_policy_path; | ||
| 158 | |||
| 159 | my $existing = $jar->removeMember($path); | ||
| 160 | if ($existing) { | ||
| 161 | my $status = $jar->overwrite(); | ||
| 162 | $self->_fatal("Cannot write $file: ",$GLOBAL_ERROR) unless $status eq AZ_OK(); | ||
| 163 | $self->_info("Removing policy","[em]",$path,"[/em]"," in ","[em]",$file,"[/em]"); | ||
| 164 | } else { | ||
| 165 | $self->_info("No policy found, leaving ","[em]",$file,"[/em]"," untouched."); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | =item $handler->has_policy() | ||
| 170 | |||
| 171 | Returns true (i.e. the path to the policy file) if a policy file is contained, | ||
| 172 | C<undef> otherwise. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub has_policy { | ||
| 177 | my $self = shift; | ||
| 178 | |||
| 179 | my $jar = $self->_read_archive(); | ||
| 180 | my $path = $self->_policy_path; | ||
| 181 | return $jar->memberNamed($path) ? $path : undef; | ||
| 182 | } | ||
| 183 | |||
| 184 | =item $handler->get_policy() | ||
| 185 | |||
| 186 | Get the policy file as string or C<undef> if no policy is contained. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub get_policy { | ||
| 191 | my $self = shift; | ||
| 192 | |||
| 193 | my $jar = $self->_read_archive(); | ||
| 194 | my $path = $self->_policy_path; | ||
| 195 | return $jar->contents($path); | ||
| 196 | } | ||
| 197 | |||
| 198 | =item $handler->extract_webxml() | ||
| 199 | |||
| 200 | Extract F<web.xml> from WAR agents, for other types, a fatal error is | ||
| 201 | raised. Return value is a string containing the web.xml. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub extract_webxml { | ||
| 206 | my $self = shift; | ||
| 207 | my $type = $self->type; | ||
| 208 | $self->_fatal("web.xml can only be read from 'war' archives (not '",$type,"')") unless $type eq "war"; | ||
| 209 | |||
| 210 | my $jar = $self->_read_archive(); | ||
| 211 | return $jar->contents("WEB-INF/web.xml"); | ||
| 212 | } | ||
| 213 | |||
| 214 | =item $handler->update_webxml($webxml) | ||
| 215 | |||
| 216 | Update F<web.xml> in WAR agents, for other types, a fatal error is | ||
| 217 | raised. Return value is a string containing the web.xml. C<$webxml> is the | ||
| 218 | descriptor as a string. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub update_webxml { | ||
| 223 | my $self = shift; | ||
| 224 | my $webxml = shift; | ||
| 225 | my $type = $self->type; | ||
| 226 | $self->_fatal("web.xml can only be updated in 'war' archives (not '",$type,"')") unless $type eq "war"; | ||
| 227 | |||
| 228 | my $jar = $self->_read_archive(); | ||
| 229 | $jar->removeMember("WEB-INF/web.xml"); | ||
| 230 | my $res = $jar->addString($webxml,"WEB-INF/web.xml"); | ||
| 231 | $self->_fatal("Cannot update WEB-INF/web.xml: ",$GLOBAL_ERROR) unless $res; | ||
| 232 | my $status = $jar->overwrite(); | ||
| 233 | $self->_fatal("Cannot write ",$self->{file},": ",$GLOBAL_ERROR) unless $status eq AZ_OK(); | ||
| 234 | $self->_info("Updated ","[em]","web.xml","[/em]"," for ",$self->{file}); | ||
| 235 | } | ||
| 236 | |||
| 237 | =item $handler->type() | ||
| 238 | |||
| 239 | Return the agent's type, which is one of "war", "osgi", "osgi-bundle", "mule" | ||
| 240 | or "jdk6" | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | sub type { | ||
| 245 | my $self = shift; | ||
| 246 | my $info = $self->info; | ||
| 247 | return $info->{type}; | ||
| 248 | } | ||
| 249 | |||
| 250 | =back | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | # ======================================================================== | ||
| 255 | |||
| 256 | sub _detect_type_by_heuristic { | ||
| 257 | my $self = shift; | ||
| 258 | my $artifact_id = shift; | ||
| 259 | return { | ||
| 260 | "jolokia-osgi" => "osgi", | ||
| 261 | "jolokia-mule" => "mule", | ||
| 262 | "jolokia-osgi-bundle" => "osgi-bundle", | ||
| 263 | "jolokia-jvm-jdk6" => "jdk6", | ||
| 264 | "jolokia-jvm" => "jvm", | ||
| 265 | "jolokia-war" => "war" | ||
| 266 | }->{$artifact_id}; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub _read_archive { | ||
| 270 | my $self = shift; | ||
| 271 | my $file = $self->{file}; | ||
| 272 | my $jar = new Archive::Zip(); | ||
| 273 | my $status = $jar->read($file); | ||
| 274 | $self->_fatal("Cannot read content of $file: ",$GLOBAL_ERROR) unless $status eq AZ_OK(); | ||
| 275 | return $jar; | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | sub _policy_path { | ||
| 280 | my $self = shift; | ||
| 281 | return ($self->type eq "war" ? "WEB-INF/classes/" : "") . "jolokia-access.xml"; | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | sub _fatal { | ||
| 286 | my $self = shift; | ||
| 287 | $self->{logger}->error(@_); | ||
| 288 | die "\n"; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub _info { | ||
| 292 | my $self = shift; | ||
| 293 | $self->{logger}->info(@_); | ||
| 294 | } | ||
| 295 | |||
| 296 | =head1 LICENSE | ||
| 297 | |||
| 298 | This file is part of jmx4perl. | ||
| 299 | Jmx4perl is free software: you can redistribute it and/or modify | ||
| 300 | it under the terms of the GNU General Public License as published by | ||
| 301 | The Free Software Foundation, either version 2 of the License, or | ||
| 302 | (at your option) any later version. | ||
| 303 | |||
| 304 | jmx4perl is distributed in the hope that it will be useful, | ||
| 305 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 306 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 307 | GNU General Public License for more details. | ||
| 308 | |||
| 309 | You should have received a copy of the GNU General Public License | ||
| 310 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||
| 311 | |||
| 312 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 313 | further details. | ||
| 314 | |||
| 315 | =head1 AUTHOR | ||
| 316 | |||
| 317 | roland@cpan.org | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | 1; | ||
| 322 |