| 1 | package UR::Context::Root; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | UR::Object::Type->define( | ||
| 10 | class_name => 'UR::Context::Root', | ||
| 11 | is => ['UR::Singleton', 'UR::Context'], | ||
| 12 | is_abstract => 1, | ||
| 13 | is_transactional => 1, | ||
| 14 | doc => 'A base level context, representing the committed state of datasources external to the application.', | ||
| 15 | ); | ||
| 16 | |||
| 17 | # this is called automatically by UR.pm at the end of the module | ||
| 18 | my $initialized = 0; | ||
| 19 | sub _initialize_for_current_process { | ||
| 20 | my $class = shift; | ||
| 21 | if ($initialized) { | ||
| 22 | die "Attempt to re-initialize the current process?"; | ||
| 23 | } | ||
| 24 | my $context_singleton_class = $ENV{UR_CONTEXT_ROOT} ||= 'UR::Context::DefaultRoot'; | ||
| 25 | $class->set_current($context_singleton_class); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub name { | ||
| 29 | my $class = shift->_singleton_class_name; | ||
| 30 | my ($name) = ($class =~ /^\w+?\:\:\w+?\:\:(\w+)$/); | ||
| 31 | die "failed to parse name from $class!" unless $name; | ||
| 32 | return lc($name); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub get_current { | ||
| 36 | #shift->_initialize_for_current_process() unless $initialized; | ||
| 37 | #eval "sub get_current { \$ENV{UR_CONTEXT_ROOT} }"; | ||
| 38 | return $ENV{UR_CONTEXT_ROOT}; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub set_current { | ||
| 42 | my $class = shift; | ||
| 43 | my $value = shift; | ||
| 44 | |||
| 45 | return $value if $value eq $ENV{UR_CONTEXT_ROOT}; | ||
| 46 | |||
| 47 | $ENV{UR_CONTEXT_ROOT} = $value; | ||
| 48 | |||
| 49 | #print "base context set to $value\n"; | ||
| 50 | #print Carp::longmess(); | ||
| 51 | |||
| 52 | eval { | ||
| 53 | local $SIG{__DIE__}; | ||
| 54 | local $SIG{__WARN__}; | ||
| 55 | $ENV{UR_CONTEXT_ROOT}->class; | ||
| 56 | }; | ||
| 57 | |||
| 58 | if ($@) { | ||
| 59 | die "The context at application initialization is set to " | ||
| 60 | . $ENV{UR_CONTEXT_ROOT} . ".\n" | ||
| 61 | . "This failed to compile:\n$@" | ||
| 62 | } | ||
| 63 | |||
| 64 | unless ($ENV{UR_CONTEXT_ROOT}->isa("UR::Context")) { | ||
| 65 | die "The context at application initialization is set to " | ||
| 66 | . $ENV{UR_CONTEXT_ROOT} . ".\n" | ||
| 67 | . "This does not inherit from UR::Context." | ||
| 68 | } | ||
| 69 | |||
| 70 | unless ($ENV{UR_CONTEXT_ROOT}->__meta__) { | ||
| 71 | die "The context at application initialization is set to " | ||
| 72 | . $ENV{UR_CONTEXT_ROOT} . ".\n" | ||
| 73 | . "This is not defined with UR::Object::Type metadata!" | ||
| 74 | } | ||
| 75 | |||
| 76 | # Initialize the bottom of the transaction stack | ||
| 77 | if (@UR::Context::Transaction::open_transaction_stack > 1) { | ||
| 78 | die "Cannot change the base context once transactions are in progress!" | ||
| 79 | } | ||
| 80 | |||
| 81 | return $value; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub access_level { | ||
| 85 | my $self = shift->_singleton_object; | ||
| 86 | return "???"; | ||
| 87 | } | ||
| 88 | |||
| 89 | # sub has_changes { return } | ||
| 90 | |||
| 91 | 1; | ||
| 92 |