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

Last change on this file since 23555 was 22529, checked in by sjm84, 14 years ago

Fixed Windows from trying to write a file into a possibly inaccessible place

File size: 10.6 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 minor_ = atoi( version.substr(4,1).c_str() );
180 update_ = atoi( version.substr(6,2).c_str() );
181 return true;
182}
183
184int Jvm::compare( Jvm otherJvm ) {
185 //era
186 if ( era_ > otherJvm.getEra() )
187 return 1;
188 else if ( era_ < otherJvm.getEra() )
189 return -1;
190
191 //major
192 if ( major_ > otherJvm.getMajor() )
193 return 1;
194 else if ( major_ < otherJvm.getMajor() )
195 return -1;
196
197 //minor
198 if ( minor_ > otherJvm.getMinor() )
199 return 1;
200 else if ( minor_ < otherJvm.getMinor() )
201 return -1;
202
203 //update
204 if ( update_ > otherJvm.getUpdate() )
205 return 1;
206 else if ( update_ < otherJvm.getUpdate() )
207 return -1;
208
209 //all the same so far, must be exactly the same
210 return 0;
211
212}
213
214int Jvm::getEra() { return era_; }
215int Jvm::getMajor() { return major_; }
216int Jvm::getMinor() { return minor_; }
217int Jvm::getUpdate() { return update_; }
218
219bool Jvm::getIsJdk() { return isJdk_; }
220bool Jvm::getIsJre() { return !isJdk_; }
221
222void Jvm::setVersionFromExeOuput() {
223 string command = "", output = "";
224 command.append( "\"" );
225 command.append( getExecutable() );
226 command.append( "\"" );
227 command.append(" -version");
228
229 //cerr << "command: " << command << endl;
230 int result = process_and_catch_output( command, output );
231 //cerr << "output: " << output << endl;
232
233 if ( result == 0 ) {
234 if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) {
235 era_ = atoi( output.substr(14,1).c_str() );
236 major_ = atoi( output.substr(16,1).c_str() );
237 minor_ = atoi( output.substr(18,1).c_str() );
238 update_ = atoi( output.substr(20,2).c_str() );
239 } else {
240 healthy_=false;
241 }
242 } else {
243 healthy_ = false;
244 }
245}
246void Jvm::setIsJdkFromJavacPresence() {
247 string javacFile = "";
248 javacFile.append( javaHome_ );
249
250 #ifdef WINDOWS
251 javacFile.append( "\\bin\\javac.exe" );
252 #endif
253 #ifndef WINDOWS
254 javacFile.append( "/bin/javac" );
255 #endif
256 struct stat stFileInfo;
257 isJdk_ = ( stat(javacFile.c_str(),&stFileInfo) == 0 );
258}
259
260//end of Jvm class methods
261
262
263#ifdef WINDOWS
264int regSearch(HKEY hKey, const char* keyName, int searchType, char* foundJavaVer ) {
265 DWORD x = 0;
266 unsigned long size = 1024;
267 FILETIME time;
268 char buffer[1024] = {0};
269
270 int foundJava = NO_JAVA_FOUND;
271
272 while (RegEnumKeyEx(
273 hKey, // handle to key to enumerate
274 x++, // index of subkey to enumerate
275 buffer, // address of buffer for subkey name
276 &size, // address for size of subkey buffer
277 NULL, // reserved
278 NULL, // address of buffer for class string
279 NULL, // address for size of class buffer
280 &time) == ERROR_SUCCESS) {
281 strcpy(foundJavaVer, buffer);
282 foundJava = searchType;
283 size = 1024;
284 }
285
286 return foundJava;
287
288}
289#endif
290
291/*
292* function to find java
293* implements the logic drawn on the dl lab whiteboard in feb 08
294* return a Jvm object which represents the jvm on disk
295*/
296bool find( Jvm &jvm, bool use_minimum, const Jvm& minimum, bool jreOnly, bool jdkOnly, string phint, string hint, bool verbose ) {
297
298 if ( verbose ) cout << "Searching for a JVM" << endl;
299
300 char *javaHomeEnv = "";
301 bool jvmFound = false;
302
303 if ( !jvmFound ) {
304
305 //try the priority hint
306 if ( verbose ) cout << " - trying priority hint: ";
307 if ( strcmp(phint.c_str(),"") != 0 ) {
308 if ( verbose ) cout << "(" << phint << ") ";
309 jvm.setJavaHome( phint );
310 if ( jvm.check() ) {
311 if ( use_minimum ) {
312 if ( jvm.compare( minimum ) >= 0 ) {
313 jvmFound = true;
314 }
315 } else {
316 jvmFound = true;
317 }
318 }
319 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
320 jvmFound = false;
321 }
322
323 }
324 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
325 }
326
327 if ( !jvmFound ) {
328
329 //try JAVA_HOME
330 if ( verbose ) cout << " - trying JAVA_HOME: ";
331 javaHomeEnv = getenv( "JAVA_HOME" );
332 if ( javaHomeEnv != NULL ) {
333 if ( verbose ) cout << "(" << javaHomeEnv << ") ";
334 jvm.setJavaHome( javaHomeEnv );
335 if ( jvm.check() ) {
336 if ( use_minimum ) {
337 if ( jvm.compare( minimum ) >= 0 ) {
338 jvmFound = true;
339 }
340 } else {
341 jvmFound = true;
342 }
343 }
344 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
345 jvmFound = false;
346 }
347
348 }
349 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
350 }
351
352 if ( !jvmFound ) {
353
354 //try JRE_HOME
355 if ( verbose ) cout << " - trying JRE_HOME: ";
356 javaHomeEnv = getenv( "JRE_HOME" );
357 if ( javaHomeEnv != NULL ) {
358 if ( verbose ) cout << "(" << javaHomeEnv << ") ";
359 jvm.setJavaHome( javaHomeEnv );
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 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
370 jvmFound = false;
371 }
372
373 }
374 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
375 }
376
377 #ifdef WINDOWS
378 if ( !jvmFound ) {
379
380 //try the registry - this code based on launch4j code
381 char foundJavaVer[8192] = {0};
382 int foundJava = NO_JAVA_FOUND;
383
384 if ( verbose ) cout << " - trying the registry: "; cout.flush();
385 HKEY hKey;
386 const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
387 const char sdk[] = "SOFTWARE\\JavaSoft\\Java Development Kit";
388
389 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(jre), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
390 foundJava = regSearch(hKey, jre, FOUND_JRE, foundJavaVer);
391 RegCloseKey(hKey);
392 }
393
394 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(sdk), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
395 foundJava = regSearch(hKey, sdk, FOUND_SDK, foundJavaVer);
396 RegCloseKey(hKey);
397 }
398
399 if ( foundJava != NO_JAVA_FOUND ) {
400 char path[1024] = {0};
401 char keyBuffer[1024];
402 unsigned long datatype;
403 unsigned long bufferlength = 1024;
404 if (foundJava == FOUND_JRE) {
405 strcpy(keyBuffer, jre);
406 } else {
407 strcpy(keyBuffer, sdk);
408 }
409
410 strcat(keyBuffer, "\\");
411 strcat(keyBuffer, foundJavaVer);
412
413 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
414 unsigned char buffer[1024] = {0};
415 if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer, &bufferlength) == ERROR_SUCCESS) {
416 int i = 0;
417 do {
418 path[i] = buffer[i];
419 } while (path[i++] != 0);
420 if (foundJava == FOUND_SDK) {
421 strcat(path, "\\jre");
422 }
423 if ( verbose ) cerr << "path: " << path << endl ;
424 jvm.setJavaHome( path );
425 if ( jvm.check() ) {
426 if ( use_minimum ) {
427 if ( jvm.compare( minimum ) >= 0 ) {
428 jvmFound = true;
429 }
430 } else {
431 jvmFound = true;
432 }
433 }
434 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
435 jvmFound = false;
436 }
437 }
438 RegCloseKey(hKey);
439 }
440 }
441 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
442 }
443 #endif
444
445 if ( !jvmFound ) {
446
447 //try the hint
448 if ( verbose ) cout << " - trying hint: ";
449 if ( strcmp(hint.c_str(),"") != 0 ) {
450 if ( verbose ) cout << "(" << hint << ") ";
451 jvm.setJavaHome( hint );
452 if ( jvm.check() ) {
453 if ( use_minimum ) {
454 if ( jvm.compare( minimum ) >= 0 ) {
455 jvmFound = true;
456 }
457 } else {
458 jvmFound = true;
459 }
460 }
461 if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
462 jvmFound = false;
463 }
464
465 }
466 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
467 }
468
469
470 return jvmFound;
471}
Note: See TracBrowser for help on using the repository browser.