1 /*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates.
3 * All rights reserved. Use is subject to license terms.
4 *
5 * This file is available and licensed under the following license:
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
16 * - Neither the name of Oracle Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34 #include "Platform.h"
35
36 #ifdef MAC
37
38 #include "MacPlatform.h"
39 #include "Helpers.h"
40 #include "Package.h"
41 #include "PropertyFile.h"
42 #include "IniFile.h"
43
44 #include <sys/sysctl.h>
45 #include <pthread.h>
46 #include <vector>
47
48 #import <Foundation/Foundation.h>
49
50 #include <CoreFoundation/CoreFoundation.h>
51 #include <CoreFoundation/CFString.h>
52
53 #ifdef __OBJC__
54 #import <Cocoa/Cocoa.h>
55 #endif //__OBJC__
56
57 //--------------------------------------------------------------------------------------------------
58
59 NSString* StringToNSString(TString Value) {
60 NSString* result = [NSString stringWithCString:Value.c_str()
61 encoding:[NSString defaultCStringEncoding]];
62 return result;
63 }
64
65 //--------------------------------------------------------------------------------------------------
66
67 MacPlatform::MacPlatform(void) : Platform(), GenericPlatform(), PosixPlatform() {
68 }
69
70 MacPlatform::~MacPlatform(void) {
71 }
72
73 bool MacPlatform::UsePListForConfigFile() {
74 return FilePath::FileExists(GetConfigFileName()) == false;
75 }
76
77 void MacPlatform::ShowMessage(TString Title, TString Description) {
78 NSString *ltitle = StringToNSString(Title);
79 NSString *ldescription = StringToNSString(Description);
80
81 NSLog(@"%@:%@", ltitle, ldescription);
82 }
83
84 void MacPlatform::ShowMessage(TString Description) {
85 TString appname = GetModuleFileName();
86 appname = FilePath::ExtractFileName(appname);
87 ShowMessage(appname, Description);
88 }
89
90
91 TCHAR* MacPlatform::ConvertStringToFileSystemString(TCHAR* Source, bool &release) {
92 TCHAR* result = NULL;
93 release = false;
94 CFStringRef StringRef = CFStringCreateWithCString(kCFAllocatorDefault, Source, kCFStringEncodingUTF8);
95
96 if (StringRef != NULL) {
97 @try {
98 CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(StringRef);
99 result = new char[length + 1];
100
101 if (CFStringGetFileSystemRepresentation(StringRef, result, length)) {
102 release = true;
103 }
104 else {
105 delete[] result;
106 result = NULL;
107 }
108 }
109 @finally {
322 std::map<TString, TString> MacPlatform::GetKeys() {
323 std::map<TString, TString> keys;
324
325 if (UsePListForConfigFile() == false) {
326 return GenericPlatform::GetKeys();
327 }
328 else {
329 keys.insert(std::map<TString, TString>::value_type(CONFIG_VERSION, _T("app.version")));
330 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY, _T("JVMMainJarName")));
331 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINMODULE_KEY, _T("JVMMainModuleName")));
332 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY, _T("JVMMainClassName")));
333 keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY, _T("JVMAppClasspath")));
334 keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY, _T("CFBundleName")));
335 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY, _T("JVMPreferencesID")));
336 keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY, _T("JVMRuntime")));
337 keys.insert(std::map<TString, TString>::value_type(PACKAGER_APP_DATA_DIR, _T("CFBundleIdentifier")));
338
339 keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY, _T("app.splash")));
340 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY, _T("app.memory")));
341 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_DEBUG, _T("app.debug")));
342
343 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPLICATION, _T("Application")));
344 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMOPTIONS, _T("JVMOptions")));
345 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROPTIONS, _T("JVMUserOptions")));
346 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROVERRIDESOPTIONS, _T("JVMUserOverrideOptions")));
347 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSJVMOPTIONS, _T("AppCDSJVMOptions")));
348 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS, _T("AppCDSGenerateCacheJVMOptions")));
349 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_ARGOPTIONS, _T("ArgOptions")));
350 }
351
352 return keys;
353 }
354
355 #ifdef DEBUG
356 bool MacPlatform::IsNativeDebuggerPresent() {
357 int state;
358 int mib[4];
359 struct kinfo_proc info;
360 size_t size;
361
|
1 /*
2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
3 * All rights reserved. Use is subject to license terms.
4 *
5 * This file is available and licensed under the following license:
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
16 * - Neither the name of Oracle Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34 #include "Platform.h"
35
36 #ifdef MAC
37
38 #include "MacPlatform.h"
39 #include "Helpers.h"
40 #include "Package.h"
41 #include "PropertyFile.h"
42 #include "IniFile.h"
43
44 #include <sys/sysctl.h>
45 #include <pthread.h>
46 #include <vector>
47
48 #import <Foundation/Foundation.h>
49 #import <AppKit/NSRunningApplication.h>
50
51 #include <CoreFoundation/CoreFoundation.h>
52 #include <CoreFoundation/CFString.h>
53
54 #ifdef __OBJC__
55 #import <Cocoa/Cocoa.h>
56 #endif //__OBJC__
57
58 #define MAC_PACKAGER_TMP_DIR "/Library/Application Support/Oracle/Java/Packager/tmp"
59
60 //--------------------------------------------------------------------------------------------------
61
62 NSString* StringToNSString(TString Value) {
63 NSString* result = [NSString stringWithCString:Value.c_str()
64 encoding:[NSString defaultCStringEncoding]];
65 return result;
66 }
67
68 //--------------------------------------------------------------------------------------------------
69
70 MacPlatform::MacPlatform(void) : Platform(), GenericPlatform(), PosixPlatform() {
71 }
72
73 MacPlatform::~MacPlatform(void) {
74 }
75
76 bool MacPlatform::UsePListForConfigFile() {
77 return FilePath::FileExists(GetConfigFileName()) == false;
78 }
79
80 void MacPlatform::ShowMessage(TString Title, TString Description) {
81 NSString *ltitle = StringToNSString(Title);
82 NSString *ldescription = StringToNSString(Description);
83
84 NSLog(@"%@:%@", ltitle, ldescription);
85 }
86
87 void MacPlatform::ShowMessage(TString Description) {
88 TString appname = GetModuleFileName();
89 appname = FilePath::ExtractFileName(appname);
90 ShowMessage(appname, Description);
91 }
92
93 const char* MacPlatform::getTmpDirString() {
94 return MAC_PACKAGER_TMP_DIR;
95 }
96
97 void MacPlatform::reactivateAnotherInstance() {
98 if (singleInstanceProcessId == 0) {
99 printf("Unable to reactivate another instance, PID is undefined");
100 return;
101 }
102 NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: singleInstanceProcessId];
103 if (app != nil) {
104 [app activateWithOptions: NSApplicationActivateIgnoringOtherApps];
105 } else {
106 printf("Unable to reactivate another instance PID: %d", singleInstanceProcessId);
107 }
108 }
109
110 TCHAR* MacPlatform::ConvertStringToFileSystemString(TCHAR* Source, bool &release) {
111 TCHAR* result = NULL;
112 release = false;
113 CFStringRef StringRef = CFStringCreateWithCString(kCFAllocatorDefault, Source, kCFStringEncodingUTF8);
114
115 if (StringRef != NULL) {
116 @try {
117 CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(StringRef);
118 result = new char[length + 1];
119
120 if (CFStringGetFileSystemRepresentation(StringRef, result, length)) {
121 release = true;
122 }
123 else {
124 delete[] result;
125 result = NULL;
126 }
127 }
128 @finally {
341 std::map<TString, TString> MacPlatform::GetKeys() {
342 std::map<TString, TString> keys;
343
344 if (UsePListForConfigFile() == false) {
345 return GenericPlatform::GetKeys();
346 }
347 else {
348 keys.insert(std::map<TString, TString>::value_type(CONFIG_VERSION, _T("app.version")));
349 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY, _T("JVMMainJarName")));
350 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINMODULE_KEY, _T("JVMMainModuleName")));
351 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY, _T("JVMMainClassName")));
352 keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY, _T("JVMAppClasspath")));
353 keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY, _T("CFBundleName")));
354 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY, _T("JVMPreferencesID")));
355 keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY, _T("JVMRuntime")));
356 keys.insert(std::map<TString, TString>::value_type(PACKAGER_APP_DATA_DIR, _T("CFBundleIdentifier")));
357
358 keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY, _T("app.splash")));
359 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY, _T("app.memory")));
360 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_DEBUG, _T("app.debug")));
361 keys.insert(std::map<TString, TString>::value_type(CONFIG_APPLICATION_INSTANCE, _T("app.application.instance")));
362
363 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPLICATION, _T("Application")));
364 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMOPTIONS, _T("JVMOptions")));
365 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROPTIONS, _T("JVMUserOptions")));
366 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROVERRIDESOPTIONS, _T("JVMUserOverrideOptions")));
367 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSJVMOPTIONS, _T("AppCDSJVMOptions")));
368 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS, _T("AppCDSGenerateCacheJVMOptions")));
369 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_ARGOPTIONS, _T("ArgOptions")));
370 }
371
372 return keys;
373 }
374
375 #ifdef DEBUG
376 bool MacPlatform::IsNativeDebuggerPresent() {
377 int state;
378 int mib[4];
379 struct kinfo_proc info;
380 size_t size;
381
|