]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOComponent.m
fixed bundle resource lookup on MacOSX, changed resource lookup in
[sope] / sope-appserver / NGObjWeb / WOComponent.m
index b81edcd1fbe922a9fd0708f8b5f987762cab9086..a21d60feca8122608294768e73cd29c14e18263e 100644 (file)
@@ -1,20 +1,20 @@
 /*
-  Copyright (C) 2000-2004 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OpenGroupware.org.
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
@@ -25,7 +25,7 @@
 #include <NGObjWeb/WODynamicElement.h>
 #include "WOContext+private.h"
 #include "WOElement+private.h"
-#include "WOComponentDefinition.h"
+#include <NGObjWeb/WOComponentDefinition.h>
 #include <NGObjWeb/WOResourceManager.h>
 #include <NGObjWeb/WOApplication.h>
 #include <NGObjWeb/WOResponse.h>
@@ -53,7 +53,9 @@
 
 static Class NSDateClass      = Nil;
 static Class WOComponentClass = Nil;
-static BOOL  profElements                      = NO;
+
+static NGLogger *perfLogger                    = nil;
+
 static BOOL  debugOn                           = NO;
 static BOOL  debugComponentAwake               = NO;
 static BOOL  debugTemplates                    = NO;
@@ -66,19 +68,23 @@ static BOOL  wakeupPageOnCreation              = NO;
   return [super version] + 0 /* v2 */;
 }
 + (void)initialize {
-  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
+  NSUserDefaults  *ud;
+  NGLoggerManager *lm;
   static BOOL didInit = NO;
+
   if (didInit) return;
   didInit = YES;
   
   NSAssert2([super version] == 2,
             @"invalid superclass (%@) version %i !",
             NSStringFromClass([self superclass]), [super version]);
-  
-  WOComponentClass = [WOComponent class];
-  NSDateClass      = [NSDate class];
-  
-  profElements        = [ud boolForKey:@"WOProfileElements"];
+
+  ud = [NSUserDefaults standardUserDefaults];
+  lm = [NGLoggerManager defaultLoggerManager];
+
+  WOComponentClass    = [WOComponent class];
+  NSDateClass         = [NSDate class];
+  perfLogger          = [lm loggerForDefaultKey:@"WOProfileElements"];
   debugOn             = [WOApplication isDebuggingEnabled];
   debugComponentAwake = [ud boolForKey:@"WODebugComponentAwake"];
   abortOnAwakeComponentInCtxDealloc = 
@@ -132,8 +138,8 @@ static BOOL  wakeupPageOnCreation              = NO;
     if (self->context)
       [self ensureAwakeInContext:self->context];
     else {
-      [self logWithFormat:
-              @"WARNING: no context given to -initWithContext: ..."];
+      [self warnWithFormat:
+              @"no context given to -initWithContext: ..."];
     }
   }
   return self;
@@ -147,6 +153,7 @@ static BOOL  wakeupPageOnCreation              = NO;
                         withObject:nil];
   [self->subcomponents release];
   
+  [self->wocClientObject release];
   [self->wocBindings   release];
   [self->wocVariables  release];
   [self->wocName       release];
@@ -188,9 +195,9 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 #endif
   
   if (self->componentFlags.isAwake) {
-    [self logWithFormat:
-            @"WARNING: session will dealloc, "
-           @"but component 0x%08X is awake (ctx=%@) !", self, self->context];
+    [self warnWithFormat:
+            @"session will dealloc, but component 0x%08X is awake (ctx=%@) !",
+            self, self->context];
     [self _sleepWithContext:self->context];
   }
   
@@ -219,8 +226,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
             the event?!
     */
     [self debugWithFormat:
-            @"WARNING: context %@ will dealloc, "
-            @"but component is awake in ctx %@!",
+            @"context %@ will dealloc, but component is awake in ctx %@!",
             [_notification object], [self->context contextID]];
     if (abortOnAwakeComponentInCtxDealloc)
       abort();
