JNICCCFactory.cpp

00001 
00038 #include "JNICCCFactory.h"
00039 #include "JNICCC.h"
00040 #include "JNIHelpers.h"
00041 
00042 jmethodID       JNICCCFactory::_udt_clsFactoryInterfaceUDT_create = NULL;
00043 jmethodID       JNICCCFactory::_udt_clsFactoryInterfaceUDT_cloneFactory = NULL;
00044 
00045 bool JNICCCFactory::initJNITypes(JNIEnv* env){
00046 
00047         if(_udt_clsFactoryInterfaceUDT_create != NULL)
00048                 return true;
00049 
00050         if(udt_clsFactoryInterfaceUDT==NULL)
00051                 return false;
00052 
00053         _udt_clsFactoryInterfaceUDT_cloneFactory = env->GetMethodID(udt_clsFactoryInterfaceUDT, "cloneFactory", "()Lcom/barchart/udt/FactoryInterfaceUDT;");
00054         _udt_clsFactoryInterfaceUDT_create = env->GetMethodID(udt_clsFactoryInterfaceUDT, "create", "()Lcom/barchart/udt/CCC;");
00055 
00056         return true;
00057 }
00058 
00059 JNIEnv* JNICCCFactory::AttachToJVM()
00060 {
00061         JNIEnv* env = NULL;
00062 
00063         jint result = _javaVM->GetEnv(reinterpret_cast<void**>(&env),JNI_VERSION_1_4);
00064 
00065         if(result == JNI_EDETACHED)
00066                 _javaVM->AttachCurrentThread(reinterpret_cast<void**>(&env),NULL);
00067 
00068         return env;
00069 }
00070 
00071 
00072 JNICCCFactory::JNICCCFactory(JNIEnv* env, jobject factoryUDT) {
00073 
00074         initJNITypes(env);
00075 
00076         if(factoryUDT==NULL){
00077                 ;//TODO create new exception
00078         }
00079 
00080         env->GetJavaVM(&_javaVM);
00081 
00082         //We need to create a global JNI ref since
00083         //we are storing the jobject as a member variable
00084         //for use later on
00085         _factoryUDT = env->NewGlobalRef(factoryUDT);
00086 
00087 }
00088 
00089 JNICCCFactory::~JNICCCFactory(){
00090 
00091         JNIEnv* env = AttachToJVM();
00092 
00093         if(_factoryUDT != NULL){
00094                 env->DeleteLocalRef(_factoryUDT);
00095         }
00096 
00097 }
00098 
00099 CCC* JNICCCFactory::create(){
00100 
00101         JNIEnv* env = AttachToJVM();
00102 
00103         jobject objCCC = env->CallObjectMethod(_factoryUDT,_udt_clsFactoryInterfaceUDT_create);
00104 
00105         if(objCCC==NULL && !env->ExceptionCheck() ){
00106                 UDT_ThrowExceptionUDT_Message(env, 0, "failed to allocate CCC class via com.barchart.udt.FactoryInterfaceUDT");
00107         }
00108 
00109         return reinterpret_cast<CCC*> ((intptr_t) env->GetLongField(objCCC,udt_clsCCC_fld_nativeHandleID));
00110 }
00111 
00112 CCCVirtualFactory* JNICCCFactory::clone(){
00113 
00114         JNIEnv* env = AttachToJVM();
00115 
00116         jobject objFactoryUDT = env->CallObjectMethod(_factoryUDT,_udt_clsFactoryInterfaceUDT_cloneFactory);
00117 
00118         return new JNICCCFactory(env,objFactoryUDT);
00119 
00120 }
00121 

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1