1 /* |
|
2 * Copyright (c) 2010 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.mj.impl.cenrep; |
|
19 |
|
20 import com.nokia.mid.cenrep.CentralRepository; |
|
21 import com.nokia.mid.cenrep.CentralRepositoryException; |
|
22 import com.nokia.mj.impl.gcf.utils.NativeError; |
|
23 import com.nokia.mj.impl.rt.support.ApplicationInfo; |
|
24 import com.nokia.mj.impl.utils.Logger; |
|
25 import com.nokia.mj.impl.rt.support.Finalizer; |
|
26 import com.nokia.mj.impl.rt.support.Jvm; |
|
27 |
|
28 /** |
|
29 * CentralRepositoryImpl implements the functionality specified in |
|
30 * com.nokia.mid.cenrep.CentralRepository class. |
|
31 */ |
|
32 public class CentralRepositoryImpl extends CentralRepository |
|
33 { |
|
34 |
|
35 static |
|
36 { |
|
37 try |
|
38 { |
|
39 Jvm.loadSystemLibrary("javacentrep"); |
|
40 } |
|
41 catch (Exception e) |
|
42 { |
|
43 Logger.ELOG(Logger.EJavaCentrep, |
|
44 "Unable to load javacentrep dll."); |
|
45 } |
|
46 } |
|
47 |
|
48 private static final int INITIAL = 0; |
|
49 |
|
50 private static final int OPEN = 1; |
|
51 |
|
52 private static final int CLOSED = 2; |
|
53 |
|
54 /* |
|
55 * Central Repository API Function server. |
|
56 */ |
|
57 private int iFunctionSourceHandle; |
|
58 |
|
59 /** |
|
60 * Handle to the native side peer object. |
|
61 */ |
|
62 private int iCenrepHandle = 0; |
|
63 |
|
64 /** |
|
65 * Repository ID |
|
66 */ |
|
67 private String iRepositoryId = null; |
|
68 |
|
69 private Finalizer iFinalizer; |
|
70 |
|
71 private int iState; |
|
72 |
|
73 /** |
|
74 * Hidden default constructor. |
|
75 */ |
|
76 private CentralRepositoryImpl() |
|
77 { |
|
78 } |
|
79 |
|
80 /** |
|
81 * Hidden constructor. |
|
82 * |
|
83 * @param repositoryId it is platform specific and in S60 it is |
|
84 * Symbian Central Repository UID. |
|
85 */ |
|
86 private CentralRepositoryImpl(String repositoryId) |
|
87 throws CentralRepositoryException |
|
88 { |
|
89 checkAccess(); |
|
90 iState = INITIAL; |
|
91 int cenrepUid = CentralRepositoryUid.getIntValue(repositoryId); |
|
92 this.iRepositoryId = repositoryId; |
|
93 iFinalizer = registerFinalize(); |
|
94 |
|
95 iFunctionSourceHandle = _createFunctionSource(); |
|
96 iCenrepHandle = _createNativePeer(iFunctionSourceHandle, cenrepUid); |
|
97 iState = OPEN; |
|
98 } |
|
99 |
|
100 /** |
|
101 * See class CentralRepository for comments. |
|
102 */ |
|
103 static public CentralRepository open(String repositoryId) |
|
104 throws CentralRepositoryException |
|
105 { |
|
106 return new CentralRepositoryImpl(repositoryId); |
|
107 } |
|
108 |
|
109 /** |
|
110 * See class CentralRepository for comments. |
|
111 */ |
|
112 public void close() throws CentralRepositoryException |
|
113 { |
|
114 synchronized (this) |
|
115 { |
|
116 |
|
117 if (iState != CLOSED) |
|
118 { |
|
119 iState = CLOSED; |
|
120 _close(iFunctionSourceHandle, iCenrepHandle); |
|
121 _dispose(iFunctionSourceHandle, iCenrepHandle); |
|
122 } |
|
123 iRepositoryId = null; |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * See class CentralRepository for comments. |
|
129 */ |
|
130 public String getString(String key) |
|
131 throws CentralRepositoryException |
|
132 { |
|
133 synchronized (this) |
|
134 { |
|
135 |
|
136 if (iState == CLOSED) |
|
137 { |
|
138 throw new CentralRepositoryException("Connection Already Closed"); |
|
139 } |
|
140 long cenrepKey = CentralRepositoryKey.getLongValue(key); |
|
141 String value = _getString(iFunctionSourceHandle, iCenrepHandle, cenrepKey); |
|
142 return value; |
|
143 } |
|
144 } |
|
145 |
|
146 /** |
|
147 * See class CentralRepository for comments. |
|
148 */ |
|
149 public int getInt(String key) |
|
150 throws CentralRepositoryException |
|
151 { |
|
152 synchronized (this) |
|
153 { |
|
154 |
|
155 if (iState == CLOSED) |
|
156 { |
|
157 throw new CentralRepositoryException("Connection Already Closed"); |
|
158 } |
|
159 long cenrepKey = CentralRepositoryKey.getLongValue(key); |
|
160 int res = _getInt(iFunctionSourceHandle, iCenrepHandle, cenrepKey); |
|
161 return res; |
|
162 } |
|
163 } |
|
164 |
|
165 /** |
|
166 * See class CentralRepository for comments. |
|
167 */ |
|
168 public void setString(String key, String value) |
|
169 throws CentralRepositoryException |
|
170 { |
|
171 synchronized (this) |
|
172 { |
|
173 |
|
174 if (iState == CLOSED) |
|
175 { |
|
176 throw new CentralRepositoryException("Connection Already Closed"); |
|
177 } |
|
178 long cenrepKey = CentralRepositoryKey.getLongValue(key); |
|
179 if (value == null) |
|
180 { |
|
181 throw new CentralRepositoryException("Value is null"); |
|
182 } |
|
183 _setString(iFunctionSourceHandle, iCenrepHandle, cenrepKey, value); |
|
184 } |
|
185 } |
|
186 |
|
187 /** |
|
188 * See class CentralRepository for comments. |
|
189 */ |
|
190 public void setInt(String key, int value) |
|
191 throws CentralRepositoryException |
|
192 { |
|
193 synchronized (this) |
|
194 { |
|
195 |
|
196 if (iState == CLOSED) |
|
197 { |
|
198 throw new CentralRepositoryException("Connection Already Closed"); |
|
199 } |
|
200 long cenrepKey = CentralRepositoryKey.getLongValue(key); |
|
201 _setInt(iFunctionSourceHandle, iCenrepHandle, cenrepKey, value); |
|
202 } |
|
203 } |
|
204 |
|
205 /** |
|
206 * Registers with Finalizer to call a method when the object gets collected |
|
207 * by GC |
|
208 * |
|
209 * @return Finalizer object that will be notified when GC happens |
|
210 */ |
|
211 private Finalizer registerFinalize() |
|
212 { |
|
213 return new Finalizer() |
|
214 { |
|
215 public void finalizeImpl() |
|
216 { |
|
217 registeredFinalize(); |
|
218 } |
|
219 }; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Called when the object is finalized by the garbage collector. |
|
224 */ |
|
225 final void registeredFinalize() |
|
226 { |
|
227 try |
|
228 { |
|
229 close(); |
|
230 } |
|
231 catch (CentralRepositoryException e) |
|
232 { |
|
233 // Ignore |
|
234 } |
|
235 } |
|
236 |
|
237 /** |
|
238 * Creates a native side function source. |
|
239 * |
|
240 * @return handle to the native function source. |
|
241 */ |
|
242 private native int _createFunctionSource(); |
|
243 |
|
244 /** |
|
245 * Create native side peer of this Java class. It opens a repository. |
|
246 * |
|
247 * @param FunctionSource Handle handle to the native function source. |
|
248 * @param arepositoryId is platform specific and in S60 it is |
|
249 * Symbian Central Repository UID. |
|
250 * @return handle to the native side peer. |
|
251 * @return Symbian OS error code, if openning fails. |
|
252 */ |
|
253 private native int _createNativePeer( |
|
254 int aFunctionSourceHandle, |
|
255 int repositoryId); |
|
256 |
|
257 /** |
|
258 * Closes a repository. |
|
259 * |
|
260 * @param FunctionSourceHandle handle to the native function source. |
|
261 * @param cenrepHandle handle to the native side peer object. |
|
262 * @return Symbian OS error code, if closing fails. |
|
263 */ |
|
264 private native void _close(int aFunctionSourceHandle, int cenrepHandle); |
|
265 |
|
266 /** |
|
267 * Dispose native side resources owned by this class. |
|
268 * |
|
269 * @param FunctionSourceHandle handle to the native function source. |
|
270 * @param cenrepHandle handle to the native side peer object. |
|
271 */ |
|
272 private native void _dispose(int aFunctionSourceHandle, int cenrepHandle); |
|
273 |
|
274 /** |
|
275 * Returns string stored in given key. |
|
276 * |
|
277 * @param FunctionSourceHandle handle to the native function source. |
|
278 * @param cenrepHandle handle to the native side peer object. |
|
279 * @param key the key of setting to be read. |
|
280 * @param value returns the value of the setting if it is the string. |
|
281 * @return Symbian OS error code, if key is not found or |
|
282 * stored data is not string. |
|
283 */ |
|
284 private native String _getString( |
|
285 int aFunctionSourceHandle, |
|
286 int cenrepHandle, |
|
287 long key); |
|
288 |
|
289 /** |
|
290 * Returns integer stored in given key. |
|
291 * |
|
292 * @param FunctionSourceHandle handle to the native function source. |
|
293 * @param cenrepHandle handle to the native side peer object. |
|
294 * @param key the key of setting to be read. |
|
295 * @param value the value of the setting if it is an integer. |
|
296 * @return Symbian OS error code, if key is not found or |
|
297 * stored data is not integer. |
|
298 */ |
|
299 private native int _getInt( |
|
300 int aFunctionSourceHandle, |
|
301 int cenrepHandle, |
|
302 long key); |
|
303 |
|
304 /** Stores string value in key. |
|
305 * |
|
306 * @param FunctionSourceHandle handle to the native function source. |
|
307 * @param cenrepHandle handle to the native side peer object. |
|
308 * @param key the key of setting to be stored. |
|
309 * @param value the string value of the setting to be stored. |
|
310 * @return Symbian OS error code, if string value cannot be stored. |
|
311 */ |
|
312 private native void _setString( |
|
313 int aFunctionSourceHandle, |
|
314 int cenrepHandle, |
|
315 long key, |
|
316 String value); |
|
317 |
|
318 /** Stores integer value in key. |
|
319 * |
|
320 * @param FunctionSourceHandle handle to the native function source. |
|
321 * @param cenrepHandle handle to the native side peer object. |
|
322 * @param key the key of setting to be stored. |
|
323 * @param value the integer value of the setting to be stored. |
|
324 * @return Symbian OS error code, if integer value cannot be stored. |
|
325 */ |
|
326 private native void _setInt( |
|
327 int aFunctionSourceHandle, |
|
328 int cenrepHandle, |
|
329 long key, |
|
330 int value); |
|
331 |
|
332 |
|
333 /** |
|
334 * Checks if MIDlet is permited to access the central repository. |
|
335 * Only MIDlets in manufacturer or operator domain are allowed. |
|
336 * @throws CentralRepositoryException if MIDlet is not in manufacturer or |
|
337 * operator domain. |
|
338 */ |
|
339 private void checkAccess() |
|
340 throws SecurityException |
|
341 { |
|
342 |
|
343 ApplicationInfo appInfo = ApplicationInfo.getInstance(); |
|
344 String protectionDomain = appInfo.getProtectionDomain(); |
|
345 |
|
346 if (protectionDomain.equals(ApplicationInfo.MANUFACTURER_DOMAIN) == false && |
|
347 protectionDomain.equals(ApplicationInfo.OPERATOR_DOMAIN) == false) |
|
348 { |
|
349 Logger.ELOG(Logger.EJavaCentrep, "Protection Domain: " + protectionDomain + |
|
350 ", Access denied"); |
|
351 throw new SecurityException("Access denied!"); |
|
352 } |
|
353 } |
|
354 } |
|