javaruntimes/starterutils/src/runtimestarterutils.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:  This class contains utilities for starting the JVM.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <sstream>
       
    20 
       
    21 #include "commsclientendpoint.h"
       
    22 #include "commslistener.h"
       
    23 #include "commsmessage.h"
       
    24 #include "rtcmessages.h"
       
    25 #include "coremessages.h"
       
    26 
       
    27 #include "runtimestarterutils.h"
       
    28 #include "osthreadsupervisor.h"
       
    29 
       
    30 #include "logger.h"
       
    31 #include "exception"
       
    32 #include "runtimeexception.h"
       
    33 #include "javacommonutils.h"
       
    34 #include "bootclasspath.h"
       
    35 #include "ueiargsparser.h"
       
    36 
       
    37 #include "threaddumper.h"
       
    38 
       
    39 namespace java // codescanner::namespace
       
    40 {
       
    41 namespace runtime // codescanner::namespace
       
    42 {
       
    43 
       
    44 /**
       
    45  * This class is for listening the messages from Java Captain related to
       
    46  * development fetures.
       
    47  */
       
    48 class DevelopmentFeaturesListener : public java::comms::CommsListener
       
    49 {
       
    50 public:
       
    51     /**
       
    52      * Default contructor of DevelopmentFeaturesListener.
       
    53      */
       
    54     DevelopmentFeaturesListener();
       
    55 
       
    56     /**
       
    57      * Destructor of DevelopmentFeaturesListener.
       
    58      */
       
    59     ~DevelopmentFeaturesListener();
       
    60 
       
    61     /**
       
    62      * This processes messages only with moduleId PLUGIN_ID_JAVACAPTAIN_CORE_C
       
    63      *@see commslistener.h
       
    64      */
       
    65     void processMessage(CommsMessage& message);
       
    66 };
       
    67 } // end namespace runtime
       
    68 } // end namespace java
       
    69 
       
    70 using namespace java::runtime;
       
    71 using namespace java::util;
       
    72 
       
    73 
       
    74 const wchar_t* const JAVA_CAPTAIN = L"javacaptain";
       
    75 const int DEVELOPMENT_MODULE_ID = java::comms::PLUGIN_ID_JAVACAPTAIN_CORE_C; // codescanner::constnames
       
    76 
       
    77 OS_EXPORT
       
    78 RuntimeStarterUtils::RuntimeStarterUtils(): mComms(0),
       
    79         mSupervisor(0),
       
    80         mListener(0)
       
    81 {
       
    82     JELOG2(EJavaRuntime);
       
    83 }
       
    84 
       
    85 OS_EXPORT RuntimeStarterUtils::~RuntimeStarterUtils()
       
    86 {
       
    87     JELOG2(EJavaRuntime);
       
    88     delete mSupervisor;
       
    89     mSupervisor = 0;
       
    90 
       
    91     delete mListener;
       
    92     mListener = 0;
       
    93 
       
    94     delete mComms;
       
    95     mComms = 0;
       
    96 }
       
    97 
       
    98 OS_EXPORT
       
    99 void RuntimeStarterUtils::startThreadSupervisor(bool tryThreadDumping)
       
   100 {
       
   101     JELOG2(EJavaRuntime);
       
   102     // Delete the old one if exists.
       
   103     if (mSupervisor)
       
   104     {
       
   105         delete mSupervisor;
       
   106         mSupervisor = 0;
       
   107     }
       
   108     mSupervisor = new OsThreadSupervisor(tryThreadDumping); // codescanner::nonleavenew
       
   109 }
       
   110 
       
   111 OS_EXPORT
       
   112 void RuntimeStarterUtils::enableDevelopmentFeatures(JvmStarter& jvmStarter,
       
   113         bool askForArgs,
       
   114         std::wstring *pApplicationArgs)
       
   115 {
       
   116     JELOG2(EJavaRuntime);
       
   117 
       
   118     // Try to find if there is already client endpoint available.
       
   119     CommsClientEndpoint* comms =
       
   120         java::comms::CommsClientEndpoint::find(JAVA_CAPTAIN);
       
   121 
       
   122     if (comms == 0)
       
   123     {
       
   124         // Not found, so creating new one.
       
   125         mComms = new CommsClientEndpoint(); // codescanner::nonleavenew
       
   126         comms = mComms;
       
   127     }
       
   128 
       
   129     // Create a listener for receiving development messages from JavaCaptain.
       
   130     mListener = new DevelopmentFeaturesListener(); // codescanner::nonleavenew
       
   131     comms->registerListener(DEVELOPMENT_MODULE_ID, mListener);
       
   132 
       
   133     // If necessary ask more arguments from JC
       
   134     if (askForArgs)
       
   135     {
       
   136         LOG(EJavaRuntime, EInfo, "Getting extra args from Java Captain");
       
   137 
       
   138         java::comms::CommsMessage message;
       
   139         java::comms::CommsMessage reply;
       
   140 
       
   141         // Send a message to Java captain in order to get debug attributes.
       
   142         java::captain::setGetExtraArguments(message);
       
   143 
       
   144         comms->connect(java::comms::IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   145         int status = comms->sendReceive(message, reply, 1);
       
   146 
       
   147         if (status == 0)
       
   148         {
       
   149             std::wstring runtimeArgs;
       
   150             std::wstring applicationArgs;
       
   151             java::captain::getGetExtraArgumentsAck(reply, runtimeArgs,
       
   152                                                    applicationArgs);
       
   153 
       
   154             if (pApplicationArgs)
       
   155             {
       
   156                 *pApplicationArgs = applicationArgs;
       
   157             }
       
   158 
       
   159             addRuntimeArguments(runtimeArgs, jvmStarter);
       
   160         }
       
   161     }
       
   162 }
       
   163 
       
   164 void RuntimeStarterUtils::addRuntimeArguments(const std::wstring& aRuntimeArgs,
       
   165         JvmStarter& aJvmStarter)
       
   166 {
       
   167     if (aRuntimeArgs.length() > 0)
       
   168     {
       
   169         UeiArgsParser parser;
       
   170         std::wstring jvmArgs = parser.convertUeiArgsToJvmArgs(aRuntimeArgs);
       
   171 
       
   172         std::wstringstream stream;
       
   173         stream << jvmArgs; // codescanner::leave
       
   174 
       
   175         std::wstring jvmArg;
       
   176         while (stream.good())
       
   177         {
       
   178             stream >> jvmArg; // codescanner::leave
       
   179             if (jvmArg.length() > 0)
       
   180             {
       
   181                 LOG1(EJavaRuntime, EInfo, "  Attrib: %S", jvmArg.c_str());
       
   182                 aJvmStarter.appendRawJvmArgument(jvmArg);
       
   183             }
       
   184         }
       
   185     }
       
   186 }
       
   187 OS_EXPORT
       
   188 void RuntimeStarterUtils::getExtBootClassPath(std::wstring& extendedBootClassPath)
       
   189 {
       
   190     getExtendedBootClassPath(extendedBootClassPath);
       
   191 }
       
   192 
       
   193 DevelopmentFeaturesListener::DevelopmentFeaturesListener()
       
   194 {
       
   195     JELOG2(EJavaRuntime);
       
   196 }
       
   197 
       
   198 DevelopmentFeaturesListener::~DevelopmentFeaturesListener()
       
   199 {
       
   200     JELOG2(EJavaRuntime);
       
   201 }
       
   202 
       
   203 void DevelopmentFeaturesListener::processMessage(CommsMessage& message)
       
   204 {
       
   205     JELOG2(EJavaRuntime);
       
   206     if (message.getMessageId() == java::captain::CORE_MSG_ID_DO_THREAD_DUMP)
       
   207     {
       
   208         WLOG(EJavaRuntime, "Doing THREAD DUMP");
       
   209         ThreadDump::doDump();
       
   210     }
       
   211     else
       
   212     {
       
   213         ELOG2(EJavaRuntime, "Unknown message sent to development features "
       
   214               "listener. messageId = %d, moduleId = %d.",
       
   215               message.getMessageId(), message.getModuleId());
       
   216     }
       
   217 }