JniUtil.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/farrago/JniUtil.h#33 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #ifndef Fennel_JniUtil_Included
00025 #define Fennel_JniUtil_Included
00026 
00027 #include "fennel/common/AtomicCounter.h"
00028 #include "fennel/common/PseudoUuid.h"
00029 #include "fennel/tuple/TupleDescriptor.h"
00030 #include "fennel/farrago/JavaThreadTracker.h"
00031 
00032 #include <jni.h>
00033 #include <locale>
00034 #include <fstream>
00035 
00036 FENNEL_BEGIN_NAMESPACE
00037 
00041 class FENNEL_FARRAGO_EXPORT JniExceptionChecker
00042 {
00043     JNIEnv *pEnv;
00044 
00049     void checkExceptions();
00050 
00051 public:
00052     explicit JniExceptionChecker(JNIEnv *pEnvInit)
00053     {
00054         pEnv = pEnvInit;
00055     }
00056 
00057     ~JniExceptionChecker();
00058 
00059     JNIEnv *operator->() const
00060     {
00061         return pEnv;
00062     }
00063 };
00064 
00073 class FENNEL_FARRAGO_EXPORT JniEnvRef
00074 {
00075     JNIEnv *pEnv;
00076 
00077 public:
00081     explicit JniEnvRef(JNIEnv *pEnvInit)
00082     {
00083         pEnv = pEnvInit;
00084     }
00085 
00086     void operator = (JniEnvRef const &other)
00087     {
00088         pEnv = other.pEnv;
00089     }
00090 
00091     JniExceptionChecker operator->() const
00092     {
00093         return JniExceptionChecker(pEnv);
00094     }
00095 
00096     JNIEnv *get()
00097     {
00098         return pEnv;
00099     }
00100 
00101     void handleExcn(std::exception &ex);
00102 };
00103 
00113 class FENNEL_FARRAGO_EXPORT JniEnvAutoRef
00114     : public JniEnvRef
00115 {
00116     bool needDetach;
00117 
00118 public:
00122     explicit JniEnvAutoRef();
00123 
00132     void suppressDetach();
00133 
00134     ~JniEnvAutoRef();
00135 };
00136 
00137 // TODO jvs 21-Aug-2007:  templatize and clean this up as part of
00138 // memory allocation cleanup (FNL-55)
00139 
00145 class FENNEL_FARRAGO_EXPORT JniLocalRefReaper
00146 {
00147     JNIEnv *pEnv;
00148     jobject obj;
00149 
00150 public:
00151     JniLocalRefReaper(JniEnvRef &pEnvInit, jobject objInit)
00152     {
00153         pEnv = pEnvInit.get();
00154         obj = objInit;
00155     }
00156 
00157     ~JniLocalRefReaper()
00158     {
00159         if (obj) {
00160             pEnv->DeleteLocalRef(obj);
00161             obj = NULL;
00162         }
00163     }
00164 };
00165 
00166 class ConfigMap;
00167 
00171 class FENNEL_FARRAGO_EXPORT JniUtilParams
00172 {
00173 public:
00174     static ParamName paramJniHandleTraceFile;
00175 
00179     std::string jniHandleTraceFile;
00180 
00184     explicit JniUtilParams();
00185 
00189     void readConfig(ConfigMap const &configMap);
00190 };
00191 
00192 
00196 class FENNEL_FARRAGO_EXPORT JniUtil
00197 {
00198     friend class JniEnvAutoRef;
00199 
00203     static JavaVM *pVm;
00204 
00208     static jmethodID methGetClassName;
00209 
00213     static jmethodID methGetInterfaces;
00214 
00218     static jmethodID methGetModifiers;
00219 
00223     static jclass classModifier;
00224 
00228     static jmethodID methIsPublic;
00229 
00233     static jmethodID methIterator;
00234 
00238     static jmethodID methHasNext;
00239 
00243     static jmethodID methNext;
00244 
00248     static jmethodID methToString;
00249 
00259     static JNIEnv *getAttachedJavaEnv(bool &needDetach);
00260 
00264     static AtomicCounter handleCount;
00265 
00270     static bool traceHandleCountEnabled;
00271 
00276     static bool closeHandleCountTraceOnZero;
00277 
00281     static std::ofstream handleCountTraceStream;
00282 
00286     static JavaThreadTracker threadTracker;
00287 
00291     static void traceHandleCount(
00292         const char *pAction, const char *pType, const void *pHandle);
00293 
00294 public:
00298     static const jint jniVersion = JNI_VERSION_1_2;
00299 
00303     static jmethodID methGetJavaStreamHandle;
00304 
00308     static jmethodID methGetIndexRoot;
00309 
00313     static jmethodID methBase64Decode;
00314     static jclass classRhBase64;
00315 
00319     static jmethodID methRandomUUID;
00320     static jclass classUUID;
00321 
00325     static jmethodID methFarragoTransformInit;
00326 
00330     static jmethodID methFarragoTransformExecute;
00331 
00335     static jmethodID methFarragoTransformSetInputFetchTimeout;
00336 
00340     static jmethodID methFarragoTransformRestart;
00341 
00345     static jclass classFarragoTransformInputBinding;
00346 
00350     static jmethodID methFarragoTransformInputBindingCons;
00351 
00355     static jmethodID methFarragoRuntimeContextStatementClassForName;
00356 
00360     static jmethodID methFarragoRuntimeContextFindFarragoTransform;
00361 
00365     static jclass classUtil;
00366 
00370     static jmethodID methUtilGetStackTrace;
00371 
00373     static jclass classLong;
00374 
00376     static jclass classInteger;
00377 
00379     static jclass classShort;
00380 
00382     static jclass classDouble;
00383 
00385     static jclass classFloat;
00386 
00388     static jclass classBoolean;
00389 
00391     static jmethodID methLongValueOf;
00392 
00394     static jmethodID methIntegerValueOf;
00395 
00397     static jmethodID methShortValueOf;
00398 
00400     static jmethodID methDoubleValueOf;
00401 
00403     static jmethodID methFloatValueOf;
00404 
00406     static jmethodID methBooleanValueOf;
00407 
00409     static jmethodID methLongValue;
00410 
00412     static jmethodID methIntValue;
00413 
00415     static jmethodID methShortValue;
00416 
00418     static jmethodID methDoubleValue;
00419 
00421     static jmethodID methFloatValue;
00422 
00424     static jmethodID methBooleanValue;
00425 
00431     static void initDebug(char const *envVarName);
00432 
00438     static jint init(JavaVM *pVm);
00439 
00446     static void configure(const JniUtilParams &params);
00447 
00451     static void shutdown();
00452 
00460     static std::string getClassName(jclass jClass);
00461 
00472     static std::string getFirstPublicInterfaceName(jclass jClass);
00473 
00483     static std::string toStdString(JniEnvRef pEnv,jstring jString);
00484 
00494     static jstring toString(JniEnvRef pEnv,jobject jObject);
00495 
00505     static jobject getIter(JniEnvRef pEnv,jobject jCollection);
00506 
00516     static jobject getNextFromIter(JniEnvRef pEnv,jobject jIter);
00517 
00527     static uint lookUpEnum(std::string *pSymbols,std::string const &symbol);
00528 
00529     // TODO jvs 13-Oct-2006:  reprivate this
00534     static void detachJavaEnv();
00535 
00546     static void incrementHandleCount(const char *pType, const void *pHandle);
00547 
00558     static void decrementHandleCount(const char *pType, const void *pHandle);
00559 
00565     static inline int getHandleCount()
00566     {
00567         return handleCount;
00568     }
00569 
00573     static std::string getXmi(const TupleDescriptor &tupleDesc);
00574 
00578     static ThreadTracker &getThreadTracker();
00579 };
00580 
00581 class FENNEL_FARRAGO_EXPORT JniPseudoUuidGenerator
00582     : public PseudoUuidGenerator
00583 {
00584 public:
00585     virtual void generateUuid(PseudoUuid &pseudoUuid);
00586 };
00587 
00588 // NOTE jvs 16-Oct-2004:  This crazy kludge is for problems arising on Linux
00589 // when using multiple JNI libs.  This has to be included in each
00590 // JNI_OnLoad as a workaround (and it must be a macro, not a function).
00591 // Code was taken from _Stl_loc_assign_ids() in stlport/src/locale_impl.cpp.
00592 #define FENNEL_JNI_ONLOAD_COMMON() \
00593 { \
00594   _STL::num_get< \
00595     char, \
00596     _STL::istreambuf_iterator<char, _STL::char_traits<char> > >::id._M_index = \
00597       12; \
00598   _STL::num_get<char, const char*>::id._M_index          = 13; \
00599   _STL::num_put< \
00600     char, \
00601     _STL::ostreambuf_iterator<char, _STL::char_traits<char> > >::id._M_index = \
00602       14; \
00603   _STL::num_put<char, char*>::id._M_index                = 15; \
00604   _STL::time_get< \
00605     char, \
00606     _STL::istreambuf_iterator<char, _STL::char_traits<char> > >::id._M_index = \
00607       16; \
00608   _STL::time_get<char, const char*>::id._M_index         = 17; \
00609   _STL::time_put< \
00610     char, \
00611     _STL::ostreambuf_iterator<char, _STL::char_traits<char> > >::id._M_index = \
00612       18; \
00613   _STL::time_put<char, char*>::id._M_index               = 19; \
00614 }
00615 
00616 FENNEL_END_NAMESPACE
00617 
00618 #endif
00619 
00620 // End JniUtil.h

Generated on Mon Jun 22 04:00:19 2009 for Fennel by  doxygen 1.5.1