source: release-kits/shared/search4j/search4j.cpp@ 17187

Last change on this file since 17187 was 15892, checked in by oranfry, 16 years ago

change to search4j: using libraries and headers the proper way

File size: 6.2 KB
Line 
1/*
2* Search4j utility
3* Command Line utility to search a system for java, launch a jar, or compare java versions
4* base on launch4j
5*/
6
7#include "libsearch4j.h"
8
9void usage();
10
11enum Action {
12 Find,
13 Launch,
14 Compare
15};
16
17enum JvmProperty {
18 JavaHome,
19 Version,
20 Executable
21};
22
23string actionToString( Action a ) {
24 if ( a == Find )
25 return "Find";
26 else if ( a == Launch )
27 return "Launch";
28 else if ( a == Compare )
29 return "Compare";
30 return "Unknown";
31}
32
33string jvmPropertyToString( JvmProperty jp ) {
34 if ( jp == JavaHome )
35 return "JavaHome";
36 else if ( jp == Version )
37 return "Version";
38 else if ( jp == Executable )
39 return "Executable";
40 return "Unknown";
41}
42
43
44int main ( int argc, char** argv ) {
45
46 bool verbose = false;
47 string hint = "";
48 bool use_minimum = false;
49 bool useJavaw = false;
50 Jvm minimum;
51 JvmProperty jvmProperty = JavaHome;
52 Action action = Find;
53 string arg1 = "";
54
55 //parse commandline arguments
56 for (int i=1; i<argc; i++) {
57 if ( strcmp(argv[i], "--verbose") == 0 ) {
58 verbose = true;
59 } else if ( strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ) {
60 usage();
61 return 0;
62 } else if ( strcmp(argv[i], "-v") == 0 ) {
63 jvmProperty = Version;
64 } else if ( strcmp(argv[i], "-e") == 0 ) {
65 jvmProperty = Executable;
66 } else if ( strcmp(argv[i], "-c") == 0 ) {
67 action = Compare;
68 if ( i == argc-1 ) {
69 usage();
70 return -1;
71 } else {
72 arg1 = argv[++i];
73 }
74
75 } else if ( strcmp(argv[i], "-m") == 0 ) {
76 if ( i == argc-1 ) {
77 usage();
78 return -1;
79 } else {
80 if ( !minimum.setVersionFromString( argv[++i] ) ) {
81 usage();
82 return 0;
83 }
84 use_minimum = true;
85 }
86
87 } else if ( strcmp(argv[i], "-l") == 0 ) {
88 if ( i == argc-1 ) {
89 usage();
90 return -1;
91 } else {
92 action = Launch;
93 arg1 = argv[++i];
94 }
95
96 } else if ( strcmp(argv[i], "-h") == 0 ) {
97 if ( i == argc-1 ) {
98 usage();
99 return -1;
100 } else {
101 hint = argv[++i];
102 }
103
104 #ifdef WINDOWS
105 } else if ( strcmp(argv[i], "-w") == 0 ) {
106 useJavaw = true;
107 #endif
108
109 } else {
110 cout << "unknown option: " << argv[i] << endl;
111 return -1;
112 }
113 }
114
115 //summarise commandline arguments
116 if ( verbose ) {
117 cout << "Action: " << actionToString( action ) << endl;
118 }
119
120 //find java
121 Jvm foundJvm;
122 bool found = find( foundJvm, use_minimum, minimum, hint, verbose );
123
124 //check if it was found
125 if ( found ) {
126 if ( verbose ) cout << "Found JVM at '" << foundJvm.getJavaHome() << "'" << endl;
127 } else {
128 //not found - exit with -1
129 if ( verbose ) cout << "JVM not found" << endl;
130 return -1;
131 }
132
133
134 //Find Action
135 //---------------
136 if ( action == Find ) {
137
138 if ( verbose ) cout << "Property to print: " << jvmPropertyToString( jvmProperty ) << endl;
139
140 //found - print out info about it
141 if ( jvmProperty == JavaHome ) {
142 cout << foundJvm.getJavaHome() << endl;
143 } else if ( jvmProperty == Version ) {
144 cout << foundJvm.getVersion() << endl;
145 } else if ( jvmProperty == Executable ) {
146 #ifdef WINDOWS
147 if ( useJavaw ) {
148 cout << foundJvm.getWinExecutable() << endl;
149 } else {
150 cout << foundJvm.getExecutable() << endl;
151 }
152 #else
153 cout << foundJvm.getExecutable() << endl;
154 #endif
155 } else {
156 return -1; //should never happen
157 }
158
159
160 //Compare Action
161 //---------------
162 } else if ( action == Compare ) {
163
164 if ( verbose ) cout << "Compare to java at: " << arg1 << endl;
165
166 //load a dummy jvm for comparison
167 Jvm givenJvm;
168 if ( !givenJvm.setVersionFromString(arg1) ) {
169 if ( verbose ) cout << "Could not parse that version string" << endl;
170 return -1;
171 }
172
173 //compare
174 cout << foundJvm.compare( givenJvm );
175 return 0;
176
177
178 //Launch Action
179 //---------------
180 } else if ( action == Launch ) {
181
182 if ( verbose ) cout << "Jar to launch: " << arg1 << endl;
183
184 string cmd = "\"", output = "";
185 #ifdef WINDOWS
186 if ( useJavaw ) {
187 cmd.append( foundJvm.getWinExecutable() );
188 } else {
189 cmd.append( foundJvm.getExecutable() );
190 }
191 #else
192 cmd.append( foundJvm.getExecutable() );
193 #endif
194 cmd.append( "\" -jar " );
195 cmd.append( arg1 );
196
197 process( cmd, true );
198
199 }
200
201 return 0;
202}
203
204void usage() {
205
206 cout
207 << "-----------------" << endl
208 << " search4j: usage" << endl
209 << "-----------------" << endl
210 << "Three usage methods: find, compare and launch" << endl
211 << endl
212 << "find: find java and print out information about it" << endl
213 << endl
214 << " search4j [-v|-e]" << endl
215 << " eg: search4j -e" << endl
216 << endl
217 << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl
218 << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl
219 << " 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
220 << endl
221 << "compare: compare the found java with the given java version string" << endl
222 << endl
223 << " search4j -c VERSION_STRING" << endl
224 << " eg: search4j -c VERSION_STRING" << endl
225 << endl
226 << " print -1 if found java is older" << endl
227 << " print 0 if found java is same" << endl
228 << " print 1 if found java is newer" << endl
229 << endl
230 << "launch: launch the given executable jar with the found java" << endl
231 << endl
232 << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl
233 << " eg: search4j -l greenstone3.jar" << endl
234 << endl
235 << " specify the location of the jar relative to the current directory" << endl
236 << endl
237 << "Global Options:" << endl
238 << " -m VERSION_STRING: (minimum) find a java of the given version or newer, or fail" << endl
239 << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl
240 << " -w: (windows) find and/or use the javaw.exe executable instead of java.exe (in windows only)" << endl
241 << " --verbose : have search4j print out information about what its doing" << endl
242 << " --help : display this usage screen" << endl
243 << endl
244 ;
245}
Note: See TracBrowser for help on using the repository browser.