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 .
21 #include "osl/security.h"
24 /* On Windows, jpipe.dll must not have static dependencies on any other URE DLLs
25 (sal3.dll, uwinapi.dll), as Java System.LoadLibrary could otherwise not load
26 it. Therefore, on Windows, this code goes into a jpipx.dll that the jpipe.dll
27 wrapper loads with LoadLibraryEx(LOAD_WITH_ALTERED_SEARCH_PATH).
28 The function names in this wrapped code are truncated from the long JNICALL
29 names, as JNICALL causes some "@N" with different numeric values for
30 N (and probably different across 32 and 64 bit) to be added to the symbol
31 names, which the calls to GetProcAddress in wrapper/wrapper.c would otherwise
32 have to take into account.
35 /*****************************************************************************/
36 /* exception macros */
38 static void ThrowException(JNIEnv
* env
, char const * type
, char const * msg
) {
40 (*env
)->ExceptionClear(env
);
41 c
= (*env
)->FindClass(env
, type
);
43 (*env
)->ExceptionClear(env
);
44 (*env
)->FatalError(env
, "JNI FindClass failed");
46 if ((*env
)->ThrowNew(env
, c
, msg
) != 0) {
47 (*env
)->ExceptionClear(env
);
48 (*env
)->FatalError(env
, "JNI ThrowNew failed");
52 /*****************************************************************************/
53 /* helper functions prototypes */
55 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
);
56 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
);
58 /*****************************************************************************/
61 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
)
65 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
69 "java/lang/RuntimeException",
70 "native pipe cannot find class");
74 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
78 "java/lang/RuntimeException",
79 "native pipe cannot find field");
82 return (oslPipe
) SAL_INT_CAST(
83 sal_IntPtr
, (*env
)->GetLongField(env
, obj_this
, fid
));
86 /*****************************************************************************/
87 /* convert jstring to rtl_uString */
89 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
)
92 rtl_uString
* ustr
= NULL
;
93 cstr
= (*env
)->GetStringUTFChars(env
, jstr
, NULL
);
94 rtl_uString_newFromAscii(&ustr
, cstr
);
95 (*env
)->ReleaseStringUTFChars(env
, jstr
, cstr
);
99 /*****************************************************************************/
101 * Class: com_sun_star_lib_connections_pipe_PipeConnection
103 * Signature: (Lcom/sun/star/beans/NativeService;)V
105 SAL_DLLPUBLIC_EXPORT
void
107 PipeConnection_create
109 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI
111 (JNIEnv
* env
, jobject obj_this
, jstring name
)
125 oslSecurity psec
= osl_getCurrentSecurity();
126 oslPipe npipe
= NULL
;
127 rtl_uString
* pname
= NULL
;
128 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
131 "java/lang/RuntimeException",
132 "native pipe cannot synchronize on the object");
137 /* check connection state */
138 npipe
= getPipe(env
, obj_this
);
139 if ((*env
)->ExceptionOccurred(env
) != NULL
)
144 "com/sun/star/io/IOException",
145 "native pipe is already connected");
149 /* save the pipe name */
150 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
154 "java/lang/RuntimeException",
155 "native pipe cannot find class");
159 fid
= (*env
)->GetFieldID(env
, tclass
,
160 "_aDescription", "Ljava/lang/String;");
164 "java/lang/RuntimeException",
165 "native pipe cannot find field");
169 (*env
)->SetObjectField(env
, obj_this
, fid
, (jobject
)name
);
171 /* convert pipe name to rtl_uString */
172 pname
= jstring2ustring(env
, name
);
176 "java/lang/RuntimeException",
177 "native pipe cannot convert name");
183 npipe
= osl_createPipe(pname
, osl_Pipe_OPEN
, psec
);
187 "java/lang/RuntimeException",
188 "cannot create native pipe");
194 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
198 "java/lang/RuntimeException",
199 "native pipe cannot find class");
203 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
207 "java/lang/RuntimeException",
208 "native pipe cannot find field");
211 (*env
)->SetLongField(
212 env
, obj_this
, fid
, SAL_INT_CAST(jlong
, (sal_IntPtr
) npipe
));
215 rtl_uString_release(pname
);
216 (*env
)->MonitorExit(env
, obj_this
);
217 osl_freeSecurityHandle(psec
);
224 osl_closePipe(npipe
);
225 osl_releasePipe(npipe
);
227 rtl_uString_release(pname
);
229 (*env
)->MonitorExit(env
, obj_this
);
231 osl_freeSecurityHandle(psec
);
238 /*****************************************************************************/
240 * Class: com_sun_star_lib_connections_pipe_PipeConnection
244 SAL_DLLPUBLIC_EXPORT
void
248 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
250 (JNIEnv
* env
, jobject obj_this
)
258 oslPipe npipe
; /* native pipe */
259 jclass tclass
; /* this class */
260 jfieldID fid
; /* a field identifier */
262 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
265 "java/lang/RuntimeException",
266 "native pipe cannot synchronize on the object");
271 /* check connection state */
272 npipe
= getPipe(env
, obj_this
);
273 if ((*env
)->ExceptionOccurred(env
) != NULL
)
278 "com/sun/star/io/IOException",
279 "native pipe is not connected");
283 /* remove the reference to the pipe */
284 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
288 "java/lang/RuntimeException",
289 "native pipe cannot find class");
293 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
297 "java/lang/RuntimeException",
298 "native pipe cannot find field");
302 (*env
)->SetLongField(env
, obj_this
, fid
, (jlong
)0);
304 /* release the pipe */
305 osl_closePipe(npipe
);
306 osl_releasePipe(npipe
);
309 (*env
)->MonitorExit(env
, obj_this
);
316 (*env
)->MonitorExit(env
, obj_this
);
324 /*****************************************************************************/
326 * Class: com_sun_star_lib_connections_pipe_PipeConnection
330 SAL_DLLPUBLIC_EXPORT jint
334 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
336 (JNIEnv
* env
, jobject obj_this
, jobjectArray buffer
, jint len
)
346 oslPipe npipe
; /* native pipe */
347 void * nbuff
= NULL
; /* native read buffer */
348 jbyteArray bytes
; /* java read buffer */
349 jint nread
; /* number of bytes has been read */
352 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
355 "java/lang/RuntimeException",
356 "native pipe cannot synchronize on the object");
361 /* check connection state */
362 npipe
= getPipe(env
, obj_this
);
363 if ((*env
)->ExceptionOccurred(env
) != NULL
)
368 "com/sun/star/io/IOException",
369 "native pipe is not connected");
374 osl_acquirePipe( npipe
);
377 /* allocate a buffer */
378 if ((nbuff
= malloc(len
)) == NULL
)
381 "java/lang/RuntimeException",
382 "native pipe out of memory");
389 (*env
)->MonitorExit(env
, obj_this
);
392 nread
= osl_readPipe(npipe
, nbuff
, len
);
394 /* enter monitor again */
395 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
398 "java/lang/RuntimeException",
399 "native pipe cannot synchronize on the object");
406 bytes
= (*env
)->NewByteArray(env
, len
);
410 "java/lang/RuntimeException",
411 "native pipe out of memory");
416 (*env
)->SetByteArrayRegion(env
, bytes
, 0, len
, nbuff
);
417 (*env
)->SetObjectArrayElement(env
, buffer
, 0, bytes
);
418 (*env
)->DeleteLocalRef(env
, bytes
);
423 if ( state
>= ACQUIRED
)
424 osl_releasePipe( npipe
);
427 (*env
)->MonitorExit(env
, obj_this
);
437 (*env
)->MonitorExit(env
, obj_this
);
445 /*****************************************************************************/
447 * Class: com_sun_star_lib_connections_pipe_PipeConnection
451 SAL_DLLPUBLIC_EXPORT
void
455 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
457 (JNIEnv
* env
, jobject obj_this
, jbyteArray buffer
)
466 oslPipe npipe
; /* native pipe */
467 long count
; /* number of bytes has been written */
468 jsize nwrite
; /* number of bytes to write */
469 jbyte
* nbuff
= NULL
; /* native buffer */
471 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
474 "java/lang/RuntimeException",
475 "native pipe cannot synchronize on the object");
480 /* check connection state */
481 npipe
= getPipe(env
, obj_this
);
482 if ((*env
)->ExceptionOccurred(env
) != NULL
)
487 "com/sun/star/io/IOException",
488 "native pipe is not connected");
492 nwrite
= (*env
)->GetArrayLength(env
, buffer
);
495 nbuff
= (*env
)->GetByteArrayElements(env
, buffer
, NULL
);
499 "java/lang/RuntimeException",
500 "native pipe out of memory");
505 (*env
)->MonitorExit(env
, obj_this
);
507 count
= osl_writePipe(npipe
, nbuff
, nwrite
);
508 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
511 "java/lang/RuntimeException",
512 "native pipe cannot synchronize on the object");
518 "com/sun/star/io/IOException",
519 "native pipe: failed to write");
524 (*env
)->ReleaseByteArrayElements(env
, buffer
, nbuff
, JNI_ABORT
);
525 (*env
)->MonitorExit(env
, obj_this
);
532 (*env
)->ReleaseByteArrayElements(env
, buffer
, nbuff
, JNI_ABORT
);
534 (*env
)->MonitorExit(env
, obj_this
);
542 /*****************************************************************************/
544 * Class: com_sun_star_lib_connections_pipe_PipeConnection
548 SAL_DLLPUBLIC_EXPORT
void
552 JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI
554 (JNIEnv
* env
, jobject obj_this
)
556 (void) env
; /* not used */
557 (void) obj_this
; /* not used */
561 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */