]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOCoreApplication.m
minor code cleanups
[sope] / sope-appserver / NGObjWeb / WOCoreApplication.m
index 218ec0bea2572ffaf7e594b36da992b545a2423b..632d34d12dfa720ebf7b6718d4c2b89041820631 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.
 */
@@ -28,6 +28,7 @@
 #include <EOControl/EOControl.h>
 #include <NGStreams/NGStreams.h>
 #include <NGStreams/NGNet.h>
+#include <NGExtensions/NGResourceLocator.h>
 #include "WORunLoop.h"
 #include "common.h"
 
@@ -48,6 +49,7 @@ NGObjWeb_DECLARE NSString *WOApplicationDidTerminateNotification =
 
 @interface WOCoreApplication(PrivateMethods)
 + (void)_initDefaults;
++ (id)logger;
 - (NSDictionary *)memoryStatistics;
 - (WOResponse *)handleException:(NSException *)_exc;
 @end
@@ -61,9 +63,10 @@ NGObjWeb_DECLARE NSString *WOApplicationDidTerminateNotification =
 
 @implementation WOCoreApplication
 
-static BOOL  perflog          = NO;
-static BOOL  outputValidateOn = NO;
-static Class NSDateClass      = Nil;
+static BOOL     outputValidateOn = NO;
+static Class    NSDateClass      = Nil;
+static NGLogger *logger          = nil;
+static NGLogger *perfLogger      = nil;
 
 + (int)version {
   return 1;
@@ -74,8 +77,8 @@ static NSMutableArray *activeApps = nil; // THREAD
 
 + (id)application {
   if (WOApp == nil) {
-    NSLog(@"WARNING(%s): some code called +application without "
-          @"an active app !", __PRETTY_FUNCTION__);
+    [self warnWithFormat:@"%s: some code called +application without an "
+            @"active app !", __PRETTY_FUNCTION__];
 #if DEBUG && 0
 #  warning REMOVE THAT ABORT IN PRODUCTION CODE !!!
     abort();
@@ -95,7 +98,7 @@ static NSMutableArray *activeApps = nil; // THREAD
   unsigned idx;
   
   if (WOApp != self) {
-    [self logWithFormat:
+    [self warnWithFormat:
             @"tried to deactivate inactive application !\n"
             @"  self:   %@\n"
             @"  active: %@",
@@ -118,13 +121,17 @@ static NSMutableArray *activeApps = nil; // THREAD
     properly if this is called first.
   */
   static BOOL didInit = NO;
-  NSUserDefaults *ud;
+  NSUserDefaults  *ud;
+  NGLoggerManager *lm;
   if (didInit) return;
   didInit = YES;
   [self _initDefaults];
-  
-  ud = [NSUserDefaults standardUserDefaults];
-  perflog          = [ud boolForKey:@"WOProfileApplication"];
+
+  lm         = [NGLoggerManager defaultLoggerManager];
+  logger     = [lm loggerForClass:self];
+  perfLogger = [lm loggerForDefaultKey:@"WOProfileApplication"];
+
+  ud               = [NSUserDefaults standardUserDefaults];
   outputValidateOn = [ud boolForKey:@"WOOutputValidationEnabled"];
   NSDateClass      = [NSDate class];
 }
@@ -184,6 +191,16 @@ static NSMutableArray *activeApps = nil; // THREAD
   [super dealloc];
 }
 
+/* NGLogging */
+
++ (id)logger {
+  return logger;
+}
+
+- (id)logger {
+  return logger;
+}
+
 /* signals */
 
 - (void)processHupSignal:(int)_signal {
@@ -238,7 +255,7 @@ static NSMutableArray *activeApps = nil; // THREAD
 
   adaptorClass = NSClassFromString(_name);
   if (adaptorClass == Nil) {
-    [self logWithFormat:@"ERROR: did not find adaptor class %@", _name];
+    [self errorWithFormat:@"did not find adaptor class %@", _name];
     return nil;
   }
 
@@ -285,21 +302,23 @@ static NSMutableArray *activeApps = nil; // THREAD
   WOResponse *r  = nil;
   
   if ([self respondsToSelector:@selector(handleException:)]) {
-    NSLog(@"WARNING: calling deprecated -handleException method !");
+    [self warnWithFormat:@"calling deprecated -handleException method !"];
     return [self handleException:_exc];
   }
   
 #if 0 && DEBUG
-  [self logWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
+  [self errorWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
   abort();
 #endif
   
   if (_ctx == nil) {
-    [self logWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
+    [self errorWithFormat:@"%@: caught (without context):\n  %@.",
+      self, _exc];
     [self terminate];
   }
   else if (rq == nil) {
-    [self logWithFormat:@"%@: caught (without request):\n  %@.", self, _exc];
+    [self errorWithFormat:@"%@: caught (without request):\n  %@.",
+      self, _exc];
     [self terminate];
   }
   else {
@@ -309,7 +328,7 @@ static NSMutableArray *activeApps = nil; // THREAD
   }
   
   if ((r = [WOResponse responseWithRequest:rq]) == nil)
-    [self logWithFormat:@"could not create response !"];
+    [self warnWithFormat:@"could not create response !"];
     
   [r setStatus:500];
   return r;
@@ -421,7 +440,7 @@ static NSMutableArray *activeApps = nil; // THREAD
         adaptor = [self adaptorWithName:[moreAdaptors objectAtIndex:i]
                         arguments:nil];
         if (adaptor == nil) {
-          [self logWithFormat:@"could not find WOAdaptor '%@' !",
+          [self warnWithFormat:@"could not find WOAdaptor '%@' !",
                 [moreAdaptors objectAtIndex:i]];
           continue;
         }
@@ -565,8 +584,8 @@ static NSMutableArray *activeApps = nil; // THREAD
   static BOOL didWarn = NO;
 
   if (!didWarn) {
-    [self logWithFormat:
-            @"WARNING: output validation is enabled, this will "
+    [self warnWithFormat:
+            @"output validation is enabled, this will "
             @"slow down request processing!"];
     didWarn = YES;
   }
@@ -602,7 +621,7 @@ static NSMutableArray *activeApps = nil; // THREAD
       if ([self hideValidationIssue:issue])
         continue;
       [self logWithFormat:@"validate-output[%@]: %@",
-             [(NSException *)issue name], [issue reason]];
+              [(NSException *)issue name], [issue reason]];
       continue;
     }
     
@@ -622,24 +641,24 @@ static NSMutableArray *activeApps = nil; // THREAD
   WOResponse     *response = nil;
   NSTimeInterval startDispatch = 0.0;
   
-  if (perflog)
+  if (perfLogger)
     startDispatch = [[NSDateClass date] timeIntervalSince1970];
   
   /* let request handler process the request */
   {
     NSTimeInterval startDispatch = 0.0;
     
-    if (perflog)
+    if (perfLogger)
       startDispatch = [[NSDateClass date] timeIntervalSince1970];
     
     /* the call ;-) */
     response = [handler handleRequest:_request];
     
-    if (perflog) {
+    if (perfLogger) {
       NSTimeInterval rt;
       rt = [[NSDateClass date] timeIntervalSince1970] - startDispatch;
-      NSLog(@"  [woapp-rq]: request handler took %4.3fs.",
-            rt < 0.0 ? -1.0 : rt);
+      [perfLogger logWithFormat:@"[woapp-rq]: request handler took %4.3fs.",
+                                    rt < 0.0 ? -1.0 : rt];
     }
   }
   
@@ -648,11 +667,11 @@ static NSMutableArray *activeApps = nil; // THREAD
   if (outputValidateOn)
     [self validateOutputOfResponse:response];
   
-  if (perflog) {
+  if (perfLogger) {
     NSTimeInterval rt;
     rt = [[NSDateClass date] timeIntervalSince1970] - startDispatch;
-    NSLog(@"[woapp]: dispatchRequest took %4.3fs.",
-          rt < 0.0 ? -1.0 : rt);
+    [perfLogger logWithFormat:@"[woapp]: dispatchRequest took %4.3fs.",
+                  rt < 0.0 ? -1.0 : rt];
   }
   
   return response;
@@ -661,14 +680,14 @@ static NSMutableArray *activeApps = nil; // THREAD
   WORequestHandler *handler;
   
   if ([self respondsToSelector:@selector(handleRequest:)]) {
-    [self logWithFormat:
-            @"WARNING: calling deprecated -handleRequest: method .."];
+    [self warnWithFormat:
+            @"calling deprecated -handleRequest: method .."];
     return [self handleRequest:_request];
   }
   
   /* find request handler for request */
   if ((handler = [self handlerForRequest:_request]) == nil) {
-    [self logWithFormat:@"ERROR: got no request handler for request: %@ !",
+    [self errorWithFormat:@"got no request handler for request: %@ !",
            _request];
     return nil;
   }
@@ -685,79 +704,67 @@ static NSMutableArray *activeApps = nil; // THREAD
                    ];
 }
 
-@end /* WOCoreApplication */
+/* defaults */
 
-@implementation WOCoreApplication(Defaults)
++ (int)sopeMajorVersion {
+  return SOPE_MAJOR_VERSION;
+}
++ (int)sopeMinorVersion {
+  return SOPE_MINOR_VERSION;
+}
++ (NSString *)ngobjwebShareDirectorySubPath {
+  return [NSString stringWithFormat:@"share/sope-%i.%i/ngobjweb/",
+                     [self sopeMajorVersion], [self sopeMinorVersion]];
+}
++ (NGResourceLocator *)ngobjwebResourceLocator {
+  return [NGResourceLocator resourceLocatorForGNUstepPath:
+                              @"Library/Libraries/Resources/NGObjWeb"
+                            fhsPath:[self ngobjwebShareDirectorySubPath]];
+}
 
-+ (void)_initDefaults {
-  static BOOL didInit = NO;
-#if !COMPILE_AS_FRAMEWORK
-  NSFileManager *fm;
-  NSDictionary  *env;
-  NSString      *relPath;
-#else
++ (NSArray *)resourcesSearchPathes {
+  // TODO: is this actually used somewhere?
+  return [[self ngobjwebResourceLocator] searchPathes];
+}
+
++ (NSString *)findNGObjWebResource:(NSString *)_name ofType:(NSString *)_ext {
+#if COMPILE_AS_FRAMEWORK
   NSBundle *bundle;
+  
+  bundle = [NSBundle bundleForClass:[WOCoreApplication class]];
+  return [bundle pathForResource:_name ofType:_ext];
+#else
+  return [[self ngobjwebResourceLocator] lookupFileWithName:_name 
+                                         extension:_ext];
 #endif
-  NSDictionary  *owDefaults = nil;
-  NSString      *apath;
+}
+
++ (void)_initDefaults {
+  static BOOL didInit = NO;
+  NSDictionary *owDefaults = nil;
+  NSString     *apath;
   
   if (didInit) return;
   didInit = YES;
   
-#if COMPILE_AS_FRAMEWORK
-  bundle = [NSBundle bundleForClass:[WOCoreApplication class]];
-  apath  = [bundle pathForResource:@"Defaults" ofType:@"plist"];
-      
-  if (apath == nil) {
-    NSLog(@"ERROR: cannot find Defaults.plist "
-              @"resource of NGObjWeb framework (%@) !", bundle);
-  }
-#else
-  fm   = [NSFileManager defaultManager];
-  env  = [[NSProcessInfo processInfo] environment];
-
-  /* 
-       TODO: the following is a dirty hack because GNUstep people decided
-             to change the directory structure. SIGH!
-  */
-#if APPLE_FOUNDATION_LIBRARY || NeXT_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY
-  relPath = @"Library/Libraries";
-#else    
-  relPath = @"Libraries";
-#endif
-  relPath = [relPath stringByAppendingPathComponent:@"Resources"];
-  relPath = [relPath stringByAppendingPathComponent:@"NGObjWeb"];
-  relPath = [relPath stringByAppendingPathComponent:@"Defaults.plist"];
-  
-  apath = [env objectForKey:@"GNUSTEP_USER_ROOT"];
-  apath = [apath stringByAppendingPathComponent:relPath];
-  if (![fm fileExistsAtPath:apath]) {
-      apath = [env objectForKey:@"GNUSTEP_LOCAL_ROOT"];
-      apath = [apath stringByAppendingPathComponent:relPath];
-  }
-  if (![fm fileExistsAtPath:apath]) {
-    apath = [env objectForKey:@"GNUSTEP_SYSTEM_ROOT"];
-    apath = [apath stringByAppendingPathComponent:relPath];
-  }
-  if (![fm fileExistsAtPath:apath]) {
-    apath = relPath;
-  }
-  if (![fm fileExistsAtPath:apath]) {
-    NSLog(@"ERROR: cannot find Defaults.plist "
-         @"resource of NGObjWeb library !");
-  }
+  apath = [self findNGObjWebResource:@"Defaults" ofType:@"plist"];
+  if (apath == nil)
+    [self errorWithFormat:@"cannot find Defaults.plist resource of NGObjWeb library!"];
+#if HEAVY_DEBUG
+  else
+    [self debugWithFormat:@"Note: loading default defaults: %@", apath];
 #endif
-  owDefaults = [NSDictionary dictionaryWithContentsOfFile:apath];
   
+  owDefaults = [NSDictionary dictionaryWithContentsOfFile:apath];
   if (owDefaults) {
     [[NSUserDefaults standardUserDefaults] registerDefaults:owDefaults];
 #if HEAVY_DEBUG
-    [self logWithFormat:@"did register NGObjWeb defaults: %@\n%@", 
-           apath, owDefaults];
+    [self debugWithFormat:@"did register NGObjWeb defaults: %@\n%@", 
+            apath, owDefaults];
 #endif
   }
   else {
-    [self logWithFormat:@"ERROR: could not load NGObjWeb defaults: '%@'",
+    [self errorWithFormat:@"could not load NGObjWeb defaults: '%@'",
            apath];
   }
 }
@@ -794,13 +801,16 @@ static NSMutableArray *activeApps = nil; // THREAD
   woport = [[self userDefaults] objectForKey:@"WOPort"];
   if ([woport isKindOfClass:[NSNumber class]])
     return woport;
-  
   woport = [woport stringValue];
+  if ([woport length] > 0 && isdigit([woport characterAtIndex:0]))
+    return [NSNumber numberWithInt:[woport intValue]];
+  
   addr   = NGSocketAddressFromString(woport);
   
   if ([addr isKindOfClass:[NGInternetSocketAddress class]])
     return [NSNumber numberWithInt:[(NGInternetSocketAddress *)addr port]];
-  
+
+  [self fatalWithFormat:@"GOT NO PORT FOR ADDR: %@ (%@)", addr, woport];
   return nil;
 }
 
@@ -828,4 +838,4 @@ static NSMutableArray *activeApps = nil; // THREAD
   return s;
 }
 
-@end /* WOCoreApplication(Defaults) */
+@end /* WOCoreApplication */