|
1 /* |
|
2 * Copyright (c) 2008-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: MessageDispatcher |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "commsmessage.h" |
|
20 #include "javacommonutils.h" |
|
21 #include "javadataaccess.h" |
|
22 #include "javastorageexception.h" |
|
23 #include "logger.h" |
|
24 #include "messagedispatcher.h" |
|
25 #include "statementutils.h" |
|
26 |
|
27 using namespace java::comms; |
|
28 using namespace java::storage; |
|
29 using namespace java::util; |
|
30 using namespace std; |
|
31 |
|
32 MessageDispatcher::MessageDispatcher() |
|
33 { |
|
34 JELOG2(EJavaStorage); |
|
35 mDataAccess.reset(JavaDataAccess::createInstance()); |
|
36 mUtils.reset(new StatementUtils()); |
|
37 } |
|
38 |
|
39 MessageDispatcher::~MessageDispatcher() |
|
40 { |
|
41 } |
|
42 |
|
43 std::string MessageDispatcher::createAndSendMessage( |
|
44 JavaStorageMessage::MessageIdentifier aMsgId, |
|
45 const string& aHeaders, |
|
46 const string& aDatabaseName) |
|
47 { |
|
48 JELOG2(EJavaStorage); |
|
49 |
|
50 string sessionID = ""; |
|
51 CommsMessage receivedMessage; |
|
52 int statusCode = -1; |
|
53 |
|
54 string headers = aHeaders; |
|
55 createHeader(aMsgId, headers); |
|
56 |
|
57 switch (aMsgId) |
|
58 { |
|
59 case JavaStorageMessage::EOpen: |
|
60 { |
|
61 mDataAccess->open(headers, aDatabaseName, receivedMessage); |
|
62 |
|
63 receivedMessage>>statusCode; |
|
64 |
|
65 if (statusCode >= 0) |
|
66 { |
|
67 receivedMessage>>sessionID; |
|
68 LOG1(EJavaStorage, EInfo, "Received sessionID: %s", |
|
69 sessionID.c_str()); |
|
70 } |
|
71 break; |
|
72 } |
|
73 case JavaStorageMessage::EClose: |
|
74 { |
|
75 mDataAccess->close(headers, receivedMessage); |
|
76 receivedMessage>>statusCode; |
|
77 break; |
|
78 } |
|
79 case JavaStorageMessage::EStartTransaction: |
|
80 { |
|
81 mDataAccess->execute(headers, L"BEGIN;", receivedMessage); |
|
82 receivedMessage>>statusCode; |
|
83 break; |
|
84 } |
|
85 case JavaStorageMessage::ECommit: |
|
86 { |
|
87 mDataAccess->execute(headers, L"COMMIT;", receivedMessage); |
|
88 receivedMessage>>statusCode; |
|
89 break; |
|
90 } |
|
91 case JavaStorageMessage::ERollback: |
|
92 { |
|
93 mDataAccess->execute(headers, L"ROLLBACK;", receivedMessage); |
|
94 receivedMessage>>statusCode; |
|
95 break; |
|
96 } |
|
97 default: |
|
98 { |
|
99 ELOG1(EJavaStorage, "Unknown command: %d", aMsgId); |
|
100 throw JavaStorageException(aMsgId, "Unknown message", |
|
101 __FILE__, __FUNCTION__, __LINE__); |
|
102 } |
|
103 } |
|
104 |
|
105 if (statusCode < 0) |
|
106 { |
|
107 string errorMessage = ""; |
|
108 readErrorMessage(receivedMessage, errorMessage); |
|
109 throw JavaStorageException(statusCode, errorMessage.c_str(), |
|
110 __FILE__, __FUNCTION__, __LINE__); |
|
111 } |
|
112 |
|
113 return sessionID; |
|
114 } |
|
115 |
|
116 int MessageDispatcher::createAndSendMessage( |
|
117 JavaStorageMessage::MessageIdentifier aMsgId, |
|
118 const string& aHeaders, |
|
119 const string& identifier, |
|
120 const JavaStorageApplicationEntry_t& aEntry) |
|
121 { |
|
122 JELOG2(EJavaStorage); |
|
123 |
|
124 CommsMessage receivedMessage; |
|
125 int statusCode = -1; |
|
126 int retVal = -1; |
|
127 |
|
128 string headers = aHeaders; |
|
129 createHeader(aMsgId, headers); |
|
130 |
|
131 wstring insertion(L", "); |
|
132 |
|
133 switch (aMsgId) |
|
134 { |
|
135 case JavaStorageMessage::EWrite: |
|
136 { |
|
137 wstring sqlStatement = L""; |
|
138 mUtils->createWriteStatement( |
|
139 aEntry, identifier, insertion, sqlStatement); |
|
140 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
141 |
|
142 receivedMessage>>statusCode; |
|
143 break; |
|
144 } |
|
145 case JavaStorageMessage::ERead: |
|
146 { |
|
147 // Contains only one entry. It is already checked. |
|
148 JavaStorageApplicationEntry_t::const_iterator iter = aEntry.begin(); |
|
149 |
|
150 wstring sqlStatement = L"SELECT * FROM "; |
|
151 sqlStatement.append( |
|
152 JavaCommonUtils::utf8ToWstring(identifier.c_str())); |
|
153 sqlStatement.append(L" WHERE ID = '") |
|
154 .append((*iter).entryValue()).append(L"';"); |
|
155 |
|
156 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
157 |
|
158 receivedMessage>>statusCode; |
|
159 |
|
160 if (statusCode >= 0) |
|
161 { |
|
162 // Const cast required. This prevent need to create yet another |
|
163 // createAndSend subroutine. |
|
164 JavaStorageApplicationEntry_t& temp = |
|
165 const_cast<JavaStorageApplicationEntry_t&>(aEntry); |
|
166 |
|
167 // Value was added by the API so remove that it does not exists |
|
168 // in the response twice. |
|
169 temp.clear(); |
|
170 populateEntry(receivedMessage, temp); |
|
171 } |
|
172 break; |
|
173 } |
|
174 case JavaStorageMessage::ERemove: |
|
175 { |
|
176 wstring sqlStatement = L"DELETE FROM "; |
|
177 |
|
178 sqlStatement.append( |
|
179 JavaCommonUtils::utf8ToWstring(identifier.c_str())); |
|
180 |
|
181 if (aEntry.size() > 0) |
|
182 { |
|
183 mUtils->createWhereStatement(aEntry, sqlStatement); |
|
184 } |
|
185 else // Delete whole table contents. |
|
186 { |
|
187 sqlStatement.append(L";"); |
|
188 } |
|
189 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
190 |
|
191 receivedMessage>>statusCode; |
|
192 |
|
193 if (statusCode >= 0) |
|
194 { |
|
195 wstring temp = L""; |
|
196 receivedMessage>>temp; |
|
197 // How many entries removed i.e. rows removed. |
|
198 retVal = JavaCommonUtils::wstringToInt(temp); |
|
199 } |
|
200 break; |
|
201 } |
|
202 case JavaStorageMessage::ECreateTable: |
|
203 { |
|
204 wstring sqlStatement = L""; |
|
205 mUtils->createTableStatement( |
|
206 aEntry, identifier, insertion, sqlStatement); |
|
207 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
208 receivedMessage>>statusCode; |
|
209 break; |
|
210 } |
|
211 case JavaStorageMessage::EAppendTable: |
|
212 { |
|
213 // NOTE: SQLite supports adding only one column at the time. |
|
214 // For this reason several calls need to be executed. |
|
215 |
|
216 wstring prefix = L"ALTER TABLE "; |
|
217 |
|
218 prefix.append( |
|
219 JavaCommonUtils::utf8ToWstring(identifier.c_str())); |
|
220 |
|
221 // Define added columns |
|
222 // ID will always be primary key |
|
223 prefix.append(L" ADD "); |
|
224 |
|
225 JavaStorageApplicationEntry_t::const_iterator colIter; |
|
226 |
|
227 for (colIter = aEntry.begin(); colIter != aEntry.end(); colIter++) |
|
228 { |
|
229 if ((*colIter).entryName() == L"") |
|
230 { |
|
231 throw JavaStorageException(EInvalidDataStructure, |
|
232 "Column name not defined", |
|
233 __FILE__, __FUNCTION__, __LINE__); |
|
234 } |
|
235 |
|
236 wstring sqlStatement = prefix; |
|
237 sqlStatement.append((*colIter).entryName()); |
|
238 |
|
239 if (JavaStorageEntry::STRING == (*colIter).entryType()) |
|
240 { |
|
241 sqlStatement.append(L" varchar"); |
|
242 } |
|
243 else if (JavaStorageEntry::INT == (*colIter).entryType()) |
|
244 { |
|
245 sqlStatement.append(L" int"); |
|
246 } |
|
247 else |
|
248 { |
|
249 ELOG(EJavaStorage, "Unknown column type"); |
|
250 throw JavaStorageException(EInvalidDataStructure, |
|
251 "Unknown column type", |
|
252 __FILE__, __FUNCTION__, __LINE__); |
|
253 } |
|
254 sqlStatement.append(L";"); |
|
255 |
|
256 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
257 |
|
258 receivedMessage>>statusCode; |
|
259 |
|
260 if (statusCode < 0) |
|
261 { |
|
262 break; // stop execution on first error. |
|
263 } |
|
264 else |
|
265 { |
|
266 // Reset for next round |
|
267 receivedMessage.reset(); |
|
268 } |
|
269 } |
|
270 break; |
|
271 } |
|
272 default: |
|
273 { |
|
274 ELOG1(EJavaStorage, "Unknown command: %d", aMsgId); |
|
275 throw JavaStorageException(aMsgId, "Unknown message", |
|
276 __FILE__, __FUNCTION__, __LINE__); |
|
277 } |
|
278 } |
|
279 |
|
280 if (statusCode < 0) |
|
281 { |
|
282 string errorMessage = ""; |
|
283 readErrorMessage(receivedMessage, errorMessage); |
|
284 throw JavaStorageException(statusCode, errorMessage.c_str(), |
|
285 __FILE__, __FUNCTION__, __LINE__); |
|
286 } |
|
287 return retVal; |
|
288 } |
|
289 |
|
290 void MessageDispatcher::handleSearch( |
|
291 JavaStorageMessage::MessageIdentifier aMsgId, |
|
292 const string& aHeaders, |
|
293 const string& aTableName, |
|
294 const JavaStorageApplicationEntry_t& aEntry, |
|
295 JavaStorageApplicationList_t& aAppList |
|
296 ) |
|
297 { |
|
298 int statusCode = -1; |
|
299 CommsMessage receivedMessage; |
|
300 |
|
301 string headers = aHeaders; |
|
302 createHeader(aMsgId, headers); |
|
303 |
|
304 wstring sqlStatement = L""; |
|
305 mUtils->createSearchStatement(aEntry, aTableName, sqlStatement); |
|
306 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
307 |
|
308 receivedMessage>>statusCode; |
|
309 |
|
310 if (statusCode >= 0) |
|
311 { |
|
312 populateEntry(receivedMessage, aAppList); |
|
313 } |
|
314 else |
|
315 { |
|
316 string errorMessage = ""; |
|
317 readErrorMessage(receivedMessage, errorMessage); |
|
318 throw JavaStorageException(statusCode, errorMessage.c_str(), |
|
319 __FILE__, __FUNCTION__, __LINE__); |
|
320 } |
|
321 } |
|
322 |
|
323 void MessageDispatcher::handleUpdate( |
|
324 JavaStorageMessage::MessageIdentifier aMsgId, |
|
325 const string& aHeaders, |
|
326 const string& identifier, |
|
327 const JavaStorageApplicationEntry_t& aUpdateEntry, |
|
328 const JavaStorageApplicationEntry_t& aMatchEntry) |
|
329 { |
|
330 int statusCode = -1; |
|
331 CommsMessage receivedMessage; |
|
332 string headers = aHeaders; |
|
333 createHeader(aMsgId, headers); |
|
334 |
|
335 wstring sqlStatement = L""; |
|
336 mUtils->createUpdateStatement( |
|
337 aUpdateEntry, aMatchEntry, identifier, sqlStatement); |
|
338 |
|
339 mDataAccess->execute(headers, sqlStatement, receivedMessage); |
|
340 receivedMessage>>statusCode; |
|
341 |
|
342 if (statusCode < 0) |
|
343 { |
|
344 string errorMessage = ""; |
|
345 readErrorMessage(receivedMessage, errorMessage); |
|
346 throw JavaStorageException(statusCode, errorMessage.c_str(), |
|
347 __FILE__, __FUNCTION__, __LINE__); |
|
348 } |
|
349 } |
|
350 |
|
351 void MessageDispatcher::populateEntry(CommsMessage& aReceivedMessage, |
|
352 JavaStorageApplicationEntry_t& aEntry) |
|
353 { |
|
354 JELOG2(EJavaStorage); |
|
355 |
|
356 wstring data; |
|
357 aReceivedMessage>>data; |
|
358 |
|
359 wstring::size_type startIdx = 0; |
|
360 wstring::size_type endIdx = 0; |
|
361 wstring appDelim = L";#\n;"; |
|
362 wstring attrDelim = L";\n;"; |
|
363 |
|
364 // Skipp app delimiter |
|
365 if (data.find(appDelim) == 0) |
|
366 { |
|
367 startIdx = appDelim.size(); |
|
368 endIdx = startIdx; |
|
369 } |
|
370 |
|
371 // This method reads only one storage row. If multiple ones are given |
|
372 // they are ignored. |
|
373 if ((endIdx = data.find(appDelim, startIdx)) != string::npos) |
|
374 { |
|
375 WLOG(EJavaStorage, "Removing multiple app occurrences"); |
|
376 data = data.substr(0, endIdx); |
|
377 } |
|
378 |
|
379 // Reset counter |
|
380 endIdx = startIdx; |
|
381 |
|
382 wstring nameDelim = L"="; |
|
383 |
|
384 while (string::npos != startIdx || string::npos != endIdx) |
|
385 { |
|
386 // Read name |
|
387 endIdx = data.find(nameDelim, startIdx); |
|
388 if (endIdx == string::npos) break; // Not found |
|
389 |
|
390 wstring name = data.substr(startIdx, (endIdx - startIdx)); |
|
391 |
|
392 // Read value. Skip name and delimiter |
|
393 startIdx = endIdx + 1; |
|
394 endIdx = data.find(attrDelim, startIdx); |
|
395 if (endIdx == string::npos) break; // Not found |
|
396 |
|
397 wstring value = L""; |
|
398 |
|
399 if (endIdx != startIdx) |
|
400 { |
|
401 value = data.substr(startIdx, (endIdx - startIdx)); |
|
402 } |
|
403 // else empty value. |
|
404 |
|
405 JavaStorageEntry entry; |
|
406 entry.setEntry(name, value); |
|
407 aEntry.insert(entry); |
|
408 |
|
409 // Skip value and delimiter |
|
410 startIdx = endIdx + attrDelim.size(); |
|
411 if (startIdx == string::npos) break; // Not found |
|
412 } |
|
413 } |
|
414 |
|
415 void MessageDispatcher::populateEntry(CommsMessage& aReceivedMessage, |
|
416 JavaStorageApplicationList_t& aAppList) |
|
417 { |
|
418 JELOG2(EJavaStorage); |
|
419 |
|
420 wstring data; |
|
421 aReceivedMessage>>data; |
|
422 |
|
423 // ################# TEMP ################################################# |
|
424 // LOG1WSTR(EJavaStorage, EInfo, "Response: %s", data); |
|
425 // ################# END OF TEMP ########################################## |
|
426 |
|
427 wstring appDelim = L";#\n;"; |
|
428 |
|
429 if (data.size() <= appDelim.size()) |
|
430 { |
|
431 LOG(EJavaStorage, EInfo, "No entries to populate"); |
|
432 return; |
|
433 } |
|
434 |
|
435 wstring::size_type startIdx = appDelim.size(); |
|
436 wstring::size_type endIdx = startIdx; |
|
437 |
|
438 // This does two if else less within while loop as its known string |
|
439 // terminates always despite of column amount. |
|
440 data.append(appDelim); |
|
441 |
|
442 JavaStorageApplicationEntry_t appEntry; |
|
443 |
|
444 do |
|
445 { |
|
446 endIdx = data.find(appDelim, startIdx); |
|
447 |
|
448 if (endIdx == string::npos) break; // No token found |
|
449 |
|
450 wstring appString = data.substr(startIdx, (endIdx - startIdx)); |
|
451 |
|
452 CommsMessage oneApp; |
|
453 oneApp<<appString; |
|
454 populateEntry(oneApp, appEntry); |
|
455 |
|
456 aAppList.push_back(appEntry); |
|
457 appEntry.clear(); |
|
458 |
|
459 // Skip app delimiter |
|
460 startIdx = endIdx + appDelim.size(); |
|
461 } |
|
462 while (string::npos != startIdx && string::npos != endIdx); |
|
463 } |
|
464 |
|
465 void MessageDispatcher::createHeader(int aMsgId, string& aHeaders) |
|
466 { |
|
467 // Add command ID that server can identify open action |
|
468 string msgID = JavaCommonUtils::intToString(aMsgId); |
|
469 string msgIDSize = JavaCommonUtils::intToString(msgID.size()); |
|
470 // Insert size which must be fit to one char. |
|
471 aHeaders.insert(0, msgIDSize); |
|
472 // Insert msgID |
|
473 aHeaders.insert(1, msgID); |
|
474 } |
|
475 |
|
476 void MessageDispatcher::readErrorMessage(CommsMessage& aReceivedMessage, |
|
477 string& aErrMsg) |
|
478 { |
|
479 wstring wideErrorMsg; |
|
480 aReceivedMessage>>wideErrorMsg; |
|
481 |
|
482 char* errorMsg = JavaCommonUtils::wstringToUtf8(wideErrorMsg); |
|
483 string errorMessage(errorMsg); |
|
484 delete[] errorMsg; |
|
485 aErrMsg = errorMessage; |
|
486 ELOG1(EJavaStorage, "Error message %s", aErrMsg.c_str()); |
|
487 } |