You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ea...@apache.org on 2008/06/05 18:17:40 UTC
svn commit: r663650 - in /incubator/uima/uimacpp/trunk/src:
framework/log.cpp framework/uima/log.hpp jni/jni.cpp jni/uima/jni.hpp
Author: eae
Date: Thu Jun 5 09:17:39 2008
New Revision: 663650
URL: http://svn.apache.org/viewvc?rev=663650&view=rev
Log:
UIMA-1049 Bhavani's 1049-2 patch
Modified:
incubator/uima/uimacpp/trunk/src/framework/log.cpp
incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp
incubator/uima/uimacpp/trunk/src/jni/jni.cpp
incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp
Modified: incubator/uima/uimacpp/trunk/src/framework/log.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/log.cpp?rev=663650&r1=663649&r2=663650&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/log.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/log.cpp Thu Jun 5 09:17:39 2008
@@ -49,6 +49,8 @@
#define UIMA_LOG_APPLICATION_KEY_UNKNOWN _TEXT("???")
const size_t UIMA_LOG_STATIC_CONVERSION_BUFSIZE = 1024;
+static apr_pool_t * logPool=0;
+static apr_thread_mutex_t * logMutex=0;
/* ----------------------------------------------------------------------- */
/* Forward declarations */
@@ -146,7 +148,7 @@
void LogFacility::doLog(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const
/* ----------------------------------------------------------------------- */
{
- apr_thread_mutex_lock(mutex);
+ apr_thread_mutex_lock(logMutex);
string method="";
if (isLoggable(enType)) {
for (int i=0; i < vecLoggers.size(); i++) {
@@ -154,7 +156,7 @@
method,cpszMsg,lUserCode);
}
}
- apr_thread_mutex_unlock(mutex);
+ apr_thread_mutex_unlock(logMutex);
}
bool LogFacility::isLoggable(LogStream::EnEntryType enType) const {
@@ -242,20 +244,21 @@
iv_lLastUserCode(0),
iv_logStream(*this, LogStream::EnMessage),
vecLoggers(ResourceManager::getInstance().getLoggers()),
- iv_logLevel(ResourceManager::getInstance().getLoggingLevel()),
- iv_logPool(NULL) {
- apr_status_t rv = apr_pool_create( &iv_logPool,NULL );
- if ( rv == APR_SUCCESS ) {
- UnicodeStringRef ref(crEngineName);
- ref.extract(iv_strOrigin, CCSID::getDefaultName() );
- apr_thread_mutex_create(&mutex,APR_THREAD_MUTEX_UNNESTED, iv_logPool);
- } else {
- UIMA_EXC_THROW_NEW(ExcOutOfMemory,
+ iv_logLevel(ResourceManager::getInstance().getLoggingLevel()) {
+ if (logPool == NULL) {
+ apr_status_t rv = apr_pool_create( &logPool,NULL );
+ if ( rv == APR_SUCCESS ) {
+ UnicodeStringRef ref(crEngineName);
+ ref.extract(iv_strOrigin, CCSID::getDefaultName() );
+ apr_thread_mutex_create(&logMutex,APR_THREAD_MUTEX_UNNESTED, logPool);
+ } else {
+ UIMA_EXC_THROW_NEW(ExcOutOfMemory,
UIMA_ERR_ENGINE_OUT_OF_MEMORY,
UIMA_MSG_ID_EXC_OUT_OF_MEMORY,
ErrorMessage(UIMA_MSG_ID_EXCON_CREATING_POOL_FOR_CLASS,"uima::LogFacility"),
ErrorInfo::unrecoverable);
- }
+ }
+ }
}
LogFacility::LogFacility(icu::UnicodeString const & crEngineName,
@@ -263,19 +266,21 @@
iv_lLastUserCode(0),
iv_logStream(*this, crLoggingLevel),
vecLoggers(ResourceManager::getInstance().getLoggers()),
- iv_logLevel(crLoggingLevel),
- iv_logPool(0) {
- apr_status_t rv = apr_pool_create( &iv_logPool,NULL );
- if ( rv == APR_SUCCESS ) {
- UnicodeStringRef ref(crEngineName);
- ref.extract(iv_strOrigin, CCSID::getDefaultName() );
- apr_thread_mutex_create(&mutex,APR_THREAD_MUTEX_UNNESTED, iv_logPool);
- } else {
- UIMA_EXC_THROW_NEW(ExcOutOfMemory,
+ iv_logLevel(crLoggingLevel) {
+
+ if (logPool == NULL) {
+ apr_status_t rv = apr_pool_create( &logPool,NULL );
+ if ( rv == APR_SUCCESS ) {
+ UnicodeStringRef ref(crEngineName);
+ ref.extract(iv_strOrigin, CCSID::getDefaultName() );
+ apr_thread_mutex_create(&logMutex,APR_THREAD_MUTEX_UNNESTED, logPool);
+ } else {
+ UIMA_EXC_THROW_NEW(ExcOutOfMemory,
UIMA_ERR_ENGINE_OUT_OF_MEMORY,
UIMA_MSG_ID_EXC_OUT_OF_MEMORY,
ErrorMessage(UIMA_MSG_ID_EXCON_CREATING_POOL_FOR_CLASS,"uima::LogFacility"),
ErrorInfo::unrecoverable);
+ }
}
}
@@ -342,3 +347,4 @@
+
Modified: incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp?rev=663650&r1=663649&r2=663650&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp Thu Jun 5 09:17:39 2008
@@ -267,9 +267,7 @@
LogStream iv_logStream;
LogStream::EnEntryType iv_logLevel;
vector<Logger*> & vecLoggers;
- apr_pool_t * iv_logPool;
- apr_thread_mutex_t *mutex;
-
+
/* --- functions --- */
TyMessageId getTypeAsMessageId(LogStream::EnEntryType enType) const;
void doLog(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode = 0) const;
@@ -389,3 +387,4 @@
+
Modified: incubator/uima/uimacpp/trunk/src/jni/jni.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/jni/jni.cpp?rev=663650&r1=663649&r2=663650&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/jni/jni.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/jni/jni.cpp Thu Jun 5 09:17:39 2008
@@ -70,7 +70,7 @@
/* ----------------------------------------------------------------------- */
/* Forward declarations */
/* ----------------------------------------------------------------------- */
-static JNILogger * iv_logger = 0;
+static JNILogger * singleton_jni_logger = 0;
static jobject getSerializedCasData (JNIEnv* jeEnv, jobject joJTaf, jint jiWhichData, uima::internal::SerializedCAS & crSerializedCAS);
/* ----------------------------------------------------------------------- */
@@ -85,41 +85,42 @@
/********************************************************************
***** JNILogger
********************************************************************/
- JNILogger::JNILogger(JNIEnv * env) : iv_jnienv(env), cv_clazz(0), cv_logMethod(0) {
- assert( EXISTS(iv_jnienv) );
+ JNILogger::JNILogger(JNIEnv * env) : /*iv_jnienv(env),*/ cv_clazz(0), cv_logMethod(0) {
+ assert( EXISTS(env) );
try {
+ env->GetJavaVM(&iv_jvm);
- cv_clazz = iv_jnienv->FindClass(JAVA_LOGGER_PROXY);
+ cv_clazz = env->FindClass(JAVA_LOGGER_PROXY);
if (cv_clazz == NULL ) {
uima::ResourceManager::getInstance().getLogger().logError(
JAVA_LOGGER_PROXY " class not found. Could not setup Java logging. ");
- iv_jnienv->ExceptionClear();
+ env->ExceptionClear();
return;
}
- cv_clazz = (jclass) iv_jnienv->NewGlobalRef(cv_clazz);
+ cv_clazz = (jclass) env->NewGlobalRef(cv_clazz);
if (cv_clazz == NULL) {
uima::ResourceManager::getInstance().getLogger().logError(
"Setup global reference to " JAVA_LOGGER_PROXY " class failed. Could not setup Java logging. ");
cerr << "JNILogger() ERROR: CPPJEDIIEngine could not construct " << endl;
- iv_jnienv->ExceptionClear();
+ env->ExceptionClear();
return;
}
//query the current logging level
- jmethodID iv_getLoggingLevelMethod = iv_jnienv->GetStaticMethodID(cv_clazz,
+ jmethodID iv_getLoggingLevelMethod = env->GetStaticMethodID(cv_clazz,
"getLoggingLevel",
"()I");
if (iv_getLoggingLevelMethod == NULL) {
uima::ResourceManager::getInstance().getLogger().logError(
JAVA_LOGGER_PROXY ".getLoggingLevel() not found. Could not setup Java logging. " );
cout << "JNILogger() ERROR: CPPJEDIIEngine.getLoggingLevel() not found " << endl;
- iv_jnienv->ExceptionClear();
+ env->ExceptionClear();
return;
}
//log method
- cv_logMethod = iv_jnienv->GetStaticMethodID(cv_clazz, "log", "("
+ cv_logMethod = env->GetStaticMethodID(cv_clazz, "log", "("
"I" // level
"Ljava/lang/String;" // source class
"Ljava/lang/String;" // source method
@@ -130,18 +131,18 @@
uima::ResourceManager::getInstance().getLogger().logError(
JAVA_LOGGER_PROXY ".log(int,string,string,string) not found. Could not setup Java logging. " );
cout << "JNILogger() ERROR: CPPJEDIIEngine.log() not found " << endl;
- iv_jnienv->ExceptionClear();
+ env->ExceptionClear();
return;
}
//get the current logging level
- jint logginglevel = iv_jnienv->CallStaticIntMethod(cv_clazz, iv_getLoggingLevelMethod);
+ jint logginglevel = env->CallStaticIntMethod(cv_clazz, iv_getLoggingLevelMethod);
if (logginglevel == 0) {
uima::ResourceManager::getInstance().getLogger().logError(
"JNILogger() Could not determine current logging level. Setup Java logging failed. " );
cout << "JNILogger() ERROR: calling CPPJEDIIEngine.getLoggingLevel() " << endl;
- iv_jnienv->ExceptionClear();
+ env->ExceptionClear();
return;
}
@@ -155,7 +156,7 @@
} catch (...) {
cout << "Exception in JNILogger() " << endl;
- iv_jnienv->ExceptionDescribe();
+ env->ExceptionDescribe();
}
}
@@ -165,6 +166,17 @@
string methodname,
string message,
long lUserCode) {
+
+ //get JNI env associated with the current thread
+ JNIEnv * jnienv=0;
+ try {
+ iv_jvm->AttachCurrentThread((void**)&jnienv, NULL);
+
+ if (jnienv == 0) {
+ cerr << "JNILogger::log() failed to get JNI env handle." << endl;
+ return;
+ }
+
stringstream str;
if (entype == uima::LogStream::EnMessage) {
if (lUserCode != 0) {
@@ -180,10 +192,9 @@
// Convert the std::strings to Unicode using the default converter
UnicodeString ustrsource(classname.c_str(), classname.length());
UnicodeString ustrmethod(methodname.c_str(), methodname.length());
- try {
- jstring jsrcclass = iv_jnienv->NewString((jchar const *) ustrsource.getBuffer(), ustrsource.length());
- jstring jsrcmethod = iv_jnienv->NewString((jchar const *) ustrmethod.getBuffer(), ustrmethod.length());
- jstring jmessage = iv_jnienv->NewString((jchar const *) msg.getBuffer(), msg.length());
+ jstring jsrcclass = jnienv->NewString((jchar const *) ustrsource.getBuffer(), ustrsource.length());
+ jstring jsrcmethod = jnienv->NewString((jchar const *) ustrmethod.getBuffer(), ustrmethod.length());
+ jstring jmessage = jnienv->NewString((jchar const *) msg.getBuffer(), msg.length());
jint loglevel;
if ( entype == uima::LogStream::EnError) {
@@ -194,23 +205,10 @@
loglevel = 1;
}
- //get jnienv handle for the current thread
- apr_os_thread_t threadid = apr_os_thread_current();
- map<apr_os_thread_t, JNIEnv*>::iterator ite = this->threadid2jnienv.find(threadid);
- if (ite == this->threadid2jnienv.end() ) {
- cerr << "JNILogger::log() failed to get handle to JNI env for current thread." << endl;
- return;
- }
- iv_jnienv = ite->second;
-
- if (iv_jnienv == 0) {
- cerr << "JNILogger::log() failed to get JNI env handle." << endl;
- return;
- }
// Call exception clear
- iv_jnienv->ExceptionClear();
+ jnienv->ExceptionClear();
- iv_jnienv->CallStaticVoidMethod(cv_clazz,
+ jnienv->CallStaticVoidMethod(cv_clazz,
cv_logMethod,
loglevel,
jsrcclass,
@@ -218,13 +216,15 @@
jmessage);
// Check for exceptions :
- jthrowable exc = iv_jnienv->ExceptionOccurred();
+ jthrowable exc = jnienv->ExceptionOccurred();
if (exc != NULL) {
- iv_jnienv->ExceptionDescribe();
- iv_jnienv->ExceptionClear();
+ jnienv->ExceptionDescribe();
+ jnienv->ExceptionClear();
}
+ iv_jvm->DetachCurrentThread();
//cout << "ThreadId: " << threadid << " JNILogger::log() DONE" << endl;
} catch (...) {
+ iv_jvm->DetachCurrentThread();
cout << "JNILogger::log(...) Exception in JavaLogging()" << endl;
}
}
@@ -355,13 +355,11 @@
return;
}
//setup JNI logger
- if (iv_logger == NULL) {
+ if (singleton_jni_logger == NULL) {
cout << "creating JNILogger" << endl;
- iv_logger = new JNILogger(jeEnv);
- uima::ResourceManager::getInstance().registerLogger(iv_logger);
+ singleton_jni_logger = new JNILogger(jeEnv);
+ uima::ResourceManager::getInstance().registerLogger(singleton_jni_logger);
}
- cout << "set JNILogger in JNIInstance " << endl;
- pInstance->iv_logger = iv_logger;
// setting engine
JNIUtils::setCppInstance(jeEnv, joJTaf, pInstance);
@@ -427,8 +425,6 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
uima::ErrorInfo errInfo;
UIMA_TPRINT("creating engine");
@@ -548,8 +544,6 @@
deSerializer.deserializeDefinitions( rSerCAS, casDef );
UIMA_TPRINT(" done deserializing definitions");
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
engineBase.reinitTypeSystem();
uima::CAS * pCAS = pInstance->getCAS();
@@ -653,8 +647,6 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
uima::AnalysisEngine * pEngine = pInstance->getEngine();
uima::TyErrorId tyErrorId = pEngine->destroy();
@@ -713,8 +705,6 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
uima::AnalysisEngine * pEngine = pInstance->getEngine();
assert( EXISTS(pEngine) );
@@ -1122,8 +1112,6 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
uima::AnalysisEngine * pEngine = pInstance->getEngine();
uima::TyErrorId tyErrorId = pEngine->batchProcessComplete();
@@ -1157,8 +1145,6 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- pInstance->iv_logger->setJNIEnv(jeEnv);
-
uima::AnalysisEngine * pEngine = pInstance->getEngine();
uima::TyErrorId tyErrorId = pEngine->collectionProcessComplete();
@@ -1291,3 +1277,4 @@
+
Modified: incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp?rev=663650&r1=663649&r2=663650&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp (original)
+++ incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp Thu Jun 5 09:17:39 2008
@@ -79,20 +79,15 @@
string methodname,
string message,
long errorCode) ;
- void setJNIEnv(JNIEnv * env) {
- apr_os_thread_t threadid = apr_os_thread_current();
- threadid2jnienv[threadid] = env;
- iv_jnienv=env;
- }
+
private:
/** Format the log message */
std::string format(uima::LogStream::EnEntryType enType,
const string & cpszMsg,
long lUserCode) const;
- JNIEnv * iv_jnienv; //handle to Java environment
+ JavaVM * iv_jvm;
jclass cv_clazz ; //proxy class on java side
jmethodID cv_logMethod; //log method
- map<apr_os_thread_t, JNIEnv*> threadid2jnienv;
};
@@ -109,22 +104,17 @@
bool iv_hasNext;
public:
- JNILogger * iv_logger;
+ //JNILogger * iv_logger;
JNIInstance() :
iv_pEngine(NULL),
iv_pCAS(NULL),
iv_serializedCAS(),
iv_pSegment(NULL),
iv_serializedSegment(),
- iv_hasNext(false),
- iv_logger(0) {}
+ iv_hasNext(false){}
~JNIInstance() {
- if (iv_logger != NULL) {
- uima::ResourceManager::getInstance().unregisterLogger(iv_logger);
- delete iv_logger;
- iv_logger=NULL;
- }
+
}
uima::AnalysisEngine * getEngine() {