JniEnvAutoRef Class Reference

An implementation of JniEnvRef which can be used in contexts where no JNIEnv is available yet. More...

#include <JniUtil.h>

Inheritance diagram for JniEnvAutoRef:

JniEnvRef List of all members.

Public Member Functions

 JniEnvAutoRef ()
 Uses GetEnv to access current thread's JNIEnv pointer.
void suppressDetach ()
 Suppresses default detach-on-destruct behavior.
 ~JniEnvAutoRef ()
JniExceptionChecker operator-> () const
JNIEnv * get ()
void handleExcn (std::exception &ex)

Private Attributes

bool needDetach

Detailed Description

An implementation of JniEnvRef which can be used in contexts where no JNIEnv is available yet.

When an environment is already available, it is used automatically, but when unavailable, this class takes care of attaching the thread (in the constructor) and detaching it (in the destructor). For threads created within Fennel, attach/detach can be optimized by allocating a JniEnvAutoRef on a thread's initial stack frame so that it will be available to all methods called.

Definition at line 113 of file JniUtil.h.


Constructor & Destructor Documentation

JniEnvAutoRef::JniEnvAutoRef (  )  [explicit]

Uses GetEnv to access current thread's JNIEnv pointer.

Definition at line 523 of file JniUtil.cpp.

JniEnvAutoRef::~JniEnvAutoRef (  ) 

Definition at line 528 of file JniUtil.cpp.

References JniUtil::detachJavaEnv(), and needDetach.

00529 {
00530     if (needDetach) {
00531         JniUtil::detachJavaEnv();
00532     }
00533 }


Member Function Documentation

void JniEnvAutoRef::suppressDetach (  ) 

Suppresses default detach-on-destruct behavior.

REVIEW jvs 13-Oct-2006: Get rid of this and arrange for all native-spawned threads to attach on start and detach on end.

Definition at line 535 of file JniUtil.cpp.

References needDetach.

Referenced by JavaTraceTarget::onThreadStart(), and JavaThreadTracker::onThreadStart().

00536 {
00537     needDetach = false;
00538 }

JniExceptionChecker JniEnvRef::operator-> (  )  const [inline, inherited]

Definition at line 91 of file JniUtil.h.

00092     {
00093         return JniExceptionChecker(pEnv);
00094     }

JNIEnv* JniEnvRef::get (  )  [inline, inherited]

Definition at line 96 of file JniUtil.h.

Referenced by JniLocalRefReaper::JniLocalRefReaper().

00097     {
00098         return pEnv;
00099     }

void JniEnvRef::handleExcn ( std::exception &  ex  )  [inherited]

Definition at line 540 of file JniUtil.cpp.

References JavaExcn::getJavaException(), FennelExcn::getMessage(), and JniEnvRef::pEnv.

Referenced by Java_com_lucidera_farrago_fennel_LucidEraJni_registerStreamFactory(), Java_net_sf_farrago_fennel_FennelStorage_executeJavaCmd(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamFetch(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamGraphClose(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamGraphGetInputStreams(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamGraphOpen(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamRestart(), Java_net_sf_farrago_fennel_FennelStorage_tupleStreamTransformFetch(), and JNI_OnLoad().

00541 {
00542     JavaExcn *pJavaExcn = dynamic_cast<JavaExcn *>(&ex);
00543     if (pJavaExcn) {
00544         pEnv->Throw(pJavaExcn->getJavaException());
00545         return;
00546     }
00547     std::string what;
00548     FennelExcn *pFennelExcn = dynamic_cast<FennelExcn *>(&ex);
00549     if (pFennelExcn) {
00550         what = pFennelExcn->getMessage();
00551     } else {
00552         std::bad_alloc *pBadAllocExcn =
00553             dynamic_cast<std::bad_alloc *>(&ex);
00554         if (pBadAllocExcn) {
00555             // Convert bad_alloc's terrible error mesage into something fit for
00556             // human consumption.
00557             what = FennelResource::instance().internalError("malloc failed");
00558         } else {
00559             what = FennelResource::instance().internalError(ex.what());
00560         }
00561     }
00562     // TODO:  need special-case handling for out-of-memory here
00563     jclass classSQLException = pEnv->FindClass("java/sql/SQLException");
00564     jstring jMessage = pEnv->NewStringUTF(what.c_str());
00565     jmethodID constructor = pEnv->GetMethodID(
00566         classSQLException,"<init>","(Ljava/lang/String;)V");
00567     jthrowable t = (jthrowable)
00568         pEnv->NewObject(classSQLException,constructor,jMessage);
00569     pEnv->Throw(t);
00570 }


Member Data Documentation

bool JniEnvAutoRef::needDetach [private]

Definition at line 116 of file JniUtil.h.

Referenced by suppressDetach(), and ~JniEnvAutoRef().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:33 2009 for Fennel by  doxygen 1.5.1