javaruntimes/jvmargmodifier/file/src/jvmargsfilereader.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:  A default empty implementation for JvmArgs modifier.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <fstream>
       
    19 
       
    20 #include "logger.h"
       
    21 #include "javacommonutils.h"
       
    22 #include "exceptionbase.h"
       
    23 #include "driveutilities.h"
       
    24 
       
    25 #include "jvmargsfilereader.h"
       
    26 
       
    27 namespace java // codescanner::namespace
       
    28 {
       
    29 namespace runtime // codescanner::namespace
       
    30 {
       
    31 
       
    32 #ifdef __SYMBIAN32__
       
    33 const wchar_t JVM_ARGS_DIR[] = L"java\\";
       
    34 #else
       
    35 const wchar_t JVM_ARGS_DIR[] = L"java/";
       
    36 #endif
       
    37 
       
    38 const wchar_t* const IDENTIFIERS[] = {L"Midp", L"Installer", L"TCK_runner", 0};
       
    39 const wchar_t* const JVM_ARGS_FILENAMES[] = {L"midpargs.txt", L"installerargs.txt", L"tckrunnerargs.txt", 0};
       
    40 const wchar_t* const APP_ARGS_FILENAMES[] = {L"midpappargs.txt", L"installerappargs.txt", L"tckrunnerappargs.txt", 0};
       
    41 
       
    42 
       
    43 JvmArgsFileReader::JvmArgsFileReader(const std::wstring& aIdentifier)
       
    44         : mIdentifier(aIdentifier)
       
    45 {
       
    46 }
       
    47 
       
    48 JvmArgsFileReader::~JvmArgsFileReader()
       
    49 {
       
    50 }
       
    51 
       
    52 const std::list<std::wstring>& JvmArgsFileReader::getJvmArguments()
       
    53 {
       
    54     std::wstring filename;
       
    55     for (int i = 0; IDENTIFIERS[i] != 0; ++i) // codescanner::accessArrayElementWithoutCheck2
       
    56     {
       
    57         if (mIdentifier.compare(IDENTIFIERS[i]) == 0) // codescanner::accessArrayElementWithoutCheck2
       
    58         {
       
    59             filename = JVM_ARGS_FILENAMES[i];
       
    60             break;
       
    61         }
       
    62     }
       
    63 
       
    64     if (!filename.empty())
       
    65     {
       
    66         readArgs(filename , mJvmArgs);
       
    67     }
       
    68     else
       
    69     {
       
    70         WLOG2(EJavaRuntime, "%s: unknown identifier: %S", __PRETTY_FUNCTION__, mIdentifier.c_str());
       
    71     }
       
    72     return mJvmArgs;
       
    73 }
       
    74 
       
    75 const std::list<std::wstring>& JvmArgsFileReader::getApplicationArguments()
       
    76 {
       
    77     std::wstring filename;
       
    78     for (int i = 0; IDENTIFIERS[i] != 0; ++i) // codescanner::accessArrayElementWithoutCheck2
       
    79     {
       
    80         if (mIdentifier.compare(IDENTIFIERS[i]) == 0) // codescanner::accessArrayElementWithoutCheck2
       
    81         {
       
    82             filename = APP_ARGS_FILENAMES[i];
       
    83             break;
       
    84         }
       
    85     }
       
    86 
       
    87     if (!filename.empty())
       
    88     {
       
    89         readArgs(filename , mApplicationArgs);
       
    90     }
       
    91     else
       
    92     {
       
    93         WLOG2(EJavaRuntime, "%s: unknown identifier: %S", __PRETTY_FUNCTION__, mIdentifier.c_str());
       
    94     }
       
    95     return mApplicationArgs;
       
    96 }
       
    97 
       
    98 void JvmArgsFileReader::readArgs(const std::wstring& aFilename, std::list<std::wstring>& aArgs)
       
    99 {
       
   100     java::fileutils::driveInfos drives;
       
   101     java::fileutils::DriveUtilities::getAccesibleDrives(drives);
       
   102 
       
   103     for (java::fileutils::driveInfos::const_iterator iter = drives.begin(); iter != drives.end(); iter++)
       
   104     {
       
   105         std::string path = makeFullFilename((*iter).iRootPath.c_str(), JVM_ARGS_DIR, aFilename);
       
   106 
       
   107         std::ifstream file;
       
   108         file.open(path.c_str());
       
   109         if (file.good())
       
   110         {
       
   111             LOG1(EJavaRuntime, EInfo, "Using JVM args from %s", path.c_str());
       
   112             std::string arg;
       
   113             while (file >> arg)
       
   114             {
       
   115                 try
       
   116                 {
       
   117                     std::wstring str = java::util::JavaCommonUtils::utf8ToWstring(arg.c_str());
       
   118                     aArgs.push_back(str);
       
   119                     LOG1(EJavaRuntime, EInfo, " '%S'", str.c_str());
       
   120                 }
       
   121                 catch (java::util::ExceptionBase& e)
       
   122                 {
       
   123                     ELOG2(EJavaRuntime, "%s: utf8ToWstring failed: %s", __PRETTY_FUNCTION__, e.toString().c_str());
       
   124                 }
       
   125             }
       
   126             file.close();
       
   127             break;
       
   128         }
       
   129         file.close();
       
   130     }
       
   131 }
       
   132 
       
   133 std::string JvmArgsFileReader::makeFullFilename(const std::wstring& aDrive, const std::wstring& aDir, const std::wstring& aFilename)
       
   134 {
       
   135     std::wstring path;
       
   136     path.append(aDrive).append(aDir).append(aFilename);
       
   137 
       
   138     std::string fullFilename;
       
   139     try
       
   140     {
       
   141         char* utf8 = java::util::JavaCommonUtils::wstringToUtf8(path);
       
   142         fullFilename.assign(utf8);
       
   143         delete[] utf8;
       
   144     }
       
   145     catch (java::util::ExceptionBase& e)
       
   146     {
       
   147         ELOG2(EJavaRuntime, "%s: wstringToUtf8 failed: %s", __PRETTY_FUNCTION__, e.toString().c_str());
       
   148     }
       
   149 
       
   150     LOG1(EJavaRuntime, EInfoHeavyLoad, "makeFullFilename returns %s", fullFilename.c_str());
       
   151     return fullFilename;
       
   152 }
       
   153 
       
   154 
       
   155 
       
   156 } // end namespace runtime
       
   157 } // end namespace java
       
   158