<commit_before># OpenXPKI::Server::Context (Singleton)
# Written by <NAME> for the OpenXPKI project 2005
# Copyright (c) 2005-2006 by The OpenXPKI Project

package OpenXPKI::Server::Context;

use strict;
use base qw( Exporter );

use Storable qw(dclone);

use OpenXPKI::Debug;
use Data::Dumper;

our @EXPORT_OK = qw( CTX );

#use Smart::Comments;

use OpenXPKI::Exception;

my $context = {
    initialized => 0,

    exported => {
    	# always created by this package
        config           => undef,
        watchdog         => undef,
        workflow_factory => undef,
    	crypto_layer     => undef,
    	pki_realm        => undef,
        pki_realm_by_cfg => undef,
    	volatile_vault   => undef,
    	log              => undef,
    	dbi              => undef,
    	dbi_log          => undef,

    	# user-settable
    	api            => undef,
    	server         => undef,
        acl            => undef,
        session        => undef,
        authentication => undef,
        notification   => undef,
        workflow_id    => undef,
    },
};

our %who_forked_me;

# only called statically
sub CTX {
    my @objects = @_;

    # not needed as we would get an exception if the requested object is not
    # defined anyway. Removed as it affects the new auto mock objects for testing
    if (0) {
    if (! $context->{initialized}) {
        OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_NOT_INITIALIZED",
            params => { 'objects' => join ":", @objects },
	        log => undef, # do not log exception
	    );
    }}

    # TODO: add access control? (idea: limit access to this method to
    # authorized parts of the code only, explicity excluding interface
    # implementations...)

    my @return;
    foreach my $object (@objects) {

    	if (! exists $context->{exported}->{$object}) {
    	    OpenXPKI::Exception->throw (
    		message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_OBJECT_NOT_FOUND",
                    params  => {OBJECT => $object},
    		log => undef, # do not log exception message
    		);
    	}
    	if (! defined $context->{exported}->{$object}) {

    	    # FIXME - should add some code to check if this is a test script!
    	    # For testing, we serve Mock Objects for logger and session without prior init
    	    if ($object eq "session") {
    	        use OpenXPKI::Server::Session::Mock;
    	        my $session = OpenXPKI::Server::Session::Mock->new();
                OpenXPKI::Server::Context::setcontext({'session' => $session});
                $session->set_pki_realm('I18N_OPENXPKI_DEPLOYMENT_TEST_DUMMY_CA');
    	    } elsif ($object eq 'log') {
    	        use OpenXPKI::Server::Log;
    	        OpenXPKI::Server::Context::setcontext({ 'log' => OpenXPKI::Server::Log->new(CONFIG => undef) });
    	    } else {
        	    OpenXPKI::Exception->throw (
                    message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_OBJECT_NOT_DEFINED",
                    params  => {OBJECT => $object},
                    log => undef, # do not log exception message
        		);
    	    }
    	}
    	# FIXME: handle objects properly?
    	#push @return, dclone($context->{exported}->{$object});
    	push @return, $context->{exported}->{$object};
    }

    if (wantarray) {
	   return @return;
    } else {
	   if (scalar @return) {
	       return $return[0];
	   } else {
	       return;
	   }
    }
}

# you cannot initialize the stored objects in the context
# itself because the modules uses the context too. If you
# try it then a use statement tries to check for CTX before
# all required modules for the Context module are loaded.
# The export will be performed after all required module
# are loaded.
#
# Example:
#
# context depends on init and init on dbi<commit_msg>Do not serve default CTX('session') and CTX('log') for test code<commit_after># OpenXPKI::Server::Context (Singleton)
# Written by <NAME> for the OpenXPKI project 2005
# Copyright (c) 2005-2006 by The OpenXPKI Project

package OpenXPKI::Server::Context;

use strict;
use base qw( Exporter );

use Storable qw(dclone);

use OpenXPKI::Debug;
use Data::Dumper;

our @EXPORT_OK = qw( CTX );

#use Smart::Comments;

use OpenXPKI::Exception;

my $context = {
    initialized => 0,

    exported => {
    	# always created by this package
        config           => undef,
        watchdog         => undef,
        workflow_factory => undef,
    	crypto_layer     => undef,
    	pki_realm        => undef,
        pki_realm_by_cfg => undef,
    	volatile_vault   => undef,
    	log              => undef,
    	dbi              => undef,
    	dbi_log          => undef,

    	# user-settable
    	api            => undef,
    	server         => undef,
        acl            => undef,
        session        => undef,
        authentication => undef,
        notification   => undef,
        workflow_id    => undef,
    },
};

our %who_forked_me;

# only called statically
sub CTX {
    my @objects = @_;

    # not needed as we would get an exception if the requested object is not
    # defined anyway. Removed as it affects the new auto mock objects for testing
    if (0) {
    if (! $context->{initialized}) {
        OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_NOT_INITIALIZED",
            params => { 'objects' => join ":", @objects },
	        log => undef, # do not log exception
	    );
    }}

    # TODO: add access control? (idea: limit access to this method to
    # authorized parts of the code only, explicity excluding interface
    # implementations...)

    my @return;
    foreach my $object (@objects) {
	    OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_OBJECT_NOT_FOUND",
            params  => {OBJECT => $object},
    		log => undef, # do not log exception message
		) unless exists $context->{exported}->{$object};

	    OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SERVER_CONTEXT_CTX_OBJECT_NOT_DEFINED",
            params  => {OBJECT => $object},
            log => undef, # do not log exception message
		) unless defined $context->{exported}->{$object};

    	push @return, $context->{exported}->{$object};
    }

    return @return if wantarray;
    return $return[0] if scalar @return;
    return;
}

# you cannot initialize the stored objects in the context
# itself because the modules uses the context too. If you
# try it then a use statement tries to check for CTX before
# all required modules for the Context module are loaded.
# The export will be performed after all required module
# are loaded.
#
# Example:
#
# context depends on init and init on dbi
{'fraction_non_alphanumeric': 0.10934691431995207, 'fraction_numerical': 0.006590772917914919, 'mean_word_length': 2.336831584207896, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2541813', 'n_tokens_mistral': 2110, 'n_tokens_neox': 1996, 'n_words': 610}