javamanager/javasettings/appmngrplugin/javapackagelookup/src/jcfjadjarmatcherscanjarfiles.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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 is used to find matching JAR file for JAD file
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <apmstd.h>
       
    20 #include "appmngr2midletmanifestreader.h"
       
    21 #include "javacommonutils.h"
       
    22 #include "jcfjadjarmatcherscanjarfiles.h"
       
    23 #include "logger.h"
       
    24 
       
    25 _LIT(KSearchMask, "*.*");
       
    26 _LIT(KJarExt, ".jar");
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 //    To construct new CJcfJadJarMatcherScanJarFiles.
       
    30 // -----------------------------------------------------------------------------
       
    31 CJcfJadJarMatcherScanJarFiles::CJcfJadJarMatcherScanJarFiles(
       
    32     MJcfJadJarMatcherObserver* aObs, RFs& aFs) :
       
    33         CJcfJadJarMatcherState(aObs),
       
    34         iFf(aFs),
       
    35         iFs(aFs)
       
    36 {
       
    37 }
       
    38 
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 //    To destruct CJcfJadJarMatcherScanJarFiles. Cancel any
       
    42 //    outstanding request. Delete all C-objects owned by this object.
       
    43 // -----------------------------------------------------------------------------
       
    44 CJcfJadJarMatcherScanJarFiles::~CJcfJadJarMatcherScanJarFiles()
       
    45 {
       
    46     Cancel();
       
    47     Cleanup();
       
    48 }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 //    To execute this state. This method starts long running task,
       
    53 //    which seeks JAR files and does JAD/JAR matching for them.
       
    54 // -----------------------------------------------------------------------------
       
    55 void CJcfJadJarMatcherScanJarFiles::ExecuteL(const TDesC& aJadName,
       
    56         const TDesC& aDirName,
       
    57         TDes* aJarNamePtr)
       
    58 {
       
    59     JELOG2(EJavaStorage);
       
    60 
       
    61     Cleanup();
       
    62 
       
    63     std::auto_ptr<AppMngr2MidletManifestReader> reader(
       
    64         new(ELeave) AppMngr2MidletManifestReader(iFs));
       
    65 
       
    66     reader->ReadManifestL(aJadName, iAttributes);
       
    67 
       
    68     // Take only name without path and postfix.
       
    69     TInt postfixOffset = aJadName.LocateReverse('.');
       
    70     TInt slashOffset = aJadName.LocateReverse('\\');
       
    71     ++slashOffset;
       
    72 
       
    73     TPtrC jadName = aJadName.Mid(slashOffset, (postfixOffset-slashOffset));
       
    74 
       
    75     iFullJadName.reset(jadName.Alloc());
       
    76 
       
    77     iDir = aDirName.AllocL();
       
    78     iJarFileNamePtr = aJarNamePtr;
       
    79 
       
    80     iState = EOpenJadFile;
       
    81     SetObjectActive();
       
    82 }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 //    To match JAR and JAD files. This method seeks JAR files from
       
    87 //    given directory. If matching JAR file is found, then notification is sent
       
    88 //    to the observer of this object and execution of this active object is
       
    89 //    stopped. Otherwise if matching JAR file is not found and relative path
       
    90 //    (without driver) is defined, method starts to seek the file from same
       
    91 //    directory in different driver. All available drivers searched in
       
    92 //    descenting order: Y: -> A: and finally Z:. The JAD file 'jad' and the JAR
       
    93 //    file 'jar' is said to be matching iff Match('jad', 'jar') == true
       
    94 //    Otherwise files are said to be unmatching. This method is assumed to be
       
    95 //    long running. When all directories are scanned, complete notification
       
    96 //    (KErrNotFound) is sent.
       
    97 // -----------------------------------------------------------------------------
       
    98 void CJcfJadJarMatcherScanJarFiles::RunL()
       
    99 {
       
   100     switch (iState)
       
   101     {
       
   102         // EOpenJadFile Preconditions. - Successfull call of Cleanup().
       
   103         // Postconditions. - iFl is initialized.
       
   104     case EOpenJadFile:
       
   105     {
       
   106         TParsePtrC parse(iDir->Des());
       
   107         TPtrC drive = parse.Drive();
       
   108 
       
   109         // Check whether drive is specified (absolute path) or not
       
   110         // (relative path).
       
   111         iIsAbsolute = (0 != drive.Length());
       
   112 
       
   113         if (iIsAbsolute)
       
   114         {
       
   115             User::LeaveIfError(iFf.FindWildByDir(
       
   116                                    KSearchMask, parse.DriveAndPath(), iFl));
       
   117         }
       
   118         else
       
   119         {
       
   120             User::LeaveIfError(iFf.FindWildByDir(
       
   121                                    KSearchMask, parse.Path(), iFl));
       
   122         }
       
   123 
       
   124         // Start scanning loop.
       
   125         iIndex = 0;
       
   126         iAdHocGuess = ETrue;
       
   127         iState = EScanJarFile;
       
   128         SetObjectActive();
       
   129         break;
       
   130     }
       
   131 
       
   132     // EScanJarFile Preconditions. - iFl is initialized. Postconditions. -
       
   133     // One file is checked whether it matches or not.
       
   134     case EScanJarFile:
       
   135         if (iIndex < iFl->Count())
       
   136         {
       
   137             // Generate a name for a file to be opened next. An ad hoc
       
   138             // guess is that it's likely that the matching file has same
       
   139             // name as the argument. If the guess is right, the whole
       
   140             // matching is very fast. On the other hand if the guess is
       
   141             // wrong, the only penalty is that one file is matched twice.
       
   142             TBool  foundMatchingJar = EFalse;
       
   143             std::auto_ptr<HBufC>nameBuf(HBufC::NewL(KMaxFileName));
       
   144             TPtr namePtr(nameBuf->Des());
       
   145             TParsePtrC driveAndPath(iFf.File());
       
   146             namePtr.Append(driveAndPath.Drive());
       
   147             namePtr.Append(driveAndPath.Path());
       
   148 
       
   149             if (iAdHocGuess)
       
   150             {
       
   151                 iAdHocGuess = EFalse;
       
   152                 TInt index = 0;
       
   153 
       
   154                 // Check (case-sensitive) the names in the file list.
       
   155                 for (; index < iFl->Count(); index++)
       
   156                 {
       
   157                     TInt offset = (*iFl)[index].iName.LocateReverse('.');
       
   158                     if (KErrNotFound != offset)
       
   159                     {
       
   160                         TPtrC name((*iFl)[index].iName.Left(offset));
       
   161                         TPtrC jadName(iFullJadName->Des());
       
   162 
       
   163                         if (0 == jadName.Compare(name))
       
   164                         {
       
   165                             // The beginning of the file name matches,
       
   166                             // but is the suffix ".jar" ?
       
   167                             TPtrC suffix((*iFl)[index].iName.Mid(offset));
       
   168                             if (0 == suffix.CompareF(KJarExt))
       
   169                             {
       
   170                                 foundMatchingJar = ETrue;
       
   171                                 break;
       
   172                             }
       
   173                         }
       
   174                     }
       
   175                 }
       
   176 
       
   177                 if (foundMatchingJar)
       
   178                 {
       
   179                     namePtr.Append((*iFl)[index].iName);
       
   180                 }
       
   181                 else
       
   182                 {
       
   183                     namePtr.Append((*iFl)[iIndex++].iName);
       
   184                 }
       
   185             }
       
   186             else
       
   187             {
       
   188                 namePtr.Append((*iFl)[iIndex++].iName);
       
   189             }
       
   190 
       
   191             TParsePtrC absName(*nameBuf);
       
   192 
       
   193             // Parse JAD File attributes for the matching.
       
   194             std::auto_ptr<AppMngr2MidletManifestReader> reader(
       
   195                 new(ELeave) AppMngr2MidletManifestReader(iFs));
       
   196 
       
   197             RPointerArray<Java::MJavaAttribute>jarAttributes;
       
   198 
       
   199             TRAPD(parseErr, reader->ReadManifestL(namePtr, jarAttributes));
       
   200 
       
   201             if (KErrNone == parseErr)
       
   202             {
       
   203                 if (Match(iAttributes, jarAttributes))
       
   204                 {
       
   205                     LOG(EJavaStorage, EInfo, "JAR and JAD match");
       
   206                     iJarFileNamePtr->SetLength(0);
       
   207                     iJarFileNamePtr->Append(namePtr);
       
   208 
       
   209                     iValue = KErrNone;
       
   210                     iState = EEnd;
       
   211                 }
       
   212             }
       
   213             jarAttributes.ResetAndDestroy();
       
   214             SetObjectActive();
       
   215         }
       
   216         else
       
   217         {
       
   218             delete iFl;
       
   219             iFl = NULL;
       
   220 
       
   221             if (iIsAbsolute || KErrNotFound == iFf.FindWild(iFl))
       
   222             {
       
   223                 // End of loop.
       
   224                 iValue = KErrNotFound;
       
   225                 iState = EEnd;
       
   226             }
       
   227 
       
   228             iIndex = 0;
       
   229             iAdHocGuess = ETrue;
       
   230             SetObjectActive();
       
   231         }
       
   232         break;
       
   233 
       
   234         // EEnd Preconditions. - None. Postconditions. - Matching round is
       
   235         // completed. The ExecuteL() activates a new one.
       
   236     case EEnd:
       
   237         Cleanup();
       
   238         NotifyObserver(iValue);
       
   239     }
       
   240 }
       
   241 
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 //    To cancel this object activity. This method cancels the JAR
       
   245 //    file scanning.
       
   246 // -----------------------------------------------------------------------------
       
   247 void CJcfJadJarMatcherScanJarFiles::DoCancel()
       
   248 {
       
   249     NotifyObserver(KErrCancel);
       
   250     Cleanup();
       
   251 }
       
   252 
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 //    To complete pending request and set this object active.
       
   256 // -----------------------------------------------------------------------------
       
   257 void CJcfJadJarMatcherScanJarFiles::SetObjectActive()
       
   258 {
       
   259     TRequestStatus *status = &iStatus;
       
   260     User::RequestComplete(status, KErrNone);
       
   261     if (!IsActive())
       
   262     {
       
   263         SetActive();
       
   264     }
       
   265 }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 //    To do clean-up for next round.
       
   269 // -----------------------------------------------------------------------------
       
   270 void CJcfJadJarMatcherScanJarFiles::Cleanup()
       
   271 {
       
   272     delete iDir;
       
   273     iDir = NULL;
       
   274     delete iFl;
       
   275     iFl = NULL;
       
   276     iFullJadName.reset(0);
       
   277     iAttributes.ResetAndDestroy();
       
   278 
       
   279     iState = EBegin;
       
   280 }
       
   281