Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Resolves Singleton is not thread safe violation (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
konishon committed Oct 16, 2019
1 parent bdb4464 commit d28b0e7
Show file tree
Hide file tree
Showing 45 changed files with 127 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.util.concurrent.atomic.AtomicInteger;


@SuppressWarnings("PMD.UseNotifyAllInsteadOfNotify")
public class FieldSightNotificationUtils {
private NotificationManager notifManager;
private static final String CHANNEL_ID = "fieldsight_notification_channel";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fieldsight.naxa.common;

import androidx.lifecycle.LiveData;

import android.os.AsyncTask;

import org.odk.collect.android.application.Collect;
Expand All @@ -25,13 +26,11 @@ private SiteOverideLocalSource() {
this.siteDao = fieldSightDatabase.getSiteDAO();
}

public static SiteOverideLocalSource getInstance() {
public synchronized static SiteOverideLocalSource getInstance() {

if (siteOverideLocalSource == null) {
synchronized (SiteOverideLocalSource.class) {
if (siteOverideLocalSource == null) {
siteOverideLocalSource = new SiteOverideLocalSource();
}
}
siteOverideLocalSource = new SiteOverideLocalSource();

}
return siteOverideLocalSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory {

@SuppressLint("StaticFieldLeak")
private static volatile ViewModelFactory viewModelFactory;
private static ViewModelFactory viewModelFactory;

private final GeneralFormRepository generalFormRepository;
private final ScheduledFormRepository scheduledFormRepository;
Expand All @@ -59,15 +59,15 @@ public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory {


public ViewModelFactory(
GeneralFormRepository repository,
ScheduledFormRepository scheduledFormRepository,
StageFormRepository stageFormRepository,
SubStageRepository subStageRepository,
ProjectRepository projectRepository,
SiteRepository siteRepository,
SurveyFormRepository surveyFormRepository,
FieldSightNotificationRepository notificationRepository,
ContactRepository contactRepository
GeneralFormRepository repository,
ScheduledFormRepository scheduledFormRepository,
StageFormRepository stageFormRepository,
SubStageRepository subStageRepository,
ProjectRepository projectRepository,
SiteRepository siteRepository,
SurveyFormRepository surveyFormRepository,
FieldSightNotificationRepository notificationRepository,
ContactRepository contactRepository
) {

this.generalFormRepository = repository;
Expand All @@ -81,30 +81,28 @@ public ViewModelFactory(
this.contactRepository = contactRepository;
}

public static ViewModelFactory getInstance() {
public static synchronized ViewModelFactory getInstance() {

if (viewModelFactory == null) {
synchronized (ViewModelFactory.class) {
if (viewModelFactory == null) {
GeneralFormRepository generalFormRepository = GeneralFormRepository.getInstance(
GeneralFormLocalSource.getInstance(), GeneralFormRemoteSource.getInstance());
ScheduledFormRepository scheduledFormRepository = ScheduledFormRepository.getInstance(
ScheduledFormsLocalSource.getInstance(), ScheduledFormsRemoteSource.getInstance());

StageFormRepository stageFormRepository = StageFormRepository.getInstance(StageLocalSource.getInstance(), StageRemoteSource.getInstance());
SubStageRepository subStageRepository = SubStageRepository.getInstance(SubStageLocalSource.getInstance());
ProjectRepository projectRepository = ProjectRepository.getInstance(ProjectLocalSource.getInstance(), ProjectSitesRemoteSource.getInstance());
SiteRepository siteRepository = SiteRepository.getInstance(SiteLocalSource.getInstance());
SurveyFormRepository surveyFormRepository = SurveyFormRepository.getInstance(SurveyFormLocalSource.getInstance());
FieldSightNotificationRepository notificationRepository = FieldSightNotificationRepository.getInstance(FieldSightNotificationLocalSource.getInstance());
ContactRepository contactRepository = ContactRepository.getInstance(ContactLocalSource.getInstance());


viewModelFactory = new ViewModelFactory(generalFormRepository, scheduledFormRepository,
stageFormRepository, subStageRepository, projectRepository, siteRepository,
surveyFormRepository, notificationRepository, contactRepository);
}
}

GeneralFormRepository generalFormRepository = GeneralFormRepository.getInstance(
GeneralFormLocalSource.getInstance(), GeneralFormRemoteSource.getInstance());
ScheduledFormRepository scheduledFormRepository = ScheduledFormRepository.getInstance(
ScheduledFormsLocalSource.getInstance(), ScheduledFormsRemoteSource.getInstance());

StageFormRepository stageFormRepository = StageFormRepository.getInstance(StageLocalSource.getInstance(), StageRemoteSource.getInstance());
SubStageRepository subStageRepository = SubStageRepository.getInstance(SubStageLocalSource.getInstance());
ProjectRepository projectRepository = ProjectRepository.getInstance(ProjectLocalSource.getInstance(), ProjectSitesRemoteSource.getInstance());
SiteRepository siteRepository = SiteRepository.getInstance(SiteLocalSource.getInstance());
SurveyFormRepository surveyFormRepository = SurveyFormRepository.getInstance(SurveyFormLocalSource.getInstance());
FieldSightNotificationRepository notificationRepository = FieldSightNotificationRepository.getInstance(FieldSightNotificationLocalSource.getInstance());
ContactRepository contactRepository = ContactRepository.getInstance(ContactLocalSource.getInstance());


viewModelFactory = new ViewModelFactory(generalFormRepository, scheduledFormRepository,
stageFormRepository, subStageRepository, projectRepository, siteRepository,
surveyFormRepository, notificationRepository, contactRepository);

}
return viewModelFactory;
}
Expand Down Expand Up @@ -147,11 +145,10 @@ public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
} else if (modelClass.isAssignableFrom(DownloadViewModel.class)) {
//noinspection unchecked
return (T) new DownloadViewModel();
}else if (modelClass.isAssignableFrom(FragmentHostViewModel.class)) {
} else if (modelClass.isAssignableFrom(FragmentHostViewModel.class)) {
//noinspection unchecked
return (T) new FragmentHostViewModel();
}
else if (modelClass.isAssignableFrom(FieldSightFormViewModel.class)) {
} else if (modelClass.isAssignableFrom(FieldSightFormViewModel.class)) {
//noinspection unchecked
return (T) new FieldSightFormViewModel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.content.Context;

import org.odk.collect.android.application.Collect;
Expand Down Expand Up @@ -35,18 +36,17 @@ public abstract class FieldSightConfigDatabase extends RoomDatabase {

private static final String DB_PATH = Collect.METADATA_PATH + File.separator + "fieldsight_cofig";

public static FieldSightConfigDatabase getDatabase(final Context context) {
public synchronized static FieldSightConfigDatabase getDatabase(final Context context) {

if (fieldSightConfigDatabase == null) {
synchronized (FieldSightConfigDatabase.class) {
if (fieldSightConfigDatabase == null) {
fieldSightConfigDatabase = Room.databaseBuilder(context.getApplicationContext(),
FieldSightConfigDatabase.class, DB_PATH)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
}
}
fieldSightConfigDatabase = Room.databaseBuilder(context.getApplicationContext(),
FieldSightConfigDatabase.class, DB_PATH)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
}


return fieldSightConfigDatabase;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private ContactLocalSource() {
}


public static ContactLocalSource getInstance() {
public synchronized static ContactLocalSource getInstance() {
if (INSTANCE == null) {
INSTANCE = new ContactLocalSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ContactRemoteSource implements BaseRemoteDataSource<FieldSightConta
private static ContactRemoteSource contactRemoteSource;


public static ContactRemoteSource getInstance() {
public synchronized static ContactRemoteSource getInstance() {
if (contactRemoteSource == null) {
contactRemoteSource = new ContactRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ public class ContactRepository implements BaseRepository<FieldSightContactModel>
private static ContactRepository contactRepository;


public static ContactRepository getInstance(ContactLocalSource localSource) {
public static synchronized ContactRepository getInstance(ContactLocalSource localSource) {
if (contactRepository == null) {
synchronized (ContactRepository.class) {
if (contactRepository == null) {
contactRepository = new ContactRepository(localSource);
}
}
contactRepository = new ContactRepository(localSource);
}
return contactRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class FieldSightNotificationLocalSource implements BaseLocalDataSource<Fi
boolean isDeployedFromSite;
String siteIdentifier;

public static FieldSightNotificationLocalSource getInstance() {
public synchronized static FieldSightNotificationLocalSource getInstance() {
if (INSTANCE == null) {
INSTANCE = new FieldSightNotificationLocalSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EducationalMaterialsLocalSource implements BaseLocalDataSource<Em>
private static EducationalMaterialsLocalSource educationalMaterialsLocalSource;
private final EducationalMaterialsDao dao;

public static EducationalMaterialsLocalSource getInstance() {
public synchronized static EducationalMaterialsLocalSource getInstance() {
if (educationalMaterialsLocalSource == null) {
educationalMaterialsLocalSource = new EducationalMaterialsLocalSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class EducationalMaterialsRemoteSource implements BaseRemoteDataSource<Em
private static EducationalMaterialsRemoteSource INSTANCE;


public static EducationalMaterialsRemoteSource getInstance() {
public synchronized static EducationalMaterialsRemoteSource getInstance() {
if (INSTANCE == null) {
INSTANCE = new EducationalMaterialsRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class FlagFormRemoteSource {
private final FSInstancesDao instancesDao;


public static FlagFormRemoteSource getFlagFormRemoteSource() {
public synchronized static FlagFormRemoteSource getFlagFormRemoteSource() {
if (flagFormRemoteSource == null) {
flagFormRemoteSource = new FlagFormRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class InstanceRemoteSource {
private static InstanceRemoteSource instanceRemoteSource;


public static InstanceRemoteSource getInstanceRemoteSource() {
public static synchronized InstanceRemoteSource getInstanceRemoteSource() {
if (instanceRemoteSource == null) {
instanceRemoteSource = new InstanceRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private FieldSightFormsLocalSourcev3() {
this.dao = database.getFieldSightFOrmDAOV3();
}

public static FieldSightFormsLocalSourcev3 getInstance() {
public static synchronized FieldSightFormsLocalSourcev3 getInstance() {
if (localSourcev3 == null) {
localSourcev3 = new FieldSightFormsLocalSourcev3();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private GeneralFormLocalSource() {
}


public static GeneralFormLocalSource getInstance() {
public static synchronized GeneralFormLocalSource getInstance() {
if (generalFormLocalSource == null) {
generalFormLocalSource = new GeneralFormLocalSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class GeneralFormRemoteSource implements BaseRemoteDataSource<GeneralForm
private static GeneralFormRemoteSource generalFormRemoteSource;
private final ProjectLocalSource projectLocalSource;

public static GeneralFormRemoteSource getInstance() {
public static synchronized GeneralFormRemoteSource getInstance() {
if (generalFormRemoteSource == null) {
generalFormRemoteSource = new GeneralFormRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ public class GeneralFormRepository implements BaseRepository<GeneralForm> {
private final GeneralFormLocalSource localSource;
private final GeneralFormRemoteSource remoteSource;

public static GeneralFormRepository getInstance(GeneralFormLocalSource localSource, GeneralFormRemoteSource remoteSource) {
public static synchronized GeneralFormRepository getInstance(GeneralFormLocalSource localSource, GeneralFormRemoteSource remoteSource) {
if (generalFormRepository == null) {
synchronized (GeneralFormRepository.class) {
if (generalFormRepository == null) {
generalFormRepository = new GeneralFormRepository(localSource, remoteSource);
}
}
generalFormRepository = new GeneralFormRepository(localSource, remoteSource);
}
return generalFormRepository;
}
Expand Down Expand Up @@ -54,7 +50,6 @@ public LiveData<List<GeneralForm>> getByProjectId(boolean forcedUpdate, String p
}



public LiveData<List<GeneralFormAndSubmission>> getFormsBySiteId(@NonNull String siteId, @NonNull String projectId) {
return localSource.getFormsBySiteId(siteId, projectId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static OkHttpClient createOkHttpClient(boolean cacheRequest) {
.build();
}

public static <T> T createService(Class<T> serviceClass) {
public synchronized static <T> T createService(Class<T> serviceClass) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(createOkHttpClient(false))
Expand All @@ -94,7 +94,7 @@ public static <T> T createService(Class<T> serviceClass) {
return retrofit.create(serviceClass);
}

public static <T> T createCacheService(Class<T> serviceClass) {
public synchronized static <T> T createCacheService(Class<T> serviceClass) {
if (cacheablesRetrofit == null) {
cacheablesRetrofit = new Retrofit.Builder()
.client(createOkHttpClient(true))
Expand All @@ -107,7 +107,7 @@ public static <T> T createCacheService(Class<T> serviceClass) {
return cacheablesRetrofit.create(serviceClass);
}

public static Retrofit getRxClient() {
public synchronized static Retrofit getRxClient() {


if (okHttp == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ public class FieldSightNotificationRepository implements BaseRepository<FieldSig


private final FieldSightNotificationLocalSource localSource;
private static FieldSightNotificationRepository fieldSightNotificationRepository ;
private static FieldSightNotificationRepository fieldSightNotificationRepository;


public static FieldSightNotificationRepository getInstance(FieldSightNotificationLocalSource localSource) {
public synchronized static FieldSightNotificationRepository getInstance(FieldSightNotificationLocalSource localSource) {
if (fieldSightNotificationRepository == null) {
synchronized (FieldSightNotificationLocalSource.class) {
if (fieldSightNotificationRepository == null) {
fieldSightNotificationRepository = new FieldSightNotificationRepository(localSource);
}
}
fieldSightNotificationRepository = new FieldSightNotificationRepository(localSource);
}
return fieldSightNotificationRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private LastSubmissionLocalSource() {
}


public static LastSubmissionLocalSource getInstance() {
public synchronized static LastSubmissionLocalSource getInstance() {
if (lastSubmissionLocalSource == null) {
lastSubmissionLocalSource = new LastSubmissionLocalSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class LastSubmissionRemoteSource implements BaseRemoteDataSource<LastSubm

private static LastSubmissionRemoteSource lastSubmissionRemoteSource;

public static LastSubmissionRemoteSource getInstance() {
public synchronized static LastSubmissionRemoteSource getInstance() {
if (lastSubmissionRemoteSource == null) {
lastSubmissionRemoteSource = new LastSubmissionRemoteSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private ProjectLocalSource() {
}


public static ProjectLocalSource getInstance() {
public static synchronized ProjectLocalSource getInstance() {
if (projectLocalSource == null) {
projectLocalSource = new ProjectLocalSource();
}
Expand Down
Loading

0 comments on commit d28b0e7

Please sign in to comment.