Author: carnold Date: Thu Apr 27 16:00:29 2006 New Revision: 397672 URL: http://svn.apache.org/viewcvs?rev=397672&view=rev Log: Bug 39437: Eliminate unnecessary conversion to local code page
Modified: logging/log4j/trunk/src/java/org/apache/log4j/nt/nteventlog.cpp Modified: logging/log4j/trunk/src/java/org/apache/log4j/nt/nteventlog.cpp URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/nt/nteventlog.cpp?rev=397672&r1=397671&r2=397672&view=diff ============================================================================== --- logging/log4j/trunk/src/java/org/apache/log4j/nt/nteventlog.cpp (original) +++ logging/log4j/trunk/src/java/org/apache/log4j/nt/nteventlog.cpp Thu Apr 27 16:00:29 2006 @@ -1,5 +1,5 @@ /* - * Copyright 1999-2005 The Apache Software Foundation. + * Copyright 1999-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,71 +24,8 @@ #include "org_apache_log4j_Priority.h" #include "org_apache_log4j_nt_NTEventLogAppender.h" #include <windows.h> +#include <jni.h> -// Borrowed unabashedly from the JNI Programmer's Guide -void JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg) { - - jclass classForName = env->FindClass(name); - // If cls is null, an exception has already been thrown. - if (classForName != (jclass)0) { - env->ThrowNew(classForName, msg); - } - // cleanup - env->DeleteLocalRef(classForName); - return; -} - -// Borrowed unabashedly from the JNI Programmer's Guide -char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr) { - static jmethodID midStringGetBytes = 0; - jbyteArray bytes = 0; - jthrowable exc; - char *nstr = 0; - - //if (env->EnsureLocalCapacity(2) < 0) { - // out of memory - //return 0; - //} - - if (midStringGetBytes == 0) { - // Lookup and cache the String.getBytes() method id. - jclass stringClass = env->FindClass("java/lang/String"); - if (stringClass == 0) { - // An exception will have been thrown. - return 0; - } - midStringGetBytes = env->GetMethodID(stringClass, "getBytes", "()[B"); - env->DeleteLocalRef(stringClass); - if (midStringGetBytes == 0) { - // An exception will have been thrown. - return 0; - } - } - - bytes = (jbyteArray)env->CallObjectMethod(jstr, midStringGetBytes); - exc = env->ExceptionOccurred(); - if (exc == 0) { - // Attempt to malloc enough room for the length of the Java - // string plus one byte for the 0-terminator. - jint len = env->GetArrayLength(bytes); - nstr = (char *)malloc(len + 1); - if (nstr == 0) { - // malloc failed -- throw an OutOfMemoryError - JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0); - env->DeleteLocalRef(bytes); - return 0; - } - // copy to the malloc'd array and 0-terminate - env->GetByteArrayRegion(bytes, 0, len, (jbyte *)nstr); - nstr[len] = 0; - } else { - // cleanup - env->DeleteLocalRef(exc); - } - // cleanup - env->DeleteLocalRef(bytes); - return nstr; -} /* * Convert log4j Priority to an EventLog category. Each category is @@ -125,39 +62,39 @@ return ret_val; } -HKEY regGetKey(TCHAR *subkey, DWORD *disposition) { +HKEY regGetKey(wchar_t *subkey, DWORD *disposition) { HKEY hkey = 0; - RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, NULL, + RegCreateKeyExW(HKEY_LOCAL_MACHINE, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hkey, disposition); return hkey; } -void regSetString(HKEY hkey, TCHAR *name, TCHAR *value) { - RegSetValueEx(hkey, name, 0, REG_SZ, (LPBYTE)value, lstrlen(value) + sizeof(TCHAR)); +void regSetString(HKEY hkey, wchar_t *name, wchar_t *value) { + RegSetValueExW(hkey, name, 0, REG_SZ, (LPBYTE)value, (wcslen(value) + 1) * sizeof(wchar_t)); } -void regSetDword(HKEY hkey, TCHAR *name, DWORD value) { - RegSetValueEx(hkey, name, 0, REG_DWORD, (LPBYTE)&value, sizeof(DWORD)); +void regSetDword(HKEY hkey, wchar_t *name, DWORD value) { + RegSetValueExW(hkey, name, 0, REG_DWORD, (LPBYTE)&value, sizeof(DWORD)); } /* * Add this source with appropriate configuration keys to the registry. */ -void addRegistryInfo(char *source) { - const TCHAR *prefix = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"; +void addRegistryInfo(wchar_t *source) { + const wchar_t *prefix = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"; DWORD disposition; HKEY hkey = 0; - TCHAR subkey[256]; + wchar_t subkey[256]; - lstrcpy(subkey, prefix); - lstrcat(subkey, source); + wcscpy(subkey, prefix); + wcscat(subkey, source); hkey = regGetKey(subkey, &disposition); if (disposition == REG_CREATED_NEW_KEY) { - regSetString(hkey, "EventMessageFile", "NTEventLogAppender.dll"); - regSetString(hkey, "CategoryMessageFile", "NTEventLogAppender.dll"); - regSetDword(hkey, "TypesSupported", (DWORD)7); - regSetDword(hkey, "CategoryCount", (DWORD)8); + regSetString(hkey, L"EventMessageFile", L"NTEventLogAppender.dll"); + regSetString(hkey, L"CategoryMessageFile", L"NTEventLogAppender.dll"); + regSetDword(hkey, L"TypesSupported", (DWORD)7); + regSetDword(hkey, L"CategoryCount", (DWORD)8); } //RegSetValueEx(hkey, "EventMessageFile", 0, REG_SZ, (LPBYTE)dllname, lstrlen(dllname)); //RegSetValueEx(hkey, "CategoryMessageFile", 0, REG_SZ, (LPBYTE)dllname, lstrlen(dllname)); @@ -175,19 +112,25 @@ JNIEXPORT jint JNICALL Java_org_apache_log4j_nt_NTEventLogAppender_registerEventSource( JNIEnv *env, jobject java_this, jstring server, jstring source) { - char *nserver = 0; - char *nsource = 0; + jchar *nserver = 0; + jchar *nsource = 0; if (server != 0) { - nserver = JNU_GetStringNativeChars(env, server); + jsize serverLen = env->GetStringLength(server); + nserver = (jchar*) malloc((serverLen +1) * sizeof(jchar)); + env->GetStringRegion(server, 0, serverLen, nserver); + nserver[serverLen] = 0; } if (source != 0) { - nsource = JNU_GetStringNativeChars(env, source); - } - addRegistryInfo(nsource); - jint handle = (jint)RegisterEventSource(nserver, nsource); - free((void *)nserver); - free((void *)nsource); + jsize sourceLen = env->GetStringLength(source); + nsource = (jchar*) malloc((sourceLen +1) * sizeof(jchar)); + env->GetStringRegion(source, 0, sourceLen, nsource); + nsource[sourceLen] = 0; + } + addRegistryInfo((wchar_t*) nsource); + jint handle = (jint)RegisterEventSourceW((const wchar_t*) nserver, (const wchar_t*) nsource); + free(nserver); + free(nsource); return handle; } @@ -202,25 +145,25 @@ jboolean localHandle = JNI_FALSE; if (handle == 0) { // Client didn't give us a handle so make a local one. - handle = (jint)RegisterEventSource(NULL, "Log4j"); + handle = (jint)RegisterEventSourceW(NULL, L"Log4j"); localHandle = JNI_TRUE; } // convert Java String to character array - const int numStrings = 1; - LPCTSTR array[numStrings]; - char *nstr = JNU_GetStringNativeChars(env, jstr); - array[0] = nstr; + jsize msgLen = env->GetStringLength(jstr); + jchar* msg = (jchar*) malloc((msgLen + 1) * sizeof(jchar)); + env->GetStringRegion(jstr, 0, msgLen, msg); + msg[msgLen] = 0; // This is the only message supported by the package. It is backed by // a message resource which consists of just '%1' which is replaced // by the string we just created. const DWORD messageID = 0x1000; - ReportEvent((HANDLE)handle, getType(priority), + ReportEventW((HANDLE)handle, getType(priority), getCategory(priority), - messageID, NULL, 1, 0, array, NULL); + messageID, NULL, 1, 0, (const wchar_t**) &msg, NULL); - free((void *)nstr); + free((void *)msg); if (localHandle == JNI_TRUE) { // Created the handle here so free it here too. DeregisterEventSource((HANDLE)handle); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]