#include "Config.h" namespace rho { IMPLEMENT_LOGCLASS(CConfigBase, "ConfigJNI"); //ConfigFactorySingleton staff const char* const CConfigBase::FACTORY_SINGLETON_CLASS = "com.rho.config.ConfigFactorySingleton"; jclass CConfigBase::s_clsFactorySingleton = 0; jmethodID CConfigBase::s_midFactorySetInstance; jmethodID CConfigBase::s_midFactoryGetInstance; //ConfigFactory staff const char* const CConfigBase::IFACTORY_CLASS = "com.rho.config.IConfigFactory"; jclass CConfigBase::s_clsIFactory = 0; jmethodID CConfigBase::s_midGetApiSingleton; jmethodID CConfigBase::s_midGetApiObject; //ConfigSingletonBase staff const char* const CConfigBase::SINGLETON_BASE_CLASS = "com.rho.config.ConfigSingletonBase"; jclass CConfigBase::s_clsSingletonBase = 0; //ConfigBase staff const char* const CConfigBase::OBJECT_BASE_CLASS = "com.rho.config.ConfigBase"; jclass CConfigBase::s_clsObjectBase = 0; //Method tasks const char* const CConfigBase::GETCONFIGPATH_TASK_CLASS = "com.rho.config.ConfigSingletonBase$getConfigPathTask"; jclass CConfigBase::s_clsgetConfigPathTask = 0; jmethodID CConfigBase::s_midgetConfigPathTask; const char* const CConfigBase::SETCONFIGPATH_TASK_CLASS = "com.rho.config.ConfigSingletonBase$setConfigPathTask"; jclass CConfigBase::s_clssetConfigPathTask = 0; jmethodID CConfigBase::s_midsetConfigPathTask; const char* const CConfigBase::GETPROPERTYSTRING_TASK_CLASS = "com.rho.config.ConfigSingletonBase$getPropertyStringTask"; jclass CConfigBase::s_clsgetPropertyStringTask = 0; jmethodID CConfigBase::s_midgetPropertyStringTask; const char* const CConfigBase::SETPROPERTYSTRING_TASK_CLASS = "com.rho.config.ConfigSingletonBase$setPropertyStringTask"; jclass CConfigBase::s_clssetPropertyStringTask = 0; jmethodID CConfigBase::s_midsetPropertyStringTask; const char* const CConfigBase::GETPROPERTYINT_TASK_CLASS = "com.rho.config.ConfigSingletonBase$getPropertyIntTask"; jclass CConfigBase::s_clsgetPropertyIntTask = 0; jmethodID CConfigBase::s_midgetPropertyIntTask; const char* const CConfigBase::SETPROPERTYINT_TASK_CLASS = "com.rho.config.ConfigSingletonBase$setPropertyIntTask"; jclass CConfigBase::s_clssetPropertyIntTask = 0; jmethodID CConfigBase::s_midsetPropertyIntTask; const char* const CConfigBase::GETPROPERTYBOOL_TASK_CLASS = "com.rho.config.ConfigSingletonBase$getPropertyBoolTask"; jclass CConfigBase::s_clsgetPropertyBoolTask = 0; jmethodID CConfigBase::s_midgetPropertyBoolTask; const char* const CConfigBase::SETPROPERTYBOOL_TASK_CLASS = "com.rho.config.ConfigSingletonBase$setPropertyBoolTask"; jclass CConfigBase::s_clssetPropertyBoolTask = 0; jmethodID CConfigBase::s_midsetPropertyBoolTask; const char* const CConfigBase::ISPROPERTYEXISTS_TASK_CLASS = "com.rho.config.ConfigSingletonBase$isPropertyExistsTask"; jclass CConfigBase::s_clsisPropertyExistsTask = 0; jmethodID CConfigBase::s_midisPropertyExistsTask; const char* const CConfigBase::REMOVEPROPERTY_TASK_CLASS = "com.rho.config.ConfigSingletonBase$removePropertyTask"; jclass CConfigBase::s_clsremovePropertyTask = 0; jmethodID CConfigBase::s_midremovePropertyTask; const char* const CConfigBase::LOADFROMFILE_TASK_CLASS = "com.rho.config.ConfigSingletonBase$loadFromFileTask"; jclass CConfigBase::s_clsloadFromFileTask = 0; jmethodID CConfigBase::s_midloadFromFileTask; //---------------------------------------------------------------------------------------------------------------------- JNIEnv* CConfigBase::jniInit() { JNIEnv *env = jnienv(); if(!env) { LOG(FATAL) + "JNI init failed: JNIEnv is null"; return 0; } return jniInit(env); } //---------------------------------------------------------------------------------------------------------------------- JNIEnv* CConfigBase::jniInit(JNIEnv* env) { static bool initialized = false; env = MethodExecutorJni::jniInit(env); if (!env) { LOG(FATAL) + "JNI init failed"; return 0; } if(!initialized) { //init ConfigFactorySingleton JNI s_clsFactorySingleton = loadClass(env, FACTORY_SINGLETON_CLASS); if (!s_clsFactorySingleton) return 0; s_midFactorySetInstance = env->GetStaticMethodID(s_clsFactorySingleton, "setInstance", "(Lcom/rho/config/IConfigFactory;)V"); if(!s_midFactorySetInstance) { LOG(FATAL) + "Failed to get method 'setInstance' for java class " + FACTORY_SINGLETON_CLASS; return NULL; } s_midFactoryGetInstance = env->GetStaticMethodID(s_clsFactorySingleton, "getInstance", "()Lcom/rho/config/IConfigFactory;"); if(!s_midFactoryGetInstance) { LOG(FATAL) + "Failed to get method 'getInstance' for java class " + FACTORY_SINGLETON_CLASS; return NULL; } //init IConfigFactory JNI s_clsIFactory = loadClass(env, IFACTORY_CLASS); if (!s_clsIFactory) return 0; s_midGetApiSingleton = env->GetMethodID(s_clsIFactory, "getApiSingleton", "()Lcom/rho/config/IConfigSingleton;"); if(!s_midGetApiSingleton) { LOG(FATAL) + "Failed to get method 'getApiSingleton' for java class " + IFACTORY_CLASS; return NULL; } s_midGetApiObject = env->GetMethodID(s_clsIFactory, "getApiObject", "(Ljava/lang/String;)Lcom/rho/config/IConfig;"); if(!s_midGetApiObject) { LOG(FATAL) + "Failed to get method 'getApiObject' for java class " + IFACTORY_CLASS; return NULL; } s_clsSingletonBase = loadClass(env, SINGLETON_BASE_CLASS); if (!s_clsSingletonBase) return 0; s_clsObjectBase = loadClass(env, OBJECT_BASE_CLASS); if (!s_clsObjectBase) return 0; s_clsgetConfigPathTask = loadClass(env, GETCONFIGPATH_TASK_CLASS); if (!s_clsgetConfigPathTask) return 0; s_midgetConfigPathTask = env->GetMethodID(s_clsgetConfigPathTask, "", "(Lcom/rho/config/IConfigSingleton;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midgetConfigPathTask) { LOG(FATAL) + "Failed to get constructor for java class " + GETCONFIGPATH_TASK_CLASS; return NULL; } s_clssetConfigPathTask = loadClass(env, SETCONFIGPATH_TASK_CLASS); if (!s_clssetConfigPathTask) return 0; s_midsetConfigPathTask = env->GetMethodID(s_clssetConfigPathTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midsetConfigPathTask) { LOG(FATAL) + "Failed to get constructor for java class " + SETCONFIGPATH_TASK_CLASS; return NULL; } s_clsgetPropertyStringTask = loadClass(env, GETPROPERTYSTRING_TASK_CLASS); if (!s_clsgetPropertyStringTask) return 0; s_midgetPropertyStringTask = env->GetMethodID(s_clsgetPropertyStringTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midgetPropertyStringTask) { LOG(FATAL) + "Failed to get constructor for java class " + GETPROPERTYSTRING_TASK_CLASS; return NULL; } s_clssetPropertyStringTask = loadClass(env, SETPROPERTYSTRING_TASK_CLASS); if (!s_clssetPropertyStringTask) return 0; s_midsetPropertyStringTask = env->GetMethodID(s_clssetPropertyStringTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Ljava/lang/String;ZLcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midsetPropertyStringTask) { LOG(FATAL) + "Failed to get constructor for java class " + SETPROPERTYSTRING_TASK_CLASS; return NULL; } s_clsgetPropertyIntTask = loadClass(env, GETPROPERTYINT_TASK_CLASS); if (!s_clsgetPropertyIntTask) return 0; s_midgetPropertyIntTask = env->GetMethodID(s_clsgetPropertyIntTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midgetPropertyIntTask) { LOG(FATAL) + "Failed to get constructor for java class " + GETPROPERTYINT_TASK_CLASS; return NULL; } s_clssetPropertyIntTask = loadClass(env, SETPROPERTYINT_TASK_CLASS); if (!s_clssetPropertyIntTask) return 0; s_midsetPropertyIntTask = env->GetMethodID(s_clssetPropertyIntTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;IZLcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midsetPropertyIntTask) { LOG(FATAL) + "Failed to get constructor for java class " + SETPROPERTYINT_TASK_CLASS; return NULL; } s_clsgetPropertyBoolTask = loadClass(env, GETPROPERTYBOOL_TASK_CLASS); if (!s_clsgetPropertyBoolTask) return 0; s_midgetPropertyBoolTask = env->GetMethodID(s_clsgetPropertyBoolTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midgetPropertyBoolTask) { LOG(FATAL) + "Failed to get constructor for java class " + GETPROPERTYBOOL_TASK_CLASS; return NULL; } s_clssetPropertyBoolTask = loadClass(env, SETPROPERTYBOOL_TASK_CLASS); if (!s_clssetPropertyBoolTask) return 0; s_midsetPropertyBoolTask = env->GetMethodID(s_clssetPropertyBoolTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;ZZLcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midsetPropertyBoolTask) { LOG(FATAL) + "Failed to get constructor for java class " + SETPROPERTYBOOL_TASK_CLASS; return NULL; } s_clsisPropertyExistsTask = loadClass(env, ISPROPERTYEXISTS_TASK_CLASS); if (!s_clsisPropertyExistsTask) return 0; s_midisPropertyExistsTask = env->GetMethodID(s_clsisPropertyExistsTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midisPropertyExistsTask) { LOG(FATAL) + "Failed to get constructor for java class " + ISPROPERTYEXISTS_TASK_CLASS; return NULL; } s_clsremovePropertyTask = loadClass(env, REMOVEPROPERTY_TASK_CLASS); if (!s_clsremovePropertyTask) return 0; s_midremovePropertyTask = env->GetMethodID(s_clsremovePropertyTask, "", "(Lcom/rho/config/IConfigSingleton;Ljava/lang/String;ZLcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midremovePropertyTask) { LOG(FATAL) + "Failed to get constructor for java class " + REMOVEPROPERTY_TASK_CLASS; return NULL; } s_clsloadFromFileTask = loadClass(env, LOADFROMFILE_TASK_CLASS); if (!s_clsloadFromFileTask) return 0; s_midloadFromFileTask = env->GetMethodID(s_clsloadFromFileTask, "", "(Lcom/rho/config/IConfigSingleton;Lcom/rhomobile/rhodes/api/IMethodResult;)V"); if(!s_midloadFromFileTask) { LOG(FATAL) + "Failed to get constructor for java class " + LOADFROMFILE_TASK_CLASS; return NULL; } initialized = true; LOG(TRACE) + "CConfig JNI init succeeded"; } return env; } //---------------------------------------------------------------------------------------------------------------------- void CConfigBase::setJavaFactory(JNIEnv* env, jobject jFactory) { LOG(TRACE) + "setJavaFactory"; env = jniInit(env); if (!env) { LOG(FATAL) + "JNI initialization failed"; return; } env->CallStaticVoidMethod(s_clsFactorySingleton, s_midFactorySetInstance, jFactory); LOG(TRACE) + "setJavaFactory succeeded"; } //---------------------------------------------------------------------------------------------------------------------- jobject CConfigBase::getFactory(JNIEnv* env) { jobject res = env->CallStaticObjectMethod(s_clsFactorySingleton, s_midFactoryGetInstance); return res; } //---------------------------------------------------------------------------------------------------------------------- jobject CConfigBase::getSingleton(JNIEnv* env) { jhobject jhFactory = getFactory(env); jobject res = env->CallObjectMethod(jhFactory.get(), s_midGetApiSingleton); return res; } //---------------------------------------------------------------------------------------------------------------------- jobject CConfigBase::getObject(JNIEnv* env) { jhstring jhId = rho_cast(env, m_id); jhobject jhFactory = getFactory(env); jobject res = env->CallObjectMethod(jhFactory.get(), s_midGetApiObject, jhId.get()); return res; } //---------------------------------------------------------------------------------------------------------------------- }