]> err.no Git - sope/blob - sope-appserver/SoOFS/OFSResourceManager.m
added strict OSX bundle dependencies
[sope] / sope-appserver / SoOFS / OFSResourceManager.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #include "OFSResourceManager.h"
23 #include "OFSBaseObject.h"
24 #include <NGObjWeb/WOComponentDefinition.h>
25 #include "common.h"
26
27 @interface WOResourceManager(UsedPrivates)
28 - (WOComponentDefinition *)definitionForComponent:(id)_name
29   inFramework:(NSString *)_framework
30   languages:(NSArray *)_languages;
31 @end
32
33 @interface WOComponentDefinition(UsedPrivates)
34 - (void)setComponentClass:(Class)_clazz;
35 @end
36
37 @implementation OFSResourceManager
38
39 static BOOL debugOn = NO;
40
41 + (void)initialize {
42   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
43   
44   debugOn = [ud boolForKey:@"SoOFSResourceManagerDebugEnabled"];
45 }
46
47 - (id)initWithBaseObject:(id)_object inContext:(id)_ctx {
48   if ((self = [super init])) {
49     self->baseObject = _object;
50     self->context    = _ctx;
51     
52     if (self->baseObject == nil) {
53       [self release];
54       return nil;
55     }
56     
57     if (self->context == nil)
58       [self debugWithFormat:@"WARNING: got not context !"];
59   }
60   return self;
61 }
62
63 - (void)dealloc {
64   [super dealloc];
65 }
66
67 - (void)invalidate {
68   self->baseObject = nil;
69   self->context    = nil;
70 }
71
72 /* accessors */
73
74 - (id)context {
75   if (self->context == nil)
76     return [[WOApplication application] context];
77   return self->context;
78 }
79
80 /* base object lookup */
81
82 - (BOOL)doesAcquireResources {
83   return YES;
84 }
85
86 - (id)soObjectForResourceNamed:(NSString *)_name
87   inFramework:(NSString *)_frameworkName
88   languages:(NSArray *)_languages
89 {
90   id resourceObject;
91   
92   if (debugOn) {
93     [self debugWithFormat:@"lookup resource object named '%@' in %@",
94             _name, self->baseObject];
95   }
96   
97   if ([self doesAcquireResources]) {
98     NSException *error = nil;
99     id subctx;
100     
101     /* 
102        Note: this will first look into the object traversal stack of the
103              context which might be different to the base object!
104              e.g. its common to use the resource manager on the container
105              of the clientObject (baseObject==container) instead of the
106              object itself.
107     */
108     subctx = [[self context] createSubContext];
109     resourceObject = [self->baseObject 
110                           traverseKey:_name
111                           inContext:subctx
112                           error:&error
113                           acquire:YES];
114     if (error) {
115       if (debugOn) {
116         [self debugWithFormat:@"  name: %@", _name];
117         [self debugWithFormat:@"  base: %@", self->baseObject];
118         [self debugWithFormat:@"  ctx:  %@", subctx];
119       }
120       [self logWithFormat:@"ERROR: %@", error];
121     }
122   }
123   else {
124     resourceObject = [self->baseObject lookupName:_name 
125                                        inContext:[self context]
126                                        acquire:YES];
127   }
128   
129   if (debugOn)
130     [self debugWithFormat:@"  found: %@", resourceObject];
131   return resourceObject;
132 }
133
134 /* components */
135
136 - (WOComponentDefinition *)definitionForComponent:(id)_name
137   inFramework:(NSString *)_framework
138   languages:(NSArray *)_languages
139 {
140   WOComponentDefinition *cdef;
141   
142   cdef = [super definitionForComponent:_name
143                 inFramework:_framework
144                 languages:_languages];
145   [cdef setComponentClass:NSClassFromString(@"WOComponent")];
146   return cdef;
147 }
148
149 /* resource manager methods */
150
151 - (NSString *)forcedComponentExtension {
152   /* the content negotiation should select an extension for us ! */
153   return nil;
154 }
155
156 - (NSString *)resourceNameForComponentNamed:(NSString *)_name {
157   NSString *ext;
158   
159   if ((ext = [self forcedComponentExtension])) {
160     if ([[_name pathExtension] length] == 0)
161       _name = [_name stringByAppendingPathExtension:ext];
162   }
163   return _name;
164 }
165
166 - (NSString *)pathForResourceNamed:(NSString *)_name
167   inFramework:(NSString *)_fw
168   languages:(NSArray *)_langs
169 {
170   // TODO: add a cache
171   id obj;
172   
173   obj = [self soObjectForResourceNamed:_name inFramework:_fw languages:_langs];
174   if (obj == nil)
175     [self debugWithFormat:@"found no resource object named '%@'", _name];
176   else
177     [self debugWithFormat:@"found resource object '%@': %@", _name, obj];
178   
179   return [obj storagePath];
180 }
181
182 - (NSString *)urlForResourceNamed:(NSString *)_name
183   inFramework:(NSString *)_fw
184   languages:(NSArray *)_langs
185   request:(WORequest *)_request
186 {
187   // TODO: add a cache
188   id obj;
189   
190   obj = [self soObjectForResourceNamed:_name inFramework:_fw languages:_langs];
191   if (obj == nil)
192     [self logWithFormat:@"found no object named '%@'", _name];
193   
194   return [obj baseURLInContext:[self context]];
195 }
196
197 /* debugging */
198
199 - (BOOL)isDebuggingEnabled {
200   return debugOn;
201 }
202
203 /* description */
204
205 - (NSString *)description {
206   NSMutableString *ms;
207
208   ms = [NSMutableString stringWithCapacity:64];
209   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
210   
211   if (self->baseObject) 
212     [ms appendFormat:@" base=%@", self->baseObject];
213   if (self->context) 
214     [ms appendFormat:@" ctx=0x%08X", self->context];
215   
216   [ms appendString:@">"];
217   return ms;
218 }
219
220 @end /* OFSResourceManager */