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
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) INRIA - Allan CORNET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
/*--------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include "machine.h"
#include "JVM_commons.h"
#include "JVM_functions.h"
#include "sci_mem_alloc.h"
/*--------------------------------------------------------------------------*/
#define JVM_TYPE "client"
/* #define JVM_TYPE "server" */
/* Every form of Sun's Java runtime comes with both the "client VM" and the "server VM."
Unfortunately, Java applications and applets run by default in the client VM.
The Server VM is much faster than the Client VM,
but it has the downside of taking around 10% longer to start up, and it uses more memory.
*/
/*--------------------------------------------------------------------------*/
static JavaVM *SearchCreatedJavaVMEmbedded(char *SCILAB_PATH);
static JavaVM *SearchCreatedJavaVMPath(void);
#ifdef __APPLE__
/* I guess Apple likes to make my life harder ...
* They are renaming the name of the dynamic lib especially for jni lib...
* Therefor, I must change the name only for mac os X from dynlib
*/
#undef SHARED_LIB_EXT
#define SHARED_LIB_EXT ".jnilib"
#define LIBJAVANAME "libjava"
#else
#define LIBJAVANAME "libjvm"
#endif
/*--------------------------------------------------------------------------*/
static BOOL EMBEDDED_JRE = FALSE;
/*--------------------------------------------------------------------------*/
BOOL LoadDynLibJVM(char *SCILAB_PATH)
{
/* 1. search in SCI/java/jre
* 2. search in LD_LIBRARY_PATH and co (see man dlopen)
* else ERROR Java not found */
BOOL bOK = FALSE;
char *JVMLibFullName = NULL;
/* 1. search in SCI/java/jre */
JVMLibFullName =
(char *)MALLOC((strlen(SCILAB_PATH) + strlen(JRE_PATH) + strlen("/bin/") + strlen(JVM_TYPE) + strlen("/libjava") + strlen(SHARED_LIB_EXT) + 1)
* sizeof(char));
sprintf(JVMLibFullName, "%s%s%s%s%s%s", SCILAB_PATH, JRE_PATH, "/bin/", JVM_TYPE, "/libjava", SHARED_LIB_EXT);
if (LoadFunctionsJVM(JVMLibFullName) == NULL)
{
/* 2. search in LD_LIBRARY_PATH */
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
};
JVMLibFullName = (char *)MALLOC((strlen(LIBJAVANAME) + strlen(SHARED_LIB_EXT) + 1) * sizeof(char));
sprintf(JVMLibFullName, "%s%s", LIBJAVANAME, SHARED_LIB_EXT);
if (LoadFunctionsJVM(JVMLibFullName))
{
bOK = TRUE;
}
}
else
{
EMBEDDED_JRE = TRUE;
bOK = TRUE;
}
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
};
return bOK;
}
/*--------------------------------------------------------------------------*/
BOOL withEmbeddedJRE(void)
{
return EMBEDDED_JRE;
}
/*--------------------------------------------------------------------------*/
static JavaVM *SearchCreatedJavaVMEmbedded(char *SCILAB_PATH)
{
JavaVM *jvm = NULL;
jsize jvm_count = 0;
jint res = 0;
char *JVMLibFullName = NULL;
/* search in SCI/java/jre */
JVMLibFullName =
(char *)MALLOC((strlen(SCILAB_PATH) + strlen(JRE_PATH) + strlen("/bin/") + strlen(JVM_TYPE) + strlen("/libjava") + strlen(SHARED_LIB_EXT) + 1)
* sizeof(char));
sprintf(JVMLibFullName, "%s%s%s%s%s%s", SCILAB_PATH, JRE_PATH, "/bin/", JVM_TYPE, "/libjava", SHARED_LIB_EXT);
FreeDynLibJVM();
if (LoadFunctionsJVM(JVMLibFullName))
{
res = SciJNI_GetCreatedJavaVMs(&jvm, 1, &jvm_count);
if (res != JNI_OK)
{
fprintf(stderr, "\nJNI_GetCreatedJavaVMs failed to detect any started Java VM.\n");
return NULL;
}
if (jvm_count == 1)
{
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
}
return jvm;
}
else
{
jvm = NULL;
}
}
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
}
return jvm;
}
/*--------------------------------------------------------------------------*/
static JavaVM *SearchCreatedJavaVMPath(void)
{
JavaVM *jvm = NULL;
jsize jvm_count = 0;
jint res = 0;
char *JVMLibFullName = NULL;
FreeDynLibJVM();
JVMLibFullName = (char *)MALLOC((strlen("libjava") + strlen(SHARED_LIB_EXT) + 1) * sizeof(char));
sprintf(JVMLibFullName, "%s%s", "libjava", SHARED_LIB_EXT);
if (LoadFunctionsJVM(JVMLibFullName))
{
res = SciJNI_GetCreatedJavaVMs(&jvm, 1, &jvm_count);
if (res != JNI_OK)
{
fprintf(stderr, "\nJNI_GetCreatedJavaVMs failed to detect any started Java VM.\n");
return NULL;
}
if (jvm_count == 1) /* We could update this to behave differently when two (or more) JVMs are already started */
{
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
};
return jvm;
}
else
{
jvm = NULL;
}
}
if (JVMLibFullName)
{
FREE(JVMLibFullName);
JVMLibFullName = NULL;
};
return jvm;
}
/*--------------------------------------------------------------------------*/
JavaVM *FindCreatedJavaVM(char *SCILAB_PATH)
{
JavaVM *jvm = NULL;
#ifndef __APPLE__
/* Under Mac OS X, we are using the JVM provided by the distribution.
* However, this might change with Java 7 since Apple will no longer provide
* Java with Mac OS X */
jvm = SearchCreatedJavaVMEmbedded(SCILAB_PATH);
if (jvm)
{
return jvm;
}
else
{
#endif
jvm = SearchCreatedJavaVMPath();
if (jvm)
{
return jvm;
}
#ifndef __APPLE__
}
#endif
return NULL;
}
/*--------------------------------------------------------------------------*/
|