Make client service implementation synchronous
@@ -47,6 +47,9 @@ public class OpeneCardServiceClient {
private static final Logger LOG = LoggerFactory.getLogger(OpeneCardServiceClient.class);
+ // lock object to make access to the openecard service globally locked
+ private static final Object SYNC = new Object();
+
private final Context appCtx;
private Promise<OpeneCardService> oecService;
private boolean isInitialized;
@@ -79,6 +82,7 @@ public class OpeneCardServiceClient {
* @return The result of the start function.
*/
public ServiceResponse startService() {
+ synchronized (SYNC) {
try {
if (! isInitialized) {
Intent i = createOpeneCardIntent();
@@ -87,11 +91,13 @@ public class OpeneCardServiceClient {
// wait until service is connected, then call startService
OpeneCardService s = oecService.deref();
+ assert(s != null);
return s.startService();
} catch (InterruptedException | RemoteException ex) {
return new ServiceErrorResponse(INTERNAL_ERROR, ex.getMessage());
}
}
+ }
/**
* Synchronously stop the Open eCard Stack.
@@ -100,6 +106,7 @@ public class OpeneCardServiceClient {
* @throws IllegalStateException Thrown in case the service is already stopped.
*/
public ServiceResponse stopService() throws IllegalStateException {
+ synchronized (SYNC) {
try {
if (! isInitialized) {
throw new IllegalStateException("Trying to stop uninitialized service.");
@@ -107,6 +114,7 @@ public class OpeneCardServiceClient {
// stop then unbind service
OpeneCardService s = oecService.deref();
+ assert(s != null);
ServiceResponse res = s.stopService();
unbindService();
return res;
@@ -114,6 +122,7 @@ public class OpeneCardServiceClient {
return new ServiceErrorResponse(INTERNAL_ERROR, ex.getMessage());
}
}
+ }
/**
* Unbinds the service client.
@@ -143,12 +152,14 @@ public class OpeneCardServiceClient {
* @see {@link #isInitialized()} for information when this method may be called.
*/
public OpeneCardContext getContext() throws IllegalStateException {
+ synchronized (SYNC) {
if (isInitialized()) {
return OpeneCardServiceImpl.getContext();
} else {
throw new IllegalStateException("Requested unitialized Context object.");
}
}
+ }
private Intent createOpeneCardIntent() {
return new Intent(appCtx, OpeneCardServiceImpl.class);
