javatools/javaupdater/src/main.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14: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:  OMJ S60 environment updater process.
       
    15 *               When OMJ is installed, this program is executed to allow
       
    16 *               new Java environment taken into use.
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32std.h>
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include <apgtask.h>
       
    24 #include <w32std.h>
       
    25 #include <swinstapi.h>
       
    26 #include <centralrepository.h>
       
    27 #include <hal.h>
       
    28 
       
    29 #include "javauids.h"
       
    30 #include "systemamstraderprivateclient.h"
       
    31 #include "logger.h"
       
    32 
       
    33 // This file is used to indicate when converting old S60 midlets
       
    34 // to OMJ midlets has been done.
       
    35 _LIT(KConversionOngoing, "D:\\OMJConverting.dat");
       
    36 
       
    37 
       
    38 /**
       
    39  * Kill old and running javaregistry.exe. After killing next call of
       
    40  * Registry API re-starts the new server process.
       
    41  */
       
    42 static void KillRegistry()
       
    43 {
       
    44     JELOG2(EJavaConverters);
       
    45     TFullName processName;
       
    46     _LIT(KJavaRegistryProcess, "javaregistr*");
       
    47     TFindProcess finder(KJavaRegistryProcess);
       
    48 
       
    49     if (finder.Next(processName) != KErrNotFound)
       
    50     {
       
    51         RProcess processToKill;
       
    52         TInt err = processToKill.Open(finder);
       
    53 
       
    54         if (KErrNone != err)
       
    55         {
       
    56             WLOG1(EJavaConverters,
       
    57                   "javaupdater: KillRegistry: Process open: %d", err);
       
    58         }
       
    59         else
       
    60         {
       
    61             LOG(EJavaConverters, EInfo, "javaupdater: KillRegistry: Killing javaregistry");
       
    62             processToKill.Kill(KErrNone);
       
    63             processToKill.Close();
       
    64         }
       
    65     }
       
    66     else
       
    67     {
       
    68         WLOG(EJavaConverters, "javaupdater: KillRegistry: Registry process not found");
       
    69     }
       
    70 }
       
    71 
       
    72 /**
       
    73  * Kill running System AMS processes.
       
    74  */
       
    75 static void KillSystemAms()
       
    76 {
       
    77     JELOG2(EJavaConverters);
       
    78 
       
    79     TBool connectionOK(EFalse);
       
    80     TRequestStatus status(KRequestPending);
       
    81     RSystemAMSTraderPrivateClient amsClient;
       
    82 
       
    83     connectionOK = amsClient.ShutdownJavaEnv(status);
       
    84     if (connectionOK)
       
    85     {
       
    86         // systemams process in this device supports nice shutdown.
       
    87         // Wait until the S60 java environment has been shutdown
       
    88         LOG(EJavaConverters,
       
    89             EInfo, "javaupdater: KillSystemAms: Waiting for systemams nice shutdown");
       
    90         User::WaitForRequest(status);
       
    91         LOG(EJavaConverters,
       
    92             EInfo, "javaupdater: KillSystemAms: Systemams has shutdown");
       
    93         amsClient.Close();
       
    94     }
       
    95     else
       
    96     {
       
    97         // must kill SystemAMS trader and SystemAMS itself
       
    98         TFullName processName;
       
    99         _LIT(KJavaSystemAmsTraderProcess, "SystemAMS.EXE*");
       
   100         TFindProcess finder(KJavaSystemAmsTraderProcess);
       
   101 
       
   102         if (finder.Next(processName) == KErrNone)
       
   103         {
       
   104             RProcess processToKill;
       
   105             TInt err = processToKill.Open(finder);
       
   106 
       
   107             if (KErrNone != err)
       
   108             {
       
   109                 WLOG1(EJavaConverters,
       
   110                       "javaupdater: KillSystemAms: Cannot open SystemAMS trader process: %d", err);
       
   111             }
       
   112             else
       
   113             {
       
   114                 LOG(EJavaConverters, EInfo,
       
   115                     "javaupdater:KillSystemAms: Killing SystemAMS trader");
       
   116                 processToKill.Kill(KErrNone);
       
   117                 processToKill.Close();
       
   118             }
       
   119         }
       
   120 
       
   121         // now kill SystemAMS itself
       
   122         _LIT(KJavaSystemAmsProcess, "SystemAMSCore.EXE*");
       
   123         finder.Find(KJavaSystemAmsProcess);
       
   124 
       
   125         if (finder.Next(processName) == KErrNone)
       
   126         {
       
   127             RProcess processToKill;
       
   128             TInt err = processToKill.Open(finder);
       
   129 
       
   130             if (KErrNone != err)
       
   131             {
       
   132                 WLOG1(EJavaConverters,
       
   133                       "javaupdater: KillSystemAms: Cannot open SystemAMS process: %d", err);
       
   134             }
       
   135             else
       
   136             {
       
   137                 LOG(EJavaConverters, EInfo, "javaupdater: KillSystemAms Killing SystemAMS ");
       
   138                 processToKill.Kill(KErrNone);
       
   139                 processToKill.Close();
       
   140             }
       
   141         }
       
   142     }
       
   143 }
       
   144 
       
   145 /**
       
   146  * Try to kill old javapreinstaller
       
   147  */
       
   148 static void KillOldPreinstaller()
       
   149 {
       
   150     JELOG2(EJavaConverters);
       
   151 
       
   152     TFullName processName;
       
   153     _LIT(KJavaOldPreinstallerProcess, "MIDP2SilentMIDletInstall.exe*");
       
   154     TFindProcess finder(KJavaOldPreinstallerProcess);
       
   155 
       
   156     if (finder.Next(processName) == KErrNone)
       
   157     {
       
   158         RProcess processToKill;
       
   159         TInt err = processToKill.Open(finder);
       
   160 
       
   161         if (KErrNone != err)
       
   162         {
       
   163             WLOG1(EJavaConverters,
       
   164                   "javaupdater: KillOldPreinstallerL: Process open: %d", err);
       
   165         }
       
   166         else
       
   167         {
       
   168             LOG(EJavaConverters, EInfo, "javaupdater: Killing MIDP2SilentMIDletInstall");
       
   169             processToKill.Kill(KErrNone);
       
   170             processToKill.Close();
       
   171         }
       
   172     }
       
   173 }
       
   174 
       
   175 /**
       
   176  * Start OMJ trader (systemams.exe) in IAD mode.
       
   177  * It will start Java Captain in IAD mode and
       
   178  * it will start Java Preinstaller in IAD mode.
       
   179  */
       
   180 _LIT(KOmjTraderExe, "systemams.exe");
       
   181 _LIT(KOmjTraderArg, "iad");
       
   182 static void StartOmjTrader()
       
   183 {
       
   184     JELOG2(EJavaConverters);
       
   185     RProcess proc;
       
   186     int rc = proc.Create(KOmjTraderExe, KOmjTraderArg);
       
   187     if (rc == KErrNone)
       
   188     {
       
   189         proc.Resume();
       
   190         LOG(EJavaConverters, EInfo, "javaupdater: StartOmjTrader systemams.exe was started ok");
       
   191     }
       
   192     else
       
   193     {
       
   194         ELOG1(EJavaConverters, "javaupdater: StartOmjTrader failed: %d", rc);
       
   195     }
       
   196     proc.Close();
       
   197 }
       
   198 
       
   199 /**
       
   200  * Copies (and configures) the certificates from legacy java
       
   201  */
       
   202 _LIT(KJavaCertfictatesConfiguratorExe, "javacertificatesconfigurator.exe");
       
   203 static void ConfigureCertificates()
       
   204 {
       
   205     JELOG2(EJavaConverters);
       
   206     RProcess proc;
       
   207     int rc = proc.Create(KJavaCertfictatesConfiguratorExe, KNullDesC());
       
   208     if (rc == KErrNone)
       
   209     {
       
   210         proc.Resume();
       
   211         TRequestStatus status;
       
   212         proc.Logon(status);
       
   213         User::WaitForRequest(status);
       
   214     }
       
   215     else
       
   216     {
       
   217         ELOG1(EJavaConverters, "javaupdater: ConfigureCertificates failed: %d", rc);
       
   218     }
       
   219     proc.Close();
       
   220 }
       
   221 
       
   222 /**
       
   223  * Wait until preinstallation of OMJ midlets has been done.
       
   224  */
       
   225 static void WaitUntilPreinstallerReady()
       
   226 {
       
   227     // Create a temporary file that javapreinstaller.exe will
       
   228     // then destroy after reinstallation has been done
       
   229     TUint value;
       
   230     RFs   fs;
       
   231     RFile flagFile;
       
   232     TBool flagFileExists(ETrue);
       
   233     TInt err = fs.Connect();
       
   234     if (KErrNone != err)
       
   235     {
       
   236         ELOG1(EJavaConverters,
       
   237               "javaupdater: WaitUntilPreinstallerReady: Cannot connect to RFs, err %d",
       
   238               err);
       
   239         return;
       
   240     }
       
   241     err = flagFile.Replace(fs, KConversionOngoing, EFileShareAny|EFileWrite);
       
   242     if (KErrNone != err)
       
   243     {
       
   244         ELOG1(EJavaConverters,
       
   245               "javaupdater: WaitUntilPreinstallerReady: Cannot create conversion flag file, err %d",
       
   246               err);
       
   247         fs.Close();
       
   248         return;
       
   249     }
       
   250     flagFile.Close();
       
   251 
       
   252     // Now wait until the file is destroyed
       
   253     do
       
   254     {
       
   255         TRequestStatus status;
       
   256         fs.NotifyChange(ENotifyFile, status, KConversionOngoing);
       
   257         User::WaitForRequest(status);
       
   258         LOG1(EJavaConverters,
       
   259              EInfo,
       
   260              "javaupdater: WaitUntilPreinstallerReady: Wait ended with status %d",
       
   261              status.Int());
       
   262 
       
   263         // Check whether the flag file has been destroyed
       
   264         err = fs.Att(KConversionOngoing, value);
       
   265         if ((KErrNotFound == err) || (KErrPathNotFound == err))
       
   266         {
       
   267             flagFileExists = EFalse;
       
   268         }
       
   269         else if (KErrNone == err)
       
   270         {
       
   271             flagFileExists = ETrue;
       
   272             LOG(EJavaConverters,
       
   273                 EInfo,
       
   274                 "javaupdater: WaitUntilPreinstallerReady: Flag file still exists, must wait more");
       
   275         }
       
   276         else
       
   277         {
       
   278             WLOG1(EJavaConverters,
       
   279                   "javaupdater: WaitUntilPreinstallerReady: Unexpected error %d, stop waiting",
       
   280                   err);
       
   281             flagFileExists = EFalse;
       
   282         }
       
   283 
       
   284     }
       
   285     while (flagFileExists);
       
   286 
       
   287     fs.Close();
       
   288 }
       
   289 
       
   290 /**
       
   291  * Create cleanup stack and run the cleaner code inside TRAP harness
       
   292  * to log unexpected leaves.
       
   293  */
       
   294 TInt E32Main()
       
   295 {
       
   296     __UHEAP_MARK;
       
   297     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   298     KillRegistry();
       
   299     KillSystemAms();
       
   300     KillOldPreinstaller();
       
   301     ConfigureCertificates();
       
   302     StartOmjTrader();
       
   303     WaitUntilPreinstallerReady();
       
   304     delete cleanupStack;
       
   305     __UHEAP_MARKEND;
       
   306     return KErrNone;
       
   307 }