|
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: JavaStorageImpl |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "javacommonutils.h" |
|
20 #include "javaoslayer.h" |
|
21 #include "javastorageimpl.h" |
|
22 #include "javastorageexception.h" |
|
23 #include "javastoragenames.h" |
|
24 #include "javauid.h" |
|
25 #include "logger.h" |
|
26 #include "messagedispatcher.h" |
|
27 |
|
28 const int CLOSED = 0; |
|
29 const int OPEN = 1; |
|
30 |
|
31 using namespace java::storage; |
|
32 using namespace java::util; |
|
33 using namespace std; |
|
34 |
|
35 JavaStorageImpl::JavaStorageImpl() |
|
36 : mConnectionStatus(CLOSED), mTransactionStatus(CLOSED) |
|
37 { |
|
38 JELOG2(EJavaStorage); |
|
39 mMsgDispatcher.reset(new MessageDispatcher()); |
|
40 } |
|
41 |
|
42 OS_EXPORT JavaStorageImpl::~JavaStorageImpl() |
|
43 { |
|
44 if (OPEN == mConnectionStatus) |
|
45 { |
|
46 close(); |
|
47 } |
|
48 } |
|
49 |
|
50 OS_EXPORT void JavaStorageImpl::open() throw(JavaStorageException) |
|
51 { |
|
52 open(JAVA_DATABASE_NAME); |
|
53 } |
|
54 |
|
55 OS_EXPORT void JavaStorageImpl::open(const std::string& aStorageName) |
|
56 throw(JavaStorageException) |
|
57 { |
|
58 JELOG2(EJavaStorage); |
|
59 |
|
60 if (CLOSED == mConnectionStatus) |
|
61 { |
|
62 LOG1(EJavaStorage, EInfo, "Opening connection to %s ", |
|
63 aStorageName.c_str()); |
|
64 |
|
65 if (aStorageName == "") |
|
66 { |
|
67 throw JavaStorageException(EInvalidStorage, "Invalid storage name", |
|
68 __FILE__, __FUNCTION__, __LINE__); |
|
69 } |
|
70 |
|
71 mSessionID = mMsgDispatcher->createAndSendMessage( |
|
72 JavaStorageMessage::EOpen, "", aStorageName); |
|
73 |
|
74 if (mSessionID == "") |
|
75 { |
|
76 ELOG(EJavaStorage, "No sessionID received"); |
|
77 throw JavaStorageException(EInternalError, "Internal Server Error", |
|
78 __FILE__, __FUNCTION__, __LINE__); |
|
79 } |
|
80 |
|
81 LOG1(EJavaStorage, EInfo, "Connection sessionID %s ", |
|
82 mSessionID.c_str()); |
|
83 |
|
84 mConnectionStatus = OPEN; |
|
85 } |
|
86 else |
|
87 { |
|
88 WLOG(EJavaStorage, "Connection already open"); |
|
89 } |
|
90 } |
|
91 |
|
92 OS_EXPORT void JavaStorageImpl::close() throw(JavaStorageException) |
|
93 { |
|
94 JELOG2(EJavaStorage); |
|
95 |
|
96 if (OPEN == mConnectionStatus) |
|
97 { |
|
98 string tempSessionId = mSessionID; |
|
99 mSessionID = ""; // Clean in one place. |
|
100 mConnectionStatus = CLOSED; |
|
101 mTransactionStatus = CLOSED; |
|
102 |
|
103 mMsgDispatcher->createAndSendMessage(JavaStorageMessage::EClose, |
|
104 tempSessionId, |
|
105 ""); |
|
106 } |
|
107 else |
|
108 { |
|
109 WLOG(EJavaStorage, "Connection not open"); |
|
110 } |
|
111 } |
|
112 |
|
113 OS_EXPORT void JavaStorageImpl::startTransaction() throw(JavaStorageException) |
|
114 { |
|
115 JELOG2(EJavaStorage); |
|
116 |
|
117 if (OPEN == mConnectionStatus |
|
118 && CLOSED == mTransactionStatus) |
|
119 { |
|
120 mMsgDispatcher->createAndSendMessage( |
|
121 JavaStorageMessage::EStartTransaction, mSessionID, ""); |
|
122 |
|
123 mTransactionStatus = OPEN; |
|
124 } |
|
125 else |
|
126 { |
|
127 WLOG(EJavaStorage, "Cannot open new transaction because \ |
|
128 connection closed or already in trasaction"); |
|
129 } |
|
130 } |
|
131 |
|
132 OS_EXPORT void JavaStorageImpl::commitTransaction() |
|
133 throw(JavaStorageException) |
|
134 { |
|
135 JELOG2(EJavaStorage); |
|
136 |
|
137 if (OPEN == mConnectionStatus && OPEN == mTransactionStatus) |
|
138 { |
|
139 mTransactionStatus = CLOSED; |
|
140 |
|
141 mMsgDispatcher->createAndSendMessage( |
|
142 JavaStorageMessage::ECommit, mSessionID, ""); |
|
143 } |
|
144 else |
|
145 { |
|
146 WLOG(EJavaStorage, |
|
147 "Cannot commit because connection closed or not in trasaction"); |
|
148 } |
|
149 } |
|
150 |
|
151 OS_EXPORT void JavaStorageImpl::rollbackTransaction() |
|
152 throw(JavaStorageException) |
|
153 { |
|
154 JELOG2(EJavaStorage); |
|
155 |
|
156 if (OPEN == mConnectionStatus && OPEN == mTransactionStatus) |
|
157 { |
|
158 mTransactionStatus = CLOSED; |
|
159 |
|
160 mMsgDispatcher->createAndSendMessage( |
|
161 JavaStorageMessage::ERollback, mSessionID, ""); |
|
162 } |
|
163 else |
|
164 { |
|
165 /* |
|
166 * Transaction is reset. If connection is already closed there is no |
|
167 * possibility to recover transaction. SQLite DB will create hot journal |
|
168 * and create auto-rollback at some stage. To prevent that storage |
|
169 * server does rollback if close is called while in transaction. This |
|
170 * way server and client transactions keep in sync. |
|
171 */ |
|
172 mTransactionStatus = CLOSED; |
|
173 WLOG(EJavaStorage, |
|
174 "Cannot rollback because connection closed or not in trasaction"); |
|
175 } |
|
176 } |
|
177 |
|
178 OS_EXPORT void JavaStorageImpl::read(const string& aTablename, |
|
179 const Uid& aUid, |
|
180 JavaStorageApplicationEntry_t& aEntries) |
|
181 throw(JavaStorageException) |
|
182 |
|
183 { |
|
184 JELOG2(EJavaStorage); |
|
185 |
|
186 if (OPEN != mConnectionStatus) |
|
187 { |
|
188 throw JavaStorageException(EInvalidConnection, "No connection", |
|
189 __FILE__, __FUNCTION__, __LINE__); |
|
190 } |
|
191 |
|
192 if (aTablename == "" || aUid.toString() == L"") |
|
193 { |
|
194 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
195 __FILE__, __FUNCTION__, __LINE__); |
|
196 } |
|
197 |
|
198 JavaStorageEntry entry; |
|
199 entry.setEntry(ID, aUid.toString()); |
|
200 aEntries.insert(entry); |
|
201 |
|
202 mMsgDispatcher->createAndSendMessage(JavaStorageMessage::ERead, |
|
203 mSessionID, |
|
204 aTablename, |
|
205 aEntries); |
|
206 } |
|
207 |
|
208 OS_EXPORT void JavaStorageImpl::write( |
|
209 const string& aTablename, |
|
210 const JavaStorageApplicationEntry_t& aEntries) |
|
211 throw(JavaStorageException) |
|
212 { |
|
213 JELOG2(EJavaStorage); |
|
214 |
|
215 if (OPEN != mConnectionStatus) |
|
216 { |
|
217 throw JavaStorageException(EInvalidConnection, "No connection", |
|
218 __FILE__, __FUNCTION__, __LINE__); |
|
219 } |
|
220 |
|
221 if (aTablename == "" || aEntries.size() == 0) |
|
222 { |
|
223 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
224 __FILE__, __FUNCTION__, __LINE__); |
|
225 } |
|
226 |
|
227 mMsgDispatcher->createAndSendMessage(JavaStorageMessage::EWrite, |
|
228 mSessionID, |
|
229 aTablename, |
|
230 aEntries); |
|
231 } |
|
232 |
|
233 OS_EXPORT void JavaStorageImpl::update( |
|
234 const string& aTablename, |
|
235 const JavaStorageApplicationEntry_t& aUpdateEntry, |
|
236 const JavaStorageApplicationEntry_t& aMatchEntry) |
|
237 throw(JavaStorageException) |
|
238 { |
|
239 JELOG2(EJavaStorage); |
|
240 |
|
241 if (OPEN != mConnectionStatus) |
|
242 { |
|
243 throw JavaStorageException(EInvalidConnection, "No connection", |
|
244 __FILE__, __FUNCTION__, __LINE__); |
|
245 } |
|
246 |
|
247 if (aTablename == "" || aUpdateEntry.size() < 1 || aMatchEntry.size() < 1) |
|
248 { |
|
249 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
250 __FILE__, __FUNCTION__, __LINE__); |
|
251 } |
|
252 |
|
253 mMsgDispatcher->handleUpdate(JavaStorageMessage::EUpdate, |
|
254 mSessionID, |
|
255 aTablename, |
|
256 aUpdateEntry, |
|
257 aMatchEntry); |
|
258 } |
|
259 |
|
260 OS_EXPORT void JavaStorageImpl::search( |
|
261 const string& aTablename, |
|
262 const JavaStorageApplicationEntry_t& aSearchPattern, |
|
263 JavaStorageApplicationList_t& aApplicationList) |
|
264 throw(JavaStorageException) |
|
265 { |
|
266 JELOG2(EJavaStorage); |
|
267 |
|
268 if (OPEN != mConnectionStatus) |
|
269 { |
|
270 throw JavaStorageException(EInvalidConnection, "No connection", |
|
271 __FILE__, __FUNCTION__, __LINE__); |
|
272 } |
|
273 |
|
274 if (aTablename == "") |
|
275 { |
|
276 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
277 __FILE__, __FUNCTION__, __LINE__); |
|
278 } |
|
279 |
|
280 mMsgDispatcher->handleSearch(JavaStorageMessage::ESearch, |
|
281 mSessionID, |
|
282 aTablename, |
|
283 aSearchPattern, |
|
284 aApplicationList); |
|
285 } |
|
286 |
|
287 OS_EXPORT int JavaStorageImpl::remove( |
|
288 const std::string& aTablename, |
|
289 const JavaStorageApplicationEntry_t& aRemoveQuery) |
|
290 throw(JavaStorageException) |
|
291 { |
|
292 JELOG2(EJavaStorage); |
|
293 |
|
294 if (aTablename == "") |
|
295 { |
|
296 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
297 __FILE__, __FUNCTION__, __LINE__); |
|
298 } |
|
299 |
|
300 if (OPEN != mConnectionStatus) |
|
301 { |
|
302 throw JavaStorageException(EInvalidConnection, "No connection", |
|
303 __FILE__, __FUNCTION__, __LINE__); |
|
304 } |
|
305 |
|
306 return mMsgDispatcher->createAndSendMessage(JavaStorageMessage::ERemove, |
|
307 mSessionID, |
|
308 aTablename, |
|
309 aRemoveQuery); |
|
310 } |
|
311 |
|
312 OS_EXPORT void JavaStorageImpl::createTable( |
|
313 const string& aTablename, |
|
314 const JavaStorageApplicationEntry_t& aTableColumns) |
|
315 throw(JavaStorageException) |
|
316 { |
|
317 JELOG2(EJavaStorage); |
|
318 |
|
319 if (OPEN != mConnectionStatus) |
|
320 { |
|
321 throw JavaStorageException(EInvalidConnection, "No connection", |
|
322 __FILE__, __FUNCTION__, __LINE__); |
|
323 } |
|
324 |
|
325 if (aTablename == "" || aTableColumns.size() < 1) |
|
326 { |
|
327 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
328 __FILE__, __FUNCTION__, __LINE__); |
|
329 } |
|
330 |
|
331 mMsgDispatcher->createAndSendMessage(JavaStorageMessage::ECreateTable, |
|
332 mSessionID, |
|
333 aTablename, |
|
334 aTableColumns); |
|
335 } |
|
336 |
|
337 OS_EXPORT void JavaStorageImpl::appendTable( |
|
338 const string& aTablename, |
|
339 const JavaStorageApplicationEntry_t& aTableColumns) |
|
340 throw(JavaStorageException) |
|
341 { |
|
342 JELOG2(EJavaStorage); |
|
343 |
|
344 if (OPEN != mConnectionStatus) |
|
345 { |
|
346 throw JavaStorageException(EInvalidConnection, "No connection", |
|
347 __FILE__, __FUNCTION__, __LINE__); |
|
348 } |
|
349 |
|
350 if (aTablename == "" || aTableColumns.size() < 1) |
|
351 { |
|
352 throw JavaStorageException(EInvalidArgument, "Invalid argument", |
|
353 __FILE__, __FUNCTION__, __LINE__); |
|
354 } |
|
355 |
|
356 mMsgDispatcher->createAndSendMessage(JavaStorageMessage::EAppendTable, |
|
357 mSessionID, |
|
358 aTablename, |
|
359 aTableColumns); |
|
360 } |