1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <Cocoa/Cocoa.h>
  27 
  28 #import "JVMArgs.h"
  29 #import "JavaAppLauncher.h"
  30 
  31 
  32 static void dummyTimer(CFRunLoopTimerRef timer, void *info) {}
  33 static void ParkEventLoop() {
  34     // RunLoop needs at least one source, and 1e20 is pretty far into the future
  35     CFRunLoopTimerRef t = CFRunLoopTimerCreate(kCFAllocatorDefault, 1.0e20, 0.0, 0, 0, dummyTimer, NULL);
  36     CFRunLoopAddTimer(CFRunLoopGetCurrent(), t, kCFRunLoopDefaultMode);
  37     CFRelease(t);
  38 
  39     // Park this thread in the main run loop.
  40     int32_t result;
  41     do {
  42         result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e20, false);
  43     } while (result != kCFRunLoopRunFinished);
  44 }
  45 
  46 int main(int argc, char *argv[]) {
  47     NSAutoreleasePool *pool = [NSAutoreleasePool new];
  48 
  49     @try {
  50         NSBundle *mainBundle = [NSBundle mainBundle];
  51 
  52         // pick apart the Info.plist, and release all the temporary objects
  53         NSAutoreleasePool *argParsingPool = [NSAutoreleasePool new];
  54         JVMArgs *args = [JVMArgs jvmArgsForBundle:mainBundle argc:argc argv:argv];
  55         JavaAppLauncher *launcher = [JavaAppLauncher new];
  56         launcher.args = args;
  57         [argParsingPool drain];
  58 
  59         // kick off a new thread to instantiate the JVM on
  60         NSThread *thread = [[NSThread alloc] initWithTarget:launcher selector:@selector(findAndLoadJVM) object:nil];
  61         struct rlimit limit;
  62         int err = getrlimit(RLIMIT_STACK, &limit);
  63         if (err == 0 && limit.rlim_cur != 0LL) {
  64             [thread setStackSize:limit.rlim_cur];
  65         }
  66         [thread start];
  67         [thread release];
  68 
  69         [launcher release];
  70 
  71         ParkEventLoop();
  72 
  73     } @catch (NSException *e) {
  74         NSLog(@"%@: %@", e, [e callStackSymbols]);
  75     }
  76 
  77     [pool drain];
  78 
  79     return 0;
  80 }