4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1998,2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains native methods for the Java SLP implementation.
31 * So far this is just the syslog function.
33 * The file also contains two support functions, one for throwing exceptions
34 * given a class name, and one for correctly converting unicode Strings to C
42 #define CLASS_JAVA_LANG_OUTOFMEMORYERROR "java/lang/OutOfMemoryError"
43 #define CLASS_JAVA_LANG_STRING "java/lang/String"
45 #define METHOD_GETBYTES "getBytes"
47 #define SIG_JAVA_LANG_STRING_GETBYTES "()[B"
50 * Given a class name of an exception and a message attempt to throw
51 * a new instance of the exception.
54 JNU_ThrowByName(JNIEnv
*env
, const char *name
, const char *msg
)
56 jclass
class = (*env
)->FindClass(env
, name
);
59 * If class is NULL FindClass() encountered a problem locating the
60 * desired class and has already called ThrowNew() with an
67 (*env
)->ThrowNew(env
, class, msg
);
68 (*env
)->DeleteLocalRef(env
, class);
72 * Convert a Java String into a native set of characters using the
73 * method String.getBytes(). This will ensure that the appropriate
74 * character set encoding will be used. This is necessary if the
75 * Java String uses unicode characters that cannot be easily
76 * encoded into native chars.
78 * The buffer returned must be released by using free() once it is
81 * This function returns NULL if an exception has been thrown during its
85 *JNU_GetStringNativeChars(JNIEnv
*env
, jstring jstr
)
90 jbyteArray bytes
= NULL
;
94 * Need a local reference for (1) FindClass(), (2) the bytes and
95 * (3) the FindClass() in ThrowByName() if all goes wrong.
97 if ((*env
)->EnsureLocalCapacity(env
, 3) < 0) {
100 CLASS_JAVA_LANG_OUTOFMEMORYERROR
,
106 class = (*env
)->FindClass(env
, CLASS_JAVA_LANG_STRING
);
109 * If class is NULL FindClass() encountered a problem locating the
110 * desired class and has already called ThrowNew() with an
117 method
= (*env
)->GetMethodID(
121 SIG_JAVA_LANG_STRING_GETBYTES
);
124 * If method is NULL GetMethodID() encountered a problem
125 * locating the desired method and has already called
126 * ThrowNew() with an exception.
128 if (method
!= NULL
) {
130 * Call String.getBytes(), creating our temporary
133 bytes
= (*env
)->CallObjectMethod(env
, jstr
, method
);
135 /* See if CallObjectMethod() threw an exception */
136 if ((*env
)->ExceptionCheck(env
) == JNI_FALSE
) {
138 len
= (*env
)->GetArrayLength(env
, bytes
);
141 * Allocate a buffer for the native characters,
142 * need an extra char for string terminator.
143 * Note: calloc will provide the terminating
146 result
= (char *)calloc(len
+ 1, sizeof (char));
149 * If allocation failed assume we are out of
152 if (result
== NULL
) {
155 CLASS_JAVA_LANG_OUTOFMEMORYERROR
,
159 * Copy the encoded bytes into the
160 * native string buffer
162 (*env
)->GetByteArrayRegion(
172 (*env
)->DeleteLocalRef(env
, bytes
);
176 /* Clean up by deleting the local references */
177 (*env
)->DeleteLocalRef(env
, class);
183 * Class: com_sun_slp_Syslog
185 * Signature: (ILjava/lang/String;)V
189 void JNICALL
Java_com_sun_slp_Syslog_syslog(JNIEnv
*env
,
194 char *msg
= JNU_GetStringNativeChars(env
, jmsg
);
197 * Check to see if the String conversion was successful,
198 * if it wasn't an exception will have already been thrown.
201 openlog("slpd", LOG_PID
, LOG_DAEMON
);
202 syslog(priority
, "%s", msg
);