aboutsummaryrefslogtreecommitdiffstats
path: root/alljoyn_java/jni/JAboutObject.h
blob: f0c2032db3878b8dde16d9229d00a20b63625a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/******************************************************************************
 *    Copyright (c) Open Connectivity Foundation (OCF), AllJoyn Open Source
 *    Project (AJOSP) Contributors and others.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    All rights reserved. This program and the accompanying materials are
 *    made available under the terms of the Apache License, Version 2.0
 *    which accompanies this distribution, and is available at
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Copyright (c) Open Connectivity Foundation and Contributors to AllSeen
 *    Alliance. All rights reserved.
 *
 *    Permission to use, copy, modify, and/or distribute this software for
 *    any purpose with or without fee is hereby granted, provided that the
 *    above copyright notice and this permission notice appear in all
 *    copies.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 *    WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 *    WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 *    DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 *    PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 *    TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 *    PERFORMANCE OF THIS SOFTWARE.
 ******************************************************************************/

#ifndef _ALLJOYN_JABOUTOBJECT_H
#define _ALLJOYN_JABOUTOBJECT_H

#include <jni.h>
#include <alljoyn/AboutObj.h>
#include <alljoyn/AboutDataListener.h>
#include <qcc/Debug.h>

#include "alljoyn_jni_helper.h"

#define QCC_MODULE "ALLJOYN_JAVA"

class JBusAttachment;

/**
 * This is classes primary responsibility is to convert the value returned from
 * the Java AboutDataListener to a C++ values expected for a C++ AboutDataListener
 *
 * This class also implements the C++ AboutObj so that for every Java AboutObj
 * an instance of this AboutDataListener also exists.
 */
class JAboutObject : public ajn::AboutObj, public ajn::AboutDataListener {
  public:
    JAboutObject(JBusAttachment* bus, AnnounceFlag isAboutIntfAnnounced) :
        AboutObj(*reinterpret_cast<ajn::BusAttachment*>(bus), isAboutIntfAnnounced), busPtr(bus) {
        QCC_DbgPrintf(("JAboutObject::JAboutObject"));
        MID_getAboutData = NULL;
        MID_getAnnouncedAboutData = NULL;
        jaboutDataListenerRef = NULL;
        jaboutObjGlobalRef = NULL;
    }

    QStatus announce(JNIEnv* env, jobject thiz, jshort sessionPort, jobject jaboutDataListener) {
        QCC_UNUSED(thiz);
        // Make sure the jaboutDataListener is the latest version of the Java AboutDataListener
        if (env->IsInstanceOf(jaboutDataListener, CLS_AboutDataListener)) {
            JLocalRef<jclass> clazz = env->GetObjectClass(jaboutDataListener);

            MID_getAboutData = env->GetMethodID(clazz, "getAboutData", "(Ljava/lang/String;)Ljava/util/Map;");
            if (!MID_getAboutData) {
                return ER_FAIL;
            }
            MID_getAnnouncedAboutData = env->GetMethodID(clazz, "getAnnouncedAboutData", "()Ljava/util/Map;");
            if (!MID_getAnnouncedAboutData) {
                return ER_FAIL;
            }
        } else {
            return ER_FAIL;
        }
        QCC_DbgPrintf(("AboutObj_announce jaboutDataListener is an instance of CLS_AboutDataListener"));


        /*
         * The weak global reference jaboutDataListener cannot be directly used.  We
         * have to get a "hard" reference to it and then use that.  If you try to
         * use a weak reference directly you will crash and burn.
         */
        //The user can change the AboutDataListener between calls check to see
        // we already have a a jaboutDataListenerRef if we do delete that ref
        // and create a new one.
        if (jaboutDataListenerRef != NULL) {
            GetEnv()->DeleteGlobalRef(jaboutDataListenerRef);
            jaboutDataListenerRef = NULL;
        }
        jaboutDataListenerRef = env->NewGlobalRef(jaboutDataListener);
        if (!jaboutDataListenerRef) {
            QCC_LogError(ER_FAIL, ("Can't get new local reference to AboutDataListener"));
            return ER_FAIL;
        }

        return Announce(static_cast<ajn::SessionPort>(sessionPort), *this);
    }

    ~JAboutObject() {
        QCC_DbgPrintf(("JAboutObject::~JAboutObject"));
        if (jaboutDataListenerRef != NULL) {
            GetEnv()->DeleteGlobalRef(jaboutDataListenerRef);
            jaboutDataListenerRef = NULL;
        }
    }

