From 284597c8afb6af2ea0ce77a5ab439901d4b9d2da Mon Sep 17 00:00:00 2001 From: helge Date: Mon, 7 Jun 2004 15:09:14 +0000 Subject: [PATCH] git-svn-id: http://svn.opengroupware.org/SOGo/trunk@23 d1b88da0-ebda-0310-925b-ed51d893ca5b --- OGoContentStore/GNUmakefile | 6 ++ OGoContentStore/OCSChannelManager.h | 17 ++++ OGoContentStore/OCSChannelManager.m | 151 ++++++++++++++++++++++++++++ OGoContentStore/OCSContext.h | 6 ++ OGoContentStore/OCSFolderManager.h | 16 +++ OGoContentStore/OCSFolderManager.m | 19 ++++ OGoContentStore/common.h | 1 + 7 files changed, 216 insertions(+) diff --git a/OGoContentStore/GNUmakefile b/OGoContentStore/GNUmakefile index 8d11000b..ef9bc9da 100644 --- a/OGoContentStore/GNUmakefile +++ b/OGoContentStore/GNUmakefile @@ -5,6 +5,8 @@ include $(GNUSTEP_MAKEFILES)/common.make LIBRARY_NAME = libOGoContentStore libOGoContentStore_HEADER_FILES += \ + NSURL+OCS.h \ + \ OCSContext.h \ OCSFieldInfo.h \ OCSFolder.h \ @@ -13,6 +15,8 @@ libOGoContentStore_HEADER_FILES += \ OCSChannelManager.h \ libOGoContentStore_OBJC_FILES += \ + NSURL+OCS.m \ + \ OCSContext.m \ OCSFieldInfo.m \ OCSFolder.m \ @@ -20,6 +24,8 @@ libOGoContentStore_OBJC_FILES += \ OCSFolderType.m \ OCSChannelManager.m \ +libOGoContentStore_LIBRARY_DEPENDS_UPON += -lGDLAccess + -include GNUmakefile.preamble include $(GNUSTEP_MAKEFILES)/library.make -include GNUmakefile.postamble diff --git a/OGoContentStore/OCSChannelManager.h b/OGoContentStore/OCSChannelManager.h index d733149f..acfde2dd 100644 --- a/OGoContentStore/OCSChannelManager.h +++ b/OGoContentStore/OCSChannelManager.h @@ -25,10 +25,27 @@ #import +/* + OCSChannelManager + + This object manages the connection pooling. +*/ + +@class NSURL, NSMutableDictionary; +@class EOAdaptorChannel, EOAdaptor; + @interface OCSChannelManager : NSObject { + NSMutableDictionary *urlToAdaptor; } ++ (id)defaultChannelManager; + +/* channels */ + +- (EOAdaptorChannel *)acquireOpenChannelForURL:(NSURL *)_url; +- (void)releaseChannel:(EOAdaptorChannel *)_channel; + @end #endif /* __OGoContentStore_OCSChannelManager_H__ */ diff --git a/OGoContentStore/OCSChannelManager.m b/OGoContentStore/OCSChannelManager.m index 4152f1d0..6bf6f064 100644 --- a/OGoContentStore/OCSChannelManager.m +++ b/OGoContentStore/OCSChannelManager.m @@ -21,12 +21,163 @@ // $Id$ #include "OCSChannelManager.h" +#include "NSURL+OCS.h" +#include +#include +#include #include "common.h" @implementation OCSChannelManager +static BOOL debugOn = YES; + ++ (NSString *)adaptorNameForURLScheme:(NSString *)_scheme { + // TODO: map scheme to adaptors (eg 'postgresql://' to PostgreSQL72 + return @"PostgreSQL72"; +} + ++ (id)defaultChannelManager { + static OCSChannelManager *cm = nil; + if (cm == nil) + cm = [[self alloc] init]; + return cm; +} + +- (id)init { + if ((self = [super init])) { + self->urlToAdaptor = [[NSMutableDictionary alloc] initWithCapacity:4]; + } + return self; +} + - (void)dealloc { + [self->urlToAdaptor release]; [super dealloc]; } +/* DB key */ + +- (NSString *)databaseKeyForURL:(NSURL *)_url { + /* + We need to build a proper key that omits passwords and URL path components + which are not required. + */ + NSString *key; + + key = [NSString stringWithFormat:@"%@\n%@\n%@\n%@", + [_url host], [_url port], + [_url user], [_url ocsDatabaseName]]; + return key; +} + +/* adaptors */ + +- (NSDictionary *)connectionDictionaryForURL:(NSURL *)_url { + [self debugWithFormat:@"build connection dictionary for URL: %@", _url]; + return nil; +} + +- (EOAdaptor *)adaptorForURL:(NSURL *)_url { + EOAdaptor *adaptor; + NSString *adaptorName; + NSString *key; + NSDictionary *condict; + + if (_url == nil) + return nil; + if ((key = [self databaseKeyForURL:_url]) == nil) + return nil; + if ((adaptor = [self->urlToAdaptor objectForKey:key]) != nil) { + [self debugWithFormat:@"using cached adaptor: %@", adaptor]; + return adaptor; /* cached :-) */ + } + + [self debugWithFormat:@"creating new adaptor for URL: %@", _url]; + + adaptorName = [[self class] adaptorNameForURLScheme:[_url scheme]]; + if ([adaptorName length] == 0) { + [self logWithFormat:@"ERROR: cannot handle URL: %@", _url]; + return nil; + } + + condict = [self connectionDictionaryForURL:_url]; + + if ((adaptor = [EOAdaptor adaptorWithName:adaptorName]) == nil) { + [self logWithFormat:@"ERROR: did not find adaptor '%@' for URL: %@", + adaptorName, _url]; + return nil; + } + + [adaptor setConnectionDictionary:condict]; + [self->urlToAdaptor setObject:adaptor forKey:key]; + return adaptor; +} + +/* channels */ + +- (EOAdaptorChannel *)_createChannelForURL:(NSURL *)_url { + EOAdaptor *adaptor; + EOAdaptorContext *adContext; + EOAdaptorChannel *adChannel; + + if ((adaptor = [self adaptorForURL:_url]) == nil) + return nil; + + if ((adContext = [adaptor createAdaptorContext]) == nil) { + [self logWithFormat:@"ERROR: could not create adaptor context!"]; + return nil; + } + if ((adChannel = [adContext createAdaptorChannel]) == nil) { + [self logWithFormat:@"ERROR: could not create adaptor channel!"]; + return nil; + } + return adChannel; +} + +- (EOAdaptorChannel *)acquireOpenChannelForURL:(NSURL *)_url { + // TODO: naive implementation, add pooling! + EOAdaptorChannel *channel; + + if ((channel = [self _createChannelForURL:_url]) == nil) + return channel; + + if ([channel isOpen]) + return [channel retain]; + + if (![channel openChannel]) { + [self logWithFormat:@"could not open channel %@ for URL: %@", + channel, _url]; + return nil; + } + + return [channel retain]; +} +- (void)releaseChannel:(EOAdaptorChannel *)_channel { + // TODO: naive implementation, add pooling! + if ([_channel isOpen]) + [_channel closeChannel]; + + [_channel release]; +} + +/* debugging */ + +- (BOOL)isDebuggingEnabled { + return debugOn; +} + +/* description */ + +- (NSString *)description { + NSMutableString *ms; + + ms = [NSMutableString stringWithCapacity:256]; + [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])]; + + [ms appendFormat:@" #adaptors=%d", [self->urlToAdaptor count]]; + + [ms appendString:@">"]; + return ms; +} + @end /* OCSChannelManager */ diff --git a/OGoContentStore/OCSContext.h b/OGoContentStore/OCSContext.h index c40d05c5..8caf34ca 100644 --- a/OGoContentStore/OCSContext.h +++ b/OGoContentStore/OCSContext.h @@ -25,6 +25,12 @@ #import +/* + OCSContext + + Context passed to all operations. +*/ + @interface OCSContext : NSObject { } diff --git a/OGoContentStore/OCSFolderManager.h b/OGoContentStore/OCSFolderManager.h index 1af36e9a..ee7fed1d 100644 --- a/OGoContentStore/OCSFolderManager.h +++ b/OGoContentStore/OCSFolderManager.h @@ -25,10 +25,26 @@ #import +/* + OCSFolderManager + + Objects of this class manage the "folder_info" table, they manage the + model and manage the tables required for a folder. +*/ + +@class NSURL; + @interface OCSFolderManager : NSObject { + NSURL *folderInfoLocation; } +- (id)initWithFolderInfoLocation:(NSURL *)_url; + +/* accessors */ + +- (NSURL *)folderInfoLocation; + @end #endif /* __OGoContentStore_OCSFolderManager_H__ */ diff --git a/OGoContentStore/OCSFolderManager.m b/OGoContentStore/OCSFolderManager.m index 5aefadaa..e0637e88 100644 --- a/OGoContentStore/OCSFolderManager.m +++ b/OGoContentStore/OCSFolderManager.m @@ -25,8 +25,27 @@ @implementation OCSFolderManager +- (id)initWithFolderInfoLocation:(NSURL *)_url { + if (_url == nil) { + [self logWithFormat:@"ERROR: missing folder info url!"]; + [self release]; + return nil; + } + if ((self = [super init])) { + self->folderInfoLocation = [_url retain]; + } + return self; +} + - (void)dealloc { + [self->folderInfoLocation release]; [super dealloc]; } +/* accessors */ + +- (NSURL *)folderInfoLocation { + return self->folderInfoLocation; +} + @end /* OCSFolderManager */ diff --git a/OGoContentStore/common.h b/OGoContentStore/common.h index f723280b..edfa4286 100644 --- a/OGoContentStore/common.h +++ b/OGoContentStore/common.h @@ -21,5 +21,6 @@ // $Id$ #import +#import #include -- 2.39.5