@@ -239,12 +245,12 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 - (void)sleep {
   if (debugOn) {
     if (self->componentFlags.isAwake) {
-      [self debugWithFormat:
-              @"WARNING: component should not be awake if sleep is called !"];
+      [self warnWithFormat:
+              @"component should not be awake if sleep is called !"];
     }
     if (self->context == nil) {
-      [self debugWithFormat:
-              @"WARNING: context should not be nil if sleep is called !"];
+      [self warnWithFormat:
+              @"context should not be nil if sleep is called !"];
     }
   }
   
@@ -260,7 +266,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 #endif
   
   if (debugComponentAwake) 
-    [self logWithFormat:@"0x%08X ensureAwakeInContext:0x%08X", self, _ctx];
+    [self debugWithFormat:@"0x%08X ensureAwakeInContext:0x%08X", self, _ctx];
   
   if (self->context == nil)     [self _setContext:_ctx];
   if (self->application == nil) self->application = [_ctx application];
@@ -292,13 +298,13 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 }
 - (void)_sleepWithContext:(WOContext *)_ctx {
   if (debugComponentAwake) 
-    [self logWithFormat:@"0x%08X _sleepWithContext:0x%08X", self, _ctx];
+    [self debugWithFormat:@"0x%08X _sleepWithContext:0x%08X", self, _ctx];
   
   if (_ctx != self->context) {
     if ((self->context != nil) && (_ctx != nil)) {
       /* component is active in different context ... */
-      [self debugWithFormat:
-              @"WARNING: sleep context mismatch (own=0x%08X vs given=0x%08X)",
+      [self warnWithFormat:
+              @"sleep context mismatch (own=0x%08X vs given=0x%08X)",
               self->context, _ctx];
       return;
     }
@@ -338,9 +344,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   NSArray *languages = nil;
   
 #if 0 // the component might not yet be awake !
-  languages = [self hasSession]
-    ? [[self session] languages]
-    : [[[self context] request] browserLanguages];
+  languages = [[self context] resourceLookupLanguages];
 #endif
   
   return [[self resourceManager]
@@ -385,13 +389,13 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 - (WOSession *)session {
   if (self->session == nil) {
     if ((self->session = [[self context] session]) == nil) {
-      [self logWithFormat:@"could not get session object from context %@",
+      [self debugWithFormat:@"could not get session object from context %@",
               self->context];
     }
   }
   
   if (self->session == nil)
-    [self logWithFormat:@"WARNING: missing session for component!"];
+    [self warnWithFormat:@"missing session for component!"];
   
   return self->session;
 }
@@ -404,11 +408,11 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     return self->context;
   
   [self debugWithFormat:
-          @"WARNING: missing context in component 0x%08X (component%s)",
+          @"missing context in component 0x%08X (component%s)",
           self,
           self->componentFlags.isAwake ? " is awake" : " is not awake"];
   if (abortOnMissingCtx) {
-    [self logWithFormat:@"aborting, because ctx is missing !"];
+    [self errorWithFormat:@"aborting, because ctx is missing !"];
     abort();
   }
     
@@ -417,7 +421,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   [self _setContext:[self->application context]];
   
   if (self->context == nil)
-    [self logWithFormat:@"WARNING: could not determine context object!"];
+    [self warnWithFormat:@"could not determine context object!"];
   
   return self->context;
 }
@@ -434,16 +438,13 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 }
 
 - (WOComponent *)pageWithName:(NSString *)_name {
-  NSArray *languages = nil;
+  NSArray           *languages;
   WOResourceManager *rm;
   WOComponent       *component;
   
-  languages = [self hasSession]
-    ? [(WOSession *)[self session] languages]
-    : [[[self context] request] browserLanguages];
-  
-  rm = [self resourceManager];
-  
+  languages = [[self context] resourceLookupLanguages];
+  rm        = [self resourceManager];
+
   /* 
      Note: this API is somewhat broken since the component expects the
            -initWithContext: message for initialization yet we pass no
@@ -470,9 +471,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   NSArray *langs;
   IS_DEPRECATED;
   
-  langs = [self hasSession]
-    ? [(WOSession *)[self session] languages]
-    : [[[self context] request] browserLanguages];
+  langs = [[self context] resourceLookupLanguages];
   
   return [[[self application]
                  resourceManager]
@@ -484,7 +483,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 
 - (void)setName:(NSString *)_name {
   if (![_name isNotNull])
-    [self logWithFormat:@"WARNING: setting 'nil' name on component!"];
+    [self warnWithFormat:@"setting 'nil' name on component!"];
   
   ASSIGNCOPY(self->wocName, _name);
 }
@@ -594,15 +593,15 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   if (_ext) _name = [_name stringByAppendingPathExtension:_ext];
 
   if (cpath == nil) {
-    NSLog(@"WARNING: no path set in component %@", [self name]);
+    [self warnWithFormat:@"no path set in component %@", [self name]];
     return nil;
   }
   if (![fm fileExistsAtPath:cpath isDirectory:&isDirectory]) {
-    NSLog(@"WARNING: component directory %@ does not exist !", cpath);
+    [self warnWithFormat:@"component directory %@ does not exist !", cpath];
     return nil;
   }
   if (!isDirectory) {
-    NSLog(@"WARNING: component path %@ is not a directory !", cpath);
+    [self warnWithFormat:@"component path %@ is not a directory !", cpath];
     return nil;
   }
 
@@ -649,17 +648,15 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   WOElement         *tmpl;
   
   if ((resourceManager = [self resourceManager]) == nil) {
-    [self logWithFormat:@"ERROR(%s): could not determine resource manager !",
-          __PRETTY_FUNCTION__];
+    [self errorWithFormat:@"%s: could not determine resource manager !",
+            __PRETTY_FUNCTION__];
     return nil;
   }
   
-  languages = [self hasSession]
-    ? [(WOSession *)[self  session] languages]
-    : [[[self context] request] browserLanguages];
-  
-  tmpl = [resourceManager templateWithName:_name languages:languages];
-  if (debugTemplates) [self logWithFormat:@"found template: %@", tmpl];
+  languages = [[self context] resourceLookupLanguages];
+  tmpl      = [resourceManager templateWithName:_name languages:languages];
+
+  if (debugTemplates) [self debugWithFormat:@"found template: %@", tmpl];
   return tmpl;
 }
 
@@ -691,7 +688,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     
     child = [child resolveWithParent:self];
     if (child == nil) {
-      [self logWithFormat:@"Could not resolve component fault: %@", _name];
+      [self warnWithFormat:@"Could not resolve component fault: %@", _name];
       return nil;
     }
     
@@ -719,9 +716,9 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   
   if (parent == nil) {
     parent = [self parent];
-    NSLog(@"WARNING: tried to set value of binding '%@' in component '%@' "
-          @"without parent component (parent is '%@') !",
-          _name, [self name], [parent name]);
+    [self warnWithFormat:@"tried to set value of binding '%@' in component "
+            @"'%@' without parent component (parent is '%@') !",
+            _name, [self name], [parent name]];
   }
   
   [[self    retain] autorelease];
@@ -743,9 +740,9 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   
   if (parent == nil) {
     parent = [self parent];
-    NSLog(@"WARNING: tried to retrieve value of binding '%@' in"
-          @" component '%@' without parent component (parent is '%@') !",
-          _name, [self name], [parent name]);
+    [self warnWithFormat:@"tried to retrieve value of binding '%@' in"
+            @" component '%@' without parent component (parent is '%@') !",
+            _name, [self name], [parent name]];
   }
   
   [[self    retain] autorelease];
@@ -797,8 +794,8 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   NSAssert(parent != self, @"parent component equals current component");
 
   if (![parent respondsToSelector:action]) {
-    [self logWithFormat:@"parent %@ doesn't respond to %@",
-          [parent name], _name];
+    [self debugWithFormat:@"parent %@ doesn't respond to %@",
+            [parent name], _name];
     return nil;
   }
 
@@ -865,7 +862,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
             @"component %@ is not awake !", self);
   if (debugOn) {
     if (self->context != _ctx) {
-      [self debugWithFormat:@"WARNING(%s): component ctx != ctx (%@ vs %@)",
+      [self debugWithFormat:@"%s: component ctx != ctx (%@ vs %@)",
               __PRETTY_FUNCTION__, self->context, _ctx];
     }
   }
@@ -880,7 +877,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     return;
   }
   
-  if (profElements)
+  if (perfLogger)
     st = [[NSDateClass date] timeIntervalSince1970];
     
   if (template->appendResponse) {
@@ -891,7 +888,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   else
     [template appendToResponse:_response inContext:_ctx];
 
-  if (profElements) {
+  if (perfLogger) {
     NSTimeInterval diff;
     int i;
     diff = [[NSDateClass date] timeIntervalSince1970] - st;
@@ -899,10 +896,10 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     for (i = [_ctx componentStackCount]; i >= 0; i--)
       printf("  ");
 #endif
-    printf("Template %s (comp %s): %0.3fs\n",
-           [[_ctx elementID] cString],
-           [[self name] cString],
-           diff);
+    [perfLogger logWithFormat:@"Template %@ (comp %@): %0.3fs\n",
+                  [_ctx elementID],
+                  [self name],
+                  diff];
   }
 }
   
@@ -918,7 +915,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   response = [WOResponse responseWithRequest:[ctx request]];
   
   if (ctxID == nil) {
-    [self logWithFormat:@"missing ctx-id for context %@", ctx];
+    [self debugWithFormat:@"missing ctx-id for context %@", ctx];
     ctxID = @"noctx";
   }
   
@@ -1022,7 +1019,8 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     if ([self logComponentVariableCreations]) {
       /* only if we have a subclass, we can store values in ivars ... */
       if (![[self->wocVariables objectForKey:_key] isNotNull]) {
-        [self logWithFormat:@"Created component variable (class=%@): '%@'.", 
+        [self debugWithFormat:
+                @"Created component variable (class=%@): '%@'.",
                 NSStringFromClass(self->isa), _key];
       }
     }
@@ -1032,10 +1030,10 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     return;
   }
 
-  [self logWithFormat:
+  [self debugWithFormat:
           @"value %@ could not set via method or KVC "
           @"(self responds to %@: %s).",
-         _key, _key,
+               _key, _key,
           [self respondsToSelector:NSSelectorFromString(_key)] ? "yes" : "no"];
 #if 0
   return NO;
@@ -1048,7 +1046,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
     return value;
 
 #if DEBUG && 0
-  [self logWithFormat:@"KVC: accessed the component variable %@", _key];
+  [self debugWithFormat:@"KVC: accessed the component variable %@", _key];
 #endif
   if ((value = [self objectForKey:_key]))
     return value;
@@ -1077,8 +1075,8 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
   if ([self logComponentVariableCreations]) {
     /* only if we have a subclass, we can store values in ivars ... */
     if (![[self->wocVariables objectForKey:_key] isNotNull]) {
-      [self logWithFormat:@"Created component variable (class=%@): '%@'.", 
-             NSStringFromClass(self->isa), _key];
+      [self debugWithFormat:@"Created component variable (class=%@): '%@'.", 
+                   NSStringFromClass(self->isa), _key];
     }
   }
 #endif
@@ -1088,7 +1086,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 - (id)valueForUndefinedKey:(NSString *)_key {
   // Note: this is not used on libFoundation, insufficient KVC implementation
 #if DEBUG && 0
-  [self logWithFormat:@"KVC: accessed the component variable %@", _key];
+  [self debugWithFormat:@"KVC: accessed the component variable %@", _key];
 #endif
   return [self objectForKey:_key];
 }
@@ -1104,7 +1102,7 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 
 - (void)unableToSetNilForKey:(NSString *)_key {
   // TODO: should we call setValue:NSNull forKey?
-  [self logWithFormat:@"ERROR: unable to set 'nil' for key: '%@'", _key];
+  [self errorWithFormat:@"unable to set 'nil' for key: '%@'", _key];
 }
 
 #endif /* KVC */
@@ -1112,8 +1110,8 @@ static inline id _getExtraVar(WOComponent *self, NSString *_key) {
 - (void)validationFailedWithException:(NSException *)_exception
   value:(id)_value keyPath:(NSString *)_keyPath
 {
-  [self logWithFormat:
-          @"WARNING: formatter failed for value %@ (keyPath=%@): %@",
+  [self warnWithFormat:
+          @"formatter failed for value %@ (keyPath=%@): %@",
           _value, _keyPath, [_exception reason]];
 }