Commit 6fa3b764 authored by Ryan Berkheimer's avatar Ryan Berkheimer

added new Utils class for Response, added all methods

parent 332ca170
Pipeline #5768 failed with stages
in 0 seconds
......@@ -29,6 +29,13 @@ bool RecordUtils::isValid(struct record *record)
return (bool)this->jvm->CallBooleanMethod(record->jrecord, this->isValidMethodId);
}
void RecordUtils::setValid(struct record *record, bool isValid)
{
jobject jBoolVal = jvm->NewObject(this->typeUtils->getBoolClass(), this->typeUtils->createBoolMethod(), (jboolean)isValid);
this->jvm->CallVoidMethod(record->jrecord, this->setValidMethodId, jBoolVal);
this->jvm->DeleteLocalRef(jBoolVal);
}
struct record *RecordUtils::getCopy(struct record *record)
{
jobject jRecordCopy = this->jvm->CallObjectMethod(record->jrecord, this->getCopyMethodId);
......@@ -166,6 +173,7 @@ void RecordUtils::loadMethodIds()
jclass recordClass = JniUtils::getNamedClass(this->jvm, "gov/noaa/messageapi/interfaces/IRecord");
/*Intrinsic Methods*/
this->isValidMethodId = JniUtils::getMethod(this->jvm, recordClass, "isValid", this->getMethodSignature("isValid"), false);
this->setValidMethodId = JniUtils::getMethod(this->jvm, recordClass, "setValid", this->getMethodSignature("setValid"), false);
this->getCopyMethodId = JniUtils::getMethod(this->jvm, recordClass, "getCopy", this->getMethodSignature("getCopy"), false);
/*Field Related Methods*/
this->getFieldIdsMethodId = JniUtils::getMethod(this->jvm, recordClass, "getFieldIds", this->getMethodSignature("getFieldIds"), false);
......@@ -184,8 +192,13 @@ const char *RecordUtils::getMethodSignature(const char *methodName)
{
if (strcmp(methodName, "isValid") == 0)
{
return "()Ljava/lang/Boolean;";
return "()Z";
}
else if (strcmp(methodName, "setValid") == 0)
{
return "(Z)V";
}
else if (strcmp(methodName, "getCopy") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRecord;";
......@@ -196,7 +209,7 @@ const char *RecordUtils::getMethodSignature(const char *methodName)
}
else if (strcmp(methodName, "hasField") == 0)
{
return "(Ljava/lang/String;)Ljava/lang/Boolean;";
return "(Ljava/lang/String;)Z";
}
else if (strcmp(methodName, "getFields") == 0)
{
......@@ -212,7 +225,7 @@ const char *RecordUtils::getMethodSignature(const char *methodName)
}
else if (strcmp(methodName, "hasCondition") == 0)
{
return "(Ljava/lang/String;)Ljava/lang/Boolean;";
return "(Ljava/lang/String;)Z";
}
else if (strcmp(methodName, "getConditions") == 0)
{
......
......@@ -35,6 +35,7 @@ public:
struct record *getCopy(struct record *record);
bool isValid(struct record *record);
void setValid(struct record *record, bool isValid);
bool hasField(struct record *record, const char *fieldId);
struct string_list *getFieldIds(struct record *record);
......@@ -54,6 +55,7 @@ private:
/*Record Methods*/
jmethodID isValidMethodId;
jmethodID setValidMethodId;
jmethodID getCopyMethodId;
jmethodID getFieldIdsMethodId;
jmethodID getFieldsMethodId;
......
#include "ResponseUtils.h"
#include <jni.h>
/**
* Responses are returned by calling submit on a request. Responses operate asynchronously,
* setting an isComplete flag from false to true whenever the specified process is complete.
* Responses execute the request, and holds any records and/or rejections
* that are returned by the evaluation of the request. These records are an aggregate of all
* computation paths through a given request, so may contain records gathered from multiple sources
* in the same computation.
*
* @author Ryan Berkheimer
*/
/* Default Constructor */
ResponseUtils::ResponseUtils(JNIEnv *jvm, TypeUtils *typeUtils, ListUtils *listUtils)
{
this->loadGlobalRefs(jvm, typeUtils, listUtils);
this->loadMethodIds();
}
/* Default Destructor */
ResponseUtils::~ResponseUtils()
{
try
{}
catch (const std::exception &e)
{
std::cout << e.what();
}
}
/* Public API */
bool ResponseUtils::isComplete(struct response *response)
{
return (bool)this->jvm->CallBooleanMethod(response->jresponse, this->isCompleteMethodId);
}
struct request *ResponseUtils::getRequest(struct response *response)
{
jobject jrequest = this->jvm->CallObjectMethod(response->jresponse, this->getRequestMethodId);
struct request *request = (struct request *)malloc(sizeof(struct request) + sizeof(jrequest));
request->jrequest = jrequest;
return request;
}
struct rejection_list *ResponseUtils::getRejections(struct response *response)
{
jobject jRejections = this->jvm->CallObjectMethod(response->jresponse, this->getRejectionsMethodId);
int rejectionCount = this->listUtils->getListLength(jRejections);
struct rejection_list *rejection_list = (struct rejection_list *)malloc(sizeof(struct rejection_list));
rejection_list->count = rejectionCount;
rejection_list->jrejections = jRejections;
return rejection_list;
}
struct record_list *ResponseUtils::getRecords(struct response *response)
{
jobject jRecords = this->jvm->CallObjectMethod(response->jresponse, this->getRecordsMethodId);
int recordCount = this->listUtils->getListLength(jRecords);
struct record_list *record_list = (struct record_list *)malloc(sizeof(struct record_list));
record_list->count = recordCount;
record_list->jrecords = jRecords;
return record_list;
}
void ResponseUtils::setRejections(struct response *response, struct rejection_list *rejections)
{
this->jvm->CallVoidMethod(response->jresponse, this->setRejectionsMethodId, rejections->jrejections);
}
void ResponseUtils::setRecords(struct response *response, struct record_list *records)
{
this->jvm->CallVoidMethod(response->jresponse, this->setRecordsMethodId, records->jrecords);
}
void ResponseUtils::setComplete(struct response *response, bool isComplete)
{
jobject jBoolVal = jvm->NewObject(this->typeUtils->getBoolClass(), this->typeUtils->createBoolMethod(), (jboolean)isComplete);
this->jvm->CallVoidMethod(response->jresponse, this->setCompleteMethodId, jBoolVal);
this->jvm->DeleteLocalRef(jBoolVal);
}
/* Private Methods */
void ResponseUtils::loadGlobalRefs(JNIEnv *jvm, TypeUtils *typeUtils, ListUtils *listUtils)
{
this->jvm = jvm;
this->typeUtils = typeUtils;
this->listUtils = listUtils;
}
void ResponseUtils::loadMethodIds()
{
jclass responseClass = JniUtils::getNamedClass(this->jvm, "gov/noaa/messageapi/interfaces/IResponse");
this->isCompleteMethodId = JniUtils::getMethod(this->jvm, responseClass, "isComplete", this->getMethodSignature("isComplete"), false);
this->getRequestMethodId = JniUtils::getMethod(this->jvm, responseClass, "getRequest", this->getMethodSignature("getRequest"), false);
this->getRejectionsMethodId = JniUtils::getMethod(this->jvm, responseClass, "getRejections", this->getMethodSignature("getRejections"), false);
this->getRecordsMethodId = JniUtils::getMethod(this->jvm, responseClass, "getRecords", this->getMethodSignature("getRecords"), false);
this->setRejectionsMethodId = JniUtils::getMethod(this->jvm, responseClass, "setRejections", this->getMethodSignature("setRejections"), false);
this->setRecordsMethodId = JniUtils::getMethod(this->jvm, responseClass, "setRecords", this->getMethodSignature("setRecords"), false);
this->setCompleteMethodId = JniUtils::getMethod(this->jvm, responseClass, "setComplete", this->getMethodSignature("setComplete"), false);
jvm->DeleteLocalRef(responseClass);
}
const char *ResponseUtils::getMethodSignature(const char *methodName)
{
if (strcmp(methodName, "isComplete") == 0)
{
return "()Z";
}
else if (strcmp(methodName, "getRequest") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRequest;";
}
else if (strcmp(methodName, "getRejections") == 0)
{
return "()Ljava/util/List;";
}
else if (strcmp(methodName, "getRecords") == 0)
{
return "()Ljava/util/List;";
}
else if (strcmp(methodName, "setRejections") == 0)
{
return "(Ljava/util/List;)V";
}
else if (strcmp(methodName, "setRecords") == 0)
{
return "(Ljava/util/List;)V";
}
else if (strcmp(methodName, "setComplete") == 0)
{
return "(Z)V";
}
return NULL;
}
\ No newline at end of file
#ifndef _RESPONSEUTILS_H
#define _RESPONSEUTILS_H
#include <jni.h>
#include <stdbool.h>
#include "messageapi_structs.h"
#ifdef __cplusplus
#include <iostream>
#include <string>
#include "ListUtils.h"
#include "TypeUtils.h"
/**
* Responses are returned by calling submit on a request. Responses operate asynchronously,
* setting an isComplete flag from false to true whenever the specified process is complete.
* Responses execute the request, and holds records and/or rejections
* that are returned by the evaluation of the request. These records are an aggregate of all
* computation paths through a given request, so may contain records gathered from multiple sources
* in the same computation.
*
* @author Ryan Berkheimer
*/
class ResponseUtils
{
public:
/*Default constructor/destructors*/
ResponseUtils(JNIEnv *javaEnv, TypeUtils *typeUtils, ListUtils *listUtils);
~ResponseUtils();
/* Response Methods */
bool isComplete(struct response *response);
struct request *getRequest(struct response *response);
struct rejection_list *getRejections(struct response *response);
struct record_list *getRecords(struct response *response);
void setRejections(struct response *response, struct rejection_list *rejections);
void setRecords(struct response *response, struct record_list *records);
void setComplete(struct response *response, bool isComplete);
private:
/*Vars*/
JNIEnv *jvm;
ListUtils *listUtils;
TypeUtils *typeUtils;
/*Request Methods*/
jmethodID isCompleteMethodId;
jmethodID getRequestMethodId;
jmethodID getRejectionsMethodId;
jmethodID getRecordsMethodId;
jmethodID setRejectionsMethodId;
jmethodID setRecordsMethodId;
jmethodID setCompleteMethodId;
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadMethodIds();
void loadGlobalRefs(JNIEnv *env, TypeUtils *typeUtils, ListUtils *listUtils);
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getMethodSignature(const char *methodName);
};
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment