|
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 JAD file for given JAR |
|
15 * file. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "appmngr2midletmanifestreader.h" |
|
21 #include "javacommonutils.h" |
|
22 #include "jcfjadjarmatcherscanjadfiles.h" |
|
23 #include "logger.h" |
|
24 |
|
25 _LIT(KJadMask, "*.jad"); |
|
26 |
|
27 // --------------------------------------------------------------------------- |
|
28 // To construct new CJcfJadJarMatcherScanJadFiles. |
|
29 // ----------------------------------------------------------------------------- |
|
30 CJcfJadJarMatcherScanJadFiles::CJcfJadJarMatcherScanJadFiles( |
|
31 MJcfJadJarMatcherObserver* aObs, RFs& aFs) : |
|
32 CJcfJadJarMatcherState(aObs), |
|
33 iFf(aFs), |
|
34 iFs(aFs) |
|
35 { |
|
36 } |
|
37 |
|
38 |
|
39 // --------------------------------------------------------------------------- |
|
40 // To destruct CJcfJadJarMatcherScanJadFiles. Cancel any |
|
41 // outstanding request. Delete all C-objects owned by this object. |
|
42 // ----------------------------------------------------------------------------- |
|
43 CJcfJadJarMatcherScanJadFiles::~CJcfJadJarMatcherScanJadFiles() |
|
44 { |
|
45 Cancel(); |
|
46 Cleanup(); |
|
47 } |
|
48 |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // To execute this state. This method starts long running task, |
|
52 // which seeks JAD files and does JAD/JAR matching for them. Make clean-up. |
|
53 // Open JAR file asynchronously. Set new state. |
|
54 // ----------------------------------------------------------------------------- |
|
55 void CJcfJadJarMatcherScanJadFiles::ExecuteL(const TDesC& aJarName, |
|
56 const TDesC& aDirName, |
|
57 TDes* aJadNamePtr) |
|
58 { |
|
59 JELOG2(EJavaStorage); |
|
60 Cleanup(); |
|
61 |
|
62 std::auto_ptr<AppMngr2MidletManifestReader> reader( |
|
63 new(ELeave) AppMngr2MidletManifestReader(iFs)); |
|
64 |
|
65 reader->ReadManifestL(aJarName, iAttributes); |
|
66 |
|
67 iIndex = 0; |
|
68 iDir = aDirName.AllocL(); |
|
69 |
|
70 // Take only name without path and postfix. |
|
71 TInt postfixOffset = aJarName.LocateReverse('.'); |
|
72 TInt slashOffset = aJarName.LocateReverse('\\'); |
|
73 ++slashOffset; |
|
74 |
|
75 TPtrC jarName = aJarName.Mid(slashOffset, (postfixOffset-slashOffset)); |
|
76 |
|
77 iFullJarName.reset(jarName.Alloc()); |
|
78 iJadFileNamePtr = aJadNamePtr; |
|
79 |
|
80 iState = EOpenJarFile; |
|
81 SetObjectActive(); |
|
82 } |
|
83 |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // To match JAR and JAD files. This method seeks JAD files from |
|
87 // given directory. If matching JAD 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 JAD 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 if 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 CJcfJadJarMatcherScanJadFiles::RunL() |
|
99 { |
|
100 switch (iState) |
|
101 { |
|
102 // EOpenJarFile Preconditions. - Successfull call of Cleanup(). |
|
103 // Postconditions. - iFl is initialized. |
|
104 case EOpenJarFile: |
|
105 if (KErrNone == iStatus.Int()) |
|
106 { |
|
107 TParsePtrC parse(iDir->Des()); |
|
108 TPtrC drive = parse.Drive(); |
|
109 iIsAbsolute = (0 != drive.Length()); |
|
110 |
|
111 // Check whether a drive is specified (absolute path) or not |
|
112 // (relative path). |
|
113 if (iIsAbsolute) |
|
114 { |
|
115 TPtrC path = parse.DriveAndPath(); |
|
116 User::LeaveIfError(iFf.FindWildByDir(KJadMask, path, iFl)); |
|
117 } |
|
118 else |
|
119 { |
|
120 TPtrC path = parse.Path(); |
|
121 User::LeaveIfError(iFf.FindWildByDir(KJadMask, path, iFl)); |
|
122 } |
|
123 |
|
124 // Start scanning loop. |
|
125 iIndex = 0; |
|
126 iAdHocGuess = ETrue; |
|
127 iState = EScanJadFile; |
|
128 } |
|
129 else |
|
130 { |
|
131 iValue = KErrArgument; |
|
132 iState = EEnd; |
|
133 } |
|
134 |
|
135 SetObjectActive(); |
|
136 break; |
|
137 |
|
138 // EScanJadFile Preconditions. - iFl is initialized. Postconditions. - |
|
139 // One file is checked whether it matches or not. |
|
140 case EScanJadFile: |
|
141 if (iIndex < iFl->Count()) |
|
142 { |
|
143 // Generate a name for a file to be opened next. An ad hoc |
|
144 // guess is that it's likely that the matching file has same |
|
145 // name as the argument. If the guess is right, the whole |
|
146 // matching is very fast. On the other hand if the guess is |
|
147 // wrong, the only penalty is that one file is matched twice. |
|
148 std::auto_ptr<HBufC>nameBuf(HBufC::NewL(KMaxFileName)); |
|
149 TPtr namePtr(nameBuf->Des()); |
|
150 TParsePtrC driveAndPath(iFf.File()); |
|
151 namePtr.Append(driveAndPath.Drive()); |
|
152 namePtr.Append(driveAndPath.Path()); |
|
153 |
|
154 if (iAdHocGuess) |
|
155 { |
|
156 iAdHocGuess = EFalse; |
|
157 TBool find = EFalse; |
|
158 TInt index = 0; |
|
159 TParsePtrC argName(*iFullJarName); |
|
160 |
|
161 // Check (case-sensitive) the names in the file list. |
|
162 for (; index < iFl->Count(); index++) |
|
163 { |
|
164 TInt offset = (*iFl)[index].iName.LocateReverse('.'); |
|
165 if (KErrNotFound != offset) |
|
166 { |
|
167 TPtrC name((*iFl)[index].iName.Left(offset)); |
|
168 if (0 == argName.Name().Compare(name)) |
|
169 { |
|
170 find = ETrue; |
|
171 break; |
|
172 } |
|
173 } |
|
174 } |
|
175 |
|
176 if (find) |
|
177 { |
|
178 namePtr.Append((*iFl)[index].iName); |
|
179 } |
|
180 else |
|
181 { |
|
182 namePtr.Append((*iFl)[iIndex++].iName); |
|
183 } |
|
184 } |
|
185 else // !iAdHocGuess |
|
186 { |
|
187 namePtr.Append((*iFl)[iIndex++].iName); |
|
188 } |
|
189 |
|
190 LOG1WSTR(EJavaStorage, EInfo, "Matching to jad file: %s", |
|
191 (const wchar_t*)namePtr.PtrZ()); |
|
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>jadAttributes; |
|
198 reader->ReadManifestL(namePtr, jadAttributes); |
|
199 |
|
200 if (Match(iAttributes, jadAttributes)) |
|
201 { |
|
202 LOG(EJavaStorage, EInfo, "JAR and JAD match"); |
|
203 iJadFileNamePtr->SetLength(0); |
|
204 iJadFileNamePtr->Append(namePtr); |
|
205 |
|
206 iValue = KErrNone; |
|
207 iState = EEnd; |
|
208 } |
|
209 |
|
210 jadAttributes.ResetAndDestroy(); |
|
211 SetObjectActive(); |
|
212 } |
|
213 else // all entries in iF1 have scanned |
|
214 { |
|
215 delete iFl; |
|
216 iFl = NULL; |
|
217 |
|
218 // If path was absolute, there wasn't matching JAD file. |
|
219 if (iIsAbsolute || KErrNotFound == iFf.FindWild(iFl)) |
|
220 { |
|
221 // End of loop. |
|
222 iValue = KErrNotFound; |
|
223 iState = EEnd; |
|
224 } |
|
225 |
|
226 iIndex = 0; |
|
227 iAdHocGuess = ETrue; |
|
228 SetObjectActive(); |
|
229 } |
|
230 break; |
|
231 |
|
232 // EEnd Preconditions. - None. Postconditions. - Matching round is |
|
233 // completed. The ExecuteL() activates a new one |
|
234 case EEnd: |
|
235 Cleanup(); |
|
236 NotifyObserver(iValue); |
|
237 } |
|
238 } |
|
239 |
|
240 |
|
241 // --------------------------------------------------------------------------- |
|
242 // To cancel this object activity. This method cancels the JAD |
|
243 // file scanning. |
|
244 // ----------------------------------------------------------------------------- |
|
245 void CJcfJadJarMatcherScanJadFiles::DoCancel() |
|
246 { |
|
247 NotifyObserver(KErrCancel); |
|
248 Cleanup(); |
|
249 } |
|
250 |
|
251 |
|
252 // --------------------------------------------------------------------------- |
|
253 // To complete pending request and set this object active. |
|
254 // ----------------------------------------------------------------------------- |
|
255 void CJcfJadJarMatcherScanJadFiles::SetObjectActive() |
|
256 { |
|
257 TRequestStatus *status = &iStatus; |
|
258 User::RequestComplete(status, KErrNone); |
|
259 if (!IsActive()) |
|
260 { |
|
261 SetActive(); |
|
262 } |
|
263 } |
|
264 |
|
265 |
|
266 // --------------------------------------------------------------------------- |
|
267 // To do clean-up for next round. |
|
268 // ----------------------------------------------------------------------------- |
|
269 void CJcfJadJarMatcherScanJadFiles::Cleanup() |
|
270 { |
|
271 delete iDir; |
|
272 iDir = NULL; |
|
273 delete iFl; |
|
274 iFl = NULL; |
|
275 iFullJarName.reset(0); |
|
276 iAttributes.ResetAndDestroy(); |
|
277 |
|
278 iState = EBegin; |
|
279 } |
|
280 |