GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
java_any.cpp
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 /**
24  * @file java_any.cpp
25  * @author Jiunn Haur Lim <[email protected]>
26  */
27 
28 #include <graphlab.hpp>
29 #include "java_any.hpp"
30 #include "org_graphlab_Core.hpp"
31 #include "org_graphlab_Updater.hpp"
32 
33 using namespace graphlab;
34 
35 //---------------------------------------------------------------
36 // java_any instance members
37 //---------------------------------------------------------------
38 
39 java_any::java_any(JNIEnv *env, jobject &obj){
40  // create a new ref so that it doesn't get garbage collected
41  mobj = env->NewGlobalRef(obj);
42 }
43 
44 java_any::java_any() : mobj(NULL){}
45 
46 jobject &java_any::obj() {
47  return mobj;
48 }
49 
50 const jobject &java_any::obj() const {
51  return mobj;
52 }
53 
55 
56  // other doesn't have an existing ref
57  if (NULL == other.mobj){
58  this->mobj = NULL;
59  return;
60  }
61 
62  // create a new ref
63  JNIEnv *env = proxy_updater::core::get_jni_env();
64  this->mobj = env->NewGlobalRef(other.mobj);
65 
66 }
67 
69 
70  // prevent self assignment
71  if (this == &other) return *this;
72 
73  JNIEnv *env = proxy_updater::core::get_jni_env();
74  jobject obj = NULL;
75 
76  // if other has a java object, create a new ref
77  if (NULL != other.mobj){
78  obj = env->NewGlobalRef(other.mobj);
79  }
80 
81  // if this has a java object, delete ref
82  if (NULL != this->mobj){
83  env->DeleteGlobalRef(this->mobj);
84  }
85 
86  // assign!
87  this->mobj = obj;
88 
89  return *this;
90 
91 }
92 
93 void java_any::set_obj(jobject obj){
94 
95  JNIEnv *env = proxy_updater::core::get_jni_env();
96 
97  if (NULL != mobj){
98  // delete current ref
99  env->DeleteGlobalRef(mobj);
100  mobj = NULL;
101  }
102 
103  if (NULL != obj){
104  mobj = env->NewGlobalRef(obj);
105  }
106 
107 }
108 
110  if (NULL == mobj) return;
111  // delete reference to allow garbage collection
112  JNIEnv *env = proxy_updater::core::get_jni_env();
113  env->DeleteGlobalRef(mobj);
114  mobj = NULL;
115 }
116 
117 bool java_any::handle_exception(JNIEnv *env) const {
118 
119  // check for exception
120  jthrowable exc = env->ExceptionOccurred();
121  if (!exc) return false;
122 
123  env->ExceptionDescribe();
124  env->ExceptionClear();
126  env,
127  "java/lang/IllegalArgumentException",
128  "thrown from C code.");
129 
130  return true;
131 
132 }