1 | /*
|
---|
2 | * Library of functions for the Search4j utility
|
---|
3 | */
|
---|
4 |
|
---|
5 | #include <iostream>
|
---|
6 | #include <fstream>
|
---|
7 | #include <string>
|
---|
8 | #include <sstream>
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | #ifdef WINDOWS
|
---|
12 | #include <windows.h>
|
---|
13 | //minimal launch4j stuff
|
---|
14 | #define NO_JAVA_FOUND 0
|
---|
15 | #define FOUND_JRE 1
|
---|
16 | #define FOUND_SDK 2
|
---|
17 | //windows functions
|
---|
18 | #define popen _popen
|
---|
19 | #define pclose _pclose
|
---|
20 | #define strcmp _stricmp
|
---|
21 | #define ID_TIMER 1
|
---|
22 | int splashTimeout = 60;
|
---|
23 | #endif /* WINDOWS */
|
---|
24 |
|
---|
25 | void replace_all ( std::string & str, std::string const & pattern, std::string const & replacement ) {
|
---|
26 |
|
---|
27 | std::string::size_type start = str.find( pattern, 0 );
|
---|
28 |
|
---|
29 | while ( start != str.npos ) {
|
---|
30 | str.replace( start, pattern.size(), replacement );
|
---|
31 | start = str.find( pattern, start+replacement.size() );
|
---|
32 | }
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 | int process( string command, bool render ) {
|
---|
37 | STARTUPINFO si;
|
---|
38 | PROCESS_INFORMATION pi;
|
---|
39 |
|
---|
40 | memset(&pi, 0, sizeof(pi));
|
---|
41 | memset(&si, 0, sizeof(si));
|
---|
42 | si.cb = sizeof(si);
|
---|
43 |
|
---|
44 | DWORD dwExitCode = -1;
|
---|
45 | char* cmd = (char*)command.c_str();
|
---|
46 |
|
---|
47 | bool result;
|
---|
48 | if ( render ) {
|
---|
49 | result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
|
---|
50 | } else {
|
---|
51 | result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
|
---|
52 | }
|
---|
53 |
|
---|
54 | if ( result ) {
|
---|
55 | WaitForSingleObject(pi.hProcess, INFINITE);
|
---|
56 | GetExitCodeProcess(pi.hProcess, &dwExitCode);
|
---|
57 | CloseHandle(pi.hThread);
|
---|
58 | CloseHandle(pi.hProcess);
|
---|
59 | }
|
---|
60 | return dwExitCode;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int process_and_catch_output( string command, string &output ) {
|
---|
64 |
|
---|
65 | FILE *pPipe;
|
---|
66 |
|
---|
67 | char cmd[1024] = "cmd.exe /c ";
|
---|
68 | strcat(cmd, command.c_str());
|
---|
69 | strcat(cmd, " > cmd_output.txt 2>&1");
|
---|
70 | //cerr << "command: " << cmd << endl;
|
---|
71 | int code = process( cmd, false );
|
---|
72 | if ( code!= 0 )
|
---|
73 | return code;
|
---|
74 |
|
---|
75 | string line;
|
---|
76 | ifstream myfile("cmd_output.txt");
|
---|
77 | if ( !myfile.is_open() ) {
|
---|
78 | return -1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | while ( !myfile.eof() ) {
|
---|
82 | getline(myfile,line);
|
---|
83 | output.append( line );
|
---|
84 | }
|
---|
85 |
|
---|
86 | myfile.close();
|
---|
87 |
|
---|
88 | _unlink( "cmd_output.txt" );
|
---|
89 |
|
---|
90 | return code;
|
---|
91 | }
|
---|
92 |
|
---|
93 | class Jvm {
|
---|
94 |
|
---|
95 | string javaHome;
|
---|
96 |
|
---|
97 | // version string. era.major.minor_update. eg: 1.4.2_02
|
---|
98 | int era;
|
---|
99 | int major;
|
---|
100 | int minor;
|
---|
101 | int update;
|
---|
102 |
|
---|
103 | bool isJdk;
|
---|
104 | bool healthy;
|
---|
105 |
|
---|
106 |
|
---|
107 | public:
|
---|
108 | Jvm() {
|
---|
109 | healthy = false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void setJavaHome( string jh ) {
|
---|
113 | healthy = true;
|
---|
114 | javaHome = jh;
|
---|
115 | setVersionFromExeOuput();
|
---|
116 | }
|
---|
117 |
|
---|
118 | string getJavaHome() {
|
---|
119 | return javaHome;
|
---|
120 | }
|
---|
121 |
|
---|
122 | string getExecutable() {
|
---|
123 | string exec = "";
|
---|
124 | exec.append( javaHome );
|
---|
125 |
|
---|
126 | #ifdef WINDOWS
|
---|
127 | exec.append( "\\bin\\java.exe" );
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | #ifndef WINDOWS
|
---|
131 | exec.append( "/bin/java" );
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | return exec;
|
---|
135 | }
|
---|
136 |
|
---|
137 | #ifdef WINDOWS
|
---|
138 | string getWinExecutable() {
|
---|
139 | string exec = "";
|
---|
140 | exec.append( javaHome );
|
---|
141 | exec.append( "\\bin\\javaw.exe" );
|
---|
142 | return exec;
|
---|
143 | }
|
---|
144 | #endif
|
---|
145 |
|
---|
146 |
|
---|
147 | string getVersion() {
|
---|
148 | stringstream ss;
|
---|
149 | ss << era << "." << major << "." << minor << "_" << update;
|
---|
150 | return ss.str();
|
---|
151 | }
|
---|
152 |
|
---|
153 | bool check() {
|
---|
154 | return healthy;
|
---|
155 | }
|
---|
156 |
|
---|
157 | bool setVersionFromString( string version ) {
|
---|
158 | era = atoi( version.substr(0,1).c_str() );
|
---|
159 | major = atoi( version.substr(2,1).c_str() );
|
---|
160 | minor = atoi( version.substr(4,1).c_str() );
|
---|
161 | update = atoi( version.substr(6,2).c_str() );
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 | int compare( Jvm otherJvm ) {
|
---|
166 | //era
|
---|
167 | if ( era > otherJvm.getEra() )
|
---|
168 | return 1;
|
---|
169 | else if ( era < otherJvm.getEra() )
|
---|
170 | return -1;
|
---|
171 |
|
---|
172 | //major
|
---|
173 | if ( major > otherJvm.getMajor() )
|
---|
174 | return 1;
|
---|
175 | else if ( major < otherJvm.getMajor() )
|
---|
176 | return -1;
|
---|
177 |
|
---|
178 | //minor
|
---|
179 | if ( minor > otherJvm.getMinor() )
|
---|
180 | return 1;
|
---|
181 | else if ( minor < otherJvm.getMinor() )
|
---|
182 | return -1;
|
---|
183 |
|
---|
184 | //update
|
---|
185 | if ( update > otherJvm.getUpdate() )
|
---|
186 | return 1;
|
---|
187 | else if ( update < otherJvm.getUpdate() )
|
---|
188 | return -1;
|
---|
189 |
|
---|
190 | //all the same so far, must be exactly the same
|
---|
191 | return 0;
|
---|
192 |
|
---|
193 | }
|
---|
194 |
|
---|
195 | int getEra() { return era; }
|
---|
196 | int getMajor() { return major; }
|
---|
197 | int getMinor() { return minor; }
|
---|
198 | int getUpdate() { return update; }
|
---|
199 |
|
---|
200 | private:
|
---|
201 |
|
---|
202 | void setVersionFromExeOuput() {
|
---|
203 | string command = "", output = "";
|
---|
204 | command.append( "\"" );
|
---|
205 | command.append( getExecutable() );
|
---|
206 | command.append( "\"" );
|
---|
207 | command.append(" -version");
|
---|
208 |
|
---|
209 | //cerr << "command: " << command << endl;
|
---|
210 | int result = process_and_catch_output( command, output );
|
---|
211 | //cerr << "output: " << output << endl;
|
---|
212 |
|
---|
213 | if ( result == 0 ) {
|
---|
214 | if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) {
|
---|
215 | era = atoi( output.substr(14,1).c_str() );
|
---|
216 | major = atoi( output.substr(16,1).c_str() );
|
---|
217 | minor = atoi( output.substr(18,1).c_str() );
|
---|
218 | update = atoi( output.substr(20,2).c_str() );
|
---|
219 | } else {
|
---|
220 | healthy=false;
|
---|
221 | }
|
---|
222 | } else {
|
---|
223 | healthy = false;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | };
|
---|
229 |
|
---|
230 | #ifdef WINDOWS
|
---|
231 | int regSearch(HKEY hKey, const char* keyName, int searchType, char* foundJavaVer ) {
|
---|
232 | DWORD x = 0;
|
---|
233 | unsigned long size = 1024;
|
---|
234 | FILETIME time;
|
---|
235 | char buffer[1024] = {0};
|
---|
236 |
|
---|
237 | int foundJava = NO_JAVA_FOUND;
|
---|
238 |
|
---|
239 | while (RegEnumKeyEx(
|
---|
240 | hKey, // handle to key to enumerate
|
---|
241 | x++, // index of subkey to enumerate
|
---|
242 | buffer, // address of buffer for subkey name
|
---|
243 | &size, // address for size of subkey buffer
|
---|
244 | NULL, // reserved
|
---|
245 | NULL, // address of buffer for class string
|
---|
246 | NULL, // address for size of class buffer
|
---|
247 | &time) == ERROR_SUCCESS) {
|
---|
248 | strcpy(foundJavaVer, buffer);
|
---|
249 | foundJava = searchType;
|
---|
250 | size = 1024;
|
---|
251 | }
|
---|
252 |
|
---|
253 | return foundJava;
|
---|
254 |
|
---|
255 | }
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * function to find java
|
---|
260 | * implements the logic drawn on the dl lab whiteboard in feb 08
|
---|
261 | * return a Jvm object which represents the jvm on disk
|
---|
262 | */
|
---|
263 | bool find( Jvm &jvm, bool use_minimum, Jvm minimum, string hint, bool verbose ) {
|
---|
264 |
|
---|
265 | if ( verbose ) cout << "Searching for a JVM" << endl;
|
---|
266 |
|
---|
267 | char *javaHome = "";
|
---|
268 | bool jvmFound = false;
|
---|
269 |
|
---|
270 | if ( !jvmFound ) {
|
---|
271 |
|
---|
272 | //try JAVA_HOME
|
---|
273 | if ( verbose ) cout << " - trying JAVA_HOME: ";
|
---|
274 | javaHome = getenv( "JAVA_HOME" );
|
---|
275 | if ( javaHome != NULL ) {
|
---|
276 | if ( verbose ) cout << "(" << javaHome << ") ";
|
---|
277 | jvm.setJavaHome( javaHome );
|
---|
278 | if ( jvm.check() ) {
|
---|
279 | if ( use_minimum ) {
|
---|
280 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
281 | jvmFound = true;
|
---|
282 | }
|
---|
283 | } else {
|
---|
284 | jvmFound = true;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
289 | }
|
---|
290 |
|
---|
291 | if ( !jvmFound ) {
|
---|
292 |
|
---|
293 | //try JRE_HOME
|
---|
294 | if ( verbose ) cout << " - trying JRE_HOME: ";
|
---|
295 | javaHome = getenv( "JRE_HOME" );
|
---|
296 | if ( javaHome != NULL ) {
|
---|
297 | if ( verbose ) cout << "(" << javaHome << ") ";
|
---|
298 | jvm.setJavaHome( javaHome );
|
---|
299 | if ( jvm.check() ) {
|
---|
300 | if ( use_minimum ) {
|
---|
301 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
302 | jvmFound = true;
|
---|
303 | }
|
---|
304 | } else {
|
---|
305 | jvmFound = true;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
310 | }
|
---|
311 |
|
---|
312 | #ifdef WINDOWS
|
---|
313 | if ( !jvmFound ) {
|
---|
314 |
|
---|
315 | //try the registry - this code based on launch4j code
|
---|
316 | char foundJavaVer[8192] = {0};
|
---|
317 | int foundJava = NO_JAVA_FOUND;
|
---|
318 |
|
---|
319 | if ( verbose ) cout << " - trying the registry: "; cout.flush();
|
---|
320 | HKEY hKey;
|
---|
321 | const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
|
---|
322 | const char sdk[] = "SOFTWARE\\JavaSoft\\Java Development Kit";
|
---|
323 |
|
---|
324 | if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(jre), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
|
---|
325 | foundJava = regSearch(hKey, jre, FOUND_JRE, foundJavaVer);
|
---|
326 | RegCloseKey(hKey);
|
---|
327 | }
|
---|
328 |
|
---|
329 | if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(sdk), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
|
---|
330 | foundJava = regSearch(hKey, sdk, FOUND_SDK, foundJavaVer);
|
---|
331 | RegCloseKey(hKey);
|
---|
332 | }
|
---|
333 |
|
---|
334 | if ( foundJava != NO_JAVA_FOUND ) {
|
---|
335 | char path[1024] = {0};
|
---|
336 | char keyBuffer[1024];
|
---|
337 | unsigned long datatype;
|
---|
338 | unsigned long bufferlength = 1024;
|
---|
339 | if (foundJava == FOUND_JRE) {
|
---|
340 | strcpy(keyBuffer, jre);
|
---|
341 | } else {
|
---|
342 | strcpy(keyBuffer, sdk);
|
---|
343 | }
|
---|
344 |
|
---|
345 | strcat(keyBuffer, "\\");
|
---|
346 | strcat(keyBuffer, foundJavaVer);
|
---|
347 |
|
---|
348 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
|
---|
349 | unsigned char buffer[1024] = {0};
|
---|
350 | if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer, &bufferlength) == ERROR_SUCCESS) {
|
---|
351 | int i = 0;
|
---|
352 | do {
|
---|
353 | path[i] = buffer[i];
|
---|
354 | } while (path[i++] != 0);
|
---|
355 | if (foundJava == FOUND_SDK) {
|
---|
356 | strcat(path, "\\jre");
|
---|
357 | }
|
---|
358 | if ( verbose ) cerr << "path: " << path << endl ;
|
---|
359 | jvm.setJavaHome( path );
|
---|
360 | if ( jvm.check() ) {
|
---|
361 | if ( use_minimum ) {
|
---|
362 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
363 | jvmFound = true;
|
---|
364 | }
|
---|
365 | } else {
|
---|
366 | jvmFound = true;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | RegCloseKey(hKey);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
374 | }
|
---|
375 | #endif
|
---|
376 |
|
---|
377 | if ( !jvmFound ) {
|
---|
378 |
|
---|
379 | //try the hint
|
---|
380 | if ( verbose ) cout << " - trying hint: ";
|
---|
381 | if ( strcmp(hint.c_str(),"") != 0 ) {
|
---|
382 | if ( verbose ) cout << "(" << hint << ") ";
|
---|
383 | jvm.setJavaHome( hint );
|
---|
384 | if ( jvm.check() ) {
|
---|
385 | if ( use_minimum ) {
|
---|
386 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
387 | jvmFound = true;
|
---|
388 | }
|
---|
389 | } else {
|
---|
390 | jvmFound = true;
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | return jvmFound;
|
---|
399 | }
|
---|