javaruntimes/starterutils/src.s60/bootclasspath.cpp
branchRCL_3
changeset 19 04becd199f91
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:  Functionality for reading extended bootclasspath in Symbian env
       
    15 *
       
    16 */
       
    17 
       
    18 #include <centralrepository.h>
       
    19 
       
    20 #include "javaruntimeprivatecrkeys.h"
       
    21 #include "logger.h"
       
    22 #include "bootclasspath.h"
       
    23 
       
    24 
       
    25 HBufC* GetCRepClasspathL()
       
    26 {
       
    27     _LIT(KAllowedPathPrefix1, "c:\\resource\\");
       
    28     _LIT(KAllowedPathPrefix2, "c:/resource/");
       
    29     const TChar KClasspathSeparator(';');
       
    30 
       
    31     JELOG2(EJavaRuntime);
       
    32     CRepository* repository = CRepository::NewL(KCRUidJavaRuntime);
       
    33     CleanupStack::PushL(repository);
       
    34 
       
    35     HBufC* path = HBufC::NewL(NCentralRepositoryConstants::KMaxUnicodeStringLength);
       
    36     TPtr ptr = path->Des();
       
    37     TInt err = repository->Get(KJavaRuntimeMIDPClasspath, ptr);
       
    38     CleanupStack::PopAndDestroy(repository);
       
    39 
       
    40     if ((err == KErrNotFound) || (ptr.Length() == 0))
       
    41     {
       
    42         LOG(EJavaRuntime, EInfo, "No KJavaRuntimeMIDPClasspath key in Cen Rep");
       
    43         delete path;
       
    44         return 0;
       
    45     }
       
    46     else if (err != KErrNone)
       
    47     {
       
    48         ELOG1(EJavaRuntime, "Cenral Repository error %d", err);
       
    49         delete path;
       
    50         return 0;
       
    51     }
       
    52 
       
    53     // Only extensions installed to C:/resource/ are accepted.
       
    54     // If KJavaRuntimeMIDPClasspath contains extensions installed
       
    55     // to some other (unsafe) place, remove them from classpath
       
    56     // and store the pruned classpath back to central repository
       
    57     // The classpath read from central repository may be for example
       
    58     // "C:/resource/java/lib/eswt.jar;;E:/Resource/java/lib/payment.jar
       
    59     // ;c:\\resource\\java\\lib\\mia.jar;  "
       
    60 
       
    61     TInt  cpStart(0);              // start position of class path in class paths
       
    62     TInt  cpLength(0);             // length of of class path in class paths
       
    63     TBool updateCR(EFalse);        // ETrue if cen rep must be updated
       
    64 
       
    65     // Go through all class paths
       
    66     while (cpStart < ptr.Length() && cpLength < ptr.Length())
       
    67     {
       
    68         // Length of the class path, class paths are separated ';'
       
    69         cpLength = ptr.MidTPtr(cpStart).LocateF(KClasspathSeparator);
       
    70         if (cpLength == KErrNotFound)
       
    71         {
       
    72             // The class path is last one without ';', cpLength is rest from start position to total length
       
    73             cpLength = ptr.Length() - cpStart;
       
    74         }
       
    75         else
       
    76         {
       
    77             // Include ';'
       
    78             cpLength++;
       
    79         }
       
    80 
       
    81         // Process class path
       
    82         TPtr tmpPtr = ptr.MidTPtr(cpStart, cpLength);
       
    83 
       
    84         if ((tmpPtr.Left(((TDesC)KAllowedPathPrefix1).Length()).CompareF(KAllowedPathPrefix1)) &&
       
    85                 (tmpPtr.Left(((TDesC)KAllowedPathPrefix2).Length()).CompareF(KAllowedPathPrefix2)))
       
    86         {
       
    87             // Unsafe class path, delete it
       
    88             ptr.Delete(cpStart, cpLength);
       
    89             // Start of the next class path is start of the current deleted class path
       
    90             cpLength = 0;
       
    91             updateCR = ETrue;
       
    92         }
       
    93         else
       
    94         {
       
    95             // Allowed class path
       
    96             // Start of the next class path is end of the current class path
       
    97             cpStart = cpStart + cpLength;
       
    98         }
       
    99     }
       
   100 
       
   101     if (!updateCR)
       
   102     {
       
   103         // Classpath does not contain any dangerous paths
       
   104         return path;
       
   105     }
       
   106 
       
   107     // Store the cleaned classpath back to central repository (to optimize
       
   108     // next execution)
       
   109     WLOG(EJavaRuntime,
       
   110          "GetCRepClasspathL: Removing unsafe extension(s) from Cen Rep extension classpath");
       
   111     CleanupStack::PushL(path);
       
   112     repository = CRepository::NewL(KCRUidJavaRuntime);
       
   113     err = repository->Set(KJavaRuntimeMIDPClasspath, ptr);
       
   114     delete repository;
       
   115     if (err != KErrNone)
       
   116     {
       
   117         WLOG1(EJavaRuntime,
       
   118               "GetCRepClasspathL could not store cleaned classpath, err %d", err);
       
   119     }
       
   120 
       
   121     if (ptr.Length() > 0)
       
   122     {
       
   123         CleanupStack::Pop(path);
       
   124         return path;
       
   125     }
       
   126     else
       
   127     {
       
   128         WLOG(EJavaRuntime, "Cleaned classpath is empty");
       
   129         // nothing to return
       
   130         CleanupStack::PopAndDestroy(path);
       
   131         return 0;
       
   132     }
       
   133 }
       
   134 
       
   135 void java::runtime::getExtendedBootClassPath(std::wstring& bootClassPath)
       
   136 {
       
   137     JELOG2(EJavaRuntime);
       
   138     HBufC* path = 0;
       
   139     TRAP_IGNORE(path = GetCRepClasspathL());
       
   140     if (path != 0)
       
   141     {
       
   142         LOG1(EJavaRuntime, EInfo, "  Length of the bcp %d", path->Length());
       
   143         if (path->Length() > 0)
       
   144         {
       
   145             bootClassPath.assign((wchar_t*)path->Ptr(),
       
   146                                  path->Length());
       
   147             LOG1(EJavaRuntime, EInfo, "  bcp '%S'", bootClassPath.c_str());
       
   148         }
       
   149         delete path;
       
   150     }
       
   151 }