<commit_before>## OpenXPKI::Server.pm 
##
## Written 2005 by <NAME> for the OpenXPKI project
## (C) Copyright 2005-2006 by The OpenXPKI Project
## $Revision$

use strict;
use warnings;
use utf8;

package OpenXPKI::Server;
use base qw(Net::Server::Fork);

## used modules

use English;
use Socket;
use OpenXPKI::Debug 'OpenXPKI::Server';
use OpenXPKI::Exception;
use OpenXPKI::Server::Context qw( CTX );
use OpenXPKI::Server::Init;

sub new
{
    my $that = shift;
    my $class = ref($that) || $that;

    my $self = {};

    bless $self, $class;

    my $keys = { @_ };

    ## get parameters

    $self->{"CONFIG"} = $keys->{CONFIG};

    ## dump out startup configuration

    foreach my $key (keys %{$keys})
    {
        if ($key ne "CONFIG")
        {
            ##! 8: "IGNORED:  $key ::= $keys->{$key}"
        } else {
            ##! 8: "ACCEPTED: $key ::= $keys->{$key}"
        }
    }

    # we need to get a usable logger as soon as possible, hence:
    # initialize configuration, i18n and log
    OpenXPKI::Server::Init::init(
	{
	    CONFIG => $self->{CONFIG},
	    TASKS  => [ 'xml_config', 'i18n', 'log' ],
	});

    # from now on we can assume that we have CTX('log') available

    # perform the rest of the initialization
    eval
    {
	OpenXPKI::Server::Init::init(
	    {
		CONFIG => $self->{CONFIG},
		# FIXME: not needed?
#		SERVER => $self,
	    });
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during server initialization: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
	exit 1;
    }

    ## group access is allowed
    $self->{umask} = umask 0007;

    ## load the user interfaces
    eval
    {
	$self->__get_user_interfaces();
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during interface initialization: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
	exit 1;
    }

    ## start the server
    my %params;
    eval
    {
	%params = $self->__get_server_config();
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during server daemon setup: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
	exit 1;
    }

    unlink ($params{port});
    CTX('log')->log(
	MESSAGE  => "Server initialization completed",
	PRIORITY => "info",
	FACILITY => "system",
	);
    
    # clean up process list
    $0 = "openxpkid -c $self->{CONFIG}";
    
    $self->run (%params);
}


# called statically
sub exception_as_string {
    my $exc = shift;

    my $msg = "";
    ##! 8: ref $exc
    if (ref $exc eq '') {
	$msg = $exc;
    } elsif (ref $exc eq 'OpenXPKI::Exception') {
	$msg = $exc->full_message() || '<no message>';
	if ($exc->child()) {
	    $msg .= '; CHILD: [' . exception_as_string($exc->child()) . ']'
	}
    } elsif (ref $exc) {
	$msg = $EVAL_ERROR->message();
    } else {
	$msg = "<not an exception>";
    }

    return $msg;
}


sub process_request {
    my $rc;
    eval
    {
        $rc = do_process_request(@_);
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Uncaught exception: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
	exit 1;
    }

    return $rc;
}


sub do_process_request
{
    ##! 2: "start"
    my $self = shift;

    my $log = CTX('log');

    ## recover from umask of Net::Server->run
    umask $self->{umask};

    # masquerade process...<commit_msg>replaced exit with die to enable eval<commit_after>## OpenXPKI::Server.pm 
##
## Written 2005 by <NAME> for the OpenXPKI project
## (C) Copyright 2005-2006 by The OpenXPKI Project
## $Revision$

use strict;
use warnings;
use utf8;

package OpenXPKI::Server;
use base qw(Net::Server::Fork);

## used modules

use English;
use Socket;
use OpenXPKI::Debug 'OpenXPKI::Server';
use OpenXPKI::Exception;
use OpenXPKI::Server::Context qw( CTX );
use OpenXPKI::Server::Init;

sub new
{
    my $that = shift;
    my $class = ref($that) || $that;

    my $self = {};

    bless $self, $class;

    my $keys = { @_ };

    ## get parameters

    $self->{"CONFIG"} = $keys->{CONFIG};

    ## dump out startup configuration

    foreach my $key (keys %{$keys})
    {
        if ($key ne "CONFIG")
        {
            ##! 8: "IGNORED:  $key ::= $keys->{$key}"
        } else {
            ##! 8: "ACCEPTED: $key ::= $keys->{$key}"
        }
    }

    # we need to get a usable logger as soon as possible, hence:
    # initialize configuration, i18n and log
    OpenXPKI::Server::Init::init(
	{
	    CONFIG => $self->{CONFIG},
	    TASKS  => [ 'xml_config', 'i18n', 'log' ],
	});

    # from now on we can assume that we have CTX('log') available

    # perform the rest of the initialization
    eval
    {
	OpenXPKI::Server::Init::init(
	    {
		CONFIG => $self->{CONFIG},
		# FIXME: not needed?
#		SERVER => $self,
	    });
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during server initialization: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
 	$ERRNO = 1;
        die $msg;
    }

    ## group access is allowed
    $self->{umask} = umask 0007;

    ## load the user interfaces
    eval
    {
	$self->__get_user_interfaces();
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during interface initialization: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
 	$ERRNO = 1;
        die $msg;
    }

    ## start the server
    my %params;
    eval
    {
	%params = $self->__get_server_config();
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Exception during server daemon setup: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
 	$ERRNO = 1;
        die $msg;
    }

    unlink ($params{port});
    CTX('log')->log(
	MESSAGE  => "Server initialization completed",
	PRIORITY => "info",
	FACILITY => "system",
	);
    
    # clean up process list
    $0 = "openxpkid -c $self->{CONFIG}";
    
    $self->run (%params);
}


# called statically
sub exception_as_string {
    my $exc = shift;

    my $msg = "";
    ##! 8: ref $exc
    if (ref $exc eq '') {
	$msg = $exc;
    } elsif (ref $exc eq 'OpenXPKI::Exception') {
	$msg = $exc->full_message() || '<no message>';
	if ($exc->child()) {
	    $msg .= '; CHILD: [' . exception_as_string($exc->child()) . ']'
	}
    } elsif (ref $exc) {
	$msg = $EVAL_ERROR->message();
    } else {
	$msg = "<not an exception>";
    }

    return $msg;
}


sub process_request {
    my $rc;
    eval
    {
        $rc = do_process_request(@_);
    };
    if ($EVAL_ERROR) {
 	my $msg = exception_as_string($EVAL_ERROR);
 	CTX('log')->log(
 	    MESSAGE  => "Uncaught exception: " . $msg,
 	    PRIORITY => "fatal",
 	    FACILITY => "system",
 	    );
 	# die gracefully
 	$ERRNO = 1;
        die $msg;
    }

    return $rc;
}


sub do_process_request
{
    ##! 2: "start"
    my $self = shift;

    my $log = CTX('log');

    ## recover from umask of Net::Server->run
    umask $self->{umask};

    # masquerade process...
{'fraction_non_alphanumeric': 0.1701388888888889, 'fraction_numerical': 0.008012820512820512, 'mean_word_length': 2.501168770453483, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2267472', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2675, 'n_words': 675}