javamanager/javacaptain/src/rtc.cpp
changeset 21 2a9601315dfc
child 25 9ac0a0a7da70
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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:  Rtc
       
    15 *
       
    16 */
       
    17 
       
    18 #include <string>
       
    19 #include <memory>
       
    20 #include <exception>
       
    21 
       
    22 #include "commsendpoint.h"
       
    23 #include "commsmessage.h"
       
    24 #include "logger.h"
       
    25 #include "javacommonutils.h"
       
    26 #include "libraryloaderexception.h"
       
    27 #include "javaoslayer.h"
       
    28 
       
    29 #include "rtc.h"
       
    30 #include "coreinterface.h"
       
    31 #include "rtcmessages.h"
       
    32 #include "pmcmessages.h"
       
    33 #include "applicationruntimeeventsinterface.h"
       
    34 #include "booteventprovidermessages.h"
       
    35 #include "javastorageentry.h"
       
    36 #include "javastorage.h"
       
    37 #include "javastoragenames.h"
       
    38 
       
    39 #include "midprtcplugin.h"
       
    40 #ifdef RD_JAVA_CAPTAIN_TESTRUNTIME
       
    41 #include "testrtcplugin.h"
       
    42 #endif
       
    43 
       
    44 namespace java
       
    45 {
       
    46 namespace captain
       
    47 {
       
    48 
       
    49 Rtc::Rtc() : mCore(0), mRuntimeEventsDispatcher(0), mSupportPreWarming(false)
       
    50 {
       
    51     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    52 #if defined(RD_JAVA_PREWARM) && !defined (__WINS__)
       
    53     ILOG(EJavaCaptain, "Rtc::Rtc() - Prewarm support turned on");
       
    54     mSupportPreWarming = true;
       
    55 #endif // RD_JAVA_PREWARM
       
    56 }
       
    57 
       
    58 Rtc::~Rtc()
       
    59 {
       
    60     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    61 }
       
    62 
       
    63 bool Rtc::start(CoreInterface* aCore,
       
    64                 ApplicationRuntimeEventsInterface* aRuntimeEventsDispatcher)
       
    65 {
       
    66     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    67 
       
    68     mCore = aCore;
       
    69     mCore->getComms()->registerListener(PLUGIN_ID_RTC_C, this);
       
    70     mCore->getComms()->registerListener(PLUGIN_ID_PMC_C, this);
       
    71     mRuntimeEventsDispatcher = aRuntimeEventsDispatcher;
       
    72     initialiseRuntimePlugins();
       
    73     return true;
       
    74 }
       
    75 
       
    76 bool Rtc::stop()
       
    77 {
       
    78     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    79 
       
    80     if (mCore)
       
    81     {
       
    82         mCore->getComms()->unregisterListener(PLUGIN_ID_RTC_C, this);
       
    83         mCore->getComms()->unregisterListener(PLUGIN_ID_PMC_C, this);
       
    84     }
       
    85 
       
    86     terminateRtcRuntimes();
       
    87     clearDisabledList();
       
    88     clearRuntimePlugins();
       
    89 
       
    90     return true;
       
    91 }
       
    92 
       
    93 void Rtc::terminateRtcRuntimes()
       
    94 {
       
    95     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    96 
       
    97     // Delete the objects
       
    98     for (runtimes_t::iterator iter = mRuntimes.begin() ;
       
    99             iter != mRuntimes.end(); ++iter)
       
   100     {
       
   101         delete iter->second;
       
   102     }
       
   103     // Delete the pairs from map
       
   104     mRuntimes.clear();
       
   105 }
       
   106 
       
   107 void Rtc::stopPrewarm()
       
   108 {
       
   109     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   110 
       
   111     mSupportPreWarming = false;
       
   112     // Try to find prewarmed instance
       
   113     runtimes_t::iterator iter = mRuntimes.find(PREWARM_UID);
       
   114     if (iter != mRuntimes.end())
       
   115     {
       
   116         ILOG(EJavaCaptain, "Rtc::stopPrewarm() - prewarm instance found, deleting...");
       
   117         delete iter->second;
       
   118         mRuntimes.erase(iter);
       
   119     }
       
   120 }
       
   121 
       
   122 void Rtc::clearDisabledList()
       
   123 {
       
   124     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   125     mDisabledUIDs.clear();
       
   126 }
       
   127 
       
   128 // RtcInterface methods
       
   129 bool Rtc::launch(const rtcLaunchInfo& aLaunchInfo)
       
   130 {
       
   131     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   132     if (mSupportPreWarming &&
       
   133             aLaunchInfo.mLaunchType == RTC_LAUNCH_TYPE_PREWARM_C &&
       
   134             mRuntimes.find(PREWARM_UID) != mRuntimes.end())
       
   135     {
       
   136         ILOG(EJavaCaptain, "Rtc::launch() - Prewarmed instance found, not creating new");
       
   137         return true;
       
   138     }
       
   139     int options = RTC_LAUNCH_OPTIONS_NONE_C;
       
   140     CommsMessage dummy;
       
   141     return launch(aLaunchInfo, options, dummy);
       
   142 }
       
   143 
       
   144 bool Rtc::terminate(const rtcTerminateInfo& aTerminateInfo)
       
   145 {
       
   146     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   147     int options = RTC_TERMINATE_OPTIONS_NONE_C;
       
   148     CommsMessage dummy;
       
   149     return terminate(aTerminateInfo, options, dummy);
       
   150 }
       
   151 
       
   152 bool Rtc::launch(const rtcLaunchInfo& aLaunchInfo, const int& aOptions, CommsMessage& aRequester)
       
   153 {
       
   154     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   155 
       
   156     LOG1WSTR(EJavaCaptain, EInfo, "Rtc::launch(uid=%s)", aLaunchInfo.mUID.toString());
       
   157     LOG3(EJavaCaptain, EInfo, "Rtc::launch(runtime=%s, type=%d, options=%d)",
       
   158          aLaunchInfo.mRtcType.c_str(), aLaunchInfo.mLaunchType, aOptions);
       
   159 
       
   160     bool launchSuccess = false;
       
   161 
       
   162     if (aOptions & RTC_LAUNCH_OPTIONS_RUNNING_IND_REQ_C)
       
   163     {
       
   164         mLaunchers.insert(std::make_pair(aLaunchInfo.mUID, aRequester));
       
   165     }
       
   166     if (aOptions & RTC_LAUNCH_OPTIONS_TERMINATE_IND_REQ_C)
       
   167     {
       
   168         mTerminators.insert(std::make_pair(aLaunchInfo.mUID, aRequester));
       
   169     }
       
   170 
       
   171     if (mDisabledUIDs.find(aLaunchInfo.mUID) != mDisabledUIDs.end())
       
   172     {
       
   173         notifyLaunchers(aLaunchInfo.mUID, -1, 0);
       
   174         notifyTerminators(aLaunchInfo.mUID, -1);
       
   175         launchSuccess = false;
       
   176     }
       
   177     else
       
   178     {
       
   179         rtcLaunchInfo finalLaunchInfo(aLaunchInfo.mUID,
       
   180                                       aLaunchInfo.mLaunchType, aLaunchInfo.mRtcType,
       
   181                                       (!aLaunchInfo.mApplicationArguments.empty() ? aLaunchInfo.mApplicationArguments
       
   182                                        : getGlobalApplicationArguments()),
       
   183                                       (!aLaunchInfo.mRuntimeArguments.empty() ? aLaunchInfo.mRuntimeArguments
       
   184                                        : getGlobalRuntimeArguments()));
       
   185 
       
   186         RtcRuntimeInterface* runtime = getOrCreateRtcRuntime(finalLaunchInfo);
       
   187 
       
   188         if ((0 == runtime) || (false == runtime->launch(finalLaunchInfo)))
       
   189         {
       
   190             deleteRuntime(finalLaunchInfo.mUID);
       
   191             notifyLaunchers(finalLaunchInfo.mUID, -1, 0);
       
   192             notifyTerminators(finalLaunchInfo.mUID, -1);
       
   193             launchSuccess = false;
       
   194         }
       
   195         else
       
   196         {
       
   197             if (runtime->isRunning())
       
   198             {
       
   199                 notifyLaunchers(finalLaunchInfo.mUID, 0, runtime->pid());
       
   200             }
       
   201             launchSuccess = true;
       
   202         }
       
   203     }
       
   204 
       
   205     return launchSuccess;
       
   206 }
       
   207 
       
   208 bool Rtc::terminate(const rtcTerminateInfo& aTerminateInfo, const int& aOptions, CommsMessage& aRequester)
       
   209 {
       
   210     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   211 
       
   212     LOG1WSTR(EJavaCaptain, EInfo, "Rtc::terminate(uid=%s)", aTerminateInfo.mUID.toString());
       
   213     LOG1(EJavaCaptain, EInfo, "Rtc::terminate(options=%d)", aOptions);
       
   214 
       
   215     bool terminateSuccess = false;
       
   216 
       
   217     if (aOptions & RTC_TERMINATE_OPTIONS_TERMINATE_IND_REQ_C)
       
   218     {
       
   219         mTerminators.insert(std::make_pair(aTerminateInfo.mUID, aRequester));
       
   220     }
       
   221 
       
   222     RtcRuntimeInterface* runtime = getRtcRuntime(aTerminateInfo.mUID);
       
   223 
       
   224     if (0 == runtime ||
       
   225             true == runtime->terminate(aTerminateInfo))
       
   226     {
       
   227         notifyTerminators(aTerminateInfo.mUID, 0);
       
   228         mRuntimeEventsDispatcher->arTerminated(aTerminateInfo.mUID, 0);
       
   229         terminateSuccess = true;
       
   230     }
       
   231 
       
   232     return terminateSuccess;
       
   233 }
       
   234 
       
   235 void Rtc::notifyLaunchers(const Uid& aUid, const int& aStatus, const int& aPid)
       
   236 {
       
   237     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   238 
       
   239     requesters_t::iterator iter = mLaunchers.find(aUid);
       
   240 
       
   241     if (iter != mLaunchers.end())
       
   242     {
       
   243         CommsMessage msg;
       
   244         setApplicationRunningIndParams(msg, aUid, aStatus, aPid);
       
   245         CommsEndpoint* comms = mCore->getComms();
       
   246         while (comms && iter != mLaunchers.end())
       
   247         {
       
   248             msg.setModuleId(iter->second.getModuleId());
       
   249             msg.setReceiver(iter->second.getSender());
       
   250             msg.setMessageRef(iter->second.getMessageRef());
       
   251             LOG1(EJavaCaptain, EInfo, "informing launcher %d", msg.getReceiver());
       
   252             comms->send(msg);
       
   253             mLaunchers.erase(iter);
       
   254             iter = mLaunchers.find(aUid);
       
   255         }
       
   256     }
       
   257 }
       
   258 
       
   259 void Rtc::notifyTerminators(const Uid& aUid, const int& aStatus)
       
   260 {
       
   261     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   262 
       
   263     requesters_t::iterator iter = mTerminators.find(aUid);
       
   264 
       
   265     if (iter != mTerminators.end())
       
   266     {
       
   267         CommsMessage msg;
       
   268         setApplicationTerminatedIndParams(msg, aUid, aStatus);
       
   269         CommsEndpoint* comms = mCore->getComms();
       
   270         while (comms && iter != mTerminators.end())
       
   271         {
       
   272             msg.setModuleId(iter->second.getModuleId());
       
   273             msg.setReceiver(iter->second.getSender());
       
   274             msg.setMessageRef(iter->second.getMessageRef());
       
   275             LOG1(EJavaCaptain, EInfo, "informing terminator %d", msg.getReceiver());
       
   276             comms->send(msg);
       
   277             mTerminators.erase(iter);
       
   278             iter = mTerminators.find(aUid);
       
   279         }
       
   280     }
       
   281 }
       
   282 
       
   283 bool Rtc::enable(const Uid& aUID)
       
   284 {
       
   285     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   286     LOG1WSTR(EJavaCaptain, EInfo, "enable(%s)", aUID.toString());
       
   287 
       
   288     mDisabledUIDs.erase(aUID);
       
   289 
       
   290     return true;
       
   291 }
       
   292 
       
   293 bool Rtc::disable(const Uid& aUID)
       
   294 {
       
   295     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   296     LOG1WSTR(EJavaCaptain, EInfo, "disable(%s)", aUID.toString());
       
   297 
       
   298     mDisabledUIDs.insert(aUID);
       
   299 
       
   300     return true;
       
   301 }
       
   302 
       
   303 void Rtc::setGlobalRuntimeArguments(const std::wstring& aArguments)
       
   304 {
       
   305     mGlobalRuntimeArguments = aArguments;
       
   306 }
       
   307 
       
   308 std::wstring Rtc::getGlobalRuntimeArguments()
       
   309 {
       
   310     return mGlobalRuntimeArguments;
       
   311 }
       
   312 
       
   313 void Rtc::setGlobalApplicationArguments(const std::wstring& aArguments)
       
   314 {
       
   315     mGlobalApplicationArguments = aArguments;
       
   316 }
       
   317 
       
   318 std::wstring Rtc::getGlobalApplicationArguments()
       
   319 {
       
   320     return mGlobalApplicationArguments;
       
   321 }
       
   322 
       
   323 void Rtc::pmcTerminated(const int& pid, const int& exitCode)
       
   324 {
       
   325     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   326 
       
   327     for (runtimes_t::iterator iter = mRuntimes.begin() ;
       
   328             iter != mRuntimes.end(); ++iter)
       
   329     {
       
   330         if (iter->second->pid() == pid)
       
   331         {
       
   332             iter->second->pmcTerminated(pid, exitCode);
       
   333 
       
   334             Uid uid = iter->second->uid();
       
   335             // iter value might not be valid anymore after the following callbacks!
       
   336             mRuntimeEventsDispatcher->arTerminated(uid, exitCode);
       
   337             notifyLaunchers(uid, -1, pid);
       
   338             notifyTerminators(uid, 0);
       
   339             break;
       
   340         }
       
   341     }
       
   342 }
       
   343 
       
   344 void Rtc::processMessage(CommsMessage& aMessage)
       
   345 {
       
   346     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   347 
       
   348     try
       
   349     {
       
   350         switch (aMessage.getMessageId())
       
   351         {
       
   352         case RTC_MSG_ID_LAUNCH_APPLICATION_REQ:
       
   353         {
       
   354             java::util::JavaOsLayer::startUpTrace("CAPTAIN: Launch req", -1, -1);
       
   355             Uid uid;
       
   356             int type = 0;
       
   357             int options = 0;
       
   358             std::string rtc;
       
   359             std::wstring applicationArguments, runtimeArguments;
       
   360             getLaunchApplicationReqParams(aMessage, uid, type, options, rtc,
       
   361                                           applicationArguments, runtimeArguments);
       
   362 
       
   363             if (runtimeArguments.length() != 0 &&
       
   364                     !aMessage.hasPermission(LAUNCH_APPLICATION))
       
   365             {
       
   366                 WLOG(EJavaCaptain, "LAUNCH_APP failed: not allowed to modify runtime arguments");
       
   367                 sendOperationFailed(aMessage, uid, options);
       
   368             }
       
   369             else
       
   370             {
       
   371                 launch(rtcLaunchInfo(uid, type, rtc, applicationArguments, runtimeArguments),
       
   372                        options, aMessage);
       
   373             }
       
   374         }
       
   375         break;
       
   376 
       
   377         case RTC_MSG_ID_TERMINATE_APPLICATION_REQ:
       
   378         {
       
   379             Uid uid;
       
   380             int options = 0;
       
   381             getTerminateApplicationReqParams(aMessage, uid, options);
       
   382 
       
   383             if (aMessage.hasPermission(STOP_APPLICATION))
       
   384             {
       
   385                 terminate(uid, options, aMessage);
       
   386             }
       
   387             else
       
   388             {
       
   389                 WLOG(EJavaCaptain, "STOP_APP failed: not allowed");
       
   390                 sendOperationFailed(aMessage, uid, options);
       
   391             }
       
   392         }
       
   393         break;
       
   394 
       
   395         case RTC_MSG_ID_APPLICATION_RUNNING_IND:
       
   396         {
       
   397             Uid uid;
       
   398             int status = 0;
       
   399             getApplicationRunningIndParams(aMessage, uid, status);
       
   400             runtimes_t::iterator rtIter = mRuntimes.find(uid);
       
   401 
       
   402             if (rtIter != mRuntimes.end())
       
   403             {
       
   404                 const int senderAddress = aMessage.getSender();
       
   405                 rtIter->second->runningInd(senderAddress, status);
       
   406                 mRuntimeEventsDispatcher->arLaunched(uid, senderAddress);
       
   407                 notifyLaunchers(uid, 0, rtIter->second->pid());
       
   408             }
       
   409         }
       
   410         break;
       
   411 
       
   412         case RTC_MSG_ID_APPLICATION_TERMINATED_IND:
       
   413         {
       
   414             Uid uid;
       
   415             int status = 0;
       
   416             getApplicationTerminatedIndParams(aMessage, uid, status);
       
   417             runtimes_t::iterator rtIter = mRuntimes.find(uid);
       
   418 
       
   419             if (rtIter != mRuntimes.end())
       
   420             {
       
   421                 rtIter->second->terminatedInd(status);
       
   422             }
       
   423         }
       
   424         break;
       
   425 
       
   426         case RTC_MSG_ID_SET_EXTRA_ARGUMENTS:
       
   427             if (aMessage.hasPermission(LAUNCH_APPLICATION))
       
   428             {
       
   429                 getSetExtraArguments(aMessage, mGlobalRuntimeArguments,
       
   430                                      mGlobalApplicationArguments);
       
   431             }
       
   432             else
       
   433             {
       
   434                 WLOG(EJavaCaptain, "SET_EXTRA_ARGUMENTS failed: not allowed");
       
   435             }
       
   436             break;
       
   437 
       
   438         case RTC_MSG_ID_GET_EXTRA_ARGUMENTS:
       
   439             if (!routeMessage(aMessage, aMessage.getSender()))
       
   440             {
       
   441                 LOG(EJavaCaptain, EWarning,
       
   442                     "Rtc::ProcessMessage: Get extra args msg is not from one of the runtimes");
       
   443 
       
   444                 // handler was not one of the runtimes then reply directly:
       
   445                 CommsMessage message;
       
   446                 message.replyTo(aMessage);
       
   447                 setGetExtraArgumentsAck(message, mGlobalRuntimeArguments, mGlobalApplicationArguments);
       
   448                 mCore->getComms()->send(message);
       
   449             }
       
   450             break;
       
   451 
       
   452         case PMC_MSG_ID_PROCESS_INITIALISED_IND:
       
   453         {
       
   454             int pid = 0;
       
   455             getProcessInitialisedIndParams(aMessage, pid);
       
   456 
       
   457             for (runtimes_t::iterator iter = mRuntimes.begin();
       
   458                     iter != mRuntimes.end(); ++iter)
       
   459             {
       
   460                 if (iter->second->pid() == pid)
       
   461                 {
       
   462                     iter->second->runningInd(aMessage.getSender(), 0);
       
   463                     break;
       
   464                 }
       
   465             }
       
   466         }
       
   467         break;
       
   468 
       
   469         default:
       
   470             LOG1(EJavaCaptain, EInfo, "Rtc::ProcessMessage, unknown messageId = %d",
       
   471                  aMessage.getMessageId());
       
   472             break;
       
   473         }
       
   474     }
       
   475 
       
   476     catch (std::exception& e)
       
   477     {
       
   478         LOG(EJavaCaptain, EInfo, "Rtc::ProcessMessage: unhandled exception");
       
   479     }
       
   480 }
       
   481 
       
   482 
       
   483 bool Rtc::routeMessage(CommsMessage& aMessage, const int& aRuntimeAddress)
       
   484 {
       
   485     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   486 
       
   487     for (runtimes_t::iterator iter = mRuntimes.begin();
       
   488             iter != mRuntimes.end(); ++iter)
       
   489     {
       
   490         if (iter->second->runtimeAddress() == aRuntimeAddress)
       
   491         {
       
   492             iter->second->processMessage(aMessage);
       
   493             return true; // handler found
       
   494         }
       
   495     }
       
   496 
       
   497     return false; // handler not found
       
   498 }
       
   499 
       
   500 void Rtc::event(const std::string& eventProvider,
       
   501                 java::comms::CommsMessage& /* aMsg */)
       
   502 {
       
   503     ILOG(EJavaCaptain, "+Rtc::event()");
       
   504     if (BOOT_EVENT_PROVIDER == eventProvider)
       
   505     {
       
   506         if (mSupportPreWarming && isThereInstalledMidlets())
       
   507         {
       
   508             ILOG(EJavaCaptain, "Rtc::event() - Boot event received, Starting prewarm VM since found Installed MIDlets");
       
   509             launchPrewarm(); //New prewarm VM instance is will be created only if it did not exist already
       
   510         }
       
   511     }
       
   512 }
       
   513 
       
   514 // ApplicationManagementEventsInterface methods
       
   515 void Rtc::amAdded(const uids_t& /* aUids */)
       
   516 {
       
   517     ILOG(EJavaCaptain, "+Rtc::amAdded()");
       
   518     // we know there is now installed MIDlets
       
   519     if (mSupportPreWarming)
       
   520     {
       
   521         ILOG(EJavaCaptain, "Rtc::amAdded() - MIDlets installed and prewarm is on -> try to start prewarm VM if not already running");
       
   522         launchPrewarm(); //New prewarm VM instance is will be created only if it did not exist already
       
   523     }
       
   524 }
       
   525 
       
   526 void Rtc::amUpdated(const uids_t& /* aUids */)
       
   527 {
       
   528     ILOG(EJavaCaptain, "+Rtc::amUpdated()");
       
   529     // we know there is now installed MIDlets
       
   530     if (mSupportPreWarming)
       
   531     {
       
   532         ILOG(EJavaCaptain, "Rtc::amUpdated() - MIDlet updated and prewarm is on -> try to start prewarm VM if not already running ");
       
   533         launchPrewarm(); //New prewarm VM instance is will be created only if it did not exist already
       
   534     }
       
   535 }
       
   536 
       
   537 void Rtc::amDeleted(const uids_t& /* aUids */)
       
   538 {
       
   539     ILOG(EJavaCaptain, "+Rtc::amDeleted()");
       
   540     // Check if prewarm can be turned off
       
   541     if (mSupportPreWarming && (!isThereInstalledMidlets()))
       
   542     {
       
   543         ILOG(EJavaCaptain,  "Rtc::amDeleted() - No MIDlets installed -> Stopping prewarm VM...");
       
   544         stopPrewarm();
       
   545     }
       
   546 }
       
   547 
       
   548 void Rtc::routeMessageToAll(CommsMessage& message)
       
   549 {
       
   550     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   551 
       
   552     for (runtimes_t::iterator iter = mRuntimes.begin();
       
   553             iter != mRuntimes.end(); ++iter)
       
   554     {
       
   555         iter->second->processMessage(message);
       
   556     }
       
   557 }
       
   558 
       
   559 void Rtc::sendOperationFailed(CommsMessage& aRequester, const Uid& aUid, const int& aOptions)
       
   560 {
       
   561     if (aOptions != 0)
       
   562     {
       
   563         CommsMessage reply;
       
   564         reply.replyTo(aRequester);
       
   565         setApplicationTerminatedIndParams(reply, aUid, -1);
       
   566         mCore->getComms()->send(reply);
       
   567     }
       
   568 }
       
   569 
       
   570 void Rtc::initialiseRuntimePlugins()
       
   571 {
       
   572     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   573 
       
   574     mPlugins.insert(std::make_pair("midp", new rtcPluginData(getMidpRtcPlugin())));
       
   575 
       
   576 #ifdef RD_JAVA_CAPTAIN_TESTRUNTIME
       
   577     mPlugins.insert(std::make_pair("test", new rtcPluginData(getTestRtcPlugin())));
       
   578 #endif
       
   579 }
       
   580 
       
   581 void Rtc::clearRuntimePlugins()
       
   582 {
       
   583     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   584 
       
   585     // Delete the objects
       
   586     for (plugins_t::iterator iter = mPlugins.begin() ;
       
   587             iter != mPlugins.end(); ++iter)
       
   588     {
       
   589         delete iter->second;
       
   590     }
       
   591     // Delete the pairs from map
       
   592     mPlugins.clear();
       
   593 }
       
   594 
       
   595 RtcRuntimeInterface* Rtc::getRtcRuntime(const Uid& aUid)
       
   596 {
       
   597     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   598 
       
   599     RtcRuntimeInterface* runtime = 0;
       
   600     runtimes_t::iterator rtIter = mRuntimes.find(aUid);
       
   601 
       
   602     if (rtIter != mRuntimes.end())
       
   603     {
       
   604         runtime = rtIter->second;
       
   605     }
       
   606 
       
   607     return runtime;
       
   608 }
       
   609 
       
   610 RtcRuntimeInterface* Rtc::getOrCreateRtcRuntime(const rtcLaunchInfo& aLaunchInfo)
       
   611 {
       
   612     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   613 
       
   614     RtcRuntimeInterface* runtime = 0;
       
   615     runtimes_t::iterator rtIter = mRuntimes.find(aLaunchInfo.mUID);
       
   616 
       
   617     if (rtIter != mRuntimes.end() &&
       
   618             -1 == rtIter->second->pid())
       
   619     {
       
   620         delete rtIter->second;
       
   621         mRuntimes.erase(rtIter);
       
   622         rtIter = mRuntimes.end();
       
   623     }
       
   624 
       
   625     if (rtIter == mRuntimes.end())
       
   626     {
       
   627         runtime = getPrewarmedRtcRuntime(aLaunchInfo);
       
   628         if (runtime == 0)
       
   629         {
       
   630             RtcPluginInterface* rtcInterface = 0;
       
   631             plugins_t::iterator pluginIter = mPlugins.find(aLaunchInfo.mRtcType);
       
   632 
       
   633             if (pluginIter != mPlugins.end())
       
   634             {
       
   635                 rtcInterface = pluginIter->second->mInterface;
       
   636             }
       
   637             else
       
   638             {
       
   639                 try
       
   640                 {
       
   641                     std::string pluginName = "javacaptain_rtc_" + aLaunchInfo.mRtcType;
       
   642 
       
   643                     std::auto_ptr<java::util::DynamicLibLoader>
       
   644                     loader(new java::util::DynamicLibLoader(pluginName.c_str()));
       
   645 
       
   646                     java::captain::RtcPluginInterface*(*getRtcPluginFunc)() =
       
   647                         (java::captain::RtcPluginInterface*(*)())
       
   648                         loader->getFunction("getRtcPlugin");
       
   649 
       
   650                     if (getRtcPluginFunc)
       
   651                     {
       
   652                         rtcInterface = getRtcPluginFunc();
       
   653                         if (rtcInterface)
       
   654                         {
       
   655                             mPlugins.insert(std::make_pair(aLaunchInfo.mRtcType,
       
   656                                                            new rtcPluginData(rtcInterface, loader)));
       
   657                         }
       
   658                     }
       
   659                 }
       
   660                 catch (java::util::LibraryLoaderException& ex)
       
   661                 {
       
   662                     LOG2(EJavaCaptain, EError, "Rtc::getRtcPlugin(%s) failed due: %s",
       
   663                          aLaunchInfo.mRtcType.c_str(), ex.toString().c_str());
       
   664                 }
       
   665             }
       
   666 
       
   667             if (rtcInterface)
       
   668             {
       
   669                 runtime = rtcInterface->createNewRuntimeInstance(aLaunchInfo.mUID, mCore);
       
   670                 if (runtime)
       
   671                 {
       
   672                     mRuntimes.insert(std::make_pair(aLaunchInfo.mUID, runtime));
       
   673                 }
       
   674             }
       
   675         }
       
   676     }
       
   677     else
       
   678     {
       
   679         runtime = rtIter->second;
       
   680     }
       
   681 
       
   682     return runtime;
       
   683 }
       
   684 
       
   685 RtcRuntimeInterface* Rtc::getPrewarmedRtcRuntime(const rtcLaunchInfo& aLaunchInfo)
       
   686 {
       
   687     JELOG2(EJavaCaptain);
       
   688     RtcRuntimeInterface* runtime = 0;
       
   689 
       
   690     if (mSupportPreWarming &&
       
   691             aLaunchInfo.mRtcType == "midp" &&
       
   692             aLaunchInfo.mLaunchType == RTC_LAUNCH_TYPE_NORMAL_C)
       
   693     {
       
   694         // Use prewarming only for normal launches and leave out push,
       
   695         // debug, auto invocation etc.
       
   696         bool createNewPrewarmProcess = false;
       
   697 
       
   698         // Then check if there exists prewarmed instance.
       
   699         runtimes_t::iterator rtIter = mRuntimes.find(PREWARM_UID);
       
   700         if (rtIter != mRuntimes.end())
       
   701         {
       
   702             if (rtIter->second->isRunning())
       
   703             {
       
   704                 // Prewarmed instance found, using it
       
   705                 ILOG(EJavaCaptain,  "Rtc::getPrewarmedRtcRuntime() -Prewarmed instance found, launching within");
       
   706                 runtime = rtIter->second;
       
   707 
       
   708                 // Removed/add runtime instance with a correct UID to collection
       
   709                 mRuntimes.erase(rtIter);
       
   710                 mRuntimes.insert(std::make_pair(aLaunchInfo.mUID, runtime));
       
   711                 ILOG(EJavaCaptain, "Rtc::getPrewarmedRtcRuntime() - Asking for new prewarm instance - previous was taken into use");
       
   712                 createNewPrewarmProcess = true;
       
   713             }
       
   714             else
       
   715             {
       
   716                 if (-1 == rtIter->second->pid())
       
   717                 {
       
   718                     ILOG(EJavaCaptain, "Rtc::getPrewarmedRtcRuntime() - Prewarmed instance found but it has been deleted.");
       
   719                     // Remove the previous instance.
       
   720                     delete rtIter->second;
       
   721                     mRuntimes.erase(rtIter);
       
   722                     createNewPrewarmProcess = true;
       
   723                 }
       
   724                 else
       
   725                 {
       
   726                     ILOG(EJavaCaptain, "Rtc::getPrewarmedRtcRuntime() - Prewarmed instance found but it is not ready yet");
       
   727                 }
       
   728             }
       
   729         }
       
   730         else
       
   731         {
       
   732             ILOG(EJavaCaptain, "Rtc::getPrewarmedRtcRuntime() - New prewarm instance - didn't exist at all!");
       
   733             createNewPrewarmProcess = true;
       
   734         }
       
   735         if (createNewPrewarmProcess)
       
   736         {
       
   737             // Create new Prewarmed process.
       
   738             launchPrewarm();
       
   739         }
       
   740     }
       
   741     return runtime;
       
   742 }
       
   743 
       
   744 void Rtc::launchPrewarm()
       
   745 {
       
   746     mSupportPreWarming = true;
       
   747     launch(rtcLaunchInfo(PREWARM_UID, RTC_LAUNCH_TYPE_PREWARM_C,
       
   748                          RTC_LAUNCH_RUNTIME_MIDP_C));
       
   749 }
       
   750 
       
   751 void Rtc::deleteRuntime(const Uid& aUid)
       
   752 {
       
   753     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   754 
       
   755     runtimes_t::iterator rtIter = mRuntimes.find(aUid);
       
   756     if (rtIter != mRuntimes.end())
       
   757     {
       
   758         delete rtIter->second;
       
   759         mRuntimes.erase(rtIter);
       
   760     }
       
   761 }
       
   762 
       
   763 // Checks from Javastorage if there are installed MIDlets
       
   764 bool Rtc::isThereInstalledMidlets() const
       
   765 {
       
   766     bool result = false;
       
   767     using namespace java::storage;
       
   768 
       
   769     JavaStorageEntry idAttribute;
       
   770     idAttribute.setEntry(ID, L"");
       
   771 
       
   772     JavaStorageApplicationEntry_t findSuiteQuery;
       
   773     findSuiteQuery.insert(idAttribute);
       
   774 
       
   775     JavaStorageApplicationList_t foundMidletSuites;
       
   776     std::auto_ptr<JavaStorage> jsAptr(JavaStorage::createInstance());
       
   777 
       
   778     try
       
   779     {
       
   780         ILOG(EJavaCaptain, "Rtc::isThereInstalledMidlets() - searching for installed suites");
       
   781         jsAptr->open(JAVA_DATABASE_NAME);
       
   782         jsAptr->search(APPLICATION_PACKAGE_TABLE, findSuiteQuery, foundMidletSuites);
       
   783     }
       
   784     catch (JavaStorageException& jse)
       
   785     {
       
   786         ELOG1(EJavaCaptain, "Rtc::isThereInstalledMidlets() - Failed to search from JavaStorage: %s ",
       
   787               jse.toString().c_str());
       
   788     }
       
   789     findSuiteQuery.clear();
       
   790 
       
   791     if (foundMidletSuites.size() > 0)
       
   792     {
       
   793         ILOG(EJavaCaptain, "Rtc::isThereInstalledMidlets() - found installed suites");
       
   794         result = true;
       
   795     }
       
   796     foundMidletSuites.clear();
       
   797 
       
   798     // Close connection
       
   799     try
       
   800     {
       
   801         jsAptr->close();
       
   802     }
       
   803     catch (JavaStorageException& jse)
       
   804     {
       
   805         ELOG1(EJavaCaptain, "Rtc::isThereInstalledMidlets() - JavaStorage close failed: %s ",
       
   806               jse.toString().c_str());
       
   807     }
       
   808 
       
   809     return result;
       
   810 }
       
   811 
       
   812 
       
   813 } // namespace captain
       
   814 } // namespace java
       
   815