| 1 | #----------------------------------------------------------------- | ||
| 2 | # MOSES::MOBY::Generators::GenServices | ||
| 3 | # Author: Martin Senger <martin.senger@gmail.com>, | ||
| 4 | # Edward Kawas <edward.kawas@gmail.com> | ||
| 5 | # | ||
| 6 | # For copyright and disclaimer see below. | ||
| 7 | # | ||
| 8 | # $Id: GenServices.pm,v 1.9 2009/03/30 13:15:03 kawas Exp $ | ||
| 9 | #----------------------------------------------------------------- | ||
| 10 | |||
| 11 | package MOSES::MOBY::Generators::GenServices; | ||
| 12 | use MOSES::MOBY::Base; | ||
| 13 | use base qw( MOSES::MOBY::Base ); | ||
| 14 | use Template; | ||
| 15 | use FindBin qw( $Bin ); | ||
| 16 | use lib $Bin; | ||
| 17 | use File::Spec; | ||
| 18 | use MOSES::MOBY::Cache::Central; | ||
| 19 | use MOSES::MOBY::Generators::Utils; | ||
| 20 | use MOSES::MOBY::Generators::GenTypes; | ||
| 21 | use MOSES::MOBY::Def::Relationship; | ||
| 22 | use strict; | ||
| 23 | |||
| 24 | # add versioning to this module | ||
| 25 | use vars qw /$VERSION/; | ||
| 26 | $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/; | ||
| 27 | |||
| 28 | #----------------------------------------------------------------- | ||
| 29 | # A list of allowed attribute names. See MOSES::MOBY::Base for details. | ||
| 30 | #----------------------------------------------------------------- | ||
| 31 | { | ||
| 32 | my %_allowed = | ||
| 33 | ( | ||
| 34 | outdir => undef, | ||
| 35 | cachedir => undef, | ||
| 36 | registry => undef, | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub _accessible { | ||
| 40 | my ($self, $attr) = @_; | ||
| 41 | exists $_allowed{$attr} or $self->SUPER::_accessible ($attr); | ||
| 42 | } | ||
| 43 | sub _attr_prop { | ||
| 44 | my ($self, $attr_name, $prop_name) = @_; | ||
| 45 | my $attr = $_allowed {$attr_name}; | ||
| 46 | return ref ($attr) ? $attr->{$prop_name} : $attr if $attr; | ||
| 47 | return $self->SUPER::_attr_prop ($attr_name, $prop_name); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | #----------------------------------------------------------------- | ||
| 52 | # init | ||
| 53 | #----------------------------------------------------------------- | ||
| 54 | sub init { | ||
| 55 | my ($self) = shift; | ||
| 56 | $self->SUPER::init(); | ||
| 57 | $self->cachedir ($MOBYCFG::CACHEDIR); | ||
| 58 | $self->registry ($MOBYCFG::REGISTRY); | ||
| 59 | $self->outdir ( $MOBYCFG::GENERATORS_OUTDIR || | ||
| 60 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'generated') ); | ||
| 61 | } | ||
| 62 | |||
| 63 | #----------------------------------------------------------------- | ||
| 64 | # generate_base | ||
| 65 | #----------------------------------------------------------------- | ||
| 66 | sub generate_base { | ||
| 67 | my ($self, @args) = @_; | ||
| 68 | my %args = | ||
| 69 | ( # some default values | ||
| 70 | outdir => $self->outdir, | ||
| 71 | cachedir => $self->cachedir, | ||
| 72 | registry => $self->registry, | ||
| 73 | service_names => [], | ||
| 74 | |||
| 75 | # other args, with no default values | ||
| 76 | # authority => 'authority' | ||
| 77 | # outcode => ref SCALAR | ||
| 78 | |||
| 79 | # and the real parameters | ||
| 80 | @args ); | ||
| 81 | $self->_check_outcode (%args); | ||
| 82 | |||
| 83 | my $outdir = File::Spec->rel2abs ($args{outdir}); | ||
| 84 | $LOG->debug ("Arguments for generating service bases: " . $self->toString (\%args)) | ||
| 85 | if ($LOG->is_debug); | ||
| 86 | $LOG->info ("Services will be generated into: '$outdir'") | ||
| 87 | unless $args{outcode}; | ||
| 88 | |||
| 89 | # get objects from a local cache | ||
| 90 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, | ||
| 91 | registry => $args{registry}); | ||
| 92 | my @names = (); | ||
| 93 | push (@names, $args{authority}, @{ $args{service_names} }) | ||
| 94 | if $args{authority}; | ||
| 95 | my @services = $cache->get_services (@names); | ||
| 96 | |||
| 97 | # generate from template | ||
| 98 | my $tt = Template->new ( ABSOLUTE => 1 ); | ||
| 99 | my $input = File::Spec->rel2abs ( MOSES::MOBY::Generators::Utils->find_file | ||
| 100 | ($Bin, | ||
| 101 | 'MOSES', 'MOBY', 'Generators', 'templates', | ||
| 102 | 'service-base.tt') ); | ||
| 103 | my $ref_sub_ref = sub { | ||
| 104 | # return ref ($entry) | ||
| 105 | return ref (shift); | ||
| 106 | }; | ||
| 107 | |||
| 108 | foreach my $obj (@services) { | ||
| 109 | my $name = $obj->name; | ||
| 110 | $LOG->debug ("$name\n"); | ||
| 111 | if ($args{outcode}) { | ||
| 112 | # check if the same service is already loaded | ||
| 113 | # (it can happen when this subroutine is called several times) | ||
| 114 | next if eval '%' . $obj->module_name . '::'; | ||
| 115 | $tt->process ( $input, { obj => $obj, | ||
| 116 | ref => $ref_sub_ref, | ||
| 117 | }, | ||
| 118 | $args{outcode} ) || $LOG->logdie ($tt->error()); | ||
| 119 | } else { | ||
| 120 | # we cannot easily check whether the same file was already | ||
| 121 | # generated - so we don't | ||
| 122 | my $outfile = | ||
| 123 | File::Spec->catfile ( $outdir, split (/::/, $obj->module_name) ) | ||
| 124 | . '.pm'; | ||
| 125 | $tt->process ( $input, { obj => $obj, | ||
| 126 | ref => $ref_sub_ref, | ||
| 127 | }, | ||
| 128 | $outfile ) || $LOG->logdie ($tt->error()); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | #----------------------------------------------------------------- | ||
| 134 | # load | ||
| 135 | # load (cachedir => dir, | ||
| 136 | # authority => 'authority', | ||
| 137 | # service_names => [..], ... ) | ||
| 138 | #----------------------------------------------------------------- | ||
| 139 | sub load { | ||
| 140 | my ($self, @args) = @_; | ||
| 141 | |||
| 142 | my $code = ''; | ||
| 143 | $self->generate_base (@args, outcode => \$code); | ||
| 144 | eval $code; | ||
| 145 | $LOG->logdie ("$@") if $@; | ||
| 146 | |||
| 147 | } | ||
| 148 | |||
| 149 | #----------------------------------------------------------------- | ||
| 150 | # generate_impl | ||
| 151 | #----------------------------------------------------------------- | ||
| 152 | sub generate_impl { | ||
| 153 | my ($self, @args) = @_; | ||
| 154 | my %args = | ||
| 155 | ( # some default values | ||
| 156 | impl_outdir => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 157 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ), | ||
| 158 | impl_prefix => $MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX, | ||
| 159 | cachedir => $self->cachedir, | ||
| 160 | registry => $self->registry, | ||
| 161 | service_names => [], | ||
| 162 | force_over => 0, | ||
| 163 | static_impl => 0, | ||
| 164 | |||
| 165 | # other args, with no default values | ||
| 166 | # authority => 'authority' | ||
| 167 | # outcode => ref SCALAR | ||
| 168 | |||
| 169 | # and the real parameters | ||
| 170 | @args ); | ||
| 171 | $self->_check_outcode (%args); | ||
| 172 | |||
| 173 | my $outdir = File::Spec->rel2abs ($args{impl_outdir}); | ||
| 174 | $LOG->debug ("Arguments for generating service implementation: " . $self->toString (\%args)) | ||
| 175 | if ($LOG->is_debug); | ||
| 176 | |||
| 177 | # get objects from a local cache | ||
| 178 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, | ||
| 179 | registry => $args{registry}); | ||
| 180 | my @names = (); | ||
| 181 | push (@names, $args{authority}, @{ $args{service_names} }) | ||
| 182 | if $args{authority}; | ||
| 183 | my @services = $cache->get_services (@names); | ||
| 184 | |||
| 185 | # generate from template | ||
| 186 | my $tt = Template->new ( ABSOLUTE => 1 ); | ||
| 187 | my $input = MOSES::MOBY::Generators::Utils->find_file | ||
| 188 | ($Bin, | ||
| 189 | 'MOSES', 'MOBY', 'Generators', 'templates', | ||
| 190 | 'service.tt'); | ||
| 191 | |||
| 192 | my $ref_sub_get_children = sub { | ||
| 193 | # $entry is a data type name whose all children have to be returned | ||
| 194 | my $entry = shift; | ||
| 195 | [ $cache->get_all_children ($entry) ]; | ||
| 196 | }; | ||
| 197 | |||
| 198 | my $ref_sub_ref = sub { | ||
| 199 | # return ref ($entry) | ||
| 200 | return ref (shift); | ||
| 201 | }; | ||
| 202 | |||
| 203 | foreach my $obj (@services) { | ||
| 204 | my $name = $obj->name; | ||
| 205 | |||
| 206 | # create paths with all children for each input | ||
| 207 | my %input_paths = (); | ||
| 208 | foreach my $input (@{ $obj->inputs }) { | ||
| 209 | my $main_name = $input->name; | ||
| 210 | my $simple = (ref ($input) eq 'MOSES::MOBY::Def::PrimaryDataSimple' ? | ||
| 211 | $input : | ||
| 212 | $input->elements->[0]); | ||
| 213 | my $tree = {}; | ||
| 214 | my $node_id = $self->_add_node ($tree, $main_name, undef, undef); | ||
| 215 | $self->_fill_tree ($cache, $tree, $node_id, $simple->datatype->name); | ||
| 216 | $input_paths{$main_name} = $self->_tree2paths ($tree); | ||
| 217 | } | ||
| 218 | # print MOSES::MOBY::Base->toString (\%input_paths); | ||
| 219 | # create implementation specific object | ||
| 220 | my $impl = { | ||
| 221 | package => ($args{impl_prefix} || 'Service') . '::' . $name, | ||
| 222 | }; | ||
| 223 | my @input_ns = (); | ||
| 224 | foreach my $in (@{ $obj->inputs }) { | ||
| 225 | if (ref ($in) eq 'MOSES::MOBY::Def::PrimaryDataSimple') { | ||
| 226 | foreach my $ns ( @{$in->namespaces} ) { | ||
| 227 | push @input_ns, $ns->name; | ||
| 228 | } | ||
| 229 | } else { | ||
| 230 | foreach my $sim (@{ $in->elements }) { | ||
| 231 | foreach my $ns ( @{$sim->namespaces} ) { | ||
| 232 | push @input_ns, $ns->name; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | if ($args{outcode}) { | ||
| 238 | $tt->process ( $input, { base => $obj, | ||
| 239 | impl => $impl, | ||
| 240 | static_impl => $args{static_impl}, | ||
| 241 | get_children => $ref_sub_get_children, | ||
| 242 | ref => $ref_sub_ref, | ||
| 243 | input_paths => \%input_paths, | ||
| 244 | input_ns =>,\@input_ns, | ||
| 245 | }, | ||
| 246 | $args{outcode} ) || $LOG->logdie ($tt->error()); | ||
| 247 | } else { | ||
| 248 | my $outfile = | ||
| 249 | File::Spec->catfile ( $outdir, split (/::/, $impl->{package}) ) | ||
| 250 | . '.pm'; | ||
| 251 | |||
| 252 | # do not overwrite an existing file (there may be already | ||
| 253 | # a real implementation code) | ||
| 254 | if (-f $outfile and ! $args{force_over}) { | ||
| 255 | $LOG->logwarn ("Implementation '$outfile' already exists. " . | ||
| 256 | "It will *not* be re-generated. Safety reasons.\n"); | ||
| 257 | next; | ||
| 258 | } | ||
| 259 | $tt->process ( $input, { base => $obj, | ||
| 260 | impl => $impl, | ||
| 261 | static_impl => $args{static_impl}, | ||
| 262 | get_children => $ref_sub_get_children, | ||
| 263 | ref => $ref_sub_ref, | ||
| 264 | input_paths => \%input_paths, | ||
| 265 | input_ns => \@input_ns, | ||
| 266 | }, | ||
| 267 | $outfile ) || $LOG->logdie ($tt->error()); | ||
| 268 | $LOG->info ("Created $outfile\n"); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | #----------------------------------------------------------------- | ||
| 274 | # generate_cgi | ||
| 275 | #----------------------------------------------------------------- | ||
| 276 | sub generate_cgi { | ||
| 277 | my ($self, @args) = @_; | ||
| 278 | my %args = | ||
| 279 | ( # some default values | ||
| 280 | outdir => $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 281 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services'), | ||
| 282 | cachedir => $self->cachedir, | ||
| 283 | registry => $self->registry, | ||
| 284 | service_names => [], | ||
| 285 | |||
| 286 | # other args, with no default values | ||
| 287 | # authority => 'authority' | ||
| 288 | # outcode => ref SCALAR | ||
| 289 | |||
| 290 | # and the real parameters | ||
| 291 | @args ); | ||
| 292 | $self->_check_outcode (%args); | ||
| 293 | |||
| 294 | my $outdir = File::Spec->rel2abs ($args{outdir} . "/../cgi" ); | ||
| 295 | $LOG->debug ("Arguments for generating cgi services: " . $self->toString (\%args)) | ||
| 296 | if ($LOG->is_debug); | ||
| 297 | $LOG->info ("CGI Services will be generated into: '$outdir'") | ||
| 298 | unless $args{outcode}; | ||
| 299 | |||
| 300 | # get objects from a local cache | ||
| 301 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, registry => $args{registry}); | ||
| 302 | my @names = (); | ||
| 303 | push (@names, $args{authority}, @{ $args{service_names} }) | ||
| 304 | if $args{authority}; | ||
| 305 | my @services = $cache->get_services (@names); | ||
| 306 | |||
| 307 | # generate from template | ||
| 308 | my $tt = Template->new ( ABSOLUTE => 1 ); | ||
| 309 | my $input = File::Spec->rel2abs ( MOSES::MOBY::Generators::Utils->find_file | ||
| 310 | ($Bin, | ||
| 311 | 'MOSES', 'MOBY', 'Generators', 'templates', | ||
| 312 | 'service-cgi.tt') ); | ||
| 313 | |||
| 314 | foreach my $obj (@services) { | ||
| 315 | my $name = $obj->name; | ||
| 316 | $LOG->debug ("$name\n"); | ||
| 317 | if ($args{outcode}) { | ||
| 318 | # check if the same service is already loaded | ||
| 319 | # (it can happen when this subroutine is called several times) | ||
| 320 | next if eval '%' . $obj->module_name . '::'; | ||
| 321 | $tt->process ( | ||
| 322 | $input, | ||
| 323 | { | ||
| 324 | obj => $obj, | ||
| 325 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 326 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 327 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 328 | }, | ||
| 329 | $args{outcode} ) | ||
| 330 | || $LOG->logdie ($tt->error()); | ||
| 331 | } else { | ||
| 332 | # we cannot easily check whether the same file was already | ||
| 333 | # generated - so we don't | ||
| 334 | my $outfile = | ||
| 335 | File::Spec->catfile ( $outdir, split (/\./, $obj->authority), $obj->name ) . '.cgi'; | ||
| 336 | $tt->process ( $input, | ||
| 337 | { | ||
| 338 | obj => $obj, | ||
| 339 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 340 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 341 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 342 | }, | ||
| 343 | $outfile ) || $LOG->logdie ($tt->error()); | ||
| 344 | chmod (0755, $outfile); | ||
| 345 | $LOG->info ("\tCGI service created at '$outfile'\n"); | ||
| 346 | |||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | #----------------------------------------------------------------- | ||
| 352 | # generate_async_cgi | ||
| 353 | #----------------------------------------------------------------- | ||
| 354 | sub generate_async_cgi { | ||
| 355 | my ($self, @args) = @_; | ||
| 356 | my %args = | ||
| 357 | ( # some default values | ||
| 358 | outdir => $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 359 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services'), | ||
| 360 | cachedir => $self->cachedir, | ||
| 361 | registry => $self->registry, | ||
| 362 | service_names => [], | ||
| 363 | |||
| 364 | # other args, with no default values | ||
| 365 | # authority => 'authority' | ||
| 366 | # outcode => ref SCALAR | ||
| 367 | |||
| 368 | # and the real parameters | ||
| 369 | @args ); | ||
| 370 | $self->_check_outcode (%args); | ||
| 371 | |||
| 372 | my $outdir = File::Spec->rel2abs ($args{outdir} . "/../cgi" ); | ||
| 373 | $LOG->debug ("Arguments for generating async cgi services: " . $self->toString (\%args)) | ||
| 374 | if ($LOG->is_debug); | ||
| 375 | $LOG->info ("Async CGI Services will be generated into: '$outdir'") | ||
| 376 | unless $args{outcode}; | ||
| 377 | |||
| 378 | # get objects from a local cache | ||
| 379 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, registry => $args{registry}); | ||
| 380 | my @names = (); | ||
| 381 | push (@names, $args{authority}, @{ $args{service_names} }) | ||
| 382 | if $args{authority}; | ||
| 383 | my @services = $cache->get_services (@names); | ||
| 384 | |||
| 385 | # generate from template | ||
| 386 | my $tt = Template->new ( ABSOLUTE => 1 ); | ||
| 387 | my $input = File::Spec->rel2abs ( MOSES::MOBY::Generators::Utils->find_file | ||
| 388 | ($Bin, | ||
| 389 | 'MOSES', 'MOBY', 'Generators', 'templates', | ||
| 390 | 'service-cgi-async.tt') ); | ||
| 391 | |||
| 392 | foreach my $obj (@services) { | ||
| 393 | my $name = $obj->name; | ||
| 394 | $LOG->debug ("$name\n"); | ||
| 395 | if ($args{outcode}) { | ||
| 396 | # check if the same service is already loaded | ||
| 397 | # (it can happen when this subroutine is called several times) | ||
| 398 | next if eval '%' . $obj->module_name . '::'; | ||
| 399 | $tt->process ( | ||
| 400 | $input, | ||
| 401 | { | ||
| 402 | obj => $obj, | ||
| 403 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 404 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 405 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 406 | }, | ||
| 407 | $args{outcode} ) | ||
| 408 | || $LOG->logdie ($tt->error()); | ||
| 409 | } else { | ||
| 410 | # we cannot easily check whether the same file was already | ||
| 411 | # generated - so we don't | ||
| 412 | my $outfile = | ||
| 413 | File::Spec->catfile ( $outdir, split (/\./, $obj->authority), $obj->name ) . '.cgi'; | ||
| 414 | $tt->process ( $input, | ||
| 415 | { | ||
| 416 | obj => $obj, | ||
| 417 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 418 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 419 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 420 | }, | ||
| 421 | $outfile ) || $LOG->logdie ($tt->error()); | ||
| 422 | chmod (0755, $outfile); | ||
| 423 | $LOG->info ("\tAsync CGI service created at '$outfile'\n"); | ||
| 424 | |||
| 425 | } | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | |||
| 430 | #----------------------------------------------------------------- | ||
| 431 | # generate_async | ||
| 432 | #----------------------------------------------------------------- | ||
| 433 | sub generate_async { | ||
| 434 | my ($self, @args) = @_; | ||
| 435 | my %args = | ||
| 436 | ( # some default values | ||
| 437 | impl_outdir => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 438 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ), | ||
| 439 | impl_prefix => $MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX, | ||
| 440 | cachedir => $self->cachedir, | ||
| 441 | registry => $self->registry, | ||
| 442 | service_names => [], | ||
| 443 | force_over => 0, | ||
| 444 | static_impl => 0, | ||
| 445 | |||
| 446 | # and the real parameters | ||
| 447 | @args ); | ||
| 448 | $self->_check_outcode (%args); | ||
| 449 | |||
| 450 | my $outdir = File::Spec->rel2abs ($args{impl_outdir}); | ||
| 451 | $LOG->debug ("Arguments for generating async services: " . $self->toString (\%args)) | ||
| 452 | if ($LOG->is_debug); | ||
| 453 | $LOG->info ("ASYNC Services will be generated into: '$outdir'") | ||
| 454 | unless $args{outcode}; | ||
| 455 | |||
| 456 | # get objects from a local cache | ||
| 457 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, registry => $args{registry}); | ||
| 458 | my @names = (); | ||
| 459 | push (@names, $args{authority}, @{ $args{service_names} }) | ||
| 460 | if $args{authority}; | ||
| 461 | my @services = $cache->get_services (@names); | ||
| 462 | |||
| 463 | # generate from template | ||
| 464 | my $tt = Template->new ( ABSOLUTE => 1 ); | ||
| 465 | my $input = MOSES::MOBY::Generators::Utils->find_file | ||
| 466 | ($Bin, | ||
| 467 | 'MOSES', 'MOBY', 'Generators', 'templates', | ||
| 468 | 'service-async.tt'); | ||
| 469 | |||
| 470 | foreach my $obj (@services) { | ||
| 471 | my $name = $obj->name; | ||
| 472 | my $impl = { | ||
| 473 | package => ($args{impl_prefix} || 'Service') . '::' . $name, | ||
| 474 | }; | ||
| 475 | $LOG->debug ("$name\n"); | ||
| 476 | if ($args{outcode}) { | ||
| 477 | # check if the same service is already loaded | ||
| 478 | # (it can happen when this subroutine is called several times) | ||
| 479 | next if eval '%' . $obj->module_name . '::'; | ||
| 480 | $tt->process ( | ||
| 481 | $input, | ||
| 482 | { | ||
| 483 | impl => $impl, | ||
| 484 | obj => $obj, | ||
| 485 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 486 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 487 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 488 | }, | ||
| 489 | $args{outcode} ) | ||
| 490 | || $LOG->logdie ($tt->error()); | ||
| 491 | } else { | ||
| 492 | # we cannot easily check whether the same file was already | ||
| 493 | # generated - so we don't | ||
| 494 | my $outfile = | ||
| 495 | File::Spec->catfile ( $outdir, split (/::/, $impl->{package}) ) | ||
| 496 | . 'Async.pm'; | ||
| 497 | $tt->process ( $input, | ||
| 498 | { | ||
| 499 | impl => $impl, | ||
| 500 | obj => $obj, | ||
| 501 | pmoses_home => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR, | ||
| 502 | generated_dir => $MOBYCFG::GENERATORS_OUTDIR, | ||
| 503 | services_dir => $MOBYCFG::GENERATORS_IMPL_OUTDIR, | ||
| 504 | }, | ||
| 505 | $outfile ) || $LOG->logdie ($tt->error()); | ||
| 506 | chmod (0755, $outfile); | ||
| 507 | $LOG->info ("\tAsync service created at '$outfile'\n"); | ||
| 508 | |||
| 509 | } | ||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | |||
| 514 | #----------------------------------------------------------------- | ||
| 515 | # update_table | ||
| 516 | #----------------------------------------------------------------- | ||
| 517 | sub update_table { | ||
| 518 | my ($self, @args) = @_; | ||
| 519 | my %args = | ||
| 520 | ( # some default values | ||
| 521 | impl_outdir => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 522 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ), | ||
| 523 | services_table => ($MOBYCFG::GENERATORS_IMPL_SERVICES_TABLE || 'SERVICES_TABLE'), | ||
| 524 | impl_prefix => ($MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX || 'Service'), | ||
| 525 | cachedir => $self->cachedir, | ||
| 526 | registry => $self->registry, | ||
| 527 | service_names => [], | ||
| 528 | |||
| 529 | # other args, with no default values | ||
| 530 | # authority => 'authority' | ||
| 531 | # outcode => ref SCALAR | ||
| 532 | |||
| 533 | # and the real parameters | ||
| 534 | @args ); | ||
| 535 | $self->_check_outcode (%args); | ||
| 536 | |||
| 537 | my $outdir = File::Spec->rel2abs ($args{impl_outdir}); | ||
| 538 | $LOG->debug ("Arguments for generating services table: " . $self->toString (\%args)) | ||
| 539 | if ($LOG->is_debug); | ||
| 540 | |||
| 541 | # read the current service table | ||
| 542 | unshift (@INC, $args{impl_outdir}); # place where SERVICES_TABLE could be | ||
| 543 | use vars qw ( $DISPATCH_TABLE ); | ||
| 544 | eval { require $args{services_table} }; | ||
| 545 | my $file_with_table; | ||
| 546 | if ($@) { | ||
| 547 | $LOG->warn ("Cannot find table of services '" . $args{services_table} . "': $@"); | ||
| 548 | $file_with_table = File::Spec->catfile ($args{impl_outdir}, $args{services_table}); | ||
| 549 | } else { | ||
| 550 | $file_with_table = $INC{ $args{services_table} }; | ||
| 551 | } | ||
| 552 | |||
| 553 | # get names of services that should be added to the service table: | ||
| 554 | my @names = (); | ||
| 555 | if ($args{service_names} and @{ $args{service_names} } > 0) { | ||
| 556 | # 1) if there are service_names given, take them, and that's it | ||
| 557 | # (TBD?: should I checked names against the cache?) | ||
| 558 | @names = @{ $args{service_names} }; | ||
| 559 | } else { | ||
| 560 | # 2) otherwise we need to get names from the cache | ||
| 561 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, | ||
| 562 | registry => $args{registry}); | ||
| 563 | my %by_authorities = $cache->get_service_names; | ||
| 564 | if ($args{authority}) { | ||
| 565 | my $authority = $by_authorities{ $args{authority} }; | ||
| 566 | $self->throw ("Unknown authority '$args{authority}'.") | ||
| 567 | unless $authority; | ||
| 568 | @names = @{ $authority }; | ||
| 569 | } else { | ||
| 570 | foreach my $authority (keys %by_authorities) { | ||
| 571 | push (@names, @{ $by_authorities{$authority} }); | ||
| 572 | } | ||
| 573 | } | ||
| 574 | } | ||
| 575 | |||
| 576 | # update dispatch table | ||
| 577 | foreach my $service_name (@names) { | ||
| 578 | $DISPATCH_TABLE->{"http://biomoby.org/#$service_name"} = | ||
| 579 | $args{impl_prefix} . '::' . $service_name; | ||
| 580 | } | ||
| 581 | # ...and write it back to a disk | ||
| 582 | require Data::Dumper; | ||
| 583 | open DISPATCH, ">$file_with_table" | ||
| 584 | or $self->throw ("Cannot open for writing '$file_with_table': $!\n"); | ||
| 585 | print DISPATCH Data::Dumper->Dump ( [$DISPATCH_TABLE], ['DISPATCH_TABLE'] ) | ||
| 586 | or $self->throw ("cannot write to '$file_with_table': $!\n"); | ||
| 587 | close DISPATCH; | ||
| 588 | $LOG->info ("Updated services table '$file_with_table'. New contents: " . | ||
| 589 | $self->toString ($DISPATCH_TABLE)); | ||
| 590 | } | ||
| 591 | |||
| 592 | #----------------------------------------------------------------- | ||
| 593 | # update_async_table | ||
| 594 | #----------------------------------------------------------------- | ||
| 595 | sub update_async_table { | ||
| 596 | my ($self, @args) = @_; | ||
| 597 | my %args = | ||
| 598 | ( # some default values | ||
| 599 | impl_outdir => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR || | ||
| 600 | MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ), | ||
| 601 | services_table => ($MOBYCFG::GENERATORS_IMPL_ASYNC_SERVICES_TABLE || 'ASYNC_SERVICES_TABLE'), | ||
| 602 | impl_prefix => ($MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX || 'Service'), | ||
| 603 | cachedir => $self->cachedir, | ||
| 604 | registry => $self->registry, | ||
| 605 | service_names => [], | ||
| 606 | |||
| 607 | # other args, with no default values | ||
| 608 | # authority => 'authority' | ||
| 609 | # outcode => ref SCALAR | ||
| 610 | |||
| 611 | # and the real parameters | ||
| 612 | @args ); | ||
| 613 | $self->_check_outcode (%args); | ||
| 614 | |||
| 615 | my $outdir = File::Spec->rel2abs ($args{impl_outdir}); | ||
| 616 | $LOG->debug ("Arguments for generating async services table: " . $self->toString (\%args)) | ||
| 617 | if ($LOG->is_debug); | ||
| 618 | |||
| 619 | # read the current service table | ||
| 620 | unshift (@INC, $args{impl_outdir}); # place where ASYNC_SERVICES_TABLE could be | ||
| 621 | use vars qw ( $DISPATCH_TABLE ); | ||
| 622 | eval { require $args{services_table} }; | ||
| 623 | my $file_with_table; | ||
| 624 | if ($@) { | ||
| 625 | $LOG->warn ("Cannot find table of async services '" . $args{services_table} . "': $@"); | ||
| 626 | $file_with_table = File::Spec->catfile ($args{impl_outdir}, $args{services_table}); | ||
| 627 | } else { | ||
| 628 | $file_with_table = $INC{ $args{services_table} }; | ||
| 629 | } | ||
| 630 | |||
| 631 | # get names of services that should be added to the service table: | ||
| 632 | my @names = (); | ||
| 633 | if ($args{service_names} and @{ $args{service_names} } > 0) { | ||
| 634 | # 1) if there are service_names given, take them, and that's it | ||
| 635 | # (TBD?: should I checked names against the cache?) | ||
| 636 | @names = @{ $args{service_names} }; | ||
| 637 | } else { | ||
| 638 | # 2) otherwise we need to get names from the cache | ||
| 639 | my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, | ||
| 640 | registry => $args{registry}); | ||
| 641 | my %by_authorities = $cache->get_service_names; | ||
| 642 | if ($args{authority}) { | ||
| 643 | my $authority = $by_authorities{ $args{authority} }; | ||
| 644 | $self->throw ("Unknown authority '$args{authority}'.") | ||
| 645 | unless $authority; | ||
| 646 | @names = @{ $authority }; | ||
| 647 | } else { | ||
| 648 | foreach my $authority (keys %by_authorities) { | ||
| 649 | push (@names, @{ $by_authorities{$authority} }); | ||
| 650 | } | ||
| 651 | } | ||
| 652 | } | ||
| 653 | # dont want the redefined errors in Async to discourage service developer | ||
| 654 | no warnings qw(redefine); | ||
| 655 | require MOBY::Async::WSRF; | ||
| 656 | |||
| 657 | # update dispatch table | ||
| 658 | foreach my $service_name (@names) { | ||
| 659 | $DISPATCH_TABLE->{$WSRF::Constants::MOBY."#$service_name"} | ||
| 660 | = $args{impl_prefix} . '::' . $service_name . 'Async'; | ||
| 661 | $DISPATCH_TABLE->{$WSRF::Constants::MOBY."#$service_name".'_submit'} | ||
| 662 | = $args{impl_prefix} . '::' . $service_name . 'Async'; | ||
| 663 | $DISPATCH_TABLE->{$WSRF::Constants::WSRPW .'/GetResourceProperty/GetResourcePropertyRequest'} | ||
| 664 | = "MOBY::Async::SimpleServer"; | ||
| 665 | $DISPATCH_TABLE->{$WSRF::Constants::WSRPW.'/GetMultipleResourceProperties/GetMultipleResourcePropertiesRequest'} | ||
| 666 | = "MOBY::Async::SimpleServer"; | ||
| 667 | $DISPATCH_TABLE->{$WSRF::Constants::WSRLW.'/ImmediateResourceTermination/DestroyRequest'} | ||
| 668 | = "MOBY::Async::SimpleServer"; | ||
| 669 | |||
| 670 | # $DISPATCH_TABLE->{"http://biomoby.org/#$service_name"} = | ||
| 671 | # $args{impl_prefix} . '::' . $service_name; | ||
| 672 | } | ||
| 673 | # ...and write it back to a disk | ||
| 674 | require Data::Dumper; | ||
| 675 | open DISPATCH, ">$file_with_table" | ||
| 676 | or $self->throw ("Cannot open for writing '$file_with_table': $!\n"); | ||
| 677 | print DISPATCH Data::Dumper->Dump ( [$DISPATCH_TABLE], ['DISPATCH_TABLE'] ) | ||
| 678 | or $self->throw ("cannot write to '$file_with_table': $!\n"); | ||
| 679 | close DISPATCH; | ||
| 680 | $LOG->info ("Updated async services table '$file_with_table'. New contents: " . | ||
| 681 | $self->toString ($DISPATCH_TABLE)); | ||
| 682 | } | ||
| 683 | |||
| 684 | |||
| 685 | #----------------------------------------------------------------- | ||
| 686 | # _check_outcode | ||
| 687 | # throws an exception if %args has an 'outcode' of a wrong type | ||
| 688 | #----------------------------------------------------------------- | ||
| 689 | sub _check_outcode { | ||
| 690 | my ($self, %args) = @_; | ||
| 691 | $self->throw ("Parameter 'outcode' should be a reference to a SCALAR.") | ||
| 692 | if $args{outcode} and ref ($args{outcode}) ne 'SCALAR'; | ||
| 693 | } | ||
| 694 | |||
| 695 | |||
| 696 | #----------------------------------------------------------------- | ||
| 697 | # Support for creating code showing how to read inputs. | ||
| 698 | # | ||
| 699 | # For each input, a tree of all children is created first (_fill_tree, | ||
| 700 | # recursively). Each branch of the tree represents a path from this | ||
| 701 | # input's name to a real value (or to an Id, if the last child is an | ||
| 702 | # Object and not a primitive type). Nodes are created by _add_node, | ||
| 703 | # each node has a pointer to its parent, an indication if it is | ||
| 704 | # already a leaf node, and a value. The value is either a data type | ||
| 705 | # name, or - for HAS members - another tree (with the same structure | ||
| 706 | # as the main tree). The leaf nodes have (in 'is_leaf') name of the | ||
| 707 | # last child (the name will be used as my $xxxx, but may be suffixed | ||
| 708 | # by a number if not unique - _select_name). | ||
| 709 | # | ||
| 710 | # Next, the tree is converted to pathes - each path for one child | ||
| 711 | # (_tree2paths). The resulting structure is understood by the template | ||
| 712 | # service.tt. An example of such structure is shown there. | ||
| 713 | # | ||
| 714 | #----------------------------------------------------------------- | ||
| 715 | |||
| 716 | { | ||
| 717 | my $node_count = 0; | ||
| 718 | sub _node_id { | ||
| 719 | return ++$node_count; | ||
| 720 | } | ||
| 721 | } | ||
| 722 | |||
| 723 | sub _select_name { | ||
| 724 | my ($member_name, $members) = @_; | ||
| 725 | return $member_name unless exists $members->{$member_name}; | ||
| 726 | foreach my $i (2..1000) { | ||
| 727 | my $numbered_name = $member_name . "_$i"; | ||
| 728 | return $numbered_name unless exists $members->{$numbered_name}; | ||
| 729 | } | ||
| 730 | return $member_name . "_$$"; # :-) | ||
| 731 | } | ||
| 732 | |||
| 733 | sub _add_node { | ||
| 734 | my ($self, $tree, $node_value, $node_parent, $leaf_name) = @_; | ||
| 735 | my $node_id = &_node_id; | ||
| 736 | $tree->{$node_id} = { value => $node_value, | ||
| 737 | parent => $node_parent, | ||
| 738 | is_leaf => $leaf_name, | ||
| 739 | }; | ||
| 740 | return $node_id; | ||
| 741 | } | ||
| 742 | |||
| 743 | sub is_primitive_type { | ||
| 744 | my ($self, $datatype_name) = @_; | ||
| 745 | return exists $MOSES::MOBY::Generators::GenTypes::PRIMITIVE_TYPES{$datatype_name}; | ||
| 746 | } | ||
| 747 | |||
| 748 | sub _fill_tree { | ||
| 749 | my ($self, $cache, $tree, $parent_id, $datatype_name) = @_; | ||
| 750 | my @children = $cache->get_all_children ($datatype_name); | ||
| 751 | if (@children == 0) { | ||
| 752 | $self->_add_node ($tree, 'id', $parent_id, 'id'); | ||
| 753 | $self->_add_node ($tree, 'namespace', $parent_id, 'namespace'); | ||
| 754 | return; | ||
| 755 | } | ||
| 756 | foreach my $child (@children) { | ||
| 757 | # $child is of type MOSES::MOBY::Def::Relationship | ||
| 758 | my $node_id = $self->_add_node ($tree, $child->memberName, $parent_id, undef); | ||
| 759 | if ($child->relationship eq HAS) { | ||
| 760 | my $subtree = {}; | ||
| 761 | my $subtree_node_id = $self->_add_node ($subtree, $child->memberName . '_element', undef, undef); | ||
| 762 | if ($self->is_primitive_type ($child->datatype)) { | ||
| 763 | $self->_add_node ($subtree, 'value', $subtree_node_id, $child->memberName); | ||
| 764 | } else { | ||
| 765 | $self->_fill_tree ($cache, $subtree, $subtree_node_id, $child->datatype); | ||
| 766 | } | ||
| 767 | $self->_add_node ($tree, $subtree, $node_id, $child->memberName); | ||
| 768 | } else { | ||
| 769 | if ($self->is_primitive_type ($child->datatype)) { | ||
| 770 | $self->_add_node ($tree, 'value', $node_id, $child->memberName); | ||
| 771 | } else { | ||
| 772 | $self->_fill_tree ($cache, $tree, $node_id, $child->datatype); | ||
| 773 | } | ||
| 774 | } | ||
| 775 | } | ||
| 776 | |||
| 777 | # my $subtree = {}; | ||
| 778 | # my $subtree_node_id = $self->_add_node ($subtree, $child->memberName . '_element', undef, undef); | ||
| 779 | # $self->_add_node ($subtree, 'data', $subtree_node_id, $child->memberName); | ||
| 780 | } | ||
| 781 | |||
| 782 | sub _tree2paths { | ||
| 783 | my ($self, $tree) = @_; | ||
| 784 | my %members = (); | ||
| 785 | my ($id, $node); | ||
| 786 | while (($id, $node) = each %$tree) { | ||
| 787 | next unless $node->{is_leaf}; | ||
| 788 | my $member_name = _select_name ($node->{is_leaf}, \%members); | ||
| 789 | my $node_value = $node->{value}; | ||
| 790 | my @path; | ||
| 791 | if (ref ($node_value) eq 'HASH') { | ||
| 792 | @path = ($self->_tree2paths ($node_value)); | ||
| 793 | } else { | ||
| 794 | @path = ($node_value); | ||
| 795 | } | ||
| 796 | while ($node->{parent}) { | ||
| 797 | $node = $tree->{ $node->{parent} }; | ||
| 798 | unshift (@path, $node->{value}); | ||
| 799 | } | ||
| 800 | $members {$member_name} = \@path; | ||
| 801 | } | ||
| 802 | return \%members; | ||
| 803 | } | ||
| 804 | |||
| 805 | |||
| 806 | 1; | ||
| 807 | __END__ |