javacommons/javastorage/javasrc/com/nokia/mj/impl/storage/StorageSessionImpl.java
branchRCL_3
changeset 19 04becd199f91
child 64 0ea12c182930
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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.storage;
       
    20 
       
    21 import com.nokia.mj.impl.utils.Uid;
       
    22 import com.nokia.mj.impl.utils.Logger;
       
    23 
       
    24 /**
       
    25  * StorageSessionImpl implements Java Storage functionality.
       
    26  */
       
    27 final class StorageSessionImpl implements StorageSession
       
    28 {
       
    29     static final int OPEN_CONN = 1;
       
    30     static final int CLOSE_CONN = 2;
       
    31     static final int START_TRANSACTION = 3;
       
    32     static final int COMMIT = 4;
       
    33     static final int ROLLBACK = 5;
       
    34     static final int READ = 6;
       
    35     static final int WRITE = 7;
       
    36     static final int SEARCH = 8;
       
    37     static final int REMOVE = 9;
       
    38     static final int CREATE_TABLE = 10;
       
    39     static final int APPEND_TABLE = 11;
       
    40     static final int UPDATE = 12;
       
    41     static final int DESTROY_SESSION = 13;
       
    42 
       
    43     private MessageDispatcher iMsgDispatcher = null;
       
    44     private String iSessionID = "";
       
    45 
       
    46     private boolean iConnectionOpen;
       
    47     private boolean iTransactionOpen;
       
    48 
       
    49     /**
       
    50      * Constructor.
       
    51      *
       
    52      * @throws StorageException if connection initiation fails.
       
    53      */
       
    54     StorageSessionImpl()
       
    55     {
       
    56         iMsgDispatcher = null;
       
    57         iConnectionOpen = false;
       
    58         iTransactionOpen = false;
       
    59     }
       
    60 
       
    61     /*** ----------------------------- PUBLIC ----------------------------- */
       
    62 
       
    63     public void open() throws StorageException
       
    64     {
       
    65         open(StorageNames.JAVA_DATABASE_NAME);
       
    66     }
       
    67 
       
    68     public void open(String aStorageName) throws StorageException
       
    69     {
       
    70         if (iMsgDispatcher == null)
       
    71         {
       
    72             iMsgDispatcher = new MessageDispatcher();
       
    73         }
       
    74 
       
    75         ensureNonEmpty(aStorageName);
       
    76         assertState(iConnectionOpen, false);
       
    77         iSessionID = iMsgDispatcher.createAndSendMessage(OPEN_CONN,
       
    78                      "",
       
    79                      aStorageName);
       
    80         iConnectionOpen = true;
       
    81 
       
    82         Logger.LOG(Logger.EJavaStorage, Logger.EInfoHeavyLoad,
       
    83                    "SessionID: " + iSessionID);
       
    84     }
       
    85 
       
    86     public void close() throws StorageException
       
    87     {
       
    88         assertState(iConnectionOpen, true);
       
    89         iMsgDispatcher.createAndSendMessage(CLOSE_CONN, iSessionID, "");
       
    90 
       
    91         iSessionID = "";
       
    92         iConnectionOpen = false;
       
    93         // Rollback is done if connection is closed while active transaction.
       
    94         iTransactionOpen = false;
       
    95     }
       
    96 
       
    97     public void destroySession()
       
    98     {
       
    99         if (iMsgDispatcher != null)
       
   100         {
       
   101             try
       
   102             {
       
   103                 iMsgDispatcher.createAndSendMessage(
       
   104                     DESTROY_SESSION, "", "");
       
   105             }
       
   106             catch (StorageException se)
       
   107             {
       
   108                 Logger.ELOG(Logger.EJavaStorage,
       
   109                             "DestroySession failed: " + se.toString());
       
   110             }
       
   111         }
       
   112 
       
   113         iConnectionOpen = false;
       
   114         iTransactionOpen = false;
       
   115     }
       
   116 
       
   117     public void startTransaction() throws StorageException
       
   118     {
       
   119         assertState(iConnectionOpen, true);
       
   120         assertState(iTransactionOpen, false);
       
   121 
       
   122         iMsgDispatcher.createAndSendMessage(START_TRANSACTION, iSessionID, "");
       
   123         iTransactionOpen = true;
       
   124     }
       
   125 
       
   126     public void commitTransaction() throws StorageException
       
   127     {
       
   128         assertState(iConnectionOpen, true);
       
   129         assertState(iTransactionOpen, true);
       
   130 
       
   131         iMsgDispatcher.createAndSendMessage(COMMIT, iSessionID, "");
       
   132         iTransactionOpen = false;
       
   133     }
       
   134 
       
   135     public void rollbackTransaction() throws StorageException
       
   136     {
       
   137         assertState(iConnectionOpen, true);
       
   138         assertState(iTransactionOpen, true);
       
   139 
       
   140         iMsgDispatcher.createAndSendMessage(ROLLBACK, iSessionID, "");
       
   141         iTransactionOpen = false;
       
   142     }
       
   143 
       
   144     public StorageEntry readEntry(String aTablename, Uid aUid)
       
   145     {
       
   146         StorageEntry[] entries = readEntries(aTablename, aUid);
       
   147 
       
   148         if (entries != null && entries.length > 0)
       
   149         {
       
   150             return entries[0];
       
   151         }
       
   152         else
       
   153         {
       
   154             return null;
       
   155         }
       
   156     }
       
   157 
       
   158     public StorageEntry[] readEntries(String aTablename, Uid aUid)
       
   159     {
       
   160         assertState(iConnectionOpen, true);
       
   161         ensureNonEmpty(aUid);
       
   162 
       
   163         StorageEntry entry = new StorageEntry();
       
   164         entry.addAttribute(new StorageAttribute(StorageNames.ID,
       
   165                                                 aUid.getStringValue()));
       
   166 
       
   167         return search(aTablename, entry);
       
   168     }
       
   169 
       
   170     public StorageEntry[] search(String aTablename, StorageEntry aSearchQuery)
       
   171     {
       
   172         assertState(iConnectionOpen, true);
       
   173 
       
   174         return iMsgDispatcher.createAndSendMessage(SEARCH,
       
   175                 iSessionID,
       
   176                 aTablename,
       
   177                 aSearchQuery);
       
   178     }
       
   179 
       
   180     public void write(String aTablename, StorageEntry aEntry)
       
   181     throws StorageException
       
   182     {
       
   183         assertState(iConnectionOpen, true);
       
   184 
       
   185         iMsgDispatcher.createAndSendMessage(WRITE,
       
   186                                             iSessionID,
       
   187                                             aTablename,
       
   188                                             aEntry);
       
   189     }
       
   190 
       
   191     public void update(String aTablename,
       
   192                        StorageEntry aUpdate,
       
   193                        StorageEntry aMatch)
       
   194     {
       
   195         assertState(iConnectionOpen, true);
       
   196 
       
   197         iMsgDispatcher.handleUpdate(iSessionID,
       
   198                                     aTablename,
       
   199                                     aUpdate,
       
   200                                     aMatch);
       
   201     }
       
   202 
       
   203     public int remove(String aTablename, Uid aUid) throws StorageException
       
   204     {
       
   205         ensureNonEmpty(aUid);
       
   206 
       
   207         StorageAttribute attribute = new StorageAttribute(
       
   208             StorageNames.ID, aUid.getStringValue());
       
   209         StorageEntry entry = new StorageEntry();
       
   210         entry.addAttribute(attribute);
       
   211 
       
   212         return remove(aTablename, entry);
       
   213     }
       
   214 
       
   215     public int remove(String aTablename, StorageEntry aEntry)
       
   216     throws StorageException
       
   217     {
       
   218         assertState(iConnectionOpen, true);
       
   219         StorageEntry[] entries = iMsgDispatcher.createAndSendMessage(REMOVE,
       
   220                                  iSessionID,
       
   221                                  aTablename,
       
   222                                  aEntry);
       
   223         if (entries != null)
       
   224         {
       
   225             return entries.length;
       
   226         }
       
   227         else
       
   228         {
       
   229             return -1;
       
   230         }
       
   231     }
       
   232 
       
   233     public void createTable(String aTablename,
       
   234                             StorageEntry aTableColumns,
       
   235                             StorageAttribute aPrimaryKey)
       
   236     {
       
   237         assertState(iConnectionOpen, true);
       
   238 
       
   239         iMsgDispatcher.handleCreateTable(iSessionID,
       
   240                                          aTablename,
       
   241                                          aTableColumns,
       
   242                                          aPrimaryKey);
       
   243     }
       
   244 
       
   245     public void appendTable(String aTablename, StorageEntry aTableColumns)
       
   246     {
       
   247         assertState(iConnectionOpen, true);
       
   248 
       
   249         iMsgDispatcher.createAndSendMessage(APPEND_TABLE,
       
   250                                             iSessionID,
       
   251                                             aTablename,
       
   252                                             aTableColumns);
       
   253     }
       
   254 
       
   255     /*** ---------------------------- PROTECTED --------------------------- */
       
   256     /*** ----------------------------- PACKAGE ---------------------------- */
       
   257     /*** ----------------------------- PRIVATE ---------------------------- */
       
   258 
       
   259     /**
       
   260      * Assert state.
       
   261      *
       
   262      * @param aState to be checked.
       
   263      * @param aExpectedState to be matched.
       
   264      * @throws StorageException if states do not match.
       
   265      */
       
   266     private void assertState(boolean aState, boolean aExpectedState)
       
   267     throws StorageException
       
   268     {
       
   269         if (aState != aExpectedState)
       
   270         {
       
   271             Logger.ELOG(Logger.EJavaStorage, "Wrong state: " + aState);
       
   272             throw new StorageException(
       
   273                 "Connection or transaction at wrong state: " + aState);
       
   274         }
       
   275     }
       
   276 
       
   277     /**
       
   278      * Ensure value is not null and empty String.
       
   279      *
       
   280      * @param aValue to be ensured.
       
   281      * @throws StorageException if value is null or empty.
       
   282      */
       
   283     private void ensureNonEmpty(String aValue) throws StorageException
       
   284     {
       
   285         if (aValue == null || aValue.equals(""))
       
   286         {
       
   287             Logger.ELOG(Logger.EJavaStorage, "Invalid argument");
       
   288             throw new StorageException("Invalid argument");
       
   289         }
       
   290     }
       
   291 
       
   292     /**
       
   293      * Ensure Uid is not null and empty String.
       
   294      *
       
   295      * @param Uid to be ensured.
       
   296      * @throws StorageException if value is null or empty.
       
   297      */
       
   298     private void ensureNonEmpty(Uid aUid) throws StorageException
       
   299     {
       
   300         if (aUid == null || aUid.equals(""))
       
   301         {
       
   302             Logger.ELOG(Logger.EJavaStorage, "Invalid argument");
       
   303             throw new StorageException("Invalid argument");
       
   304         }
       
   305     }
       
   306 
       
   307     /*** ----------------------------- NATIVE ----------------------------- */
       
   308 }