| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::Service; | ||
| 4 | |||
| 5 | use fields qw(connectors container name started); | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Servlet::Util::Exception (); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $self = shift; | ||
| 13 | |||
| 14 | $self = fields::new($self) unless ref $self; | ||
| 15 | $self->{connectors} = []; | ||
| 16 | $self->{container} = undef; | ||
| 17 | $self->{name} = undef; | ||
| 18 | $self->{started} = undef; | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | # accessors | ||
| 24 | |||
| 25 | sub getContainer { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | return $self->{container}; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub setContainer { | ||
| 32 | my $self = shift; | ||
| 33 | my $container = shift; | ||
| 34 | |||
| 35 | my $oldContainer = $self->{container}; | ||
| 36 | $self->{container} = $container; | ||
| 37 | |||
| 38 | if ($self->{started} && $self->{container}) { | ||
| 39 | $container->start(); | ||
| 40 | } | ||
| 41 | |||
| 42 | for my $connector (@{ $self->{connectors} }) { | ||
| 43 | $connector->setContainer($self->{container}); | ||
| 44 | } | ||
| 45 | |||
| 46 | if ($self->{started} && $oldContainer) { | ||
| 47 | $oldContainer->stop(); | ||
| 48 | } | ||
| 49 | |||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub getName { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | return $self->{name}; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub setName { | ||
| 60 | my $self = shift; | ||
| 61 | my $name = shift; | ||
| 62 | |||
| 63 | $self->{name} = $name; | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | # public methods | ||
| 69 | |||
| 70 | sub await { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | for my $connector (@{ $self->{connectors} }) { | ||
| 74 | $connector->await(); | ||
| 75 | } | ||
| 76 | |||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub addConnector { | ||
| 82 | my $self = shift; | ||
| 83 | my $connector = shift; | ||
| 84 | |||
| 85 | $connector->setContainer($self->getContainer()); | ||
| 86 | push @{ $self->{connectors} }, $connector; | ||
| 87 | |||
| 88 | if ($self->{started} && $connector) { | ||
| 89 | $connector->start(); | ||
| 90 | } | ||
| 91 | |||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub getConnectors { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | return wantarray ? @{ $self->{connectors} } : $self->{connectors}; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub removeConnector | ||
| 102 | { | ||
| 103 | my $self = shift; | ||
| 104 | my $connector = shift; | ||
| 105 | |||
| 106 | my $j; | ||
| 107 | for (my $i = 0; $i < @{ $self->{connectors} }; $i++) | ||
| 108 | { | ||
| 109 | if (ref $self->{connectors}->[$i] eq ref $connector) | ||
| 110 | { | ||
| 111 | $j = $i; | ||
| 112 | last; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | return 1 unless defined $j; | ||
| 117 | |||
| 118 | if ($self->{started} && $connector) | ||
| 119 | { | ||
| 120 | $connector->stop(); | ||
| 121 | } | ||
| 122 | |||
| 123 | splice @{ $self->{connectors} }, $j, 1; | ||
| 124 | |||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | # lifecycle methods | ||
| 129 | |||
| 130 | sub start { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | if ($self->{started}) { | ||
| 134 | my $msg = "start: service already started"; | ||
| 135 | Servlet::Util::Exception->throw($msg); | ||
| 136 | } | ||
| 137 | |||
| 138 | $self->{started} = 1; | ||
| 139 | |||
| 140 | $self->getContainer()->start(); | ||
| 141 | |||
| 142 | for my $connector ($self->getConnectors()) { | ||
| 143 | $connector->start(); | ||
| 144 | } | ||
| 145 | |||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub stop { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | unless ($self->{started}) { | ||
| 153 | my $msg = "stop: service not started"; | ||
| 154 | Servlet::Util::Exception->throw($msg); | ||
| 155 | } | ||
| 156 | |||
| 157 | undef $self->{started}; | ||
| 158 | |||
| 159 | for my $connector ($self->getConnectors()) { | ||
| 160 | $connector->stop(); | ||
| 161 | } | ||
| 162 | |||
| 163 | $self->getContainer()->stop(); | ||
| 164 | |||
| 165 | return 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | __END__ |