jdk/src/macosx/native/com/apple/resources/MacOSXResourceBundle.m
changeset 20833 a6aa4123f21b
parent 20832 27336e07209c
child 20834 a7d09d76cfa5
equal deleted inserted replaced
20832:27336e07209c 20833:a6aa4123f21b
     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 <dlfcn.h>
       
    27 #import <Cocoa/Cocoa.h>
       
    28 #import <JavaNativeFoundation/JavaNativeFoundation.h>
       
    29 
       
    30 #ifndef MAXPATHLEN
       
    31 #define MAXPATHLEN PATH_MAX
       
    32 #endif
       
    33 
       
    34 static jboolean
       
    35 GetPathFromCurrentBinary(char *buf, jint bufsize)
       
    36 {
       
    37     Dl_info dlinfo;
       
    38     dladdr((void *)GetPathFromCurrentBinary, &dlinfo);
       
    39     if (realpath(dlinfo.dli_fname, buf) == NULL) {
       
    40 //      fprintf(stderr, "Error: realpath(`%s') failed.\n", dlinfo.dli_fname);
       
    41         return JNI_FALSE;
       
    42     }
       
    43 
       
    44     const char *libawt = "lib/libawt.dylib";
       
    45     int strLen, libawtLen;
       
    46 
       
    47     strLen = strlen(buf);
       
    48     libawtLen = strlen(libawt);
       
    49 
       
    50     if (strLen < libawtLen ||
       
    51         strcmp(buf + strLen - libawtLen, libawt) != 0) {
       
    52         return JNI_FALSE;
       
    53     }
       
    54 
       
    55     buf[strLen - libawtLen] = '\0';
       
    56 
       
    57     return JNI_TRUE;
       
    58 }
       
    59 
       
    60 #define JAVA_DLL "libjava.dylib"
       
    61 
       
    62 static jboolean
       
    63 GetJREPath(char *buf, jint bufsize)
       
    64 {
       
    65     /* try to get the path from the current binary, if not, bail to the framework */
       
    66     if (GetPathFromCurrentBinary(buf, bufsize) == JNI_TRUE) {
       
    67         /* does the rest of the JRE exist? */
       
    68         char libjava[MAXPATHLEN];
       
    69         snprintf(libjava, MAXPATHLEN, "%s/lib/" JAVA_DLL, buf);
       
    70         if (access(libjava, F_OK) == 0) {
       
    71             return JNI_TRUE;
       
    72         }
       
    73     }
       
    74 
       
    75     return JNI_FALSE;
       
    76 }
       
    77 
       
    78 static NSString *getRunningJavaBundle()
       
    79 {
       
    80     char path[MAXPATHLEN];
       
    81     GetJREPath(path, MAXPATHLEN);
       
    82     return [[NSString alloc] initWithFormat:@"%@/bundle", [NSString stringWithUTF8String:path]];
       
    83 }
       
    84 
       
    85 /*
       
    86  * Class:     com_apple_resources_LoadNativeBundleAction
       
    87  * Method:    getPathToBundleFile
       
    88  * Signature: (Ljava/lang/String)Ljava/lang/String;
       
    89  */
       
    90 JNIEXPORT jstring JNICALL
       
    91 Java_com_apple_resources_LoadNativeBundleAction_getPathToBundleFile
       
    92     (JNIEnv *env, jclass klass, jstring filename)
       
    93 {
       
    94     jstring returnVal = NULL;
       
    95     if (filename == NULL) {
       
    96         return NULL;
       
    97     }
       
    98 
       
    99 JNF_COCOA_ENTER(env);
       
   100     NSBundle *javaBundle = [NSBundle bundleWithPath:getRunningJavaBundle()];
       
   101     NSString *baseFilename = JNFJavaToNSString(env, filename);
       
   102     NSString *propertyFilePath = [javaBundle pathForResource:baseFilename ofType:@"properties"];
       
   103 
       
   104     if (propertyFilePath != nil) {
       
   105         returnVal = JNFNSToJavaString(env, propertyFilePath);
       
   106     }
       
   107 JNF_COCOA_EXIT(env);
       
   108 
       
   109     return returnVal;
       
   110 }