1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: com_sun_star_lib_connections_pipe_PipeConnection.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "osl/security.h"
34 #include "com_sun_star_lib_connections_pipe_PipeConnection.h"
36 /*****************************************************************************/
37 /* exception macros */
39 static void ThrowException(JNIEnv
* env
, char const * type
, char const * msg
) {
41 (*env
)->ExceptionClear(env
);
42 c
= (*env
)->FindClass(env
, type
);
44 (*env
)->ExceptionClear(env
);
45 (*env
)->FatalError(env
, "JNI FindClass failed");
47 if ((*env
)->ThrowNew(env
, c
, msg
) != 0) {
48 (*env
)->ExceptionClear(env
);
49 (*env
)->FatalError(env
, "JNI ThrowNew failed");
53 /*****************************************************************************/
54 /* helper functions prototypes */
56 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
);
57 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
);
59 /*****************************************************************************/
62 static oslPipe
getPipe(JNIEnv
* env
, jobject obj_this
)
66 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
70 "java/lang/RuntimeException",
71 "native pipe cannot find class");
75 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
79 "java/lang/RuntimeException",
80 "native pipe cannot find field");
83 return (oslPipe
) SAL_INT_CAST(
84 sal_IntPtr
, (*env
)->GetLongField(env
, obj_this
, fid
));
87 /*****************************************************************************/
88 /* convert jstring to rtl_uString */
90 static rtl_uString
* jstring2ustring(JNIEnv
* env
, jstring jstr
)
93 rtl_uString
* ustr
= NULL
;
94 cstr
= (*env
)->GetStringUTFChars(env
, jstr
, NULL
);
95 rtl_uString_newFromAscii(&ustr
, cstr
);
96 (*env
)->ReleaseStringUTFChars(env
, jstr
, cstr
);
100 /*****************************************************************************/
102 * Class: com_sun_star_lib_connections_pipe_PipeConnection
104 * Signature: (Lcom/sun/star/beans/NativeService;)V
106 JNIEXPORT
void JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI
107 (JNIEnv
* env
, jobject obj_this
, jstring name
)
121 oslSecurity psec
= osl_getCurrentSecurity();
122 oslPipe npipe
= NULL
;
123 rtl_uString
* pname
= NULL
;
124 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
127 "java/lang/RuntimeException",
128 "native pipe cannot synchronize on the object");
133 /* check connection state */
134 npipe
= getPipe(env
, obj_this
);
135 if ((*env
)->ExceptionOccurred(env
) != NULL
)
140 "com/sun/star/io/IOException",
141 "native pipe is already connected");
145 /* save the pipe name */
146 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
150 "java/lang/RuntimeException",
151 "native pipe cannot find class");
155 fid
= (*env
)->GetFieldID(env
, tclass
,
156 "_aDescription", "Ljava/lang/String;");
160 "java/lang/RuntimeException",
161 "native pipe cannot find field");
165 (*env
)->SetObjectField(env
, obj_this
, fid
, (jobject
)name
);
167 /* convert pipe name to rtl_uString */
168 pname
= jstring2ustring(env
, name
);
172 "java/lang/RuntimeException",
173 "native pipe cannot convert name");
179 npipe
= osl_createPipe(pname
, osl_Pipe_OPEN
, psec
);
183 "java/lang/RuntimeException",
184 "cannot create native pipe");
190 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
194 "java/lang/RuntimeException",
195 "native pipe cannot find class");
199 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
203 "java/lang/RuntimeException",
204 "native pipe cannot find field");
207 (*env
)->SetLongField(
208 env
, obj_this
, fid
, SAL_INT_CAST(jlong
, (sal_IntPtr
) npipe
));
211 rtl_uString_release(pname
);
212 (*env
)->MonitorExit(env
, obj_this
);
213 osl_freeSecurityHandle(psec
);
220 osl_closePipe(npipe
);
221 osl_releasePipe(npipe
);
223 rtl_uString_release(pname
);
225 (*env
)->MonitorExit(env
, obj_this
);
227 osl_freeSecurityHandle(psec
);
234 /*****************************************************************************/
236 * Class: com_sun_star_lib_connections_pipe_PipeConnection
240 JNIEXPORT
void JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
241 (JNIEnv
* env
, jobject obj_this
)
249 oslPipe npipe
; /* native pipe */
250 jclass tclass
; /* this class */
251 jfieldID fid
; /* a field identifier */
253 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
256 "java/lang/RuntimeException",
257 "native pipe cannot synchronize on the object");
262 /* check connection state */
263 npipe
= getPipe(env
, obj_this
);
264 if ((*env
)->ExceptionOccurred(env
) != NULL
)
269 "com/sun/star/io/IOException",
270 "native pipe is not connected");
274 /* remove the reference to the pipe */
275 tclass
= (*env
)->GetObjectClass(env
, obj_this
);
279 "java/lang/RuntimeException",
280 "native pipe cannot find class");
284 fid
= (*env
)->GetFieldID(env
, tclass
, "_nPipeHandle", "J");
288 "java/lang/RuntimeException",
289 "native pipe cannot find field");
293 (*env
)->SetLongField(env
, obj_this
, fid
, (jlong
)0);
295 /* release the pipe */
296 osl_closePipe(npipe
);
297 osl_releasePipe(npipe
);
300 (*env
)->MonitorExit(env
, obj_this
);
307 (*env
)->MonitorExit(env
, obj_this
);
315 /*****************************************************************************/
317 * Class: com_sun_star_lib_connections_pipe_PipeConnection
321 JNIEXPORT jint JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
322 (JNIEnv
* env
, jobject obj_this
, jobjectArray buffer
, jint len
)
332 oslPipe npipe
; /* native pipe */
333 void * nbuff
= NULL
; /* native read buffer */
334 jbyteArray bytes
; /* java read buffer */
335 jint nread
; /* number of bytes has been read */
338 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
341 "java/lang/RuntimeException",
342 "native pipe cannot synchronize on the object");
347 /* check connection state */
348 npipe
= getPipe(env
, obj_this
);
349 if ((*env
)->ExceptionOccurred(env
) != NULL
)
354 "com/sun/star/io/IOException",
355 "native pipe is not connected");
360 osl_acquirePipe( npipe
);
363 /* allocate a buffer */
364 if ((nbuff
= malloc(len
)) == NULL
)
367 "java/lang/RuntimeException",
368 "native pipe out of memory");
375 (*env
)->MonitorExit(env
, obj_this
);
378 nread
= osl_readPipe(npipe
, nbuff
, len
);
380 /* enter monitor again */
381 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
384 "java/lang/RuntimeException",
385 "native pipe cannot synchronize on the object");
392 bytes
= (*env
)->NewByteArray(env
, len
);
396 "java/lang/RuntimeException",
397 "native pipe out of memory");
402 (*env
)->SetByteArrayRegion(env
, bytes
, 0, len
, nbuff
);
403 (*env
)->SetObjectArrayElement(env
, buffer
, 0, bytes
);
404 (*env
)->DeleteLocalRef(env
, bytes
);
409 if ( state
>= AQUIRED
)
410 osl_releasePipe( npipe
);
413 (*env
)->MonitorExit(env
, obj_this
);
422 (*env
)->MonitorExit(env
, obj_this
);
430 /*****************************************************************************/
432 * Class: com_sun_star_lib_connections_pipe_PipeConnection
436 JNIEXPORT
void JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
437 (JNIEnv
* env
, jobject obj_this
, jbyteArray buffer
)
446 oslPipe npipe
; /* native pipe */
447 long count
; /* number of bytes has been written */
448 jsize nwrite
; /* number of bytes to write */
449 jbyte
* nbuff
= NULL
; /* native buffer */
451 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
454 "java/lang/RuntimeException",
455 "native pipe cannot synchronize on the object");
460 /* check connection state */
461 npipe
= getPipe(env
, obj_this
);
462 if ((*env
)->ExceptionOccurred(env
) != NULL
)
467 "com/sun/star/io/IOException",
468 "native pipe is not connected");
472 nwrite
= (*env
)->GetArrayLength(env
, buffer
);
475 nbuff
= (*env
)->GetByteArrayElements(env
, buffer
, NULL
);
479 "java/lang/RuntimeException",
480 "native pipe out of memory");
485 (*env
)->MonitorExit(env
, obj_this
);
487 count
= osl_writePipe(npipe
, nbuff
, nwrite
);
488 if ((*env
)->MonitorEnter(env
, obj_this
) != 0)
491 "java/lang/RuntimeException",
492 "native pipe cannot synchronize on the object");
498 "com/sun/star/io/IOException",
499 "native pipe is failed to write");
504 (*env
)->ReleaseByteArrayElements(env
, buffer
, nbuff
, JNI_ABORT
);
505 (*env
)->MonitorExit(env
, obj_this
);
512 (*env
)->ReleaseByteArrayElements(env
, buffer
, nbuff
, JNI_ABORT
);
514 (*env
)->MonitorExit(env
, obj_this
);
522 /*****************************************************************************/
524 * Class: com_sun_star_lib_connections_pipe_PipeConnection
528 JNIEXPORT
void JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI
529 (JNIEnv
* env
, jobject obj_this
)
531 (void) env
; /* not used */
532 (void) obj_this
; /* not used */