    QStatus GetAboutData(ajn::MsgArg* msgArg, const char* language)
    {
        QCC_DbgPrintf(("JAboutObject::GetMsgArg"));

        /*
         * JScopedEnv will automagically attach the JVM to the current native
         * thread.
         */
        JScopedEnv env;

        // Note we don't check that if the jlanguage is null because null is an
        // acceptable value for the getAboutData Method call.
        JLocalRef<jstring> jlanguage = env->NewStringUTF(language);

        QStatus status = ER_FAIL;
        if (jaboutDataListenerRef != NULL && MID_getAboutData != NULL) {
            QCC_DbgPrintf(("Calling getAboutData for %s language.", language));
            JLocalRef<jobject> jannounceArg = CallObjectMethod(env, jaboutDataListenerRef, MID_getAboutData, (jstring)jlanguage);
            QCC_DbgPrintf(("JAboutObj::GetMsgArg Made Java Method call getAboutData"));
            // check for ErrorReplyBusException exception
            status = CheckForThrownException(env);
            if (ER_OK == status) {
                // Marshal the returned value
                if (!Marshal("a{sv}", jannounceArg, msgArg)) {
                    QCC_LogError(ER_FAIL, ("JAboutData(): GetMsgArgAnnounce() marshaling error"));
                    return ER_FAIL;
                }
            } else {
                QCC_DbgPrintf(("JAboutObj::GetMsgArg exception with status %s", QCC_StatusText(status)));
                return status;
            }
        }
        return ER_OK;
    }

    QStatus GetAnnouncedAboutData(ajn::MsgArg* msgArg)
    {
        QCC_DbgPrintf(("JAboutObject::~GetMsgArgAnnounce"));
        QStatus status = ER_FAIL;
        if (jaboutDataListenerRef != NULL && MID_getAnnouncedAboutData != NULL) {
            QCC_DbgPrintf(("AboutObj_announce obtained jo local ref of jaboutDataListener"));
            /*
             * JScopedEnv will automagically attach the JVM to the current native
             * thread.
             */
            JScopedEnv env;

            JLocalRef<jobject> jannounceArg = CallObjectMethod(env, jaboutDataListenerRef, MID_getAnnouncedAboutData);
            QCC_DbgPrintf(("AboutObj_announce Made Java Method call getAnnouncedAboutData"));
            // check for ErrorReplyBusException exception
            status = CheckForThrownException(env);
            if (ER_OK == status) {
                if (!Marshal("a{sv}", jannounceArg, msgArg)) {
                    QCC_LogError(ER_FAIL, ("JAboutData(): GetMsgArgAnnounce() marshaling error"));
                    return ER_FAIL;
                }
            } else {
                QCC_DbgPrintf(("JAboutObj::GetAnnouncedAboutData exception with status %s", QCC_StatusText(status)));
                return status;
            }
        }
        return status;
    }

    /**
     * This will check if the last method call threw an exception Since we are
     * looking for ErrorReplyBusExceptions we know that the exception thrown
     * correlates to a QStatus that we are trying to get.  If ER_FAIL is returned
     * then we had an issue resolving the java method calls.
     *
     * @return QStatus indicating the status that was thrown from the ErrReplyBusException
     */
    QStatus CheckForThrownException(JScopedEnv& env) {
        JLocalRef<jthrowable> ex = env->ExceptionOccurred();
        if (ex) {
            env->ExceptionClear();
            JLocalRef<jclass> clazz = env->GetObjectClass(ex);
            if (env->IsInstanceOf(ex, CLS_ErrorReplyBusException) && clazz != NULL) {
                jmethodID mid = env->GetMethodID(clazz, "getErrorStatus", "()Lorg/alljoyn/bus/Status;");
                if (!mid) {
                    return ER_FAIL;
                }
                JLocalRef<jobject> jstatus = CallObjectMethod(env, ex, mid);
                if (env->ExceptionCheck()) {
                    return ER_FAIL;
                }
                JLocalRef<jclass> statusClazz = env->GetObjectClass(jstatus);
                mid = env->GetMethodID(statusClazz, "getErrorCode", "()I");
                if (!mid) {
                    return ER_FAIL;
                }
                QStatus errorCode = (QStatus)env->CallIntMethod(jstatus, mid);
                if (env->ExceptionCheck()) {
                    return ER_FAIL;
                }
                return errorCode;
            }
            return ER_FAIL;
        }
        return ER_OK;
    }
    JBusAttachment* busPtr;
    jmethodID MID_getAboutData;
    jmethodID MID_getAnnouncedAboutData;
    jobject jaboutDataListenerRef;
    jobject jaboutObjGlobalRef;
};

#endif