-
Notifications
You must be signed in to change notification settings - Fork 16
/
platformlauncher.cpp
267 lines (234 loc) · 9 KB
/
platformlauncher.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009-2012 JRuby Team (www.jruby.org).
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*
* Author: Tomas Holy
*/
#include <stdio.h>
#include "utilsfuncs.h"
#include "platformlauncher.h"
#include "rb_w32_cmdvector.h"
using namespace std;
extern "C" int nailgunClientMain(int argc, char *argv[], char *env[]);
PlatformLauncher::PlatformLauncher()
: ArgParser()
, suppressConsole(false)
{
separateProcess = false;
}
PlatformLauncher::PlatformLauncher(const PlatformLauncher& orig)
: ArgParser(orig)
{
}
PlatformLauncher::~PlatformLauncher() {
}
list<string>* GetEnvStringsAsList() {
char * env = GetEnvironmentStrings();
list<string> * envList = new list<string>();
while (*env) {
envList->push_back(env);
env = env + strlen(env) + 1;
}
return envList;
}
bool PlatformLauncher::start(char* argv[], int argc, DWORD *retCode, const char* binaryName) {
// subvert cmd.exe's feeble attempt at command line parsing,
// the code is taken from MRI
argc = rb_w32_cmdvector(GetCommandLine(), &argv);
// remove the first argument ('jruby')
argc -= 1;
argv += 1;
platformDir = binaryName;
checkLoggingArg(argc, argv, false);
if (!initPlatformDir()
|| !parseArgs(argc, argv)
|| !checkJDKHome()) {
return false;
}
disableFolderVirtualization(GetCurrentProcess());
if (nailgunClient) {
progArgs.push_front("org.jruby.util.NailMain");
char ** nailArgv = convertToArgvArray(progArgs);
if (printCommandLine) {
printListToConsole(progArgs);
return true;
}
list<string>* envList = GetEnvStringsAsList();
char ** nailEnv = convertToArgvArray(*envList);
nailgunClientMain(progArgs.size(), nailArgv, nailEnv);
return true;
}
if (binaryName) {
// clean up the binaryName first,
// remove '.exe' from the end, and the possible path.
string bn = binaryName;
size_t found = bn.find_last_of("/\\");
if (found != string::npos) {
logMsg("The binary name contains slashes, will remove: %s", binaryName);
bn = bn.substr(found + 1);
binaryName = bn.c_str();
}
found = bn.find(".exe", bn.length() - 4);
if (found != string::npos) {
bn.erase(found, 4);
binaryName = bn.c_str();
logMsg("*** Cleaned up the binary name: %s", binaryName);
} else {
logMsg("*** No need to clean the binary name: %s", binaryName);
}
if (strnicmp(binaryName, DEFAULT_EXECUTABLE, strlen(DEFAULT_EXECUTABLE)) != 0) {
logMsg("PlatformLauncher:\n\tNon-default executable name: %s", binaryName);
logMsg("\tHence, launching with extra parameters: -S %s", binaryName);
progArgs.push_front(binaryName);
progArgs.push_front("-S");
}
}
string java("");
if (getenv("JAVACMD") != NULL) {
java = getenv("JAVACMD");
jvmLauncher.setJavaCmd(java);
separateProcess = true;
suppressConsole = false;
} else {
if (jdkhome.empty()) {
// attempt to get JDK home from registry
jvmLauncher.initialize(REQ_JAVA_VERSION);
}
if (!jdkhome.empty()) {
java = jdkhome + "\\bin\\java.exe";
} else if (getenv("JAVA_HOME") != NULL) {
string java_home = string(getenv("JAVA_HOME"));
jdkhome = java_home;
java_home = trimTrailingBackslashes(java_home);
java = java_home + "\\bin\\java.exe";
} else {
java = findOnPath("java.exe");
if (!java.empty()) {
int home_index = java.find_last_of('\\', java.find_last_of('\\') - 1);
jdkhome = java.substr(0, home_index);
}
}
}
if (java.empty()) {
printToConsole("No `java.exe' executable found on PATH.");
return 255;
}
prepareOptions();
if (printCommandLine) {
list<string> commandLine;
commandLine.push_back(java);
addOptionsToCommandLine(commandLine);
for (list<string>::iterator it = commandLine.begin(); it != commandLine.end(); it++) {
printf("%s\n", it->c_str());
}
return true;
}
if (nextAction.empty()) {
while (true) {
// run app
if (!run(retCode)) {
return false;
}
break;
}
} else {
logErr(false, true, "We should not get here.");
return false;
}
return true;
}
bool PlatformLauncher::run(DWORD *retCode) {
logMsg("Starting application...");
jvmLauncher.setSuppressConsole(suppressConsole);
bool rc = jvmLauncher.start(bootclass.c_str(), progArgs, javaOptions, separateProcess, retCode);
if (!separateProcess) {
exit(0);
}
return rc;
}
bool PlatformLauncher::checkJDKHome() {
if (!jdkhome.empty() && !jvmLauncher.initialize(jdkhome.c_str())) {
logMsg("Cannot locate java installation in specified jdkhome: %s", jdkhome.c_str());
string errMsg = "ERROR: Cannot locate Java installation in specified jdkhome:\n";
errMsg += jdkhome;
string errMsgFull = errMsg + "\nDo you want to try to use default version?";
jdkhome = "";
// Pop-up the message box only if there is no console
if (!isConsoleAttached()) {
if (::MessageBox(NULL, errMsgFull.c_str(), "Invalid jdkhome specified", MB_ICONQUESTION | MB_YESNO) == IDNO) {
return false;
}
} else {
fprintf(stdout, "%s\n", errMsg.c_str());
return false;
}
}
if (jdkhome.empty()) {
logMsg("-Xjdkhome is not set, checking for %%JAVA_HOME%%...");
char *origJavaHome = getenv("JAVA_HOME");
if (origJavaHome) {
string javaHome = trimTrailingBackslashes(origJavaHome);
logMsg("%%JAVA_HOME%% is set: %s", javaHome.c_str());
if (!jvmLauncher.initialize(javaHome.c_str())) {
logMsg("ERROR: Cannot locate java installation, specified by JAVA_HOME: %s", javaHome.c_str());
string errMsg = "Cannot locate Java installation, specified by JAVA_HOME:\n";
errMsg += javaHome;
string errMsgFull = errMsg + "\nDo you want to try to use default version?";
jdkhome = "";
// Pop-up the message box only if there is no console
if (!isConsoleAttached()) {
if (::MessageBox(NULL, errMsgFull.c_str(),
"Invalid jdkhome specified", MB_ICONQUESTION | MB_YESNO) == IDNO) {
return false;
}
} else {
fprintf(stdout, "%s\n", errMsg.c_str());
return false;
}
} else {
jdkhome = javaHome;
}
}
}
return true;
}
void PlatformLauncher::onExit() {
logMsg("onExit()");
}