#=============================================================================
# Doloforge::Service::Tomcat : configure Tomcat for any client/project
#
# Generate config files from templates, add a config file to the whole,
# reload Tomcat (well, not yet)
#
# Full help :
#   perldoc Doloforge::Service::Tomcat
#=============================================================================


package Doloforge::Service::Tomcat;

use strict;

# This base class implements the constructor and help methods in a generic way.
use base qw(Doloforge::Service);


#=============================================================================
=encoding utf8

=head1 NAME

Doloforge::Service::Tomcat : Configure Tomcat for any client/project

=head1 SYNOPSIS

    # Update the full configuration, running each client/project/service update
    doloforge config --update

=head1 DESCRIPTION

Todo

=cut


#=============================================================================
# Method update
#
# Update the service configuration on the host, depending on
# client/project/service config files...
# This method receives :
#   option: context options passesd on the command-line (quite useless there)
#   client_conf: client configuration file contents (client.$name.yaml)
#   project_conf: project configuration file contents (project.$name.client.$name.yaml)

sub update {
    my $self = shift;

    my ($option, $client_conf, $project_conf) = @_;

    # Copy the service skeleton directory into the project directory.
    $self->SUPER::update($option, $client_conf, $project_conf);

    # The folowing merged templates are all stored on shared storage, so only generated by the NFS servers
    if ($self->has_role('nfs_role')) {
        # Merge the config hashes with the proper template files, and save the result...
        $self->merge_template($option, $client_conf, $project_conf, # Configuration hashes
            # Service-specific template, from [skel]/template/tomcat_*
            "$self->{global}->{dir}->{skel}/template/$self->{name}_server.xml.tmpl",
            # Alternate project-service-specific template, to be used if it exists
            "$self->{global}->{dir}->{shared}/$client_conf->{name}/$project_conf->{name}/conf/$self->{name}_server.xml.tmpl",
            # Filled-in file, in [client]/[project]/_conf/ (it's name must be unique among all services)
            "$self->{global}->{dir}->{shared}/$client_conf->{name}/$project_conf->{name}/_conf/$self->{name}_server.xml",
            # Link to the filled-in file, which must be maintained once the project is active
            "$self->{global}->{dir}->{shared}/.doloforge/$self->{name}/conf.d/$client_conf->{name}_$project_conf->{name}_server.xml",
        );
    }
}


#=============================================================================
# Method finish
#
# Called when the whole processing is finished, when it's time to reload services.
# This generic class will do nothing special...

sub finish {
    my $self = shift;

    # If any modification was made in the configuration for this service, reload everything that depends upon it.
    # It may be too much work, if only a tiny part was modified... We'll optimize later on...
    # This will only be done on machines that have a role for this class, ie. machines where
    # the update() method have updated something...
    if ($self->{modified}) {
        # Read all /etc/tomcat5.5/server.xml parts from every project, and merge them in the system file
        # This is done using the same template system as for each project part...
        $self->merge_template(undef, undef, undef, # Configuration hashes: not available at the system level
            # Service-specific template, from [conf]/tomcat_*.tmpl
            "$self->{global}->{dir}->{conf}/$self->{name}_server.xml.tmpl",
            # Alternate project-service-specific template, to be used if it exists
            undef,
            # Filled-in file, in /etc/tomcat55/... or /[client]/[project]/_conf/ (it's name must be unique among all services)
            "$self->{global}->{dir}->{shared}/.doloforge/$self->{name}/conf.d/server.xml",
            # No linked file on the storage server, where we generate the configuration...
            undef,
        );

        # FIXME : actually reload daemon on hosts running them
        # BEWARE : daemons are reloaded on application servers, while configuration have been regenerated on nfs-server
        #   - we may not know it's been regenerated
        #   - we may wish to reload before the files are actually completed
        $self->warn("Modified $self->{modified} files: need to reload something for service '$self->{name}' ? (reload: $self->{config}->{reload})");
    }

    # Maintain a link from the /etc Tomcat configuration, to the Doloforge-generated file.
    # The link is maintained on the application servers, whereas the content itself is generated on the storage server...
    # The order of the parameters is the same as the "ln" command.
    if ($self->has_role('appserv_role')) {
        # Global link to the filled-in file: /etc/tomcat5.5/server.xml -> /srv/doloforge/shared/.doloforge/tomcat/conf.d/server.xml
        $self->link_file("$self->{global}->{dir}->{shared}/.doloforge/$self->{name}/conf.d/server.xml", $self->{config}->{serverxml})
            if ($self->{config}->{serverxml} and not -l $self->{config}->{serverxml});
    }
}



1;
__END__

=head1 AUTHOR

Nicolas Huillard <F<nicolas@huillard.net>>, Dolomède

=head1 SEE ALSO

doloforge(1)

=cut
