1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
25 #include <osl/security.h>
28 /* On Windows, jpipe.dll must not have static dependencies on any other URE DLLs
29 (sal3.dll, uwinapi.dll), as Java System.LoadLibrary could otherwise not load
30 it. Therefore, on Windows, this code goes into a jpipx.dll that the jpipe.dll
31 wrapper loads with LoadLibraryEx(LOAD_WITH_ALTERED_SEARCH_PATH).
32 The function names in this wrapped code are truncated from the long JNICALL
33 names, as JNICALL causes some "@N" with different numeric values for
34 N (and probably different across 32 and 64 bit) to be added to the symbol
35 names, which the calls to GetProcAddress in wrapper/wrapper.c would otherwise
36 have to take into account.
39 /*****************************************************************************/
40 /* exception macros */
42 static void ThrowException(JNIEnv
* env
, char const * type
, char const * msg
) {
44 (*env
)->ExceptionClear(env
);
45 c
= (*env
)->FindClass(env
, type
);
47 (*env
)->ExceptionClear(env
);
48 (*env
)->FatalError(env
, "JNI FindClass failed");
50 if ((*env
)->ThrowNew(env
, c
, msg
) != 0) {
51 (*env
)->ExceptionClear(env
);
52 (*env
)->FatalError(env
, "JNI ThrowNew failed");
56 /*****************************************************************************/
57 /* helper functions prototypes */
59 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
);
60 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
);
62 /*****************************************************************************/
65 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
)
69 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
73 "java/lang/RuntimeException",
74 "native pipe cannot find class");
78 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
82 "java/lang/RuntimeException",
83 "native pipe cannot find field");
86 return (oslPipe
) SAL_INT_CAST(
87 sal_IntPtr
, (*env
)->GetLongField(env
, obj_this
, fid
));
90 /*****************************************************************************/
91 /* convert jstring to rtl_uString */
93 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
)
96 rtl_uString
* ustr
= NULL
;
97 cstr
= (*env
)->GetStringUTFChars(env
, jstr
, NULL
);
98 rtl_uString_newFromAscii(&ustr
, cstr
);
99 (*env
)->ReleaseStringUTFChars(env
, jstr
, cstr
);
103 /*****************************************************************************/
105 * Class: com_sun_star_lib_connections_pipe_PipeConnection
107 * Signature: (Lcom/sun/star/beans/NativeService;)V
109 SAL_DLLPUBLIC_EXPORT
void
111 PipeConnection_create
113 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI
115 (JNIEnv
* env
, jobject obj_this
, jstring name
)
129 oslSecurity psec
= osl_getCurrentSecurity();
130 oslPipe npipe
= NULL
;
131 rtl_uString
* pname
= NULL
;
132 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
135 "java/lang/RuntimeException",
136 "native pipe cannot synchronize on the object");
141 /* check connection state */
142 npipe
= getPipe(env
, obj_this
);
143 if ((*env
)->ExceptionOccurred(env
) != NULL
)
148 "com/sun/star/io/IOException",
149 "native pipe is already connected");
153 /* save the pipe name */
154 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
158 "java/lang/RuntimeException",
159 "native pipe cannot find class");
163 fid
= (*env
)->GetFieldID(env
, tclass
,
164 "_aDescription", "Ljava/lang/String;");
168 "java/lang/RuntimeException",
169 "native pipe cannot find field");
173 (*env
)->SetObjectField(env
, obj_this
, fid
, (jobject
)name
);
175 /* convert pipe name to rtl_uString */
176 pname
= jstring2ustring(env
, name
);
180 "java/lang/RuntimeException",
181 "native pipe cannot convert name");
187 npipe
= osl_createPipe(pname
, osl_Pipe_OPEN
, psec
);
191 "java/lang/RuntimeException",
192 "cannot create native pipe");
198 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
202 "java/lang/RuntimeException",
203 "native pipe cannot find class");
207 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
211 "java/lang/RuntimeException",
212 "native pipe cannot find field");
215 (*env
)->SetLongField(
216 env
, obj_this
, fid
, SAL_INT_CAST(jlong
, (sal_IntPtr
) npipe
));
219 rtl_uString_release(pname
);
220 (*env
)->MonitorExit(env
, obj_this
);
221 osl_freeSecurityHandle(psec
);
228 osl_closePipe(npipe
);
229 osl_releasePipe(npipe
);
232 rtl_uString_release(pname
);
235 (*env
)->MonitorExit(env
, obj_this
);
238 osl_freeSecurityHandle(psec
);
245 /*****************************************************************************/
247 * Class: com_sun_star_lib_connections_pipe_PipeConnection
251 SAL_DLLPUBLIC_EXPORT
void
255 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
257 (JNIEnv
* env
, jobject obj_this
)
266 oslPipe npipe
= NULL
; /* native pipe */
267 jclass tclass
; /* this class */
268 jfieldID fid
; /* a field identifier */
270 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
273 "java/lang/RuntimeException",
274 "native pipe cannot synchronize on the object");
279 /* check connection state */
280 npipe
= getPipe(env
, obj_this
);
281 if ((*env
)->ExceptionOccurred(env
) != NULL
)
286 "com/sun/star/io/IOException",
287 "native pipe is not connected");
292 /* remove the reference to the pipe */
293 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
297 "java/lang/RuntimeException",
298 "native pipe cannot find class");
302 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
306 "java/lang/RuntimeException",
307 "native pipe cannot find field");
311 (*env
)->SetLongField(env
, obj_this
, fid
, (jlong
)0);
319 /* release the pipe */
320 osl_closePipe(npipe
);
321 osl_releasePipe(npipe
);
324 (*env
)->MonitorExit(env
, obj_this
);
333 /*****************************************************************************/
335 * Class: com_sun_star_lib_connections_pipe_PipeConnection
339 SAL_DLLPUBLIC_EXPORT jint
343 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
345 (JNIEnv
* env
, jobject obj_this
, jobjectArray buffer
, jint len
)
355 oslPipe npipe
= NULL
; /* native pipe */
356 void * nbuff
= NULL
; /* native read buffer */
357 jbyteArray bytes
; /* java read buffer */
358 jint nread
; /* number of bytes has been read */
359 jint nreturn
= -1; /* actual return value */
362 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
365 "java/lang/RuntimeException",
366 "native pipe cannot synchronize on the object");
371 /* check connection state */
372 npipe
= getPipe(env
, obj_this
);
373 if ((*env
)->ExceptionOccurred(env
) != NULL
)
378 "com/sun/star/io/IOException",
379 "native pipe is not connected");
384 osl_acquirePipe( npipe
);
387 /* allocate a buffer */
388 if ((nbuff
= malloc(len
)) == NULL
)
391 "java/lang/RuntimeException",
392 "native pipe out of memory");
399 (*env
)->MonitorExit(env
, obj_this
);
402 nread
= osl_readPipe(npipe
, nbuff
, len
);
404 /* enter monitor again */
405 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
408 "java/lang/RuntimeException",
409 "native pipe cannot synchronize on the object");
416 bytes
= (*env
)->NewByteArray(env
, len
);
420 "java/lang/RuntimeException",
421 "native pipe out of memory");
426 (*env
)->SetByteArrayRegion(env
, bytes
, 0, len
, nbuff
);
427 (*env
)->SetObjectArrayElement(env
, buffer
, 0, bytes
);
428 (*env
)->DeleteLocalRef(env
, bytes
);
441 osl_releasePipe(npipe
);
444 (*env
)->MonitorExit(env
, obj_this
);
453 /*****************************************************************************/
455 * Class: com_sun_star_lib_connections_pipe_PipeConnection
459 SAL_DLLPUBLIC_EXPORT
void
463 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
465 (JNIEnv
* env
, jobject obj_this
, jbyteArray buffer
)
474 oslPipe npipe
; /* native pipe */
475 sal_Int32 count
; /* number of bytes has been written */
476 jsize nwrite
; /* number of bytes to write */
477 jbyte
* nbuff
= NULL
; /* native buffer */
479 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
482 "java/lang/RuntimeException",
483 "native pipe cannot synchronize on the object");
488 /* check connection state */
489 npipe
= getPipe(env
, obj_this
);
490 if ((*env
)->ExceptionOccurred(env
) != NULL
)
495 "com/sun/star/io/IOException",
496 "native pipe is not connected");
500 nwrite
= (*env
)->GetArrayLength(env
, buffer
);
503 nbuff
= (*env
)->GetByteArrayElements(env
, buffer
, NULL
);
507 "java/lang/RuntimeException",
508 "native pipe out of memory");
513 (*env
)->MonitorExit(env
, obj_this
);
515 count
= osl_writePipe(npipe
, nbuff
, nwrite
);
516 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
519 "java/lang/RuntimeException",
520 "native pipe cannot synchronize on the object");
526 "com/sun/star/io/IOException",
527 "native pipe: failed to write");
537 (*env
)->ReleaseByteArrayElements(env
, buffer
, nbuff
, JNI_ABORT
);
540 (*env
)->MonitorExit(env
, obj_this
);
549 /*****************************************************************************/
551 * Class: com_sun_star_lib_connections_pipe_PipeConnection
555 SAL_DLLPUBLIC_EXPORT
void
559 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI
561 (JNIEnv
* env
, jobject obj_this
)
563 (void) env
; /* not used */
564 (void) obj_this
; /* not used */
568 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */