1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
20
* @version $Revision: 473703 $, $Date: 2006-11-11 13:30:52 +0100 (Sat, 11 Nov 2006) $
25
static const char *tcn_errors[] = {
27
/* TCN_TIMEUP */ "Operation timed out",
28
/* TCN_EAGAIN */ "There is no data ready",
29
/* TCN_EINTR */ "Interrupted system call",
30
/* TCN_EINPROGRESS */ "Operation in progress",
31
/* TCN_ETIMEDOUT */ "Connection timed out",
35
/* Merge IS_ETIMEDOUT with APR_TIMEUP
37
#define TCN_STATUS_IS_ETIMEDOUT(x) (APR_STATUS_IS_ETIMEDOUT((x)) || ((x) == APR_TIMEUP))
39
* Convenience function to help throw an java.lang.Exception.
41
void tcn_ThrowException(JNIEnv *env, const char *msg)
43
jclass javaExceptionClass;
45
javaExceptionClass = (*env)->FindClass(env, "java/lang/Exception");
46
if (javaExceptionClass == NULL) {
47
fprintf(stderr, "Cannot find java/lang/Exception class\n");
50
(*env)->ThrowNew(env, javaExceptionClass, msg);
51
(*env)->DeleteLocalRef(env, javaExceptionClass);
55
void tcn_ThrowMemoryException(JNIEnv *env, const char *file, int line, const char *msg)
57
jclass javaExceptionClass;
58
javaExceptionClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
59
if (javaExceptionClass == NULL) {
60
fprintf(stderr, "Cannot find java/lang/OutOfMemoryError\n");
65
char fmt[TCN_BUFFER_SZ];
66
char *f = (char *)(file + strlen(file) - 1);
67
while (f != file && '\\' != *f && '/' != *f) {
73
sprintf(fmt, "%s for [%04d@%s]", msg, line, f);
74
(*env)->ThrowNew(env, javaExceptionClass, &fmt[0]);
77
(*env)->ThrowNew(env, javaExceptionClass, msg);
78
(*env)->DeleteLocalRef(env, javaExceptionClass);
83
void tcn_Throw(JNIEnv *env, const char *fmt, ...)
85
char msg[TCN_BUFFER_SZ] = {'\0'};
89
apr_vsnprintf(msg, TCN_BUFFER_SZ, fmt, ap);
90
tcn_ThrowException(env, msg);
95
* Convenience function to help throw an APR Exception
96
* from native error code.
98
void tcn_ThrowAPRException(JNIEnv *e, apr_status_t err)
100
jclass aprErrorClass;
101
jmethodID constructorID = 0;
103
jstring jdescription;
104
char serr[512] = {0};
106
aprErrorClass = (*e)->FindClass(e, TCN_ERROR_CLASS);
107
if (aprErrorClass == NULL) {
108
fprintf(stderr, "Cannot find " TCN_ERROR_CLASS " class\n");
112
/* Find the constructor ID */
113
constructorID = (*e)->GetMethodID(e, aprErrorClass,
115
"(ILjava/lang/String;)V");
116
if (constructorID == NULL) {
118
"Cannot find constructor for " TCN_ERROR_CLASS " class\n");
122
apr_strerror(err, serr, 512);
123
/* Obtain the string objects */
124
jdescription = AJP_TO_JSTRING(serr);
125
if (jdescription == NULL) {
127
"Cannot allocate description for " TCN_ERROR_CLASS " class\n");
130
/* Create the APR Error object */
131
throwObj = (*e)->NewObject(e, aprErrorClass, constructorID,
132
(jint)err, jdescription);
133
if (throwObj == NULL) {
135
"Cannot allocate new " TCN_ERROR_CLASS " object\n");
139
(*e)->Throw(e, throwObj);
141
(*e)->DeleteLocalRef(e, aprErrorClass);
145
TCN_IMPLEMENT_CALL(jint, Error, osError)(TCN_STDARGS)
147
UNREFERENCED_STDARGS;
148
return (jint)apr_get_os_error();
151
TCN_IMPLEMENT_CALL(jint, Error, netosError)(TCN_STDARGS)
153
UNREFERENCED_STDARGS;
154
return (jint)apr_get_netos_error();
157
TCN_IMPLEMENT_CALL(jstring, Error, strerror)(TCN_STDARGS, jint err)
159
char serr[512] = {0};
163
if (err >= TCN_TIMEUP && err <= TCN_ETIMEDOUT) {
165
jerr = AJP_TO_JSTRING(tcn_errors[err + 1]);
168
apr_strerror(err, serr, 512);
169
jerr = AJP_TO_JSTRING(serr);
174
TCN_IMPLEMENT_CALL(jboolean, Status, is)(TCN_STDARGS, jint err, jint idx)
176
#define APR_IS(I, E) case I: if (E(err)) return JNI_TRUE; break
177
#define APR_ISX(I, E, T) case I: if (E(err) || (err == T)) return JNI_TRUE; break
179
UNREFERENCED_STDARGS;
181
APR_IS(1, APR_STATUS_IS_ENOSTAT);
182
APR_IS(2, APR_STATUS_IS_ENOPOOL);
184
APR_IS(4, APR_STATUS_IS_EBADDATE);
185
APR_IS(5, APR_STATUS_IS_EINVALSOCK);
186
APR_IS(6, APR_STATUS_IS_ENOPROC);
187
APR_IS(7, APR_STATUS_IS_ENOTIME);
188
APR_IS(8, APR_STATUS_IS_ENODIR);
189
APR_IS(9, APR_STATUS_IS_ENOLOCK);
190
APR_IS(10, APR_STATUS_IS_ENOPOLL);
191
APR_IS(11, APR_STATUS_IS_ENOSOCKET);
192
APR_IS(12, APR_STATUS_IS_ENOTHREAD);
193
APR_IS(13, APR_STATUS_IS_ENOTHDKEY);
194
APR_IS(14, APR_STATUS_IS_EGENERAL);
195
APR_IS(15, APR_STATUS_IS_ENOSHMAVAIL);
196
APR_IS(16, APR_STATUS_IS_EBADIP);
197
APR_IS(17, APR_STATUS_IS_EBADMASK);
198
/* empty slot: +18 */
199
APR_IS(19, APR_STATUS_IS_EDSOOPEN);
200
APR_IS(20, APR_STATUS_IS_EABSOLUTE);
201
APR_IS(21, APR_STATUS_IS_ERELATIVE);
202
APR_IS(22, APR_STATUS_IS_EINCOMPLETE);
203
APR_IS(23, APR_STATUS_IS_EABOVEROOT);
204
APR_IS(24, APR_STATUS_IS_EBADPATH);
205
APR_IS(25, APR_STATUS_IS_EPATHWILD);
206
APR_IS(26, APR_STATUS_IS_ESYMNOTFOUND);
207
APR_IS(27, APR_STATUS_IS_EPROC_UNKNOWN);
208
APR_IS(28, APR_STATUS_IS_ENOTENOUGHENTROPY);
212
APR_IS(51, APR_STATUS_IS_INCHILD);
213
APR_IS(52, APR_STATUS_IS_INPARENT);
214
APR_IS(53, APR_STATUS_IS_DETACH);
215
APR_IS(54, APR_STATUS_IS_NOTDETACH);
216
APR_IS(55, APR_STATUS_IS_CHILD_DONE);
217
APR_IS(56, APR_STATUS_IS_CHILD_NOTDONE);
218
APR_ISX(57, APR_STATUS_IS_TIMEUP, TCN_TIMEUP);
219
APR_IS(58, APR_STATUS_IS_INCOMPLETE);
221
/* empty slot: +10 */
222
/* empty slot: +11 */
223
APR_IS(62, APR_STATUS_IS_BADCH);
224
APR_IS(63, APR_STATUS_IS_BADARG);
225
APR_IS(64, APR_STATUS_IS_EOF);
226
APR_IS(65, APR_STATUS_IS_NOTFOUND);
227
/* empty slot: +16 */
228
/* empty slot: +17 */
229
/* empty slot: +18 */
230
APR_IS(69, APR_STATUS_IS_ANONYMOUS);
231
APR_IS(70, APR_STATUS_IS_FILEBASED);
232
APR_IS(71, APR_STATUS_IS_KEYBASED);
233
APR_IS(72, APR_STATUS_IS_EINIT);
234
APR_IS(73, APR_STATUS_IS_ENOTIMPL);
235
APR_IS(74, APR_STATUS_IS_EMISMATCH);
236
APR_IS(75, APR_STATUS_IS_EBUSY);
238
APR_ISX(90, APR_STATUS_IS_EAGAIN, TCN_EAGAIN);
239
APR_ISX(91, TCN_STATUS_IS_ETIMEDOUT, TCN_ETIMEDOUT);
240
APR_IS(92, APR_STATUS_IS_ECONNABORTED);
241
APR_IS(93, APR_STATUS_IS_ECONNRESET);
242
APR_ISX(94, APR_STATUS_IS_EINPROGRESS, TCN_EINPROGRESS);
243
APR_ISX(95, APR_STATUS_IS_EINTR, TCN_EINTR);
244
APR_IS(96, APR_STATUS_IS_ENOTSOCK);
245
APR_IS(97, APR_STATUS_IS_EINVAL);