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 | #endif /* WINDOWS */
|
---|
22 |
|
---|
23 | void replace_all ( std::string & str, std::string const & pattern, std::string const & replacement ) {
|
---|
24 |
|
---|
25 | std::string::size_type start = str.find( pattern, 0 );
|
---|
26 |
|
---|
27 | while ( start != str.npos ) {
|
---|
28 | str.replace( start, pattern.size(), replacement );
|
---|
29 | start = str.find( pattern, start+replacement.size() );
|
---|
30 | }
|
---|
31 |
|
---|
32 | }
|
---|
33 |
|
---|
34 | int process( string command, string &output ) {
|
---|
35 |
|
---|
36 | FILE *pPipe;
|
---|
37 |
|
---|
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
|
---|
52 | if( (pPipe = popen( cmd, "r" )) == NULL ) {
|
---|
53 | //if ( verbose ) cout << "could not start process" << endl;
|
---|
54 | pclose( pPipe );
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /* Read pipe until end of file. */
|
---|
60 | while( !feof( pPipe ) ) {
|
---|
61 | char psBuffer[128];
|
---|
62 | //if ( verbose ) cout << "get some data" << endl;
|
---|
63 | if( fgets( psBuffer, 128, pPipe ) != NULL ) {
|
---|
64 |
|
---|
65 | //if ( verbose ) cout << "got: " << psBuffer << endl;
|
---|
66 | output.append( psBuffer );
|
---|
67 |
|
---|
68 | } else {
|
---|
69 | //if ( verbose ) cout << "none left" << endl;
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* Close pipe and return return value of pPipe. */
|
---|
75 | int code = pclose( pPipe );
|
---|
76 | 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() {
|
---|
95 | healthy = false;
|
---|
96 | }
|
---|
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 | };
|
---|
213 |
|
---|
214 | #ifdef WINDOWS
|
---|
215 | int regSearch(HKEY hKey, const char* keyName, int searchType, char* foundJavaVer ) {
|
---|
216 | DWORD x = 0;
|
---|
217 | unsigned long size = 1024;
|
---|
218 | FILETIME time;
|
---|
219 | char buffer[1024] = {0};
|
---|
220 |
|
---|
221 | int foundJava = NO_JAVA_FOUND;
|
---|
222 |
|
---|
223 | while (RegEnumKeyEx(
|
---|
224 | hKey, // handle to key to enumerate
|
---|
225 | x++, // index of subkey to enumerate
|
---|
226 | buffer, // address of buffer for subkey name
|
---|
227 | &size, // address for size of subkey buffer
|
---|
228 | NULL, // reserved
|
---|
229 | NULL, // address of buffer for class string
|
---|
230 | NULL, // address for size of class buffer
|
---|
231 | &time) == ERROR_SUCCESS) {
|
---|
232 | strcpy(foundJavaVer, buffer);
|
---|
233 | foundJava = searchType;
|
---|
234 | size = 1024;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return foundJava;
|
---|
238 |
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * function to find java
|
---|
244 | * implements the logic drawn on the dl lab whiteboard in feb 08
|
---|
245 | * return a Jvm object which represents the jvm on disk
|
---|
246 | */
|
---|
247 | bool find( Jvm &jvm, bool use_minimum, Jvm minimum, string hint, bool verbose ) {
|
---|
248 |
|
---|
249 | if ( verbose ) cout << "Searching for a JVM" << endl;
|
---|
250 | char *javaHome = "";
|
---|
251 | bool jvmFound = false;
|
---|
252 |
|
---|
253 | if ( !jvmFound ) {
|
---|
254 | //try JAVA_HOME
|
---|
255 | if ( verbose ) cout << " - trying JAVA_HOME: ";
|
---|
256 | javaHome = getenv( "JAVA_HOME" );
|
---|
257 | if ( javaHome != NULL ) {
|
---|
258 | if ( verbose ) cout << "(" << javaHome << ") ";
|
---|
259 | jvm.setJavaHome( javaHome );
|
---|
260 | if ( jvm.check() ) {
|
---|
261 | if ( use_minimum ) {
|
---|
262 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
263 | jvmFound = true;
|
---|
264 | }
|
---|
265 | } else {
|
---|
266 | jvmFound = true;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
271 | }
|
---|
272 |
|
---|
273 | if ( !jvmFound ) {
|
---|
274 | //try JRE_HOME
|
---|
275 | if ( verbose ) cout << " - trying JRE_HOME: ";
|
---|
276 | javaHome = getenv( "JRE_HOME" );
|
---|
277 | if ( javaHome != NULL ) {
|
---|
278 | if ( verbose ) cout << "(" << javaHome << ") ";
|
---|
279 | jvm.setJavaHome( javaHome );
|
---|
280 | if ( jvm.check() ) {
|
---|
281 | if ( use_minimum ) {
|
---|
282 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
283 | jvmFound = true;
|
---|
284 | }
|
---|
285 | } else {
|
---|
286 | jvmFound = true;
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
291 | }
|
---|
292 |
|
---|
293 | #ifdef WINDOWS
|
---|
294 | if ( !jvmFound ) {
|
---|
295 | //try the registry - this code based on launch4j code
|
---|
296 | char foundJavaVer[128] = {0};
|
---|
297 | int foundJava = NO_JAVA_FOUND;
|
---|
298 |
|
---|
299 | if ( verbose ) cout << " - trying the registry: ";
|
---|
300 | HKEY hKey;
|
---|
301 | const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
|
---|
302 | const char sdk[] = "SOFTWARE\\JavaSoft\\Java Development Kit";
|
---|
303 |
|
---|
304 | if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(jre), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
|
---|
305 | foundJava = regSearch(hKey, jre, FOUND_JRE, foundJavaVer);
|
---|
306 | RegCloseKey(hKey);
|
---|
307 | }
|
---|
308 |
|
---|
309 | if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(sdk), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS ) {
|
---|
310 | foundJava = regSearch(hKey, sdk, FOUND_SDK, foundJavaVer);
|
---|
311 | RegCloseKey(hKey);
|
---|
312 | }
|
---|
313 |
|
---|
314 | if ( foundJava != NO_JAVA_FOUND ) {
|
---|
315 | char path[1024] = {0};
|
---|
316 | char keyBuffer[1024];
|
---|
317 | unsigned long datatype;
|
---|
318 | unsigned long bufferlength = 1024;
|
---|
319 | if (foundJava == FOUND_JRE) {
|
---|
320 | strcpy(keyBuffer, jre);
|
---|
321 | } else {
|
---|
322 | strcpy(keyBuffer, sdk);
|
---|
323 | }
|
---|
324 | strcat(keyBuffer, "\\");
|
---|
325 | strcat(keyBuffer, foundJavaVer);
|
---|
326 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
|
---|
327 | unsigned char buffer[1024] = {0};
|
---|
328 | if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer, &bufferlength) == ERROR_SUCCESS) {
|
---|
329 | int i = 0;
|
---|
330 | do {
|
---|
331 | path[i] = buffer[i];
|
---|
332 | } while (path[i++] != 0);
|
---|
333 | if (foundJava == FOUND_SDK) {
|
---|
334 | strcat(path, "\\jre");
|
---|
335 | }
|
---|
336 | jvm.setJavaHome( path );
|
---|
337 | if ( jvm.check() ) {
|
---|
338 | if ( use_minimum ) {
|
---|
339 | if ( jvm.compare( minimum ) >= 0 ) {
|
---|
340 | jvmFound = true;
|
---|
341 | }
|
---|
342 | } else {
|
---|
343 | jvmFound = true;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 | RegCloseKey(hKey);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
351 | }
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | if ( !jvmFound ) {
|
---|
355 | //try the hint
|
---|
356 | if ( verbose ) cout << " - trying hint: ";
|
---|
357 | if ( strcmp(hint.c_str(),"") != 0 ) {
|
---|
358 | if ( verbose ) cout << "(" << hint << ") ";
|
---|
359 | jvm.setJavaHome( hint );
|
---|
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 | if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | return jvmFound;
|
---|
375 | }
|
---|