| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::SessionManager::SessionManagerBase; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::SessionManager::SessionManagerBase - session manager base class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Minimal base implementation of B<Wombat::SessionManager>. This class | ||
| 16 | supports no session persistence or distributable capabilities. This | ||
| 17 | class may be subclassed to create more sophisticated Manager | ||
| 18 | implementations. Subclasses B<MUST> override C<add()>, C<getName()>, | ||
| 19 | C<getSession()>, C<getSessions()>, and C<remove()>. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use base qw(Wombat::SessionManager); | ||
| 24 | use fields qw(container digest maxInactiveInterval started); | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | use Digest::MD5 (); | ||
| 29 | use Wombat::Core::Session (); | ||
| 30 | use Wombat::Exception (); | ||
| 31 | use Wombat::Globals (); | ||
| 32 | |||
| 33 | use constant DEFAULT_MAX_INACTIVE_INTERVAL => 60; | ||
| 34 | |||
| 35 | =pod | ||
| 36 | |||
| 37 | =head1 CONSTRUCTOR | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item new() | ||
| 42 | |||
| 43 | Construct and return a B<Wombat::SessionManager::SessionManagerBase> | ||
| 44 | instance, initializing fields appropriately. If subclasses override | ||
| 45 | the constructor, they must be sure to call | ||
| 46 | |||
| 47 | $self->SUPER::new(); | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | $self = fields::new($self) unless ref $self; | ||
| 57 | |||
| 58 | $self->{container} = undef; | ||
| 59 | $self->{digest} = Digest::MD5->new(); | ||
| 60 | $self->{maxInactiveInterval} = DEFAULT_MAX_INACTIVE_INTERVAL; | ||
| 61 | $self->{started} = undef; | ||
| 62 | |||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 ACCESSOR METHODS | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item getContainer() | ||
| 73 | |||
| 74 | Return the Container for which this SessionManager manages Sessions. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub getContainer { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | return $self->{container}; | ||
| 82 | } | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =item setContainer($container) | ||
| 87 | |||
| 88 | Set the Container for which this SessionManager manages Sessions. | ||
| 89 | |||
| 90 | B<Parameters:> | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item $container | ||
| 95 | |||
| 96 | the B<Wombat::Container> | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub setContainer { | ||
| 103 | my $self = shift; | ||
| 104 | my $container = shift; | ||
| 105 | |||
| 106 | $self->{container} = $container; | ||
| 107 | |||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | =item getMaxInactiveInterval() | ||
| 114 | |||
| 115 | Return the default maximum inactive interval in seconds for Sessions | ||
| 116 | created by this SessionManager. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub getMaxInactiveInterval { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | return $self->{maxInactiveInterval}; | ||
| 124 | } | ||
| 125 | |||
| 126 | =pod | ||
| 127 | |||
| 128 | =item setMaxInactiveInterval($interval) | ||
| 129 | |||
| 130 | Set the default maximum inactive interval for Sessions created by this | ||
| 131 | SessionManager. | ||
| 132 | |||
| 133 | B<Parameters:> | ||
| 134 | |||
| 135 | =over | ||
| 136 | |||
| 137 | =item $interval | ||
| 138 | |||
| 139 | the new interval, in seconds | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub setMaxInactiveInterval { | ||
| 146 | my $self = shift; | ||
| 147 | my $interval = shift; | ||
| 148 | |||
| 149 | $self->{maxInactiveInterval} = $interval; | ||
| 150 | |||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | =pod | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | =head1 PUBLIC METHODS | ||
| 159 | |||
| 160 | =over | ||
| 161 | |||
| 162 | =item createSession() | ||
| 163 | |||
| 164 | Construct and return a B<Wombat::Session>, based on the default | ||
| 165 | settings specified by this SessionManager's fields. The session id | ||
| 166 | will be assigned by this method. | ||
| 167 | |||
| 168 | B<Throws:> | ||
| 169 | |||
| 170 | =over | ||
| 171 | |||
| 172 | =item B<Servlet::Util::IllegalStateException> | ||
| 173 | |||
| 174 | if a new Session cannot be created for, any reason | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub createSession { | ||
| 181 | my $self = shift; | ||
| 182 | |||
| 183 | my $session = Wombat::Core::Session->new(); | ||
| 184 | $session->setSessionManager($self); | ||
| 185 | $session->setNew(1); | ||
| 186 | $session->setValid(1); | ||
| 187 | $session->setCreationTime(time); | ||
| 188 | $session->setMaxInactiveInterval($self->getMaxInactiveInterval()); | ||
| 189 | |||
| 190 | my $id; | ||
| 191 | while (1) { | ||
| 192 | $id = $self->generateSessionId(); | ||
| 193 | # make sure the id is not already used by a currently active | ||
| 194 | # session | ||
| 195 | last unless $self->getSession($id); | ||
| 196 | } | ||
| 197 | $session->setId($id); | ||
| 198 | |||
| 199 | return $session; | ||
| 200 | } | ||
| 201 | |||
| 202 | =pod | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =head1 PACKAGE METHODS | ||
| 207 | |||
| 208 | Subclasses B<MUST> override all of these methods. | ||
| 209 | |||
| 210 | =over | ||
| 211 | |||
| 212 | =item add($session) | ||
| 213 | |||
| 214 | Add this Session to the set of active Sessions for this | ||
| 215 | SessionManager. | ||
| 216 | |||
| 217 | B<Parameters:> | ||
| 218 | |||
| 219 | =over | ||
| 220 | |||
| 221 | =item $session | ||
| 222 | |||
| 223 | the B<Wombat::Session> to be added | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub add {} | ||
| 230 | |||
| 231 | =pod | ||
| 232 | |||
| 233 | =item getName() | ||
| 234 | |||
| 235 | Return the display name of this SessionManager. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub getName {} | ||
| 240 | |||
| 241 | =pod | ||
| 242 | |||
| 243 | =item getSession($id) | ||
| 244 | |||
| 245 | Return the active Session managed by this SessionManager with the | ||
| 246 | specified id, or C<undef> if no session exists with that id. | ||
| 247 | |||
| 248 | B<Parameters:> | ||
| 249 | |||
| 250 | =over | ||
| 251 | |||
| 252 | =item $id | ||
| 253 | |||
| 254 | the id for the Session to be returned | ||
| 255 | |||
| 256 | =back | ||
| 257 | |||
| 258 | B<Throws:> | ||
| 259 | |||
| 260 | =over | ||
| 261 | |||
| 262 | =item B<Servlet::Util::IllegalStateException> | ||
| 263 | |||
| 264 | if the session exists but cannot be returned, for any reason | ||
| 265 | |||
| 266 | =back | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub getSession {} | ||
| 271 | |||
| 272 | =pod | ||
| 273 | |||
| 274 | =item getSessions() | ||
| 275 | |||
| 276 | Return an array containing the active Sessions managed by this | ||
| 277 | SessionManager. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub getSessions {} | ||
| 282 | |||
| 283 | =pod | ||
| 284 | |||
| 285 | =item remove($session) | ||
| 286 | |||
| 287 | Remove this Session from the active Sessions managed by this | ||
| 288 | SessionManager. | ||
| 289 | |||
| 290 | B<Parameters:> | ||
| 291 | |||
| 292 | =over | ||
| 293 | |||
| 294 | =item $session | ||
| 295 | |||
| 296 | the B<Wombat::Session> to remove | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub remove {} | ||
| 303 | |||
| 304 | =pod | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | =head1 LIFECYCLE METHODS | ||
| 309 | |||
| 310 | =over | ||
| 311 | |||
| 312 | =item start() | ||
| 313 | |||
| 314 | Prepare for active use of this SessionManager. This method should be | ||
| 315 | called before any of the public methods of the SessionManager are | ||
| 316 | utilized. | ||
| 317 | |||
| 318 | B<Throws:> | ||
| 319 | |||
| 320 | =over | ||
| 321 | |||
| 322 | =item B<Wombat::LifecycleException> | ||
| 323 | |||
| 324 | if the SessionManager has already been started | ||
| 325 | |||
| 326 | =back | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | sub start { | ||
| 331 | my $self = shift; | ||
| 332 | |||
| 333 | if ($self->{started}) { | ||
| 334 | my $msg = "start: session manager already started"; | ||
| 335 | Wombat::LifecycleException->throw($msg); | ||
| 336 | } | ||
| 337 | |||
| 338 | $self->{started} = 1; | ||
| 339 | $self->log(sprintf("%s started", $self->getName()), undef, 'INFO'); | ||
| 340 | |||
| 341 | return 1; | ||
| 342 | } | ||
| 343 | |||
| 344 | =pod | ||
| 345 | |||
| 346 | =item stop() | ||
| 347 | |||
| 348 | Gracefully terminate active use of this SessionManager. Once this | ||
| 349 | method has been called, no public methods of the SessionManager should | ||
| 350 | be utilized. | ||
| 351 | |||
| 352 | B<Throws:> | ||
| 353 | |||
| 354 | =over | ||
| 355 | |||
| 356 | =item B<Wombat::LifecycleException> | ||
| 357 | |||
| 358 | if the SessionManager is not started | ||
| 359 | |||
| 360 | =back | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub stop { | ||
| 365 | my $self = shift; | ||
| 366 | |||
| 367 | unless ($self->{started}) { | ||
| 368 | my $msg = "stop: session manager not started"; | ||
| 369 | Wombat::LifecycleException->throw($msg); | ||
| 370 | } | ||
| 371 | |||
| 372 | # XXX: can't expire active sessions! when using the Apache connector | ||
| 373 | # and shared memory cache, there's no good way to do it. you can't do | ||
| 374 | # it at child exit time, cos the first time a child exits, all cached | ||
| 375 | # sessions will be invalidated. you apparently can't do it at server | ||
| 376 | # shutdown, cos i'm getting all kinds of weird behavior. | ||
| 377 | |||
| 378 | # # expire all active sessions | ||
| 379 | # for my $session ($self->getSessions()) { | ||
| 380 | # # skip already expired sessions | ||
| 381 | # next unless $session->isValid(); | ||
| 382 | |||
| 383 | # warn "expiring session id [", $session->getId(), "]: $session\n"; | ||
| 384 | # $session->expire(); | ||
| 385 | # } | ||
| 386 | |||
| 387 | undef $self->{started}; | ||
| 388 | $self->log(sprintf("%s stopped", $self->getName()), undef, 'DEBUG'); | ||
| 389 | |||
| 390 | return 1; | ||
| 391 | } | ||
| 392 | |||
| 393 | =pod | ||
| 394 | |||
| 395 | =back | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | # private methods | ||
| 400 | |||
| 401 | sub generateSessionId { | ||
| 402 | my $self = shift; | ||
| 403 | |||
| 404 | for (my $i=0; $i < 16; $i++) { | ||
| 405 | $self->{digest}->add(int rand 2<<15); # XXX: get a good random value! | ||
| 406 | } | ||
| 407 | |||
| 408 | return $self->{digest}->hexdigest(); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub log { | ||
| 412 | my $self = shift; | ||
| 413 | |||
| 414 | $self->{container}->log(@_) if $self->{container}; | ||
| 415 | |||
| 416 | return 1; | ||
| 417 | } | ||
| 418 | |||
| 419 | 1; | ||
| 420 | __END__ |