|
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: The JNI code for Java Installer component |
|
15 * JadJarMatcher. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <e32std.h> |
|
21 #include <f32file.h> |
|
22 |
|
23 #include "com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher.h" |
|
24 #include "jadjarmatcherscanfrominbox.h" |
|
25 #include "javacommonutils.h" |
|
26 #include "logger.h" |
|
27 |
|
28 // NAMESPACE DECLARATION |
|
29 using namespace java; |
|
30 |
|
31 /** |
|
32 * See JNI methods __1findJadFilesFromInbox, __1findJarFilesFromInbox. |
|
33 * This is just internal wrapper method for handling Leaves. |
|
34 */ |
|
35 jint FindFilesFromInboxL |
|
36 (JNIEnv *aEnv, jclass, jstring aStartFilename, jobject aFiles, jobject aHandles, jobject aTimestamps, TBool aJarScan) |
|
37 { |
|
38 LOG1(EJavaInstaller, EInfo, "FindFilesFromInboxL: aJarScan %d", aJarScan); |
|
39 |
|
40 RArray<RFile> resultArray; |
|
41 CJadJarMatcherScanFromInbox* scanInbox = |
|
42 new(ELeave) CJadJarMatcherScanFromInbox(); |
|
43 CleanupStack::PushL(scanInbox); |
|
44 |
|
45 // Copy the characters in jstrings to Symbian descriptors so that |
|
46 // they can be used in S60 code. |
|
47 const jchar* startFilename = aEnv->GetStringChars(aStartFilename, 0); |
|
48 if (NULL == startFilename) |
|
49 { |
|
50 ELOG(EJavaInstaller, |
|
51 "Cannot scan Inbox, getting filename failed in JNI"); |
|
52 return KErrArgument; |
|
53 } |
|
54 HBufC16* unicodeStartFilename = |
|
55 HBufC16::New(aEnv->GetStringLength(aStartFilename) + 1); |
|
56 if (NULL == unicodeStartFilename) |
|
57 { |
|
58 ELOG(EJavaInstaller, |
|
59 "Cannot scan inbox, filename allocation failed in JNI"); |
|
60 return KErrNoMemory; |
|
61 } |
|
62 CleanupStack::PushL(unicodeStartFilename); |
|
63 *unicodeStartFilename = startFilename; |
|
64 aEnv->ReleaseStringChars(aStartFilename, startFilename); |
|
65 |
|
66 // Start Inbox scan. |
|
67 TRAPD(err, scanInbox->ScanFromInboxL( |
|
68 unicodeStartFilename, resultArray, aJarScan)); |
|
69 if (KErrNone != err) |
|
70 { |
|
71 ELOG1(EJavaInstaller, |
|
72 "FindFilesFromInboxL failed with code %d", err); |
|
73 } |
|
74 else |
|
75 { |
|
76 LOG1(EJavaInstaller, EInfo, |
|
77 "FindFilesFromInboxL returns %d matches", resultArray.Count()); |
|
78 jclass classFiles = aEnv->GetObjectClass(aFiles); |
|
79 jmethodID methodFilesAddElement = aEnv->GetMethodID( |
|
80 classFiles, "addElement", "(Ljava/lang/Object;)V"); |
|
81 jclass classHandles = aEnv->GetObjectClass(aHandles); |
|
82 jmethodID methodHandlesAddElement = aEnv->GetMethodID( |
|
83 classHandles, "addElement", "(Ljava/lang/Object;)V"); |
|
84 jclass classTimestamps = aEnv->GetObjectClass(aTimestamps); |
|
85 jmethodID methodTimestampsAddElement = aEnv->GetMethodID( |
|
86 classHandles, "addElement", "(Ljava/lang/Object;)V"); |
|
87 jclass integerClass = aEnv->FindClass("java/lang/Integer"); |
|
88 jmethodID integerConstructor = aEnv->GetMethodID( |
|
89 integerClass, "<init>", "(I)V"); |
|
90 jclass longClass = aEnv->FindClass("java/lang/Long"); |
|
91 jmethodID longConstructor = aEnv->GetMethodID( |
|
92 longClass, "<init>", "(J)V"); |
|
93 for (TInt i = 0; i < resultArray.Count(); i++) |
|
94 { |
|
95 // Add results to Java side Vector (aFiles parameter). |
|
96 TFileName fileName; |
|
97 resultArray[i].FullName(fileName); |
|
98 jstring jFileName = aEnv->NewString( |
|
99 (const unsigned short*)(fileName.Ptr()), fileName.Length()); |
|
100 aEnv->CallVoidMethod(aFiles, methodFilesAddElement, jFileName); |
|
101 // Add results to Java side Vector (aHandles parameter). |
|
102 // Caller must close the file handles returned in resultArray. |
|
103 jobject handle = aEnv->NewObject( |
|
104 integerClass, integerConstructor, &resultArray[i]); |
|
105 aEnv->CallVoidMethod(aHandles, methodHandlesAddElement, handle); |
|
106 // Add results to Java side Vector (aTimestamps parameter). |
|
107 TInt64 time = 0; |
|
108 TTime ttime; |
|
109 TInt timeErr = resultArray[i].Modified(ttime); |
|
110 if (KErrNone == timeErr) |
|
111 { |
|
112 time = ttime.Int64(); |
|
113 } |
|
114 else |
|
115 { |
|
116 ELOG1(EJavaInstaller, |
|
117 "FindFilesFromInboxL failed to get file timestamp, err=%d", |
|
118 timeErr); |
|
119 } |
|
120 jobject timestamp = aEnv->NewObject(longClass, longConstructor, time); |
|
121 aEnv->CallVoidMethod(aTimestamps, methodTimestampsAddElement, timestamp); |
|
122 } |
|
123 } |
|
124 |
|
125 CleanupStack::PopAndDestroy(unicodeStartFilename); |
|
126 CleanupStack::PopAndDestroy(scanInbox); |
|
127 return err; |
|
128 } |
|
129 |
|
130 /** |
|
131 * See JNI methods __1findJadFilesFromInbox, __1findJarFilesFromInbox. |
|
132 * This is just internal wrapper method for handling Leaves. |
|
133 */ |
|
134 jint CopyFileL(JNIEnv *aEnv, jclass, jint aHandle, jstring aFilename) |
|
135 { |
|
136 __UHEAP_MARK; |
|
137 RFile* file = reinterpret_cast<RFile*>(aHandle); |
|
138 |
|
139 // Copy the characters in jstrings to Symbian descriptors so that |
|
140 // they can be used in S60 code. |
|
141 const jchar* filename = aEnv->GetStringChars(aFilename, 0); |
|
142 if (NULL == filename) |
|
143 { |
|
144 ELOG(EJavaInstaller, |
|
145 "JadJarMatcher: Cannot copy file, getting filename failed in JNI"); |
|
146 return KErrArgument; |
|
147 } |
|
148 HBufC16* targetFilename = |
|
149 HBufC16::New(aEnv->GetStringLength(aFilename) + 1); |
|
150 if (NULL == targetFilename) |
|
151 { |
|
152 ELOG(EJavaInstaller, |
|
153 "JadJarMatcher: Cannot copy file, filename allocation failed in JNI"); |
|
154 return KErrNoMemory; |
|
155 } |
|
156 CleanupStack::PushL(targetFilename); |
|
157 *targetFilename = filename; |
|
158 aEnv->ReleaseStringChars(aFilename, filename); |
|
159 |
|
160 //TFileName sourceFilename; |
|
161 //file->FullName( sourceFilename ); |
|
162 //LOG1WSTR( EJavaInstaller, EInfo, "Copying file %s", |
|
163 // (wchar_t *)(sourceFilename.PtrZ())); |
|
164 //LOG1WSTR( EJavaInstaller, EInfo, "to %s", |
|
165 // (wchar_t *)(targetFilename->Des().PtrZ())); |
|
166 |
|
167 RFs fs; |
|
168 User::LeaveIfError(fs.Connect()); |
|
169 CleanupClosePushL(fs); |
|
170 CFileMan* fileMan = CFileMan::NewL(fs); |
|
171 CleanupStack::PushL(fileMan); |
|
172 TInt err = fileMan->Copy(*file, *targetFilename); |
|
173 CleanupStack::PopAndDestroy(fileMan); |
|
174 CleanupStack::PopAndDestroy(&fs); |
|
175 |
|
176 CleanupStack::PopAndDestroy(targetFilename); |
|
177 __UHEAP_MARKEND; |
|
178 return err; |
|
179 } |
|
180 |
|
181 /* |
|
182 * Class: com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher |
|
183 * Method: _findJadFilesFromInbox |
|
184 */ |
|
185 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher__1findJadFilesFromInbox |
|
186 (JNIEnv *aEnv, jclass aClass, jstring aJar, jobject aFiles, jobject aHandles, jobject aTimestamps) |
|
187 { |
|
188 TInt result = KErrNone; |
|
189 TRAPD(err, result = FindFilesFromInboxL(aEnv, aClass, aJar, aFiles, aHandles, aTimestamps, EFalse)); |
|
190 if (KErrNone == err) |
|
191 { |
|
192 err = result; |
|
193 } |
|
194 return err; |
|
195 } |
|
196 |
|
197 /* |
|
198 * Class: com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher |
|
199 * Method: _findJarFilesFromInbox |
|
200 */ |
|
201 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher__1findJarFilesFromInbox |
|
202 (JNIEnv *aEnv, jclass aClass, jstring aJad, jobject aFiles, jobject aHandles, jobject aTimestamps) |
|
203 { |
|
204 TInt result = KErrNone; |
|
205 TRAPD(err, result = FindFilesFromInboxL(aEnv, aClass, aJad, aFiles, aHandles, aTimestamps, ETrue)); |
|
206 if (KErrNone == err) |
|
207 { |
|
208 err = result; |
|
209 } |
|
210 return err; |
|
211 } |
|
212 |
|
213 /* |
|
214 * Class: com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher |
|
215 * Method: _copyFile |
|
216 */ |
|
217 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher__1copyFile |
|
218 (JNIEnv *aEnv, jclass aClass, jint aHandle, jstring aFilename) |
|
219 { |
|
220 TInt result = KErrNone; |
|
221 TRAPD(err, result = CopyFileL(aEnv, aClass, aHandle, aFilename)); |
|
222 if (KErrNone == err) |
|
223 { |
|
224 err = result; |
|
225 } |
|
226 return err; |
|
227 } |
|
228 |
|
229 /* |
|
230 * Class: com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher |
|
231 * Method: _closeFileHandles |
|
232 */ |
|
233 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_jadjarmatcher_JadJarMatcher__1closeFileHandles |
|
234 (JNIEnv *aEnv, jclass /*aClass*/, jintArray aHandles) |
|
235 { |
|
236 TInt count = aEnv->GetArrayLength(aHandles); |
|
237 jint* handles = aEnv->GetIntArrayElements(aHandles, NULL); |
|
238 for (TInt i = 0; i < count; i++) |
|
239 { |
|
240 RFile* file = reinterpret_cast<RFile*>(handles[i]); |
|
241 file->Close(); |
|
242 } |
|
243 aEnv->ReleaseIntArrayElements(aHandles, handles, 0); |
|
244 LOG1(EJavaInstaller, EInfo, "JadJarMatcher: Closed %d Inbox files", count); |
|
245 return KErrNone; |
|
246 } |