source: main/trunk/search4j/libsearch4j.cpp@ 32880

Last change on this file since 32880 was 32880, checked in by davidb, 4 years ago

Test for version string made a little bit more general to allow for 'openjdk version' rather than 'java version'

File size: 11.1 KB
Line 
1/*
2* Library of functions for the Search4j utility
3*/
4
5#include "libsearch4j.h"
6
7
8#ifdef WINDOWS
9//minimal launch4j stuff
10#define NO_JAVA_FOUND 0
11#define FOUND_JRE 1
12#define FOUND_SDK 2
13
14//windows functions
15#define popen _popen
16#define pclose _pclose
17#define strcmp _stricmp
18
19#endif /* WINDOWS */
20
21void replace_all ( std::string & str, std::string const & pattern, std::string const & replacement ) {
22
23 std::string::size_type start = str.find( pattern, 0 );
24
25 while ( start != str.npos ) {
26 str.replace( start, pattern.size(), replacement );
27 start = str.find( pattern, start+replacement.size() );
28 }
29
30}
31
32
33int process( string command, bool render ) {
34
35 #ifdef WINDOWS
36
37 STARTUPINFO si;
38 PROCESS_INFORMATION pi;
39 memset(&pi, 0, sizeof(pi));
40 memset(&si, 0, sizeof(si));
41 si.cb = sizeof(si);
42
43 DWORD dwExitCode = -1;
44 char* cmd = (char*)command.c_str();
45
46 bool result;
47 if ( render ) {
48 result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
49 } else {
50 result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
51 }
52
53 if ( result ) {
54 WaitForSingleObject(pi.hProcess, INFINITE);
55 GetExitCodeProcess(pi.hProcess, &dwExitCode);
56 CloseHandle(pi.hThread);
57 CloseHandle(pi.hProcess);
58 }
59 return dwExitCode;
60
61 #else
62
63 return system( command.c_str() );
64
65 #endif
66}
67
68
69
70int process_and_catch_output( string command, string &output ) {
71
72 FILE *pPipe;
73
74 char cmd[1024];
75 strcpy(cmd, command.c_str());
76 strcat(cmd, " 2>&1" );
77
78 //if ( verbose ) cout << "process(): running '" << command << "'" << endl;
79#ifdef WINDOWS
80 if( (pPipe = _popen( cmd, "rt" )) == NULL ) {
81 _pclose( pPipe );
82#else
83 if( (pPipe = popen( cmd, "r" )) == NULL ) {
84 pclose( pPipe );
85#endif
86 return -1;
87 }
88 //if ( verbose ) cout << "started process" << endl;
89
90 /* Read pipe until end of file. */
91 while( !feof( pPipe ) ) {
92 char psBuffer[128];
93 //if ( verbose ) cout << "get some data" << endl;
94 if( fgets( psBuffer, 128, pPipe ) != NULL ) {
95
96 //if ( verbose ) cout << "got: " << psBuffer << endl;
97 output.append( psBuffer );
98
99 } else {
100 //if ( verbose ) cout << "none left" << endl;
101 }
102
103 }
104
105 /* Close pipe and return return value of pPipe. */
106#ifdef WINDOWS
107 int code = _pclose( pPipe );
108#else
109 int code = pclose( pPipe );
110#endif
111
112 return code;
113
114}
115
116//Jvm class methods
117
118Jvm::Jvm()
119 : era_(1), major_(0), minor_(0), update_(0),
120 javaHome_(),
121 isJdk_(false), healthy_(false)
122{}
123
124Jvm::~Jvm()
125{
126 // javaHome automatically destructed
127}
128
129
130void Jvm::setJavaHome( string jh ) {
131 healthy_ = true;
132 javaHome_ = jh;
133 setVersionFromExeOuput();
134 setIsJdkFromJavacPresence();
135}
136
137string Jvm::getJavaHome() {
138 return javaHome_;
139}
140
141string Jvm::getExecutable() {
142 string exec = "";
143 exec.append( javaHome_ );
144
145 #ifdef WINDOWS
146 exec.append( "\\bin\\java.exe" );
147 #endif
148
149 #ifndef WINDOWS
150 exec.append( "/bin/java" );
151 #endif
152
153 return exec;
154}
155
156#ifdef WINDOWS
157string Jvm::getWinExecutable() {
158 string exec = "";
159 exec.append( javaHome_ );
160 exec.append( "\\bin\\javaw.exe" );
161 return exec;
162}
163#endif
164
165
166string Jvm::getVersion() {
167 stringstream ss;
168 ss << era_ << "." << major_ << "." << minor_ << "_" << update_;
169 return ss.str();
170}
171
172bool Jvm::check() {
173 return healthy_;
174}
175
176bool Jvm::setVersionFromString( string version ) {
177 era_ = atoi( version.substr(0,1).c_str() );
178 major_ = atoi( version.substr(2,1).c_str() );
179 if(version.length() >= 5) {
180 minor_ = atoi( version.substr(4,1).c_str() );
181 } else {
182 minor_ = 0;
183 }
184 if(version.length() >= 7) {
185 update_ = atoi( version.substr(6,2).c_str() );
186 } else {
187 update_ = 0;
188 }
189 return true;
190}
191
192int Jvm::compare( Jvm otherJvm ) {
193 //era
194 if ( era_ > otherJvm.getEra() )
195 return 1;
196 else if ( era_ < otherJvm.getEra() )
197 return -1;
198
199 //major
200 if ( major_ > otherJvm.getMajor() )
201 return 1;
202 else if ( major_ < otherJvm.getMajor() )
203 return -1;
204
205 //minor
206 if ( minor_ > otherJvm.getMinor() )
207 return 1;
208 else if ( minor_ < otherJvm.getMinor() )
209 return -1;
210
211 //update
212 if ( update_ > otherJvm.getUpdate() )
213 return 1;
214 else if ( update_ < otherJvm.getUpdate() )
215 return -1;
216
217 //all the same so far, must be exactly the same
218 return 0;
219
220}
221
222int Jvm::getEra() { return era_; }
223int Jvm::getMajor() { return major_; }
224int Jvm::getMinor() { return minor_; }
225int Jvm::getUpdate() { return update_; }
226
227bool Jvm::getIsJdk() { return isJdk_; }
228bool Jvm::getIsJre() { return !isJdk_; }
229
230void Jvm::setVersionFromExeOuput() {
231 string command = "", output = "";
232 command.append( "\"" );
233 command.append( getExecutable() );
234 command.append( "\"" );
235 command.append(" -version");
236
237 //cerr << "command: " << command << endl;
238 int result = process_and_catch_output( command, output );
239 //cerr << "output: " << output << endl;
240
241 if ( result == 0 ) {
242 // 22 is the length of sentinel string plus typical version value
243 int max_search_length = output.length() - 22;
244 int caret = 0;
245 bool found = false;
246 while (caret < max_search_length && !found) {
247 const int to_match_len = 8;
248 if ( strcmp( output.substr( caret, to_match_len ).c_str() , " version" ) == 0 ) {
249 era_ = atoi( output.substr(caret + to_match_len + 2,1).c_str() );
250 major_ = atoi( output.substr(caret + to_match_len + 4,1).c_str() );
251 minor_ = atoi( output.substr(caret + to_match_len + 6,1).c_str() );
252 update_ = atoi( output.substr(caret + to_match_len + 8,2).c_str() );
253 found = true;
254 }
255 else {
256 caret++;
257 }
258 }
259 if (!found) {
260 healthy_ = false;
261 }
262 }
263 else {
264 healthy_ = false;
265 }
266}
267void Jvm::setIsJdkFromJavacPresence() {
268 string javacFile = "";
269 javacFile.append( javaHome_ );
270
271 #ifdef WINDOWS
272 javacFile.append( "\\bin\\javac.exe" );
273 #endif
274 #ifndef WINDOWS
275 javacFile.append( "/bin/javac" );
276 #endif
277 struct stat stFileInfo;
278 isJdk_ = ( stat(javacFile.c_str(),&stFileInfo) == 0 );
279}
280
281//end of Jvm class methods
282
283
284#ifdef WINDOWS
285int regSearch(HKEY hKey, const char* keyName, int searchType, char* foundJavaVer ) {
286 DWORD x = 0;
287 unsigned long size = 1024;
288 FILETIME time;
289 char buffer[1024] = {0};
290
291 int foundJava = NO_JAVA_FOUND;
292
293 while (RegEnumKeyEx(
294 hKey, // handle to key to enumerate
295 x++, // index of subkey to enumerate
296 buffer, // address of buffer for subkey name
297 &size, // address for size of subkey buffer
298 NULL, // reserved
299 NULL, // address of buffer for class string
300 NULL, // address for size of class buffer
301 &time) == ERROR_SUCCESS) {
302 strcpy(foundJavaVer, buffer);
303 foundJava = searchType;
304 size = 1024;
305 }
306
307 return foundJava;
308
309}
310#endif
311
312/*
313* function to find java
314* implements the logic drawn on the dl lab whiteboard in feb 08
315* return a Jvm object which represents the jvm on disk
316*/
317bool find( Jvm &jvm, bool use_minimum, const Jvm& minimum, bool jreOnly, bool jdkOnly, string phint, string hint, bool verbose ) {
318
319 if ( verbose ) cout << "Searching for a JVM" << endl;
320
321 char *javaHomeEnv = NULL;
322 bool jvmFound = false;
323
324 if ( !jvmFound ) {
325
326 //try the priority hint
327 if ( verbose ) cout << " - trying priority hint: ";
328 if ( strcmp(phint.c_str(),"") != 0 ) {
329 if ( verbose ) cout << "(" << phint << ") ";
330 jvm.setJavaHome( phint );
331 if ( jvm.check() ) {
332 if ( use_minimum ) {
333 if ( jvm.compare( minimum ) >= 0 ) {
334 jvmFound = true;
335 }
336 } else {
337 jvmFound = true;
338 }
339 }
340 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
341 jvmFound = false;
342 }
343
344 }
345 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
346 }
347
348 if ( !jvmFound ) {
349
350 //try JAVA_HOME
351 if ( verbose ) cout << " - trying JAVA_HOME: ";
352 javaHomeEnv = getenv( "JAVA_HOME" );
353 if ( javaHomeEnv != NULL ) {
354 if ( verbose ) cout << "(" << javaHomeEnv << ") ";
355 jvm.setJavaHome( javaHomeEnv );
356 if ( jvm.check() ) {
357 if ( use_minimum ) {
358 if ( jvm.compare( minimum ) >= 0 ) {
359 jvmFound = true;
360 }
361 } else {
362 jvmFound = true;
363 }
364 }
365 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
366 jvmFound = false;
367 }
368
369 }
370 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
371 }
372
373 if ( !jvmFound ) {
374
375 //try JRE_HOME
376 if ( verbose ) cout << " - trying JRE_HOME: ";
377 javaHomeEnv = getenv( "JRE_HOME" );
378 if ( javaHomeEnv != NULL ) {
379 if ( verbose ) cout << "(" << javaHomeEnv << ") ";
380 jvm.setJavaHome( javaHomeEnv );
381 if ( jvm.check() ) {
382 if ( use_minimum ) {
383 if ( jvm.compare( minimum ) >= 0 ) {
384 jvmFound = true;
385 }
386 } else {
387 jvmFound = true;
388 }
389 }
390 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
391 jvmFound = false;
392 }
393
394 }
395 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
396 }
397
398 #ifdef WINDOWS
399 if ( !jvmFound ) {
400
401 //try the registry - this code based on launch4j code
402 char foundJavaVer[8192] = {0};
403 int foundJava = NO_JAVA_FOUND;
404
405 if ( verbose ) cout << " - trying the registry: "; cout.flush();
406 HKEY hKey;
407 const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
408 const char sdk[] = "SOFTWARE\\JavaSoft\\Java Development Kit";
409
410 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(jre), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
411 foundJava = regSearch(hKey, jre, FOUND_JRE, foundJavaVer);
412 RegCloseKey(hKey);
413 }
414
415 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(sdk), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
416 foundJava = regSearch(hKey, sdk, FOUND_SDK, foundJavaVer);
417 RegCloseKey(hKey);
418 }
419
420 if ( foundJava != NO_JAVA_FOUND ) {
421 char path[1024] = {0};
422 char keyBuffer[1024];
423 unsigned long datatype;
424 unsigned long bufferlength = 1024;
425 if (foundJava == FOUND_JRE) {
426 strcpy(keyBuffer, jre);
427 } else {
428 strcpy(keyBuffer, sdk);
429 }
430
431 strcat(keyBuffer, "\\");
432 strcat(keyBuffer, foundJavaVer);
433
434 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
435 unsigned char buffer[1024] = {0};
436 if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer, &bufferlength) == ERROR_SUCCESS) {
437 int i = 0;
438 do {
439 path[i] = buffer[i];
440 } while (path[i++] != 0);
441 if (foundJava == FOUND_SDK) {
442 strcat(path, "\\jre");
443 }
444 if ( verbose ) cerr << "path: " << path << endl ;
445 jvm.setJavaHome( path );
446 if ( jvm.check() ) {
447 if ( use_minimum ) {
448 if ( jvm.compare( minimum ) >= 0 ) {
449 jvmFound = true;
450 }
451 } else {
452 jvmFound = true;
453 }
454 }
455 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
456 jvmFound = false;
457 }
458 }
459 RegCloseKey(hKey);
460 }
461 }
462 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
463 }
464 #endif
465
466 if ( !jvmFound ) {
467
468 //try the hint
469 if ( verbose ) cout << " - trying hint: ";
470 if ( strcmp(hint.c_str(),"") != 0 ) {
471 if ( verbose ) cout << "(" << hint << ") ";
472 jvm.setJavaHome( hint );
473 if ( jvm.check() ) {
474 if ( use_minimum ) {
475 if ( jvm.compare( minimum ) >= 0 ) {
476 jvmFound = true;
477 }
478 } else {
479 jvmFound = true;
480 }
481 }
482 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
483 jvmFound = false;
484 }
485
486 }
487 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
488 }
489
490
491 return jvmFound;
492}
Note: See TracBrowser for help on using the repository browser.