Changeset 15892 for release-kits/shared/search4j
- Timestamp:
- 2008-06-06T11:13:01+12:00 (16 years ago)
- Location:
- release-kits/shared/search4j
- Files:
-
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
release-kits/shared/search4j/compile.sh
r15103 r15892 1 g++ search4j.cpp -o search4j 1 g++ -c libsearch4j.cpp 2 g++ -c search4j.cpp 3 g++ search4j.o libsearch4j.o -o search4j 4 -
release-kits/shared/search4j/libsearch4j.cpp
r15690 r15892 3 3 */ 4 4 5 #include <iostream> 6 #include <fstream> 7 #include <string> 8 #include <sstream> 9 using namespace std; 5 #include "libsearch4j.h" 6 10 7 11 8 #ifdef WINDOWS 9 12 10 #include <windows.h> 13 11 //minimal launch4j stuff … … 19 17 #define pclose _pclose 20 18 #define strcmp _stricmp 19 #define ID_TIMER 1 20 21 21 #endif /* WINDOWS */ 22 22 … … 32 32 } 33 33 34 int process( string command, string &output ) { 34 35 int process( string command, bool render ) { 36 37 #ifdef WINDOWS 38 39 STARTUPINFO si; 40 PROCESS_INFORMATION pi; 41 memset(&pi, 0, sizeof(pi)); 42 memset(&si, 0, sizeof(si)); 43 si.cb = sizeof(si); 44 45 DWORD dwExitCode = -1; 46 char* cmd = (char*)command.c_str(); 47 48 bool result; 49 if ( render ) { 50 result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); 51 } else { 52 result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); 53 } 54 55 if ( result ) { 56 WaitForSingleObject(pi.hProcess, INFINITE); 57 GetExitCodeProcess(pi.hProcess, &dwExitCode); 58 CloseHandle(pi.hThread); 59 CloseHandle(pi.hProcess); 60 } 61 return dwExitCode; 62 63 #else 64 65 return system( command.c_str() ); 66 67 #endif 68 } 69 70 71 72 int process_and_catch_output( string command, string &output ) { 73 74 #ifdef WINDOWS 75 76 char cmd[1024] = "cmd.exe /c "; 77 strcat(cmd, command.c_str()); 78 strcat(cmd, " > cmd_output.txt 2>&1"); 79 //cerr << "command: " << cmd << endl; 80 int code = process( cmd, false ); 81 if ( code!= 0 ) 82 return code; 83 84 string line; 85 ifstream myfile("cmd_output.txt"); 86 if ( !myfile.is_open() ) { 87 return -1; 88 } 89 90 while ( !myfile.eof() ) { 91 getline(myfile,line); 92 output.append( line ); 93 } 94 95 myfile.close(); 96 97 _unlink( "cmd_output.txt" ); 98 99 return code; 100 101 #else 35 102 36 103 FILE *pPipe; 37 104 38 /* Run command so that it writes its output to a pipe. Open this 39 * pipe with read text attribute so that we can read it 40 * like a text file. */ 41 char cmd[1024]; strcpy(cmd, command.c_str()); 42 43 #ifdef WINDOWS 44 if( (pPipe = popen( cmd, "rt" )) == NULL ) { 45 //if ( verbose ) cout << "could not start process" << endl; 46 pclose( pPipe ); 47 return -1; 48 } 49 #endif 50 51 #ifndef WINDOWS 105 char cmd[1024]; 106 strcpy(cmd, command.c_str()); 107 strcat(cmd, " 2>&1" ); 108 109 //if ( verbose ) cout << "process(): running '" << command << "'" << endl; 52 110 if( (pPipe = popen( cmd, "r" )) == NULL ) { 53 111 //if ( verbose ) cout << "could not start process" << endl; … … 55 113 return -1; 56 114 } 57 #endif 115 116 //if ( verbose ) cout << "started process" << endl; 58 117 59 118 /* Read pipe until end of file. */ … … 74 133 /* Close pipe and return return value of pPipe. */ 75 134 int code = pclose( pPipe ); 135 76 136 return code; 77 } 78 79 class Jvm { 80 81 string javaHome; 82 83 // version string. era.major.minor_update. eg: 1.4.2_02 84 int era; 85 int major; 86 int minor; 87 int update; 88 89 bool isJdk; 90 bool healthy; 91 92 93 public: 94 Jvm() { 137 138 #endif 139 140 } 141 142 //Jvm class methods 143 144 Jvm::Jvm() { 145 healthy = false; 146 } 147 148 void Jvm::setJavaHome( string jh ) { 149 healthy = true; 150 javaHome = jh; 151 setVersionFromExeOuput(); 152 } 153 154 string Jvm::getJavaHome() { 155 return javaHome; 156 } 157 158 string Jvm::getExecutable() { 159 string exec = ""; 160 exec.append( javaHome ); 161 162 #ifdef WINDOWS 163 exec.append( "\\bin\\java.exe" ); 164 #endif 165 166 #ifndef WINDOWS 167 exec.append( "/bin/java" ); 168 #endif 169 170 return exec; 171 } 172 173 #ifdef WINDOWS 174 string Jvm::getWinExecutable() { 175 string exec = ""; 176 exec.append( javaHome ); 177 exec.append( "\\bin\\javaw.exe" ); 178 return exec; 179 } 180 #endif 181 182 183 string Jvm::getVersion() { 184 stringstream ss; 185 ss << era << "." << major << "." << minor << "_" << update; 186 return ss.str(); 187 } 188 189 bool Jvm::check() { 190 return healthy; 191 } 192 193 bool Jvm::setVersionFromString( string version ) { 194 era = atoi( version.substr(0,1).c_str() ); 195 major = atoi( version.substr(2,1).c_str() ); 196 minor = atoi( version.substr(4,1).c_str() ); 197 update = atoi( version.substr(6,2).c_str() ); 198 return true; 199 } 200 201 int Jvm::compare( Jvm otherJvm ) { 202 //era 203 if ( era > otherJvm.getEra() ) 204 return 1; 205 else if ( era < otherJvm.getEra() ) 206 return -1; 207 208 //major 209 if ( major > otherJvm.getMajor() ) 210 return 1; 211 else if ( major < otherJvm.getMajor() ) 212 return -1; 213 214 //minor 215 if ( minor > otherJvm.getMinor() ) 216 return 1; 217 else if ( minor < otherJvm.getMinor() ) 218 return -1; 219 220 //update 221 if ( update > otherJvm.getUpdate() ) 222 return 1; 223 else if ( update < otherJvm.getUpdate() ) 224 return -1; 225 226 //all the same so far, must be exactly the same 227 return 0; 228 229 } 230 231 int Jvm::getEra() { return era; } 232 int Jvm::getMajor() { return major; } 233 int Jvm::getMinor() { return minor; } 234 int Jvm::getUpdate() { return update; } 235 236 void Jvm::setVersionFromExeOuput() { 237 string command = "", output = ""; 238 command.append( "\"" ); 239 command.append( getExecutable() ); 240 command.append( "\"" ); 241 command.append(" -version"); 242 243 //cerr << "command: " << command << endl; 244 int result = process_and_catch_output( command, output ); 245 //cerr << "output: " << output << endl; 246 247 if ( result == 0 ) { 248 if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) { 249 era = atoi( output.substr(14,1).c_str() ); 250 major = atoi( output.substr(16,1).c_str() ); 251 minor = atoi( output.substr(18,1).c_str() ); 252 update = atoi( output.substr(20,2).c_str() ); 253 } else { 254 healthy=false; 255 } 256 } else { 95 257 healthy = false; 96 258 } 97 98 void setJavaHome( string jh ) { 99 healthy = true; 100 javaHome = jh; 101 setVersionFromExeOuput(); 102 } 103 104 string getJavaHome() { 105 return javaHome; 106 } 107 108 string getExecutable() { 109 string exec = ""; 110 exec.append( javaHome ); 111 112 #ifdef WINDOWS 113 exec.append( "\\bin\\java.exe" ); 114 #endif 115 116 #ifndef WINDOWS 117 exec.append( "/bin/java" ); 118 #endif 119 120 return exec; 121 } 122 123 #ifdef WINDOWS 124 string getWinExecutable() { 125 string exec = ""; 126 exec.append( javaHome ); 127 exec.append( "\\bin\\javaw.exe" ); 128 return exec; 129 } 130 #endif 131 132 133 string getVersion() { 134 stringstream ss; 135 ss << era << "." << major << "." << minor << "_" << update; 136 return ss.str(); 137 } 138 139 bool check() { 140 return healthy; 141 } 142 143 bool setVersionFromString( string version ) { 144 era = atoi( version.substr(0,1).c_str() ); 145 major = atoi( version.substr(2,1).c_str() ); 146 minor = atoi( version.substr(4,1).c_str() ); 147 update = atoi( version.substr(6,2).c_str() ); 148 return true; 149 } 150 151 int compare( Jvm otherJvm ) { 152 //era 153 if ( era > otherJvm.getEra() ) 154 return 1; 155 else if ( era < otherJvm.getEra() ) 156 return -1; 157 158 //major 159 if ( major > otherJvm.getMajor() ) 160 return 1; 161 else if ( major < otherJvm.getMajor() ) 162 return -1; 163 164 //minor 165 if ( minor > otherJvm.getMinor() ) 166 return 1; 167 else if ( minor < otherJvm.getMinor() ) 168 return -1; 169 170 //update 171 if ( update > otherJvm.getUpdate() ) 172 return 1; 173 else if ( update < otherJvm.getUpdate() ) 174 return -1; 175 176 //all the same so far, must be exactly the same 177 return 0; 178 179 } 180 181 int getEra() { return era; } 182 int getMajor() { return major; } 183 int getMinor() { return minor; } 184 int getUpdate() { return update; } 185 186 private: 187 188 void setVersionFromExeOuput() { 189 string command = "", output = ""; 190 command.append( "\"" ); 191 command.append( getExecutable() ); 192 command.append( "\"" ); 193 command.append(" -version 2>&1"); 194 195 int result = process( command, output ); 196 197 if ( result == 0 ) { 198 if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) { 199 era = atoi( output.substr(14,1).c_str() ); 200 major = atoi( output.substr(16,1).c_str() ); 201 minor = atoi( output.substr(18,1).c_str() ); 202 update = atoi( output.substr(20,2).c_str() ); 203 } else { 204 healthy=false; 205 } 206 } else { 207 healthy = false; 208 } 209 } 210 211 212 }; 259 } 260 261 //end of Jvm class methods 262 213 263 214 264 #ifdef WINDOWS … … 248 298 249 299 if ( verbose ) cout << "Searching for a JVM" << endl; 300 250 301 char *javaHome = ""; 251 302 bool jvmFound = false; 252 303 253 304 if ( !jvmFound ) { 305 254 306 //try JAVA_HOME 255 307 if ( verbose ) cout << " - trying JAVA_HOME: "; … … 272 324 273 325 if ( !jvmFound ) { 326 274 327 //try JRE_HOME 275 328 if ( verbose ) cout << " - trying JRE_HOME: "; … … 293 346 #ifdef WINDOWS 294 347 if ( !jvmFound ) { 348 295 349 //try the registry - this code based on launch4j code 296 char foundJavaVer[ 128] = {0};350 char foundJavaVer[8192] = {0}; 297 351 int foundJava = NO_JAVA_FOUND; 298 352 299 if ( verbose ) cout << " - trying the registry: "; 353 if ( verbose ) cout << " - trying the registry: "; cout.flush(); 300 354 HKEY hKey; 301 355 const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment"; … … 322 376 strcpy(keyBuffer, sdk); 323 377 } 378 324 379 strcat(keyBuffer, "\\"); 325 380 strcat(keyBuffer, foundJavaVer); 381 326 382 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { 327 383 unsigned char buffer[1024] = {0}; … … 334 390 strcat(path, "\\jre"); 335 391 } 392 if ( verbose ) cerr << "path: " << path << endl ; 336 393 jvm.setJavaHome( path ); 337 394 if ( jvm.check() ) { … … 353 410 354 411 if ( !jvmFound ) { 412 355 413 //try the hint 356 414 if ( verbose ) cout << " - trying hint: "; -
release-kits/shared/search4j/search4j.cpp
r15690 r15892 144 144 cout << foundJvm.getVersion() << endl; 145 145 } else if ( jvmProperty == Executable ) { 146 #ifdef WINDOWS 146 147 if ( useJavaw ) { 147 148 cout << foundJvm.getWinExecutable() << endl; … … 149 150 cout << foundJvm.getExecutable() << endl; 150 151 } 152 #else 153 cout << foundJvm.getExecutable() << endl; 154 #endif 151 155 } else { 152 156 return -1; //should never happen … … 179 183 180 184 string cmd = "\"", output = ""; 185 #ifdef WINDOWS 181 186 if ( useJavaw ) { 182 187 cmd.append( foundJvm.getWinExecutable() ); … … 184 189 cmd.append( foundJvm.getExecutable() ); 185 190 } 191 #else 192 cmd.append( foundJvm.getExecutable() ); 193 #endif 186 194 cmd.append( "\" -jar " ); 187 195 cmd.append( arg1 );
Note:
See TracChangeset
for help on using the changeset viewer.