Authored by devel-hb

caches are configurable by embedding application

//
// RMConfiguration.h
// MapView
//
// Created by Hauke Brandes on 28.10.08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RMConfiguration : NSObject {
id propList;
}
+ (RMConfiguration*) configuration;
- (RMConfiguration*) initWithPath: (NSString*) path;
- (void) dealloc;
- (NSDictionary*) cacheConfiguration;
@end
... ...
//
// RMConfiguration.m
// MapView
//
// Created by Hauke Brandes on 28.10.08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "RMConfiguration.h"
static RMConfiguration* RMConfigurationSharedInstance = nil;
@implementation RMConfiguration
+ (RMConfiguration*) configuration
{
@synchronized (RMConfigurationSharedInstance) {
if (RMConfigurationSharedInstance != nil) return RMConfigurationSharedInstance;
RMConfigurationSharedInstance = [[RMConfiguration alloc]
initWithPath: [[NSBundle mainBundle] pathForResource:@"routeme" ofType:@"plist"]];
return RMConfigurationSharedInstance;
}
return nil;
}
- (RMConfiguration*) initWithPath: (NSString*) path
{
self = [super init];
if (self==nil) return nil;
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
if (path==nil)
{
propList = nil;
return self;
}
NSLog(@"reading configuration from %@", path);
plistData = [NSData dataWithContentsOfFile:path];
propList = [[NSPropertyListSerialization
propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error] retain];
if(!propList)
{
NSLog(@"problem reading from %@: %@", path, error);
[error release];
}
return self;
}
- (void) dealloc
{
[propList release];
[super dealloc];
}
- (NSDictionary*) cacheConfiguration
{
if (propList==nil) return nil;
return [propList objectForKey: @"caches"];
}
@end
... ...
... ... @@ -16,6 +16,8 @@
if (![super init])
return nil;
NSLog(@"initializing memory cache with capacity %d", _capacity);
cache = [[NSMutableDictionary alloc] initWithCapacity:_capacity];
if (_capacity < 1)
... ...
... ... @@ -12,6 +12,8 @@
#import "RMDiskCache.h"
#import "RMDatabaseCache.h"
#import "RMConfiguration.h"
static RMTileCache *cache = nil;
@implementation RMTileCache
... ... @@ -23,18 +25,59 @@ static RMTileCache *cache = nil;
caches = [[NSMutableArray alloc] init];
RMMemoryCache *memoryCache = [[RMMemoryCache alloc] init];
RMDiskCache *diskCache = [[RMDiskCache alloc] init];
RMDatabaseCache *dbCache = [[RMDatabaseCache alloc] init];
[self addCache:memoryCache];
[self addCache:diskCache];
[self addCache:dbCache];
[memoryCache release];
[diskCache release];
[dbCache release];
id cacheCfg = [[RMConfiguration configuration] cacheConfiguration];
if (cacheCfg==nil)
{
cacheCfg = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObject: @"memory-cache" forKey: @"type"],
[NSDictionary dictionaryWithObject: @"disk-cache" forKey: @"type"],
[NSDictionary dictionaryWithObject: @"db-cache" forKey: @"type"],
nil
];
}
for (id cfg in cacheCfg)
{
RMTileCache* newCache = nil;
@try {
NSString* type = [cfg valueForKey:@"type"];
if ([@"memory-cache" isEqualToString: type])
{
NSNumber* capacity = [cfg objectForKey:@"capacity"];
if (capacity == nil) capacity = [NSNumber numberWithInt: 32];
newCache = [[RMMemoryCache alloc] initWithCapacity: [capacity intValue]];
}
if ([@"disk-cache" isEqualToString: type])
{
NSLog(@"creating disk cache");
newCache = [[RMDiskCache alloc] init];
}
if ([@"db-cache" isEqualToString: type])
{
newCache = [[RMDatabaseCache alloc] init];
}
if (newCache)
{
[caches addObject: newCache];
[newCache release];
}
else
{
NSLog(@"failed to create cache of type %@", type);
}
}
@catch (NSException * e) {
NSLog(@"*** configuration error: %@", [e reason]);
}
}
return self;
}
... ...
... ... @@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
126692A10EB75C0A00E002D5 /* RMConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 126692A00EB75C0A00E002D5 /* RMConfiguration.m */; };
126693040EB76C0B00E002D5 /* RMConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 126692A00EB75C0A00E002D5 /* RMConfiguration.m */; };
1D3623260D0F684500981E51 /* MapViewAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* MapViewAppDelegate.m */; };
1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
... ... @@ -128,6 +130,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
1266929F0EB75C0A00E002D5 /* RMConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMConfiguration.h; sourceTree = "<group>"; };
126692A00EB75C0A00E002D5 /* RMConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMConfiguration.m; sourceTree = "<group>"; };
1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1D3623240D0F684500981E51 /* MapViewAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MapViewAppDelegate.h; sourceTree = "<group>"; };
1D3623250D0F684500981E51 /* MapViewAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MapViewAppDelegate.m; sourceTree = "<group>"; };
... ... @@ -261,6 +265,15 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1266929E0EB75BEA00E002D5 /* Configuration */ = {
isa = PBXGroup;
children = (
1266929F0EB75C0A00E002D5 /* RMConfiguration.h */,
126692A00EB75C0A00E002D5 /* RMConfiguration.m */,
);
name = Configuration;
sourceTree = "<group>";
};
19C28FACFE9D520D11CA2CBB /* Products */ = {
isa = PBXGroup;
children = (
... ... @@ -506,6 +519,7 @@
B83E64CE0E80E73F001663B6 /* Tile Images */,
B83E64B80E80E73F001663B6 /* Renderers */,
B86F26A80E8742ED007A3773 /* Layers */,
1266929E0EB75BEA00E002D5 /* Configuration */,
B8474C610EB53A01006A0BC1 /* Resources */,
);
path = Map;
... ... @@ -681,6 +695,7 @@
1D60589B0D05DD56006BFB54 /* main.m in Sources */,
1D3623260D0F684500981E51 /* MapViewAppDelegate.m in Sources */,
28D7ACF80DDB3853001CB0EB /* MapViewViewController.m in Sources */,
126692A10EB75C0A00E002D5 /* RMConfiguration.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
... ... @@ -722,6 +737,7 @@
B8474BA00EB40094006A0BC1 /* FMResultSet.m in Sources */,
B8474BA30EB40094006A0BC1 /* RMTileCacheDAO.m in Sources */,
B8474BA50EB40094006A0BC1 /* RMDatabaseCache.m in Sources */,
126693040EB76C0B00E002D5 /* RMConfiguration.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
... ...