MOSES-MOBY

 view release on metacpan or  search on metacpan

lib/MOSES/MOBY/Generators/GenServices.pm  view on Meta::CPAN

    my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir}, registry => $args{registry});
    my @names = ();
    push (@names, $args{authority}, @{ $args{service_names} })
	if $args{authority};
    my @services = $cache->get_services (@names);

    # generate from template
    my $tt = Template->new ( ABSOLUTE => 1 );
    my $input = MOSES::MOBY::Generators::Utils->find_file
	($Bin,
	 'MOSES', 'MOBY', 'Generators', 'templates',
	 'service-async.tt');

    foreach my $obj (@services) {
	my $name = $obj->name;
	my $impl = {
	    package => ($args{impl_prefix} || 'Service') . '::' . $name,
	};
	$LOG->debug ("$name\n");
	if ($args{outcode}) {
	    # check if the same service is already loaded
	    # (it can happen when this subroutine is called several times)
	    next if eval '%' . $obj->module_name . '::';
	    $tt->process ( 
	    	$input, 
	    	{ 
	    		impl   => $impl,
	    		obj 		  => $obj, 
	    	  	pmoses_home   => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR,
	    	  	generated_dir => $MOBYCFG::GENERATORS_OUTDIR,
	    	  	services_dir  => $MOBYCFG::GENERATORS_IMPL_OUTDIR,
	    	},
			$args{outcode} )
		 || $LOG->logdie ($tt->error());
	} else {
	    # we cannot easily check whether the same file was already
	    # generated - so we don't
	    my $outfile =
		File::Spec->catfile ( $outdir, split (/::/, $impl->{package}) )
		. 'Async.pm';
	    $tt->process ( $input, 
	    	{
	    		impl		  => $impl, 
	    		obj 		  => $obj, 
	    	  	pmoses_home   => $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR,
	    	  	generated_dir => $MOBYCFG::GENERATORS_OUTDIR,
	    	  	services_dir  => $MOBYCFG::GENERATORS_IMPL_OUTDIR,
	    	},
			   $outfile ) || $LOG->logdie ($tt->error());
		chmod (0755, $outfile);
		$LOG->info ("\tAsync service created at '$outfile'\n");
		 
	}
    }
}


#-----------------------------------------------------------------
# update_table
#-----------------------------------------------------------------
sub update_table {
    my ($self, @args) = @_;
    my %args =
	( # some default values
	  impl_outdir    => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR ||
			      MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ),
	  services_table => ($MOBYCFG::GENERATORS_IMPL_SERVICES_TABLE || 'SERVICES_TABLE'),
	  impl_prefix    => ($MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX || 'Service'),
	  cachedir       => $self->cachedir,
	  registry       => $self->registry,
	  service_names  => [],

	  # other args, with no default values
	  # authority     => 'authority'
	  # outcode       => ref SCALAR

	  # and the real parameters
	  @args );
    $self->_check_outcode (%args);

    my $outdir = File::Spec->rel2abs ($args{impl_outdir});
    $LOG->debug ("Arguments for generating services table: " . $self->toString (\%args))
	if ($LOG->is_debug);

    # read the current service table
    unshift (@INC, $args{impl_outdir});   # place where SERVICES_TABLE could be
    use vars qw ( $DISPATCH_TABLE );
    eval { require $args{services_table} };
    my $file_with_table;
    if ($@) {
	$LOG->warn ("Cannot find table of services '" . $args{services_table} . "': $@");
	$file_with_table = File::Spec->catfile ($args{impl_outdir}, $args{services_table});
    } else {
	$file_with_table = $INC{ $args{services_table} };
    }

    # get names of services that should be added to the service table:
    my @names = ();
    if ($args{service_names} and @{ $args{service_names} } > 0) {
	# 1) if there are service_names given, take them, and that's it
	#    (TBD?: should I checked names against the cache?)
	@names = @{ $args{service_names} };
    } else {
	# 2) otherwise we need to get names from the cache
	my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir},
					       registry => $args{registry});
	my %by_authorities = $cache->get_service_names;
	if ($args{authority}) {
	    my $authority = $by_authorities{ $args{authority} };
	    $self->throw ("Unknown authority '$args{authority}'.")
		unless $authority;
	    @names = @{ $authority };
	} else {
	    foreach my $authority (keys %by_authorities) {
		push (@names, @{ $by_authorities{$authority} });
	    }
	}
    }

    # update dispatch table
    foreach my $service_name (@names) {
	$DISPATCH_TABLE->{"http://biomoby.org/#$service_name"} =
	    $args{impl_prefix} . '::' . $service_name;
    }
    # ...and write it back to a disk
    require Data::Dumper;
    open DISPATCH, ">$file_with_table"
	or $self->throw ("Cannot open for writing '$file_with_table': $!\n");
    print DISPATCH Data::Dumper->Dump ( [$DISPATCH_TABLE], ['DISPATCH_TABLE'] )
	or $self->throw ("cannot write to '$file_with_table': $!\n");
    close DISPATCH;
    $LOG->info ("Updated services table '$file_with_table'. New contents: " .
		$self->toString ($DISPATCH_TABLE));
}

