javaruntimes/midp/runtime/javasrc/com/nokia/mj/impl/rt/midp/StorageAccessor.java
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 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.rt.midp;
       
    20 
       
    21 import java.util.Enumeration;
       
    22 
       
    23 import com.nokia.mj.impl.storage.StorageAttribute;
       
    24 import com.nokia.mj.impl.storage.StorageEntry;
       
    25 import com.nokia.mj.impl.storage.StorageFactory;
       
    26 import com.nokia.mj.impl.storage.StorageNames;
       
    27 import com.nokia.mj.impl.storage.StorageSession;
       
    28 import com.nokia.mj.impl.utils.Uid;
       
    29 
       
    30 /**
       
    31  * A utility class for reading the MIDlet specfic data from storage. At the
       
    32  * moment it is known that setMidletStartArguments() is called first and
       
    33  * rigth after that setMidletAttributes() is called. Therefore the session
       
    34  * created in setMidletStartArguments() is reused in setMidletAttributes()
       
    35  * method and then closed. If this scenario changes, then the close must
       
    36  * be done also in setMidletStartArguments() method.
       
    37  *
       
    38  * @author Nokia Corporation
       
    39  * @version $Rev$
       
    40  */
       
    41 final class StorageAccessor
       
    42 {
       
    43     /**
       
    44      * Session to storage in order to re-use.
       
    45      */
       
    46     private static StorageSession mSession;
       
    47 
       
    48     /*** ----------------------------- PACKAGE ---------------------------- */
       
    49 
       
    50     /**
       
    51      * Sets the MidletInfo to contain a set of static data related to the
       
    52      * MIDlet.
       
    53      * @param midletInfo where to store the data.
       
    54      */
       
    55     static void setMidletStartArguments(MidletInfo midletInfo)
       
    56     {
       
    57         mSession = StorageFactory.createSession();
       
    58         mSession.open();
       
    59 
       
    60         // Reading the MIDlet specific attributes from APPLICATION_TABLE.
       
    61         // MIDlet UID is a key.
       
    62         StorageEntry midletEntry =
       
    63             mSession.readEntry(StorageNames.APPLICATION_TABLE,
       
    64                                midletInfo.getUid());
       
    65         // Set the main class of the MIDlet.
       
    66         midletInfo.setMainClass(
       
    67             midletEntry.getAttribute(StorageNames.MAIN_CLASS).getValue());
       
    68 
       
    69         // Set the name of the MIDlet.
       
    70         midletInfo.setName(
       
    71             midletEntry.getAttribute(StorageNames.NAME).getValue());
       
    72 
       
    73         // Set the UID of the MIDlet suite.
       
    74         midletInfo.setSuiteUid(Uid.createUid(
       
    75                                    midletEntry.getAttribute(StorageNames.PACKAGE_ID).getValue()));
       
    76 
       
    77         // Reading the MIDlet suite specific attributes from
       
    78         // APPLICATION_PACKAGE_TABLE. MIDlet suite UID is a key.
       
    79         StorageEntry midletSuiteEntry =
       
    80             mSession.readEntry(StorageNames.APPLICATION_PACKAGE_TABLE,
       
    81                                midletInfo.getSuiteUid());
       
    82 
       
    83         // Set the name of the MIDlet suite.
       
    84         midletInfo.setSuiteName(
       
    85             midletSuiteEntry.getAttribute(StorageNames.PACKAGE_NAME).
       
    86             getValue());
       
    87         // Set the vendor of the MIDlet suite.
       
    88         midletInfo.setVendor(
       
    89             midletSuiteEntry.getAttribute(StorageNames.VENDOR).getValue());
       
    90 
       
    91         // Set the version of the MIDlet suite.
       
    92         midletInfo.setVersion(
       
    93             midletSuiteEntry.getAttribute(StorageNames.VERSION).getValue());
       
    94 
       
    95         // Set the root path of the MIDlet suite.
       
    96         midletInfo.setRootPath(
       
    97             midletSuiteEntry.getAttribute(StorageNames.ROOT_PATH).getValue());
       
    98 
       
    99         // Set the classpath of the MIDlet suite.
       
   100         midletInfo.setClassPath(
       
   101             midletSuiteEntry.getAttribute(StorageNames.JAR_PATH).getValue());
       
   102 
       
   103         // Set the drm info.
       
   104         midletInfo.setDrm(
       
   105             midletSuiteEntry.getAttribute(StorageNames.CONTENT_INFO).
       
   106             getValue().equals("1"));
       
   107 
       
   108         StorageAttribute contentId =
       
   109             midletSuiteEntry.getAttribute(StorageNames.CONTENT_ID);
       
   110         if (contentId != null)
       
   111         {
       
   112             midletInfo.setContentId(contentId.getValue());
       
   113         }
       
   114 
       
   115         // Reading the MIDlet suite specific attributes from
       
   116         // MIDP_PACKAGE_TABLE. MIDlet suite UID is a key.
       
   117         StorageEntry midletSuiteSpecificEntry =
       
   118             mSession.readEntry(StorageNames.MIDP_PACKAGE_TABLE,
       
   119                                midletInfo.getSuiteUid());
       
   120         midletInfo.setProtectionDomain(
       
   121             midletSuiteSpecificEntry.getAttribute(
       
   122                 StorageNames.SECURITY_DOMAIN_CATEGORY).
       
   123             getValue());
       
   124         // Set the hash of the MIDlet suite if available.
       
   125         StorageAttribute hash =
       
   126             midletSuiteSpecificEntry.getAttribute(StorageNames.HASH);
       
   127         if (hash != null)
       
   128         {
       
   129             midletInfo.setMidletHash(hash.getValue());
       
   130         }
       
   131 
       
   132         // Set the securoty domain name of the MIDlet suite.
       
   133         midletInfo.setProtectionDomainName(
       
   134             midletSuiteSpecificEntry.getAttribute(
       
   135                 StorageNames.SECURITY_DOMAIN).getValue());
       
   136 
       
   137         // Set the hash of the root if available.
       
   138         StorageAttribute rootHash =
       
   139             midletSuiteSpecificEntry.getAttribute(StorageNames.CERT_HASH);
       
   140         if (rootHash != null)
       
   141         {
       
   142             midletInfo.setRootHash(rootHash.getValue());
       
   143         }
       
   144 
       
   145         // Session is intentionally left open in a success case since at the
       
   146         // moment setMidletAttributes() is called before starting the MIDlet,
       
   147         // so already created session can be re-used.
       
   148     }
       
   149 
       
   150     /**
       
   151      * Sets the MidletInfo to contain MIDlet attributes stored from manifest
       
   152      * and jad file (if exists) during installation.
       
   153      * @param midletInfo where to store the attributes.
       
   154      */
       
   155     static void setMidletAttributes(MidletInfo midletInfo)
       
   156     {
       
   157         try
       
   158         {
       
   159             // Re-use the session if available.
       
   160             if (mSession == null)
       
   161             {
       
   162                 mSession = StorageFactory.createSession();
       
   163                 mSession.open();
       
   164             }
       
   165 
       
   166             // Reading the MIDlet suite attributes from
       
   167             // APPLICATION_PACKAGE_ATTRIBUTES_TABLE. MIDlet suite UID is a key.
       
   168             StorageEntry[] attributeEntries =
       
   169                 mSession.readEntries(
       
   170                     StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE,
       
   171                     midletInfo.getSuiteUid());
       
   172 
       
   173             if (attributeEntries != null)
       
   174             {
       
   175                 int midletN = 1;
       
   176 
       
   177                 // Loop through all the arguments and store them.
       
   178                 for (int i = 0; i<attributeEntries.length; ++i)
       
   179                 {
       
   180                     StorageAttribute name =
       
   181                         attributeEntries[i].getAttribute(StorageNames.NAME);
       
   182                     StorageAttribute value =
       
   183                         attributeEntries[i].getAttribute(StorageNames.VALUE);
       
   184                     midletInfo.addAttribute(name.getValue(), value.getValue());
       
   185 
       
   186                     // Determine what is the <n> of this MIDlet,
       
   187                     // (which attribute "Nokia-MIDlet-Localized-<n>" contains the localized
       
   188                     // name of this MIDlet)
       
   189                     if ((value.getValue().indexOf(midletInfo.getName()) > -1) &&
       
   190                             name.getValue().startsWith("MIDlet-"))
       
   191                     {
       
   192                         try
       
   193                         {
       
   194                             // Try to parse the rest of attribute name (after "MIDlet-")
       
   195                             // into an int. If this throws exception, the attribute was
       
   196                             // propably 'MIDlet-Name'
       
   197                             midletN = Integer.parseInt(name.getValue().substring(7));
       
   198                         }
       
   199                         catch (NumberFormatException ne) {};
       
   200                     }
       
   201                 }
       
   202 
       
   203                 // Set the localized name of the MIDlet.
       
   204                 String localizedName = midletInfo.getAttribute("Nokia-MIDlet-Localized-" + midletN);
       
   205                 if (localizedName == null)
       
   206                 {
       
   207                     localizedName = midletInfo.getName();
       
   208                 }
       
   209 
       
   210                 midletInfo.setLocalizedName(localizedName);
       
   211             }
       
   212         }
       
   213         finally
       
   214         {
       
   215             // Closing the session.
       
   216             if (mSession != null)
       
   217             {
       
   218                 mSession.close();
       
   219                 mSession.destroySession();
       
   220                 mSession = null;
       
   221             }
       
   222         }
       
   223     }
       
   224 }