javacommons/javastorage/src.linux/client/javadataaccessimpl.cpp
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     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:  JavaDataAccessImpl
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "commsclientendpoint.h"
       
    20 #include "commsmessage.h"
       
    21 #include "javacommonutils.h"
       
    22 #include "javadataaccessimpl.h"
       
    23 #include "javastorageexception.h"
       
    24 #include "javastoragemessage.h"
       
    25 #include "logger.h"
       
    26 
       
    27 using namespace java::comms;
       
    28 using namespace java::storage;
       
    29 using namespace java::util;
       
    30 using namespace std;
       
    31 
       
    32 JavaDataAccessImpl::JavaDataAccessImpl() throw(JavaStorageException)
       
    33         : mConnOpen(false), mCachedSessionID("")
       
    34 {
       
    35     JELOG2(EJavaStorage);
       
    36     mConnOpen = false;
       
    37     mCliConn.reset(new CommsClientEndpoint());
       
    38     int resultCode = mCliConn->connect(IPC_ADDRESS_JAVA_STORAGE_C);
       
    39 
       
    40     if (resultCode != 0)
       
    41     {
       
    42         ELOG1(EJavaStorage, "Cannot connect via comms: %d", resultCode);
       
    43         throw JavaStorageException(resultCode, "Comms failure",
       
    44                                    __FILE__, __FUNCTION__, __LINE__);
       
    45     }
       
    46 }
       
    47 
       
    48 OS_EXPORT JavaDataAccessImpl::~JavaDataAccessImpl()
       
    49 {
       
    50     JELOG2(EJavaStorage);
       
    51 
       
    52     // If intance is deleted prior connection is closed do close which triggers
       
    53     // rollback on server side.
       
    54     if (mConnOpen)
       
    55     {
       
    56         string msgID = JavaCommonUtils::intToString(JavaStorageMessage::EClose);
       
    57         string msgIDSize = JavaCommonUtils::intToString(msgID.size());
       
    58         string requestHeaders = msgIDSize + msgID + mCachedSessionID;
       
    59         CommsMessage message;
       
    60         close(requestHeaders, message);
       
    61     }
       
    62 
       
    63     int resultCode = mCliConn->disconnect();
       
    64 
       
    65     if (resultCode != 0)
       
    66     {
       
    67         LOG1(EJavaStorage, EInfo,
       
    68              "Comms disconnect result code: %d", resultCode);
       
    69     }
       
    70 }
       
    71 
       
    72 OS_EXPORT void JavaDataAccessImpl::open(
       
    73     const std::string& aHeaders,
       
    74     const std::string& aStorageName,
       
    75     CommsMessage& aReceivedMessage) throw(JavaStorageException)
       
    76 {
       
    77     JELOG2(EJavaStorage);
       
    78 
       
    79     CommsMessage sendStorageMessage;
       
    80     sendStorageMessage.setMessageId(IPC_JS_CONN_REG);
       
    81 
       
    82     // Headers contain MsgIdentifier,SessionId
       
    83     sendStorageMessage<<aHeaders;
       
    84     sendStorageMessage<<aStorageName;
       
    85 
       
    86     sendReceiveMessage(sendStorageMessage, aReceivedMessage);
       
    87     // SessionID is cached for the case that client deleted storage instance
       
    88     // with open connection. SessionID is required to close open connection.
       
    89     int resultCode = 0;
       
    90     aReceivedMessage>>resultCode;
       
    91     if (resultCode >= 0)
       
    92     {
       
    93         aReceivedMessage>>mCachedSessionID;
       
    94         aReceivedMessage.begin();
       
    95         mConnOpen = true;
       
    96     }
       
    97 }
       
    98 
       
    99 OS_EXPORT void JavaDataAccessImpl::close(const std::string& aHeaders,
       
   100         CommsMessage& aReceivedMessage)
       
   101 throw(JavaStorageException)
       
   102 {
       
   103     JELOG2(EJavaStorage);
       
   104 
       
   105     CommsMessage sendStorageMessage;
       
   106     sendStorageMessage.setMessageId(IPC_JS_CONN_REG);
       
   107 
       
   108     // Headers contain MsgIdentifier,SessionId
       
   109     sendStorageMessage<<aHeaders;
       
   110     sendStorageMessage<<"";
       
   111 
       
   112     sendReceiveMessage(sendStorageMessage, aReceivedMessage);
       
   113     mConnOpen = false;
       
   114 }
       
   115 
       
   116 OS_EXPORT void JavaDataAccessImpl::execute(const std::string& aHeaders,
       
   117         const std::wstring& aSqlStatement,
       
   118         CommsMessage& aReceivedMessage)
       
   119 throw(JavaStorageException)
       
   120 {
       
   121     JELOG2(EJavaStorage);
       
   122 
       
   123     CommsMessage sendStorageMessage;
       
   124     sendStorageMessage.setMessageId(IPC_JS_CONN_REG);
       
   125 
       
   126     // Headers contain MsgIdentifier,SessionId
       
   127     sendStorageMessage<<aHeaders;
       
   128     // Data is pure sqlstatement
       
   129     sendStorageMessage<<aSqlStatement;
       
   130 
       
   131     sendReceiveMessage(sendStorageMessage, aReceivedMessage);
       
   132 }
       
   133 
       
   134 void JavaDataAccessImpl::sendReceiveMessage(CommsMessage& aSendMessage,
       
   135         CommsMessage& aReceivedMessage)
       
   136 throw(JavaStorageException)
       
   137 {
       
   138     JELOG2(EJavaStorage);
       
   139 
       
   140     int resultCode = -1;
       
   141 
       
   142     aSendMessage.setReceiver(IPC_ADDRESS_JAVA_STORAGE_C);
       
   143     aSendMessage.setModuleId(PLUGIN_ID_JAVASTORAGE_CLIENT_C);
       
   144 
       
   145     resultCode = mCliConn->sendReceive(aSendMessage, aReceivedMessage, 10);
       
   146 
       
   147     if (resultCode != 0)
       
   148     {
       
   149         ELOG1(EJavaStorage, "Comms result code: %d", resultCode);
       
   150 
       
   151         throw JavaStorageException(resultCode, "Comms failure",
       
   152                                    __FILE__, __FUNCTION__, __LINE__);
       
   153     }
       
   154 }