]> err.no Git - sope/blob - sope-appserver/SoOFS/sope.m
ivar extensions to NGObjWeb core classes, moved SoOFS to separate project
[sope] / sope-appserver / SoOFS / sope.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id: sope.m 4 2004-08-20 17:04:31Z helge $
22
23 #include "SoObjects/SoApplication.h"
24
25 /*
26   An executable which can run a SoOFS based SOPE application. When started,
27   it takes the current-directory path for constructing the SoOFS root folder
28   of the SOPE application.
29   It also reads a file ".sope.plist" in the root-path to load site-local
30   configuration settings.
31   
32   TODO:
33   - load defaults from root-folder
34     DONE [".sope.plist" is loaded and registered]
35   - load products from root-folder
36   - load authenticator from root-folder
37 */
38
39 @class NSString, NSFileManager;
40
41 @interface SOPE : SoApplication
42 {
43   NSFileManager *fm;
44   NSString *rootPath;
45 }
46
47 @end
48
49 #include "SoObjects/SoClassSecurityInfo.h"
50 #include "SoOFS/OFSFolder.h"
51 #include "SoOFS/OFSFactoryContext.h"
52 #include "common.h"
53
54 NSString *SoRootFolder = @"SoRootFolder";
55
56 @implementation SOPE
57
58 + (void)initialize {
59   /* 
60      Since we are a tool, we have no bundle and need to declare security info
61      manually ...
62   */
63   SoClassSecurityInfo *si = [self soClassSecurityInfo];
64   [si declareObjectPublic];
65   [si setDefaultAccess:@"allow"];
66 }
67
68 - (void)loadLocalDefaults:(NSString *)_path {
69   NSDictionary *plist;
70
71   if ((plist = [[NSDictionary alloc] initWithContentsOfFile:_path]) == nil) {
72     [self logWithFormat:@"could not read SOPE config: %@", _path];
73     return;
74   }
75   /* 
76      TODO: we need a separate domain for this, this stuff doesn't make sense
77            at all ...
78   */
79   [[NSUserDefaults standardUserDefaults] registerDefaults:plist];
80   [self logWithFormat:@"registered site defaults: %@", _path];
81   [plist release];
82 }
83
84 - (BOOL)_bootstrap {
85   // TODO: create some bootstrap code to create initial user database,
86   //       defaults, control-panel, etc
87   return YES;
88 }
89
90 - (BOOL)_setupRoot {
91   BOOL     isDir;
92   NSString *p;
93
94   /* setup root path */
95   
96   if (self->fm == nil) {
97     [self logWithFormat:@"missing SOPE storage filemanager."];
98     return NO;
99   }
100   if ([self->rootPath length] == 0) {
101     [self logWithFormat:@"missing SOPE storage root-path."];
102     return NO;
103   }
104
105   if (![self->fm fileExistsAtPath:self->rootPath isDirectory:&isDir]) {
106     [self logWithFormat:@"SOPE storage root-path does not exist: %@", 
107             self->rootPath];
108     return NO;
109   }
110   if (!isDir) {
111     [self logWithFormat:@"SOPE storage root-path is not a directory: %@", 
112             self->rootPath];
113     return NO;
114   }
115   
116   /* bootstrap root if necessary */
117   
118   if (![self _bootstrap])
119     return NO;
120   
121   /* configure */
122   
123   [self logWithFormat:@"starting SOPE on OFS root: %@", self->rootPath];
124   
125   p = [self->rootPath stringByAppendingPathComponent:@".sope.plist"];
126   if ([self->fm isReadableFileAtPath:p])
127     [self loadLocalDefaults:p];
128   
129   return YES;
130 }
131
132 - (id)init {
133   if ((self = [super init])) {
134     // TODO: make root-path/fm configurable ?
135     self->fm       = [[NSFileManager defaultManager] retain];
136     self->rootPath = [[self->fm currentDirectoryPath] copy];
137
138     if (![self _setupRoot]) {
139       [self release];
140       return nil;
141     }
142   }
143   return self;
144 }
145 - (void)dealloc {
146   [self->fm       release];
147   [self->rootPath release];
148   [super dealloc];
149 }
150
151 /* accessors */
152
153 - (id)fileManager {
154   return self->fm;
155 }
156 - (NSString *)rootPath {
157   return self->rootPath;
158 }
159
160 /* define the root SoObject */
161
162 - (OFSFolder *)rootObjectInContext:(id)_ctx {
163   OFSFactoryContext *ctx;
164   OFSFolder *root;
165   
166   if ((root = [_ctx valueForKey:SoRootFolder]))
167     return root;
168   
169   ctx = [OFSFactoryContext contextWithFileManager:[self fileManager]
170                            storagePath:[self rootPath]];
171   
172   root = [[OFSFolder alloc] init];
173   [root takeStorageInfoFromContext:ctx];
174   [root awakeFromFetchInContext:ctx];
175   [_ctx takeValue:root forKey:SoRootFolder];
176   return [root autorelease];
177 }
178
179 /* security */
180
181 - (id)authenticatorInContext:(id)_ctx {
182   id root;
183   id auth;
184   
185   root = [self rootObjectInContext:_ctx];
186   if ((auth = [root authenticatorInContext:_ctx]))
187     return auth;
188   
189   return [super authenticatorInContext:_ctx];
190 }
191
192 /* SMI */
193
194 - (NSArray *)manageMenuChildNames {
195   NSMutableArray *ma;
196   id root;
197   
198   ma = [NSMutableArray arrayWithCapacity:16];
199   [ma addObject:@"ControlPanel"];
200   
201   root = [self rootObjectInContext:[self context]];
202   if (root != nil && (root != self)) 
203     [ma addObjectsFromArray:[root toOneRelationshipKeys]];
204   
205   return ma;
206 }
207
208 /* MacOSX support */
209
210 - (id)handleQueryWithUnboundKey:(NSString *)key {
211   /* KVC on MacOSX throws an exception when an unbound key is queried ... */
212   return nil;
213 }
214
215 @end /* SOPE */
216
217 int main(int argc, char **argv, char **env) {
218   NSAutoreleasePool *pool;
219   
220   pool = [[NSAutoreleasePool alloc] init];
221 #if LIB_FOUNDATION_LIBRARY
222   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
223 #endif
224   
225   WOWatchDogApplicationMain(@"SOPE", argc, (void*)argv);
226   
227   [pool release];
228   return 0;
229 }