summaryrefslogtreecommitdiff
path: root/jvmfwk/plugins/sunmajor/pluginlib/util_cocoa.mm
blob: 5a5d7303a18a3b0dbbab4591fa62dcd450e82079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <rtl/ustring.hxx>

#include <premac.h>
#import <Foundation/Foundation.h>
#include <postmac.h>

#import "util_cocoa.hxx"

using namespace rtl;

bool JvmfwkUtil_isLoadableJVM( OUString const & aURL )
{
    bool bRet = false;

    if ( aURL.getLength() )
    {
        NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];

        NSString *pString = [NSString stringWithCharacters:reinterpret_cast<unichar const *>(aURL.getStr()) length:aURL.getLength()];
        if ( pString )
        {
            NSURL *pURL = nil;

            // Ignore all but Oracle's JDK as loading Apple's Java and Oracle's
            // JRE will cause OS X's JavaVM framework to display a dialog and
            // invoke exit() when loaded via JNI on OS X 10.10
            NSURL *pTmpURL = [NSURL URLWithString:pString];
            if ( pTmpURL )
                pTmpURL = [pTmpURL filePathURL];
            if ( pTmpURL )
                pTmpURL = [pTmpURL URLByStandardizingPath];
            if ( pTmpURL )
                pTmpURL = [pTmpURL URLByResolvingSymlinksInPath];
            if ( pTmpURL )
            {
                NSURL *pJVMsDirURL = [NSURL URLWithString:@"file:///Library/Java/JavaVirtualMachines/"];
                if ( pJVMsDirURL )
                    pJVMsDirURL= [pJVMsDirURL filePathURL];
                if ( pJVMsDirURL )
                    pJVMsDirURL = [pJVMsDirURL URLByStandardizingPath];
                // The JVM directory must not contain softlinks or the JavaVM
                // framework bug will occur so don't resolve softlinks in the
                // JVM directory
                if ( pJVMsDirURL )
                {
                    NSString *pTmpURLString = [pTmpURL absoluteString];
                    NSString *pJVMsDirURLString = [pJVMsDirURL absoluteString];
                    if ( pTmpURLString && pJVMsDirURLString && [pJVMsDirURLString length] )
                    {
                        NSRange aJVMsDirURLRange = [pTmpURLString rangeOfString:pJVMsDirURLString];
                        if ( !aJVMsDirURLRange.location && aJVMsDirURLRange.length )
                            pURL = pTmpURL;
                    }
                }
            }

            while ( pURL )
            {
                // Check if this is a valid bundle
                NSNumber *pDir = nil;
                NSURL *pContentsURL = [pURL URLByAppendingPathComponent:@"Contents"];
                if ( pContentsURL && [pContentsURL getResourceValue:&pDir forKey:NSURLIsDirectoryKey error:nil] && pDir && [pDir boolValue] )
                {
                    NSBundle *pBundle = [NSBundle bundleWithURL:pURL];
                    if ( pBundle )
                    {
                        // Make sure that this bundle's Info.plist has the
                        // proper JVM keys to supports loading via JNI. If
                        // this bundle is a valid JVM and these keys
                        // are missing, loading the JVM will cause OS X's
                        // JavaVM framework to display a dialog and invoke
                        // exit() when loaded via JNI on OS X 10.10.
                        NSDictionary *pInfo = [pBundle infoDictionary];
                        if ( pInfo )
                        {
                            NSDictionary *pJavaVM = [pInfo objectForKey:@"JavaVM"];
                            if ( pJavaVM && [pJavaVM isKindOfClass:[NSDictionary class]] )
                            {
                                NSArray *pJVMCapabilities = [pJavaVM objectForKey:@"JVMCapabilities"];
                                if ( pJVMCapabilities )
                                {
                                    if ( [pJVMCapabilities indexOfObjectIdenticalTo:@"JNI"] == NSNotFound )
                                    {
                                        if ( [pJVMCapabilities isKindOfClass:[NSMutableArray class]] )
                                        {
                                            [static_cast<NSMutableArray *>(pJVMCapabilities) addObject:@"JNI"];
                                            bRet = true;
                                        }
                                        else if ( [pJavaVM isKindOfClass:[NSMutableDictionary class]] )
                                        {
                                            NSMutableArray *pNewJVMCapabilities = [NSMutableArray arrayWithCapacity:[pJVMCapabilities count] + 1];
                                            if ( pNewJVMCapabilities )
                                            {
                                                [pNewJVMCapabilities addObject:@"JNI"];
                                                [static_cast<NSMutableDictionary *>(pJavaVM) setObject:pNewJVMCapabilities forKey:@"JVMCapabilities"];
                                                bRet = true;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        bRet = true;
                                    }
                                }
                            }
                        }
                    }
                }

                NSURL *pOldURL = pURL;
                pURL = [pURL URLByDeletingLastPathComponent];
                if ( pURL )
                {
                    pURL = [pURL URLByStandardizingPath];
                    if ( pURL )
                    {
                        pURL = [pURL URLByResolvingSymlinksInPath];
                        if ( pURL && [pURL isEqual:pOldURL] )
                            pURL = nil;
                    }
                }
            }
        }

        [pPool release];
    }

    return bRet;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */