2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGObjWeb/WOSessionStore.h>
26 This store keeps all sessions as archived files inside of a directory.
27 It provides session fail-over, but restoring/saving a session takes some
35 The session-directory can be selected using the WOFileSessionPath default.
37 Note: it doesn't provide session distribution between instances, since the
38 store doesn't lock the session files.
41 @class NSString, NSFileManager;
43 @interface WOFileSessionStore : WOSessionStore
45 NSFileManager *fileManager;
50 #include <NGObjWeb/WOContext.h>
51 #include <NGObjWeb/WOSession.h>
52 #include <NGObjWeb/WOApplication.h>
55 @implementation WOFileSessionStore
57 static BOOL logExpire = YES;
60 return [super version] + 0;
63 NSAssert2([super version] == 2,
64 @"invalid superclass (%@) version %i !",
65 NSStringFromClass([self superclass]), [super version]);
68 - (id)initWithSessionPath:(NSString *)_path {
72 if ([_path length] == 0) {
77 fm = [NSFileManager defaultManager];
78 if (![fm fileExistsAtPath:_path isDirectory:&isDir]) {
79 if (![fm createDirectoryAtPath:_path attributes:nil]) {
80 NSLog(@"%s: could not create a directory at path: %@",
81 __PRETTY_FUNCTION__, _path);
87 NSLog(@"%s: not a directory path: %@", __PRETTY_FUNCTION__, _path);
92 if ((self = [super init])) {
93 self->snPath = [_path copy];
94 self->fileManager = [fm retain];
100 p = [[NSUserDefaults standardUserDefaults]
101 stringForKey:@"WOFileSessionPath"];
102 return [self initWithSessionPath:p];
106 [self->fileManager release];
107 [self->snPath release];
113 - (int)activeSessionsCount {
115 //return [[self->fileManager directoryContentsAtPath:self->snPath] count];
120 - (NSString *)pathForSessionID:(NSString *)_sid {
121 return [self->snPath stringByAppendingPathComponent:_sid];
124 - (void)saveSessionForContext:(WOContext *)_context {
128 if (![_context hasSession])
131 sn = [_context session];
132 snp = [self pathForSessionID:[sn sessionID]];
134 if ([sn isTerminating]) {
137 // TODO: NOT IMPLEMENTED (serialized-session termination)
138 //NSMapRemove(self->idToSession, [sn sessionID]);
140 NSLog(@"session %@ terminated at %@ ..",
141 [sn sessionID], [NSCalendarDate calendarDate]);
147 data = [NSArchiver archivedDataWithRootObject:sn];
150 if (![data writeToFile:snp atomically:YES]) {
152 @"could not write data of session %@ to file: '%@'", sn, snp];
156 [self logWithFormat:@"could not archive session: '%@'", sn];
160 - (WOSession *)restoreSessionWithID:(NSString *)_sid
161 request:(WORequest *)_request
163 NSAutoreleasePool *pool;
165 WOSession *session = nil;
167 if ([_sid length] == 0)
170 if (![_sid isKindOfClass:[NSString class]]) {
171 NSLog(@"WARNING(%s): got invalid session id (expected string !): %@",
172 __PRETTY_FUNCTION__, _sid);
176 if ([_sid isEqualToString:@"expired"])
179 pool = [[NSAutoreleasePool alloc] init];
181 snp = [self pathForSessionID:_sid];
183 if ([self->fileManager fileExistsAtPath:snp]) {
186 if ((data = [self->fileManager contentsAtPath:snp])) {
187 session = [NSUnarchiver unarchiveObjectWithData:data];
188 // NSLog(@"unarchived session: %@", session);
190 if (![session isKindOfClass:[WOSession class]]) {
191 NSLog(@"object unarchived from %@ isn't a WOSession: %@ ...",
197 [self logWithFormat:@"could not read sn file: '%@'", snp];
202 [self logWithFormat:@"session file does not exist: '%@'", snp];
212 [self logWithFormat:@"session with id %@ expired.", _sid];
215 return [session autorelease];
220 - (void)sessionExpired:(NSString *)_sessionID {
223 // TODO: NOT IMPLEMENTED (serialized session expiration)
224 NSLog(@"%@ expired.", _sessionID);
225 // NSMapRemove(self->idToSession, _sessionID);
230 - (void)sessionTerminated:(WOSession *)_session {
231 _session = RETAIN(_session);
234 // TODO: NOT IMPLEMENTED (serialized session termination)
235 NSLog(@"%@ terminated.", [_session sessionID]);
236 // NSMapRemove(self->idToSession, [_session sessionID]);
241 [[WOApplication application]
243 @"WOFileSessionStore: session %@ terminated.",
244 [_session sessionID]];
249 - (NSString *)description {
250 return [NSString stringWithFormat:@"<%@[0x%08X]: path=%@>",
251 NSStringFromClass([self class]), self, self->snPath];
254 @end /* WOFileSessionStore */