Changeset 15690 for release-kits/shared/search4j
- Timestamp:
- 2008-05-23T17:09:10+12:00 (16 years ago)
- Location:
- release-kits/shared/search4j
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
release-kits/shared/search4j
-
Property svn:ignore
set to
libsearch4j.obj
search4j.obj
-
Property svn:ignore
set to
-
release-kits/shared/search4j/compile.bat
r15093 r15690 1 cl /c -GX search4j.cpp1 cl /c -GX /DWINDOWS=yes search4j.cpp 2 2 link advapi32.lib search4j.obj /out:search4j.exe -
release-kits/shared/search4j/search4j.cpp
r15133 r15690 5 5 */ 6 6 7 #include <iostream> 8 #include <fstream> 9 #include <string> 10 #include <sstream> 11 12 using namespace std; 13 14 #ifdef WINDOWS 15 #include <windows.h> 16 17 //minimal launch4j stuff 18 #define NO_JAVA_FOUND 0 19 #define FOUND_JRE 1 20 #define FOUND_SDK 2 21 int foundJava = NO_JAVA_FOUND; 22 char foundJavaVer[128] = {0}; 23 24 //windows functions 25 #define popen _popen 26 #define strcmp _stricmp 27 #endif /* WINDOWS */ 28 29 30 //global variables 31 bool verbose = false; 32 bool use_minimum = false; 33 string hint = ""; 34 35 enum JvmProperty { 36 JavaHome, 37 Version, 38 Executable 39 }; 7 #include "libsearch4j.h" 8 9 void usage(); 40 10 41 11 enum Action { … … 45 15 }; 46 16 47 void replace_all ( std::string & str, std::string const & pattern, std::string const & replacement ) { 48 49 std::string::size_type start = str.find( pattern, 0 ); 50 51 while ( start != str.npos ) { 52 str.replace( start, pattern.size(), replacement ); 53 start = str.find( pattern, start+replacement.size() ); 54 } 55 56 } 57 58 int process( string command, string &output ) { 59 60 FILE *pPipe; 61 62 /* Run command so that it writes its output to a pipe. Open this 63 * pipe with read text attribute so that we can read it 64 * like a text file. */ 65 char cmd[1024]; strcpy(cmd, command.c_str()); 66 //if ( verbose ) cout << "process(): running '" << command << "'" << endl; 67 #ifdef WINDOWS 68 if( (pPipe = popen( cmd, "rt" )) == NULL ) { 69 if ( verbose ) cout << "could not start process" << endl; 70 pclose( pPipe ); 71 return -1; 72 } 73 #endif 74 #ifndef WINDOWS 75 if( (pPipe = popen( cmd, "r" )) == NULL ) { 76 if ( verbose ) cout << "could not start process" << endl; 77 pclose( pPipe ); 78 return -1; 79 } 80 #endif 81 82 //if ( verbose ) cout << "started process" << endl; 83 84 /* Read pipe until end of file. */ 85 while( !feof( pPipe ) ) { 86 char psBuffer[128]; 87 //if ( verbose ) cout << "get some data" << endl; 88 if( fgets( psBuffer, 128, pPipe ) != NULL ) { 89 90 //if ( verbose ) cout << "got: " << psBuffer << endl; 91 output.append( psBuffer ); 92 93 } else { 94 //if ( verbose ) cout << "none left" << endl; 95 } 96 97 } 98 99 /* Close pipe and return return value of pPipe. */ 100 int code = pclose( pPipe ); 101 return code; 102 } 103 104 class Jvm { 105 106 string javaHome; 107 108 // version string. era.major.minor_update. eg: 1.4.2_02 109 int era; 110 int major; 111 int minor; 112 int update; 113 114 bool isJdk; 115 bool healthy; 116 117 118 public: 119 Jvm() { 120 healthy = false; 121 } 122 123 void setJavaHome( string jh ) { 124 healthy = true; 125 javaHome = jh; 126 setVersionFromExeOuput(); 127 } 128 129 string getJavaHome() { 130 return javaHome; 131 } 132 133 string getExecutable() { 134 string exec = ""; 135 exec.append( javaHome ); 136 137 #ifdef WINDOWS 138 exec.append( "\\bin\\java.exe" ); 139 #endif 140 141 #ifndef WINDOWS 142 exec.append( "/bin/java" ); 143 #endif 144 145 return exec; 146 } 147 148 string getVersion() { 149 stringstream ss; 150 ss << era << "." << major << "." << minor << "_" << update; 151 return ss.str(); 152 } 153 154 bool check() { 155 return healthy; 156 } 157 158 bool setVersionFromString( string version ) { 159 era = atoi( version.substr(0,1).c_str() ); 160 major = atoi( version.substr(2,1).c_str() ); 161 minor = atoi( version.substr(4,1).c_str() ); 162 update = atoi( version.substr(6,2).c_str() ); 163 return true; 164 } 165 166 int compare( Jvm otherJvm ) { 167 //era 168 if ( era > otherJvm.getEra() ) 169 return 1; 170 else if ( era < otherJvm.getEra() ) 171 return -1; 172 173 //major 174 if ( major > otherJvm.getMajor() ) 175 return 1; 176 else if ( major < otherJvm.getMajor() ) 177 return -1; 178 179 //minor 180 if ( minor > otherJvm.getMinor() ) 181 return 1; 182 else if ( minor < otherJvm.getMinor() ) 183 return -1; 184 185 //update 186 if ( update > otherJvm.getUpdate() ) 187 return 1; 188 else if ( update < otherJvm.getUpdate() ) 189 return -1; 190 191 //all the same so far, must be exactly the same 192 return 0; 193 194 } 195 196 int getEra() { return era; } 197 int getMajor() { return major; } 198 int getMinor() { return minor; } 199 int getUpdate() { return update; } 200 201 private: 202 203 void setVersionFromExeOuput() { 204 string command = "", output = ""; 205 command.append( "\"" ); 206 command.append( getExecutable() ); 207 command.append( "\"" ); 208 command.append(" -version 2>&1"); 209 //cout << "(command: " << command << ")"; 210 int result = process( command, output ); 211 //cout << "(output: " << output << ")"; 212 if ( result == 0 ) { 213 if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) { 214 era = atoi( output.substr(14,1).c_str() ); 215 major = atoi( output.substr(16,1).c_str() ); 216 minor = atoi( output.substr(18,1).c_str() ); 217 update = atoi( output.substr(20,2).c_str() ); 218 //if ( verbose) cout << "set version to: " << era << "." << major << "." << minor << "_" << update << endl; 219 } else { 220 healthy=false; 221 } 222 } else { 223 healthy = false; 224 } 225 } 226 227 17 enum JvmProperty { 18 JavaHome, 19 Version, 20 Executable 228 21 }; 229 22 … … 248 41 } 249 42 250 void usage() {251 252 cout253 << "-----------------" << endl254 << " search4j: usage" << endl255 << "-----------------" << endl256 << "Three usage methods: find, compare and launch" << endl257 << endl258 << "find: find java and print out information about it" << endl259 << endl260 << " search4j [-v|-e]" << endl261 << " eg: search4j -e" << endl262 << endl263 << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl264 << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl265 << " if -e is specified, print the path the to the java executable. E.g. C:\\Program Files\\jre1.5.0_15\\bin\\java.exe" << endl266 << endl267 << "compare: compare the found java with the given java version string" << endl268 << endl269 << " search4j -c VERSION_STRING" << endl270 << " eg: search4j -c VERSION_STRING" << endl271 << endl272 << " print -1 if found java is older" << endl273 << " print 0 if found java is same" << endl274 << " print 1 if found java is newer" << endl275 << endl276 << "launch: launch the given executable jar with the found java" << endl277 << endl278 << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl279 << " eg: search4j -l greenstone3.jar" << endl280 << endl281 << " specify the location of the jar relative to the current directory" << endl282 << endl283 << "Global Options:" << endl284 << " -m VERSION_STRING: (minimum) find a java of the given version or newer, or fail" << endl285 << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl286 << " --verbose : have search4j print out information about what its doing" << endl287 << " --help : display this usage screen" << endl288 << endl289 ;290 }291 292 //another global293 Jvm minimum;294 295 #ifdef WINDOWS296 void regSearch(HKEY hKey, const char* keyName, int searchType) {297 DWORD x = 0;298 unsigned long size = 1024;299 FILETIME time;300 char buffer[1024] = {0};301 while (RegEnumKeyEx(302 hKey, // handle to key to enumerate303 x++, // index of subkey to enumerate304 buffer, // address of buffer for subkey name305 &size, // address for size of subkey buffer306 NULL, // reserved307 NULL, // address of buffer for class string308 NULL, // address for size of class buffer309 &time) == ERROR_SUCCESS) {310 strcpy(foundJavaVer, buffer);311 foundJava = searchType;312 size = 1024;313 }314 }315 #endif316 317 /*318 * function to find java319 * implements the logic drawn on the dl lab whiteboard in feb 08320 * return a Jvm object which represents the jvm on disk321 */322 bool find( Jvm &jvm ) {323 324 if ( verbose ) cout << "Searching for a JVM" << endl;325 char *javaHome = "";326 bool jvmFound = false;327 328 if ( !jvmFound ) {329 //try JAVA_HOME330 if ( verbose ) cout << " - trying JAVA_HOME: ";331 javaHome = getenv( "JAVA_HOME" );332 if ( javaHome != NULL ) {333 if ( verbose ) cout << "(" << javaHome << ") ";334 jvm.setJavaHome( javaHome );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 }345 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }346 }347 348 if ( !jvmFound ) {349 //try JRE_HOME350 if ( verbose ) cout << " - trying JRE_HOME: ";351 javaHome = getenv( "JRE_HOME" );352 if ( javaHome != NULL ) {353 if ( verbose ) cout << "(" << javaHome << ") ";354 jvm.setJavaHome( javaHome );355 if ( jvm.check() ) {356 if ( use_minimum ) {357 if ( jvm.compare( minimum ) >= 0 ) {358 jvmFound = true;359 }360 } else {361 jvmFound = true;362 }363 }364 }365 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }366 }367 368 #ifdef WINDOWS369 if ( !jvmFound ) {370 //try the registry - this code based on launch4j code371 if ( verbose ) cout << " - trying the registry: ";372 HKEY hKey;373 const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";374 const char sdk[] = "SOFTWARE\\JavaSoft\\Java Development Kit";375 376 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(jre), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {377 regSearch(hKey, jre, FOUND_JRE);378 RegCloseKey(hKey);379 }380 381 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(sdk), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {382 regSearch(hKey, sdk, FOUND_SDK);383 RegCloseKey(hKey);384 }385 386 if ( foundJava != NO_JAVA_FOUND ) {387 char path[1024] = {0};388 char keyBuffer[1024];389 unsigned long datatype;390 unsigned long bufferlength = 1024;391 if (foundJava == FOUND_JRE) {392 strcpy(keyBuffer, jre);393 } else {394 strcpy(keyBuffer, sdk);395 }396 strcat(keyBuffer, "\\");397 strcat(keyBuffer, foundJavaVer);398 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {399 unsigned char buffer[1024] = {0};400 if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer, &bufferlength) == ERROR_SUCCESS) {401 int i = 0;402 do {403 path[i] = buffer[i];404 } while (path[i++] != 0);405 if (foundJava == FOUND_SDK) {406 strcat(path, "\\jre");407 }408 jvm.setJavaHome( path );409 if ( jvm.check() ) {410 if ( use_minimum ) {411 if ( jvm.compare( minimum ) >= 0 ) {412 jvmFound = true;413 }414 } else {415 jvmFound = true;416 }417 }418 }419 RegCloseKey(hKey);420 }421 }422 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }423 }424 #endif425 426 if ( !jvmFound ) {427 //try the hint428 if ( verbose ) cout << " - trying hint: ";429 if ( strcmp(hint.c_str(),"") != 0 ) {430 if ( verbose ) cout << "(" << hint << ") ";431 jvm.setJavaHome( hint );432 if ( jvm.check() ) {433 if ( use_minimum ) {434 if ( jvm.compare( minimum ) >= 0 ) {435 jvmFound = true;436 }437 } else {438 jvmFound = true;439 }440 }441 }442 if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }443 }444 445 446 return jvmFound;447 }448 43 449 44 int main ( int argc, char** argv ) { 450 //cout << "testing process()" << endl; 451 //string out = ""; 452 //process( "dir", out ); 453 //cout << out << endl; 454 //return 0; 455 456 45 46 bool verbose = false; 47 string hint = ""; 48 bool use_minimum = false; 49 bool useJavaw = false; 50 Jvm minimum; 457 51 JvmProperty jvmProperty = JavaHome; 458 52 Action action = Find; … … 507 101 hint = argv[++i]; 508 102 } 103 104 #ifdef WINDOWS 105 } else if ( strcmp(argv[i], "-w") == 0 ) { 106 useJavaw = true; 107 #endif 509 108 510 109 } else { … … 521 120 //find java 522 121 Jvm foundJvm; 523 bool found = find( foundJvm );122 bool found = find( foundJvm, use_minimum, minimum, hint, verbose ); 524 123 525 124 //check if it was found … … 545 144 cout << foundJvm.getVersion() << endl; 546 145 } else if ( jvmProperty == Executable ) { 547 cout << foundJvm.getExecutable() << endl; 146 if ( useJavaw ) { 147 cout << foundJvm.getWinExecutable() << endl; 148 } else { 149 cout << foundJvm.getExecutable() << endl; 150 } 548 151 } else { 549 152 return -1; //should never happen … … 576 179 577 180 string cmd = "\"", output = ""; 578 cmd.append( foundJvm.getExecutable() ); 181 if ( useJavaw ) { 182 cmd.append( foundJvm.getWinExecutable() ); 183 } else { 184 cmd.append( foundJvm.getExecutable() ); 185 } 579 186 cmd.append( "\" -jar " ); 580 187 cmd.append( arg1 ); 581 188 582 process( cmd, output);189 process( cmd, true ); 583 190 584 191 } … … 587 194 } 588 195 196 void usage() { 197 198 cout 199 << "-----------------" << endl 200 << " search4j: usage" << endl 201 << "-----------------" << endl 202 << "Three usage methods: find, compare and launch" << endl 203 << endl 204 << "find: find java and print out information about it" << endl 205 << endl 206 << " search4j [-v|-e]" << endl 207 << " eg: search4j -e" << endl 208 << endl 209 << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl 210 << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl 211 << " if -e is specified, print the path the to the java executable. E.g. C:\\Program Files\\jre1.5.0_15\\bin\\java.exe" << endl 212 << endl 213 << "compare: compare the found java with the given java version string" << endl 214 << endl 215 << " search4j -c VERSION_STRING" << endl 216 << " eg: search4j -c VERSION_STRING" << endl 217 << endl 218 << " print -1 if found java is older" << endl 219 << " print 0 if found java is same" << endl 220 << " print 1 if found java is newer" << endl 221 << endl 222 << "launch: launch the given executable jar with the found java" << endl 223 << endl 224 << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl 225 << " eg: search4j -l greenstone3.jar" << endl 226 << endl 227 << " specify the location of the jar relative to the current directory" << endl 228 << endl 229 << "Global Options:" << endl 230 << " -m VERSION_STRING: (minimum) find a java of the given version or newer, or fail" << endl 231 << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl 232 << " -w: (windows) find and/or use the javaw.exe executable instead of java.exe (in windows only)" << endl 233 << " --verbose : have search4j print out information about what its doing" << endl 234 << " --help : display this usage screen" << endl 235 << endl 236 ; 237 }
Note:
See TracChangeset
for help on using the changeset viewer.