1 /*
2  * Copyright (c) 2020, 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 
27 #ifndef JvmLauncher_h
28 #define JvmLauncher_h
29 
30 
31 #include "jni.h" /* JNIEXPORT */
32 
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef struct {
39     const char* jliLibPath;
40     int jliLaunchArgc;
41     char** jliLaunchArgv;
42 } JvmlLauncherData;
43 
44 typedef void* JvmlLauncherHandle;
45 
46 typedef void (*JvmlLauncherAPI_CloseHandleFunc)(JvmlLauncherHandle);
47 typedef int (*JvmlLauncherAPI_GetJvmlLauncherDataSizeFunc)(JvmlLauncherHandle);
48 typedef JvmlLauncherData* (*JvmlLauncherAPI_InitJvmlLauncherDataFunc)(JvmlLauncherHandle, void*, int);
49 
50 typedef struct {
51     JvmlLauncherAPI_CloseHandleFunc closeHandle;
52     JvmlLauncherAPI_GetJvmlLauncherDataSizeFunc getJvmlLauncherDataSize;
53     JvmlLauncherAPI_InitJvmlLauncherDataFunc initJvmlLauncherData;
54 } JvmlLauncherAPI;
55 
56 typedef JvmlLauncherAPI* (*JvmlLauncherAPI_GetAPIFunc)(void);
57 
58 JNIEXPORT JvmlLauncherAPI* jvmLauncherGetAPI(void);
59 
jvmLauncherCloseHandle(JvmlLauncherAPI * api,JvmlLauncherHandle h)60 static inline void jvmLauncherCloseHandle(JvmlLauncherAPI* api, JvmlLauncherHandle h) {
61     (*api->closeHandle)(h);
62 }
63 
jvmLauncherGetJvmlLauncherDataSize(JvmlLauncherAPI * api,JvmlLauncherHandle h)64 static inline int jvmLauncherGetJvmlLauncherDataSize(JvmlLauncherAPI* api,
65                                                         JvmlLauncherHandle h) {
66     return (*api->getJvmlLauncherDataSize)(h);
67 }
68 
jvmLauncherInitJvmlLauncherData(JvmlLauncherAPI * api,JvmlLauncherHandle h,void * ptr,int bufferSize)69 static inline JvmlLauncherData* jvmLauncherInitJvmlLauncherData(JvmlLauncherAPI* api,
70                             JvmlLauncherHandle h, void* ptr, int bufferSize) {
71     return (*api->initJvmlLauncherData)(h, ptr, bufferSize);
72 }
73 
74 JvmlLauncherData* jvmLauncherCreateJvmlLauncherData(JvmlLauncherAPI* api, JvmlLauncherHandle h);
75 int jvmLauncherStartJvm(JvmlLauncherData* jvmArgs, void* JLI_Launch);
76 
77 void jvmLauncherLog(const char* format, ...);
78 
79 #define JP_LOG_ERRMSG(msg) do { jvmLauncherLog((msg)); } while (0)
80 #define JP_LOG_ERRNO JP_LOG_ERRMSG(strerror(errno))
81 #define JP_LOG_TRACE jvmLauncherLog
82 
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 
89 #ifdef __cplusplus
90 
91 #include "tstrings.h"
92 
93 class CfgFile;
94 
95 
96 class Jvm {
97 public:
98     Jvm();
99     ~Jvm();
100 
101     Jvm& initFromConfigFile(const CfgFile& cfgFile);
102 
addArgument(const tstring & value)103     Jvm& addArgument(const tstring& value) {
104         args.push_back(value);
105         return *this;
106     }
107 
setPath(const tstring & v)108     Jvm& setPath(const tstring& v) {
109         jvmPath = v;
110         return *this;
111     }
112 
getPath()113     tstring getPath() const {
114         return jvmPath;
115     }
116 
117     bool isWithSplash() const;
118 
119     void launch();
120 
121     JvmlLauncherHandle exportLauncher() const;
122 
123 private:
124     tstring jvmPath;
125     tstring_array args;
126 };
127 
128 #endif // #ifdef __cplusplus
129 
130 #endif // JvmLauncher_h
131