javacommons/utils/javasrc/com/nokia/mj/impl/rt/SystemPropertyUtils.java
branchRCL_3
changeset 19 04becd199f91
child 64 0ea12c182930
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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;
       
    20 
       
    21 import java.util.Hashtable;
       
    22 import com.nokia.mj.impl.rt.support.SystemPropertyProvider;
       
    23 import com.nokia.mj.impl.utils.Logger;
       
    24 
       
    25 /**
       
    26  * A class that will be used to store system properties in CLDC VM.
       
    27  * The class is inherited from Hashtable and there will be only one instance
       
    28  * of it per VM instance. The VM will replace the original Hashtable with
       
    29  * instace of this object which gives us the control when MIDlet is asking
       
    30  * for any system property. Original system properties are in the private
       
    31  * member and the parent hash table is used to store dynamic handlers.
       
    32  *
       
    33  * @author Nokia Corporation
       
    34  * @version 1.0
       
    35  */
       
    36 
       
    37 class SystemPropertyUtils
       
    38 {
       
    39 
       
    40     private static final String
       
    41     PROPERTY_HANDLER_CLASS_PREFIX = "com.nokia.mj.impl.properties.";
       
    42 
       
    43     /**
       
    44      * Solves the possible dynamic property value. This a utility method
       
    45      * to be called by VM dependent sytem properties extension mechanism.
       
    46      *
       
    47      * The utility will check if the value of the system property indicates
       
    48      * that the method is dynamic. if it is not, then the value is returned
       
    49      * directly, but if it is, then the value of the dynamic property will
       
    50      * be solved by using defined property handler.
       
    51      *
       
    52      * @param propertyName the name of the system property.
       
    53      * @param propertyValue the value of the system property. This must be
       
    54      *                      passed as an argument even if the original
       
    55      *                      Hashtable is also passed as third argument.
       
    56      *                      That can't be used because calling the get method
       
    57      *                      of the given Hashtable would lead to forever loop.
       
    58      * @param props A hashtable containing the system properties. This is used
       
    59      *              to store the "freezed" system property value.
       
    60      * @return value of the system property.
       
    61      */
       
    62     public static Object solvePropertyValue(Object propertyName,
       
    63                                             Object propertyValue,
       
    64                                             Hashtable systemProperties)
       
    65     {
       
    66         // Check that the property name and the obtained value are strings
       
    67         if (!(propertyName instanceof String) ||
       
    68                 !(propertyValue instanceof String))
       
    69         {
       
    70             // Not strings, return the non-string property value.
       
    71             return propertyValue;
       
    72         }
       
    73 
       
    74         String propertyNameStr = (String)propertyName;
       
    75         String propertyValueStr = (String)propertyValue;
       
    76 
       
    77         try
       
    78         {
       
    79             // If the property value starts with ':' it is a dynamic property
       
    80             if (propertyValueStr != null && propertyValueStr.length() > 0 &&
       
    81                     propertyValueStr.charAt(0) == ':')
       
    82             {
       
    83                 // Note for CDC. If some application wants to define some system
       
    84                 // property value that starts with ':' then it is needed to
       
    85                 // encode e.g. by using leading ':'.
       
    86                 if (propertyValueStr.length() > 1 &&
       
    87                         propertyValueStr.charAt(1) == ':')
       
    88                 {
       
    89                     return propertyValueStr.substring(1);
       
    90                 }
       
    91 
       
    92                 // Strip the dynamic property prefix away.
       
    93                 final String classEnding = propertyValueStr.substring(1);
       
    94                 // The result will be the end of the package and of
       
    95                 // the class name.
       
    96                 String className = PROPERTY_HANDLER_CLASS_PREFIX +
       
    97                                    classEnding;
       
    98 
       
    99                 try
       
   100                 {
       
   101                     Class clazz = clazz = Class.forName(className);
       
   102                     Object providerImpl = clazz.newInstance();
       
   103 
       
   104                     boolean isFrozen = false;
       
   105                     SystemPropertyProvider systemPropertyProvider =
       
   106                         (SystemPropertyProvider)providerImpl;
       
   107 
       
   108                     propertyValueStr =
       
   109                         systemPropertyProvider.getProperty(propertyNameStr);
       
   110 
       
   111                     isFrozen =
       
   112                         systemPropertyProvider.isStatic(propertyNameStr);
       
   113 
       
   114                     // Freeze the value if the provider wants so
       
   115                     if (propertyValueStr != null && isFrozen)
       
   116                     {
       
   117                         systemProperties.put(propertyNameStr,
       
   118                                              propertyValueStr);
       
   119                     }
       
   120                     return propertyValueStr;
       
   121                 }
       
   122                 catch (ClassNotFoundException e)
       
   123                 {
       
   124                     Logger.ELOG(Logger.EUtils, "Dynamic property handler " +
       
   125                                 className + " was not found for "
       
   126                                 + propertyName);
       
   127                     return "";
       
   128                 }
       
   129 
       
   130             }
       
   131 
       
   132             // It wasn't dynamic, so returning the static value.
       
   133             return propertyValueStr;
       
   134         }
       
   135         catch (Throwable t)
       
   136         {
       
   137             Logger.ELOG(Logger.EUtils, "Error in system properties: Key="
       
   138                         + propertyNameStr + ", Val ='" +
       
   139                         propertyValueStr, t);
       
   140             return "";
       
   141         }
       
   142     }
       
   143 }
       
   144