#-----------------------------------------------------------------
# update_async_table
#-----------------------------------------------------------------
sub update_async_table {
    my ($self, @args) = @_;
    my %args =
	( # some default values
	  impl_outdir    => ( $MOBYCFG::GENERATORS_IMPL_OUTDIR ||
			      MOSES::MOBY::Generators::Utils->find_file ($Bin, 'services') ),
	  services_table => ($MOBYCFG::GENERATORS_IMPL_ASYNC_SERVICES_TABLE || 'ASYNC_SERVICES_TABLE'),
	  impl_prefix    => ($MOBYCFG::GENERATORS_IMPL_PACKAGE_PREFIX || 'Service'),
	  cachedir       => $self->cachedir,
	  registry       => $self->registry,
	  service_names  => [],

	  # other args, with no default values
	  # authority     => 'authority'
	  # outcode       => ref SCALAR

	  # and the real parameters
	  @args );
    $self->_check_outcode (%args);

    my $outdir = File::Spec->rel2abs ($args{impl_outdir});
    $LOG->debug ("Arguments for generating async services table: " . $self->toString (\%args))
	if ($LOG->is_debug);

    # read the current service table
    unshift (@INC, $args{impl_outdir});   # place where ASYNC_SERVICES_TABLE could be
    use vars qw ( $DISPATCH_TABLE );
    eval { require $args{services_table} };
    my $file_with_table;
    if ($@) {
	$LOG->warn ("Cannot find table of async services '" . $args{services_table} . "': $@");
	$file_with_table = File::Spec->catfile ($args{impl_outdir}, $args{services_table});
    } else {
	$file_with_table = $INC{ $args{services_table} };
    }

    # get names of services that should be added to the service table:
    my @names = ();
    if ($args{service_names} and @{ $args{service_names} } > 0) {
	# 1) if there are service_names given, take them, and that's it
	#    (TBD?: should I checked names against the cache?)
	@names = @{ $args{service_names} };
    } else {
	# 2) otherwise we need to get names from the cache
	my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir},
					       registry => $args{registry});
	my %by_authorities = $cache->get_service_names;
	if ($args{authority}) {
	    my $authority = $by_authorities{ $args{authority} };
	    $self->throw ("Unknown authority '$args{authority}'.")
		unless $authority;
	    @names = @{ $authority };
	} else {
	    foreach my $authority (keys %by_authorities) {
		push (@names, @{ $by_authorities{$authority} });
	    }
	}
    }
    # dont want the redefined errors in Async to discourage service developer
    no warnings qw(redefine);
	require MOBY::Async::WSRF;



( run in 2.873 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )