javacommons/javastorage/src.s60/client/storageconnectionnative.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  storageconnectionnative
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "com_nokia_mj_impl_storage_StorageConnection.h"
       
    20 #include "commsmessage.h"
       
    21 #include "javacommonutils.h"
       
    22 #include "javadataaccess.h"
       
    23 #include "javajniutils.h"
       
    24 #include "javastorageexception.h"
       
    25 #include "logger.h"
       
    26 
       
    27 using namespace java::comms;
       
    28 using namespace java::storage;
       
    29 using namespace java::util;
       
    30 
       
    31 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_storage_StorageConnection__1startSession
       
    32 (JNIEnv *, jclass)
       
    33 {
       
    34     JELOG2(EJavaStorage);
       
    35     JavaDataAccess* dataAccess = JavaDataAccess::createInstance();
       
    36     // Return handle to session. Utilize the fact that in Symbian
       
    37     // all pointer addresses are MOD 4 so the last 2 bits are 0
       
    38     // and can be shifted out. This way the returned handle is
       
    39     // always positive whereas Symbian error codes are always negative.
       
    40     return reinterpret_cast<TUint>(dataAccess)>>2;
       
    41 }
       
    42 
       
    43 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_storage_StorageConnection__1closeSession
       
    44 (JNIEnv *, jclass, jint aSessionHandle)
       
    45 {
       
    46     JELOG2(EJavaStorage);
       
    47     JavaDataAccess* dataAccess =
       
    48         reinterpret_cast<JavaDataAccess*>(aSessionHandle<<2);
       
    49 
       
    50     delete dataAccess;
       
    51     dataAccess = NULL;
       
    52 }
       
    53 
       
    54 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_storage_StorageConnection__1open
       
    55 (JNIEnv *aEnv, jobject, jint aSessionHandle, jstring aHeaders, jstring aStorageName)
       
    56 {
       
    57     JELOG2(EJavaStorage);
       
    58     JavaDataAccess* dataAccess =
       
    59         reinterpret_cast<JavaDataAccess*>(aSessionHandle<<2);
       
    60 
       
    61     CommsMessage receivedMessage;
       
    62     const char* tempHeaders = aEnv->GetStringUTFChars(aHeaders, 0);
       
    63     const char* tempStorageName = aEnv->GetStringUTFChars(aStorageName, 0);
       
    64     std::string headers(tempHeaders);
       
    65     std::string storageName(tempStorageName);
       
    66 
       
    67     aEnv->ReleaseStringUTFChars(aHeaders, tempHeaders);
       
    68     aEnv->ReleaseStringUTFChars(aStorageName, tempStorageName);
       
    69 
       
    70     try
       
    71     {
       
    72         dataAccess->open(headers, storageName, receivedMessage);
       
    73     }
       
    74     catch (JavaStorageException& jse)
       
    75     {
       
    76         JniUtils::throwNewException(
       
    77             aEnv,
       
    78             "com/nokia/mj/impl/storage/StorageException",
       
    79             jse.toString().c_str());
       
    80 
       
    81         // Return control to Java side. Otherwise JNI Error occur about
       
    82         // pending exception.
       
    83         return 0;
       
    84     }
       
    85 
       
    86     int statusCode = -1;
       
    87     receivedMessage>>statusCode;
       
    88 
       
    89     std::string responseBody = "";
       
    90     receivedMessage>>responseBody;
       
    91 
       
    92     if (statusCode < 0 || responseBody.length() == 0)
       
    93     {
       
    94         WLOG1(EJavaStorage, "Server error. ErrorCode: %d", statusCode);
       
    95 
       
    96         std::string statusStr = "Server error. ErrorCode: ";
       
    97         statusStr.append(JavaCommonUtils::intToString(statusCode));
       
    98 
       
    99         JniUtils::throwNewException(
       
   100             aEnv,
       
   101             "com/nokia/mj/impl/storage/StorageException",
       
   102             statusStr.c_str());
       
   103 
       
   104         // Return control to Java side. Otherwise JNI Error occur about
       
   105         // pending exception.
       
   106         return 0;
       
   107     }
       
   108 
       
   109     return aEnv->NewStringUTF(responseBody.c_str());
       
   110 }
       
   111 
       
   112 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_storage_StorageConnection__1close
       
   113 (JNIEnv *aEnv, jobject, jint aSessionHandle, jstring aHeaders)
       
   114 {
       
   115     JELOG2(EJavaStorage);
       
   116     JavaDataAccess* dataAccess =
       
   117         reinterpret_cast<JavaDataAccess*>(aSessionHandle<<2);
       
   118 
       
   119     CommsMessage receivedMessage;
       
   120     const char* tempHeaders = aEnv->GetStringUTFChars(aHeaders, 0);
       
   121     std::string headers(tempHeaders);
       
   122 
       
   123     aEnv->ReleaseStringUTFChars(aHeaders, tempHeaders);
       
   124 
       
   125     try
       
   126     {
       
   127         dataAccess->close(headers, receivedMessage);
       
   128     }
       
   129     catch (JavaStorageException& jse)
       
   130     {
       
   131         JniUtils::throwNewException(
       
   132             aEnv,
       
   133             "com/nokia/mj/impl/storage/StorageException",
       
   134             jse.toString().c_str());
       
   135 
       
   136         // Return control to Java side. Otherwise JNI Error occur about
       
   137         // pending exception.
       
   138         return;
       
   139     }
       
   140 
       
   141     int statusCode = -1;
       
   142     receivedMessage>>statusCode;
       
   143 
       
   144     std::string responseBody = "";
       
   145     receivedMessage>>responseBody;
       
   146 
       
   147     if (statusCode < 0 || responseBody.length() == 0)
       
   148     {
       
   149         WLOG1(EJavaStorage, "Server error. ErrorCode: %d", statusCode);
       
   150 
       
   151         std::string statusStr = "Server error. ErrorCode: ";
       
   152         statusStr.append(JavaCommonUtils::intToString(statusCode));
       
   153 
       
   154         JniUtils::throwNewException(
       
   155             aEnv,
       
   156             "com/nokia/mj/impl/storage/StorageException",
       
   157             statusStr.c_str());
       
   158 
       
   159         // Return control to Java side. Otherwise JNI Error occur about
       
   160         // pending exception.
       
   161         return;
       
   162     }
       
   163 }
       
   164 
       
   165 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_storage_StorageConnection__1execute
       
   166 (JNIEnv *aEnv, jobject, jint aSessionHandle, jstring aHeaders, jstring aSqlStatement)
       
   167 {
       
   168     JELOG2(EJavaStorage);
       
   169 
       
   170     JavaDataAccess* dataAccess =
       
   171         reinterpret_cast<JavaDataAccess*>(aSessionHandle<<2);
       
   172 
       
   173     CommsMessage receivedMessage;
       
   174     const char* tempHeaders = aEnv->GetStringUTFChars(aHeaders, 0);
       
   175     std::string headers(tempHeaders);
       
   176 
       
   177     aEnv->ReleaseStringUTFChars(aHeaders, tempHeaders);
       
   178 
       
   179     const jchar* sqlStmtTemp = aEnv->GetStringChars(aSqlStatement, 0);
       
   180 
       
   181     std::wstring sqlStatement(
       
   182         (wchar_t*)sqlStmtTemp, aEnv->GetStringLength(aSqlStatement));
       
   183 
       
   184     aEnv->ReleaseStringChars(aSqlStatement, sqlStmtTemp);
       
   185 
       
   186     // ########################################################################
       
   187     // LOG1WSTR(EJavaStorage, EInfo, "Java Sql: '%s'", sqlStatement);
       
   188     // ########################################################################
       
   189 
       
   190     try
       
   191     {
       
   192         dataAccess->execute(headers, sqlStatement, receivedMessage);
       
   193     }
       
   194     catch (JavaStorageException& jse)
       
   195     {
       
   196         ELOG(EJavaStorage, "Execute failed to storage exception.");
       
   197 
       
   198         JniUtils::throwNewException(
       
   199             aEnv,
       
   200             "com/nokia/mj/impl/storage/StorageException",
       
   201             jse.toString().c_str());
       
   202 
       
   203         // Return control to Java side. Otherwise JNI Error occur about
       
   204         // pending exception.
       
   205         return 0;
       
   206     }
       
   207     catch (...)
       
   208     {
       
   209         ELOG(EJavaStorage, "Execute failed to unexp exception.");
       
   210 
       
   211         JniUtils::throwNewException(
       
   212             aEnv,
       
   213             "com/nokia/mj/impl/storage/StorageException",
       
   214             "Internal error");
       
   215 
       
   216         // Return control to Java side. Otherwise JNI Error occur about
       
   217         // pending exception.
       
   218         return 0;
       
   219 
       
   220     }
       
   221 
       
   222     int statusCode = -1;
       
   223     receivedMessage>>statusCode;
       
   224 
       
   225     std::wstring responseBody = L"";
       
   226     receivedMessage>>responseBody;
       
   227 
       
   228     // ########################################################################
       
   229     // LOG1WSTR(EJavaStorage, EInfo, "Server response: '%s'", responseBody);
       
   230     // ########################################################################
       
   231 
       
   232     if (statusCode < 0 || responseBody.length() == 0)
       
   233     {
       
   234         WLOG1(EJavaStorage, "Server error. ErrorCode: %d", statusCode);
       
   235 
       
   236         std::string statusStr = "Server error. ErrorCode: ";
       
   237         statusStr.append(JavaCommonUtils::intToString(statusCode));
       
   238 
       
   239         JniUtils::throwNewException(
       
   240             aEnv,
       
   241             "com/nokia/mj/impl/storage/StorageException",
       
   242             statusStr.c_str());
       
   243 
       
   244         // Return control to Java side. Otherwise JNI Error occur about
       
   245         // pending exception.
       
   246         return 0;
       
   247     }
       
   248 
       
   249     return aEnv->NewString(
       
   250                (const jchar*)responseBody.c_str(), responseBody.length());
       
   251 }