-
Notifications
You must be signed in to change notification settings - Fork 33
/
util.c
1038 lines (897 loc) · 27.8 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* @(#)util.c 1.17 07/19/07
*/
#include <windows.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "mqapp.h"
#include "registry.h"
int SourceFile(const char *file);
int myisspace(const char c);
int mymkdir(const char *path);
#define IMQ_JAVAHOME "IMQ_JAVAHOME"
#define JAVA_HOME "JAVA_HOME"
#define IMQ_DEFAULT_JAVAHOME "IMQ_DEFAULT_JAVAHOME"
#define IMQ_DEFAULT_VARHOME "IMQ_DEFAULT_VARHOME"
#define IMQ_DEFAULT_EXT_JARS "IMQ_DEFAULT_EXT_JARS"
#define IMQENV1 "\\etc\\imqenv.conf"
#define IMQENV2 "\\..\\etc\\mq\\imqenv.conf"
#define SET "set "
#define REM "REM "
#define POUND "#"
#define JDK_JRE_START "SOFTWARE\\JavaSoft\\Java Runtime Environment"
#define JDK_CURVER "CurrentVersion"
#define JDK_HOME_STR "JavaHome"
#define SKIP_WHITE(p) while (myisspace(*p)) p++
/*
* VMs to use for JNI java clients. Order is most desireable to
* least desireable.
* Relative paths are assumed to be relative to jrehome/bin
*/
char *client_vm_libs[] = {
"client\\jvm.dll",
"server\\jvm.dll",
"classic\\jvm.dll",
"hotspot\\jvm.dll"
};
int nclient_vm_libs = sizeof (client_vm_libs) / sizeof(char *);
/**
* Find a suitable Java runtime. Here is where we look.
*
* 1. IMQ_JAVAHOME
* 2. What's set for IMQ_JAVAHOME in %IMQ_HOME%\var\jdk-env.bat
* 3. IMQ_DEFAULT_JAVAHOME (may be set via imqenv.conf)
* 4. %IMQ_HOME%\jre (bundled)
*
* home Value of IMQ_HOME
* varhome Value of IMQ_VARHOME
* path Memory location to copy path to runtime into. It must be
* at least size MAX_PATH. If it is a JDK then the path
* will point to the "jre" directory.
*
* returns 0 if we find a runtime, else < 0
*/
int FindJavaRuntime(const char *home, const char *varhome, char *path) {
char tmpbuf[MAX_PATH];
char pathbuf[MAX_PATH];
char regbuf[MAX_PATH];
char versionbuf[256];
int vn = 256;
int tb = MAX_PATH;
char *p = NULL;
HANDLE hfile = 0;
*path = '\0';
regbuf[0] = '\0';
memset(tmpbuf, '\0', sizeof(tmpbuf));
memset(pathbuf, '\0', sizeof(pathbuf));
memset(versionbuf, '\0', sizeof(versionbuf));
if ((p = getenv(IMQ_JAVAHOME)) == NULL) {
/* IMQ_JAVAHOME not set. Try and get it from jdk-env.bat file */
strcpy(tmpbuf, varhome);
strcat(tmpbuf, "\\jdk-env.bat");
if (_access(tmpbuf, 00) == 0) {
/* This should set IMQ_JAVAHOME */
SourceFile(tmpbuf);
p = getenv(IMQ_JAVAHOME);
}
}
if (p == NULL) {
/* Still no IMQ_JAVAHOME. Check for IMQ_DEFAULT_JAVAHOME */
p = getenv(IMQ_DEFAULT_JAVAHOME);
}
if (p == NULL) {
/* Still no IMQ_JAVAHOME. Look for bundled */
strcpy(tmpbuf, home);
strcat(tmpbuf, "\\jre");
if (_access(tmpbuf, 00) == 0) {
p = tmpbuf;
}
}
if (p == NULL) {
// Still no IMQ_JAVAHOME. Look for JAVA_HOME variable
p = getenv("JAVA_HOME");
}
if (p == NULL) {
/* Still no JAVA_HOME, search the registry */
/* Get the version from JDK_JRE_START */
versionbuf[0]='\0';
getAnyStringFromRegistry(JDK_JRE_START, versionbuf,&vn,JDK_CURVER);
/* Get the jdk from JDK_START + version + JDK_HOME_STR */
regbuf[0]= '\0';
strcpy(regbuf, JDK_JRE_START);
strcat(regbuf, "\\");
strcat(regbuf, versionbuf);
tmpbuf[0] = '\0';
getAnyStringFromRegistry(regbuf, tmpbuf, &tb, JDK_HOME_STR);
if (tmpbuf[0] != '\0' ) {
//OK - make sure the registry isn't messages up
if (_access(tmpbuf, 00) == 0) {
p = tmpbuf;
}
}
}
if (p == NULL) {
/* Still no JAVA_HOME, fallback to the path ?? */
return 0;
}
if (_access(p, 00) == 0) {
/*
* Valid IMQ_JAVAHOME. either it is pointing to a JDK or a JRE.
* We always return a path to the JRE directory.
*/
strcpy(pathbuf, p);
strcat(pathbuf, "\\jre");
if (_access(pathbuf, 00) == 0) {
/* IMQ_JAVAHOME was pointing to a JDK */
strcpy(path, pathbuf);
} else {
/* IMQ_JAVAHOME was pointing to a JRE */
strcpy(path, p);
}
/* printf("===== Found java runtime %s\n", path); */
return 0;
} else {
//Handle new logic
//First check the registry
//Then check for JAVA_HOME
//finally we don't know what to do
/* Return invalid path so we can give a good error message */
strcpy(path, p);
return -1;
}
}
/*
* Our version of isspace(). Why? Because I'm getting link errors
* on windows if I use isspace() and I don't have time to figure out
* why.
*/
int myisspace(const char c) {
return (c != '\0' && c <= ' ');
}
/*
* Parse path and mkdir each directory one at a time
* since _mkdir will only work on one path.
*/
int mymkdir(const char *path) {
char *onePath;
char buildPath[MAX_PATH];
char p[MAX_PATH];
/* printf("mymkdir path is %s\n", path); */
buildPath[0] = '\0';
p[0] = '\0';
// Make a copy of path so we don't modify.
strcpy(p, path);
onePath = strtok(p, "/\\");
while (onePath != NULL) {
/* printf("onePath is %s\n", onePath); */
strcat(buildPath, onePath);
strcat(buildPath, "\\");
/* printf("buildPath is %s\n", buildPath);*/
// If this is drive letter, then just try to access it.
if (strlen(buildPath) == 3 && buildPath[1] == ':') {
/* printf("this is a drive, try accessing it\n"); */
if (_access(onePath, 00) != 0) {
fprintf(stderr, "Cannot access drive %s from %s.\n", buildPath, path);
return -1;
}
} else {
/* printf("Try accessing buildPath %s\n", buildPath); */
// Try accessing buildPath.
// If cannot access it, then try to mkdir it.
if (_access(buildPath, 00) != 0) {
/* printf("couldn't access %s so try to mkdir it \n", buildPath); */
if (_mkdir(buildPath) != 0) {
fprintf(stderr, "Cannot create directory %s in path %s\n", buildPath, path);
return -1;
}
}
}
onePath = strtok(NULL, "/\\");
}
return 0;
}
/*
* Set environment variables based on "set" commands in a file.
* For example if the file contains
* set COLOR=blue
* set SIZE=large
* The COLOR and SIZE would be set in the current process' environment.
* All other lines in the file are ignored.
*/
int SourceFile(const char *file) {
char buf[MAX_PATH];
char env[MAX_PATH];
FILE *fp;
char *p;
char *start = NULL;
if ((fp = fopen(file, "r")) == NULL) {
sprintf(buf, "MQ service couldn't read configuration file %s", file);
perror(buf);
return -1;
}
memset(buf, '\0', sizeof(buf));
while (fgets(buf, sizeof(buf), fp) != NULL) {
p = buf;
*env = '\0';
/* Skip leading white space */
SKIP_WHITE(p);
if (strncmp(REM, p, strlen(REM)) == 0 ||
strncmp(POUND, p, strlen(POUND)) == 0) {
// Skip comments REM or #
;
} else if (strncmp(SET, p, strlen(SET)) == 0) {
/* Skip "set "*/
p += strlen(SET);
SKIP_WHITE(p);
/* Start of variable */
start = p;
/* Find end of varible */
while (!myisspace(*p) && *p != '=') {
p++;
}
if (*p == '\0') {
continue;
}
*p = '\0';
strcpy(env, start);
strcat(env, "=");
p++;
SKIP_WHITE(p);
if (*p == '=') {
p++;
SKIP_WHITE(p);
}
/* Get value */
start = p;
while (*p != '\0' && *p != '\n') {
p++;
}
*p = '\0';
strcat(env, start);
if (*env != '\0') {
/* printf("===== Setting %s\n", env); */
_putenv(_strdup(env));
}
} else if (strncmp(IMQ_DEFAULT_VARHOME, p, strlen(IMQ_DEFAULT_VARHOME)) == 0 ||
strncmp(IMQ_DEFAULT_JAVAHOME, p, strlen(IMQ_DEFAULT_JAVAHOME)) == 0 ||
strncmp(IMQ_DEFAULT_EXT_JARS, p, strlen(IMQ_DEFAULT_EXT_JARS)) == 0) {
start = p;
/* Find end of variable */
while (!myisspace(*p) && *p != '=') {
p++;
}
if (*p == '\0') {
continue;
}
*p = '\0';
strcpy(env, start);
strcat(env, "=");
p++;
SKIP_WHITE(p);
if (*p == '=') {
p++;
SKIP_WHITE(p);
}
/* Get value */
start = p;
while (*p != '\0' && *p != '\n') {
p++;
}
*p = '\0';
strcat(env, start);
if (*env != '\0') {
/* printf("===== Setting unix style %s\n", env); */
_putenv(_strdup(env));
}
}
}
fclose(fp);
return 0;
}
/**
* Add all the jar files in the $IMQ_VARHOME/lib/ext directory to classpath.
* These are user defined jar files needed for things like JDBC connectors.
*
* Note: External JARs can also be specified with IMQ_DEFAULT_EXT_JARS
* and may be set via imqenv.conf.
*/
VOID MqAppAddLibJars(const char *libhome, char *classpath) {
WIN32_FIND_DATA dir;
char lib[MAX_PATH];
HANDLE handle;
/* Added $IMQ_HOME/lib/ext directory */
strcat(classpath, ";");
strcat(classpath, libhome);
strcat(classpath, "\\ext");
sprintf(lib, "%s\\ext\\*", libhome);
/* Add jars if any */
handle = FindFirstFile(lib, &dir);
if (handle == INVALID_HANDLE_VALUE) {
return;
}
do {
int len = strlen(dir.cFileName);
if (len <= 4) {
/* not a .jar file */
} else if (strcmp(dir.cFileName + len - 4, ".jar") != 0 &&
strcmp(dir.cFileName + len - 4, ".zip") != 0) {
/* File doesn't end in .jar or .zip */
} else {
strcat(classpath, ";");
strcat(classpath, libhome);
strcat(classpath, "\\ext\\");
strcat(classpath, dir.cFileName);
}
} while (FindNextFile(handle, &dir));
}
/*
* Do only MQ related initialization - no java initialization. This basically locates IMQ
* and sets the relevant pieces of info in the MqEnv structure.
*
* The following fields in the MqEnv are updated.
* imqhome
* imqlibhome
* imqvarhome
* imqextjars
*
* All the fields above need to point to pre-allocated space.
*
* Params:
* me ptr to MqEnv structure, must not be NULL.
*/
int MqAppInitializeNoJava(MqEnv *me)
{
char *slash_loc;
char *p;
char tmpbuf[MAX_PATH];
int newfs = 0;
/* Determine IMQ_HOME */
GetModuleFileName(0, me->imqhome, sizeof(me->imqhome));
/* Take off file name and bin directory */
slash_loc = strrchr(me->imqhome, '\\');
*slash_loc = '\0';
slash_loc = strrchr(me->imqhome, '\\');
*slash_loc = '\0';
if (me->imqlibhome[0] == '\0') {
strcpy(me->imqlibhome, me->imqhome);
strcat(me->imqlibhome, "\\lib");
}
/* Source imqenv.conf file to set default values
* For example this may set IMQ_DEFAULT_JAVAHOME, IMQ_DEFAULT_VARHOME,
* and IMQ_DEFAULT_EXT_JARS
*/
/* First check the new filesystem layout location */
memset(tmpbuf, 0, MAX_PATH);
strncpy(tmpbuf, me->imqhome, MAX_PATH - strlen(IMQENV2) - 2 );
strcat(tmpbuf, IMQENV2);
if (_access(tmpbuf, 00) == 0) {
SourceFile(tmpbuf);
/* Note: this also means a different imqetchome */
strcpy(me->imqetchome, me->imqhome);
strcat(me->imqetchome, "\\..\\etc\\mq");
newfs = 1;
} else { /* use the original imqconf.env */
memset(tmpbuf, 0, MAX_PATH);
strncpy(tmpbuf, me->imqhome, MAX_PATH - strlen(IMQENV1) - 2 );
strcat(tmpbuf, IMQENV1);
if (_access(tmpbuf, 00) == 0) {
SourceFile(tmpbuf);
strcpy(me->imqetchome, me->imqhome);
strcat(me->imqetchome, "\\etc");
} else {
/*
printf("NO ENV.CONF : %s\n", me->imqhome);
*/
}
}
/* Determine imqvarhome. This can be set via an environment variable */
if (me->imqvarhome[0] == '\0') {
if ((p = getenv("IMQ_VARHOME")) == NULL) {
if ((p = getenv("IMQ_DEFAULT_VARHOME")) != NULL) {
/* IMQ_DEFAULT_VARHOME may be set by imqenv.conf */
strcpy(me->imqvarhome, p);
} else if (newfs == 1) {
strcpy(me->imqvarhome, me->imqhome);
strcat(me->imqvarhome, "\\..\\var\\mq");
} else {
strcpy(me->imqvarhome, me->imqhome);
strcat(me->imqvarhome, "\\var");
}
} else {
strcpy(me->imqvarhome, p);
}
}
/* Determine imqextjars. This can be set via an environment variable */
if (me->imqextjars[0] == '\0') {
if ((p = getenv("IMQ_DEFAULT_EXT_JARS")) != NULL) {
/* IMQ_DEFAULT_EXT_JARS may be set by imqenv.conf */
strcpy(me->imqextjars, p);
}
}
// Change working directory to imqhome/bin
memset(tmpbuf, 0, MAX_PATH);
if (me->imqhome[0] != '\0') {
strcpy(tmpbuf, me->imqhome);
strcat(tmpbuf, "\\bin");
if (0 != _chdir(tmpbuf)){
fprintf(stderr, "Unable to change working directory to %s", tmpbuf);
}
}
/*
printf("IMQ_HOME: %s\n", me->imqhome);
printf("IMQ_VARHOME: %s\n", me->imqvarhome);
printf("IMQ_ETCHOME: %s\n", me->imqetchome);
printf("IMQ_EXTJARS: %s\n", me->imqextjars);
*/
return 0;
}
/*
* Do some initialization. This basically locates IMQ, the JDK, determines
* the run classpath (for -cp later).
*
* The following fields in the MqEnv are updated.
* imqhome
* imqlibhome
* imqvarhome
* imqextjars
* jrehome
* classpath
*
* All the fields above need to point to pre-allocated space.
*
* Params:
* me ptr to MqEnv structure, must not be NULL.
* classpath_entries Array containing entries relative to IMQ_HOME/lib that
* need to be added to the run classpath. This must not
* be NULL.
* nclasspath_entries Number of classpath entries in classpath_entries.
* append_libjars Boolean flag - if true the jars/zips in IMQ_HOME/lib/ext
* will be appended to the run classpath.
* append_classpath Boolean flag - if true the value of CLASSPATH will
* be appended to the run classpath.
*/
int MqAppInitialize(MqEnv *me, char *classpath_entries[], int nclasspath_entries,
BOOL append_libjars, BOOL append_classpath)
{
char *p;
int n;
char tmpbuf[MAX_PATH];
/*
* Initialize the MQ related fields
*/
MqAppInitializeNoJava(me);
if (*(me->jrehome) == '\0') {
/* Locate a java runtime. */
FindJavaRuntime(me->imqhome, me->imqvarhome, me->jrehome);
}
/** LKS-XXX
* Removing for glassfish v3, we need to default to the path nothing is set.
* Note: the error message will not be as clean if it fails (will do post FF)
if (*(me->jrehome) == '\0') {
fprintf(stderr, "Please specify a Java runtime using the IMQ_JAVAHOME\nenvironment variable, or -javahome command line option\n");
return -1;
}
if (_access(me->jrehome, 00) < 0) {
fprintf(stderr, "Invalid Java Runtime '%s'", me->jrehome);
return -1;
}
End of REMOVE (LKS-XXX) *
*/
/* Initialize classpath */
*(me->classpath) = '\0';
for (n = 0; n < nclasspath_entries; n++) {
if (n != 0) strcat(me->classpath, ";");
if (*classpath_entries[n] != '\\') {
strcat(me->classpath, me->imqhome);
strcat(me->classpath, "\\lib\\");
}
strcat(me->classpath, classpath_entries[n]);
}
/*
* If IMQ_DEFAULT_EXT_JARS is set, append it to the value of 'classpath'
*/
if (me->imqextjars[0] != '\0') {
strcat(me->classpath, ";");
strcat(me->classpath, me->imqextjars);
}
if (append_libjars) {
MqAppAddLibJars(me->imqlibhome, me->classpath);
}
if (append_classpath) {
/*
* If CLASSPATH is set, append it to the value of 'classpath'
*/
if ((p = getenv("CLASSPATH")) != NULL) {
strcat(me->classpath, ";");
strcat(me->classpath, p);
}
}
// Change working directory to imqhome/bin
memset(tmpbuf, 0, MAX_PATH);
if (me->imqhome[0] != '\0') {
strcpy(tmpbuf, me->imqhome);
strcat(tmpbuf, "\\bin");
if (0 != _chdir(tmpbuf)){
fprintf(stderr, "Unable to change working directory to %s", tmpbuf);
}
}
/*
printf("IMQ_HOME: %s\n", me->imqhome);
printf("IMQ_VARHOME: %s\n", me->imqvarhome);
printf("IMQ_EXTJARS: %s\n", me->imqextjars);
printf("jrehome: %s\n", me->jrehome);
*/
return 0;
}
/*
* Parse generic application arguments, setting the relevant
* fields in the MqEnv structure.
*
* The following fields in the MqEnv are updated.
* jrehome
* application_argv
* application_argc
*
* The application_argv field needs to point to pre-allocated
* array of (char *) pointers.
*
* Params:
* me ptr to MqEnv structure, must not be NULL.
* argv argument vector
* argc number of elements in argv
*/
void MqAppParseArgs (MqEnv *me, char *argv[], int argc)
{
argv++; argc--;
while (argc > 0) {
if (strcmp(*argv, "-javahome") == 0) {
argv++; argc--;
if (argc > 0) {
strncpy(me->jrehome, *argv, sizeof(me->jrehome) - 32);
strcat(me->jrehome, "\\jre");
}
} else if (strcmp(*argv, "-jrehome") == 0) {
argv++; argc--;
if (argc > 0) {
strncpy(me->jrehome, *argv, sizeof(me->jrehome));
}
} else if (strcmp(*argv, "-varhome") == 0) {
argv++; argc--;
if (argc > 0) {
strncpy(me->imqvarhome, *argv, sizeof(me->imqvarhome));
}
} else {
/* We don't recognize the option, pass it on to application */
me->application_argv[me->application_argc] = _strdup(*argv);
me->application_argc++;
}
argv++; argc--;
}
}
/*
* Create the java command line based on information
* in the MqEnv structure.
*
* No fields in the MqEnv are updated.
*
* Params:
* me ptr to MqEnv structure, must not be NULL.
* set_varhome Boolean flag that indicates whether "-Dimq.varhome"
* should be appended to the command line or not.
* cmdLine buffer for storing command line. This
* points to pre allocated space.
*/
void MqAppCreateJavaCmdLine(MqEnv *me, BOOL set_varhome, char *cmdLine)
{
char javaCmd[512];
int jvm_argc = 0, i;
char *jvm_argv_buffer[128];
char **jvm_argv = &(jvm_argv_buffer[1]);
jvm_argv[jvm_argc] = malloc(sizeof(me->classpath) + 80);
sprintf(jvm_argv[jvm_argc], "-cp");
jvm_argc++;
jvm_argv[jvm_argc] = malloc(sizeof(me->classpath) + 80);
sprintf(jvm_argv[jvm_argc], "%s", me->classpath);
jvm_argc++;
jvm_argv[jvm_argc] = malloc(sizeof(me->imqhome) + 80);
sprintf(jvm_argv[jvm_argc], "-Dimq.home=%s", me->imqhome);
jvm_argc++;
if (set_varhome) {
jvm_argv[jvm_argc] = malloc(sizeof(me->imqvarhome) + 80);
sprintf(jvm_argv[jvm_argc], "-Dimq.varhome=%s", me->imqvarhome);
jvm_argc++;
}
if (me->imqetchome != '\0' ) { /* we've set etchome */
jvm_argv[jvm_argc] = malloc(sizeof(me->imqetchome) + 80);
sprintf(jvm_argv[jvm_argc], "-Dimq.etchome=%s", me->imqetchome);
jvm_argc++;
}
if (me->jrehome[0] == '\0') {
strcat(javaCmd, "\"java\"");
} else {
sprintf(javaCmd, "\"%s\\bin\\java\"", me->jrehome);
}
/* Copy Java command and command line arguments into command line */
strcpy(cmdLine, javaCmd);
for (i = 0; i < jvm_argc; i++) {
strcat(cmdLine, " ");
strcat(cmdLine, "\"");
strcat(cmdLine, jvm_argv[i]);
strcat(cmdLine, "\"");
}
/* Copy main class into command line */
strcat(cmdLine, " ");
strcat(cmdLine, me->main_class);
strcat(cmdLine, " ");
/* Copy main class arguments into command line */
for (i = 0; i < me->application_argc; i++) {
strcat(cmdLine, " ");
strcat(cmdLine, "\"");
strcat(cmdLine, me->application_argv[i]);
strcat(cmdLine, "\"");
}
}
/*
* Run the command, wait for it to exit,
* and return the exit code.
* A process is forked to run the command via
* CreateProcess()
*
* Params:
* cmdLine buffer for storing command line. This
* points to pre allocated space.
*/
DWORD MqAppRunCmd(char *cmdLine)
{
/* Information about the forked child process */
STARTUPINFO jsi;
PROCESS_INFORMATION jpi;
/* exit code */
DWORD exitCode = 0;
ZeroMemory(&jsi, sizeof(jsi));
jsi.cb = sizeof(jsi);
ZeroMemory(&jpi, sizeof(jpi));
/*
* Run a Java application and block until it exits.
*/
/* Execute the child process */
if ( !CreateProcess(
NULL, /* Use cmdLine */
cmdLine, /* Command line */
NULL, /* Prcess handle not inheritable. */
NULL, /* Thread handle not inheritable. */
TRUE, /* Set handle inheritance to TRUE. */
0, /* No creation flags, use parent's console. */
NULL, /* Use parent's environment block */
NULL, /* Use parent's current directory */
&jsi, /* Pointer to STARTUPINFO struct [in] */
&jpi /* Pointer to PROCESS_INFORMATION struct [out] */
)
) {
printf("Starting process failed. Could not execute %s", cmdLine);
}
/* Wait for child to exit */
WaitForSingleObject (jpi.hProcess, INFINITE);
/* Get exit code from child process */
if (!GetExitCodeProcess(jpi.hProcess, &exitCode)) {
fprintf(stderr, "Unable to get child's exit code.");
}
/* Close process and thread handles */
CloseHandle(jpi.hProcess);
CloseHandle(jpi.hThread);
return (exitCode);
}
/*
* Run the java command using the information in the
* MqEnv structure passed in.
* This function uses the VM invocation APIs in JNI.
*
* Params:
* me ptr to MqEnv structure, must not be NULL.
*/
DWORD MqAppJNIRunJavaCmd(MqEnv *me)
{
FARPROC MqApp_JNI_CreateJavaVM = NULL;
HINSTANCE hinstLib;
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[4];
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
DWORD exitCode = 0;
char dllpath[MAX_PATH];
char *main_class, *tmp;
int i;
/*
* Try to load the VM's in the client_vm_libs array.
* Stop at the first successful load.
*/
for (i = 0; i < nclient_vm_libs; i++)
{
/*
* Construct path to one jvm.dll in client_vm_libs array
*/
dllpath[0] = '\0';
strcpy(dllpath, me->jrehome);
strcat(dllpath, "\\bin\\");
strcat(dllpath, client_vm_libs[i]);
if ((hinstLib = LoadLibrary(dllpath)) == NULL) {
continue;
} else {
break;
}
}
/*
* If can't load any VM libs, print error msg and return.
*/
if (hinstLib == NULL)
{
/*
* Couldn't load a VM
*/
fprintf(stderr, "Couldn't load any of the following Java VMs from %s:\n",
me->jrehome);
for (i = 0; i < nclient_vm_libs; i++)
{
fprintf(stderr, "\t%s\n", client_vm_libs[i]);
}
return (1);
}
/*
* Get function ptr to JNI_CreateJavaVM()
*/
MqApp_JNI_CreateJavaVM = GetProcAddress(hinstLib, "JNI_CreateJavaVM");
if (NULL == MqApp_JNI_CreateJavaVM)
{
fprintf(stderr, "Failed to obtain func ptr to JNI_CreateJavaVM() !\n");
return (1);
}
/*
* Setup for creating VM.
*/
/*
* Construct VM args
*/
i = 0;
options[i].optionString = malloc(sizeof(me->classpath) + 80);
sprintf(options[i].optionString, "-Djava.class.path=%s", me->classpath);
i++;
options[i].optionString = malloc(sizeof(me->imqhome) + 80);
sprintf(options[i].optionString, "-Dimq.home=%s", me->imqhome);
i++;
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = i;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = (*MqApp_JNI_CreateJavaVM)(&jvm, (void **)&env, &vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
return (1);
}
/*
* Convert main_class string in MqEnv to class string
* with slashes instead of periods.
*/
main_class = _strdup(me->main_class);
tmp = main_class;
while ((tmp = strchr(tmp, '.')) != NULL) {
*tmp = '/';
}
/*
* Load main class.
*/
cls = (*env)->FindClass(env, main_class);
if (cls == NULL)
{
fprintf(stderr, "Failed to load class: %s\n", main_class);
return (1);
}
/*
* Get method ID for main method.
*/
mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
if (mid == NULL)
{
fprintf(stderr, "Cannot find main method.\n");
return (1);
}
/*
* Construct main method (application) args.
*/
if (me->application_argc > 0) {
args = (*env)->NewObjectArray(env, me->application_argc,
(*env)->FindClass(env, "java/lang/String"), NULL);
if (args == NULL)
{
fprintf(stderr, "Failed to allocate args array.\n");
return (1);
}
for (i = 0; i < me->application_argc; i++) {
jstr = (*env)->NewStringUTF(env, me->application_argv[i]);
if (jstr == NULL)
{
fprintf(stderr,
"Failed to allocate arg: %s\n", me->application_argv[i]);
return (1);
}
(*env)->SetObjectArrayElement(env, args, i, jstr);
}
} else {
args = (*env)->NewObjectArray(env, 0,
(*env)->FindClass(env, "java/lang/String"), NULL);
}
/*
* Invoke main method.
*/
(*env)->CallStaticVoidMethod(env, cls, mid, args);
if ((*env)->ExceptionOccurred(env))
{
(*env)->ExceptionDescribe(env);
}
/*
* Cleanup:
* - Detach the current thread so that it appears to have exited when
* the application's main method exits.
* - Destroy VM.
*/
if ((*jvm)->DetachCurrentThread(jvm) != 0) {
fprintf(stderr, "Could not detach main thread.\n");
exitCode = 1;
}