Fixed fallback file when context is null

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@124266 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-17 16:04:19 +00:00
parent 6b34006394
commit be1850d35c
1 changed files with 20 additions and 18 deletions

View File

@ -22,6 +22,8 @@ public abstract class PersistenceBackendFactory {
private static final Logger logger = LoggerFactory.getLogger(PersistenceBackendFactory.class);
public static final String DEFAULT_CONTEXT = "DEFAULT_CONTEXT";
public final static String HOME_SYSTEM_PROPERTY = "user.home";
protected static final String FALLBACK_FILENAME = "fallback.log";
@ -77,22 +79,27 @@ public abstract class PersistenceBackendFactory {
return fallbackLocation;
}
protected static String sanitizeContext(String context){
protected static String sanitizeContext(final String context){
if(context==null || context.compareTo("")==0){
return DEFAULT_CONTEXT;
}
return context;
}
protected static String removeSlashFromContext(String context){
return context.replace("/", "_");
}
public static File getFallbackFile(String context){
File fallbackFile = null;
if(context!=null){
String sanitized = sanitizeContext(context);
fallbackFile = new File(getFallbackLocation(), String.format("%s.%s", sanitized, FALLBACK_FILENAME));
}else{
fallbackFile = new File(getFallbackLocation(), FALLBACK_FILENAME);
}
context = sanitizeContext(context);
String slashLessContext = removeSlashFromContext(context);
File fallbackFile = new File(getFallbackLocation(), String.format("%s.%s", slashLessContext, FALLBACK_FILENAME));
fallbackFile = new File(getFallbackLocation(), FALLBACK_FILENAME);
return fallbackFile;
}
protected static FallbackPersistenceBackend createFallback(String context){
context = sanitizeContext(context);
logger.debug("Creating {} for context {}", FallbackPersistenceBackend.class.getSimpleName(), context);
File fallbackFile = getFallbackFile(context);
logger.trace("{} for context {} is {}", FallbackPersistenceBackend.class.getSimpleName(), context, fallbackFile.getAbsolutePath());
@ -102,6 +109,7 @@ public abstract class PersistenceBackendFactory {
}
protected static PersistenceBackend discoverPersistenceBackend(String context){
context = sanitizeContext(context);
logger.debug("Discovering {} for scope {}",
PersistenceBackend.class.getSimpleName(), context);
ServiceLoader<PersistenceBackend> serviceLoader = ServiceLoader.load(PersistenceBackend.class);
@ -129,6 +137,7 @@ public abstract class PersistenceBackendFactory {
};
protected static PersistenceBackend rediscoverPersistenceBackend(PersistenceBackend actual, String context){
context = sanitizeContext(context);
Long now = Calendar.getInstance().getTimeInMillis();
Long lastCheckTimestamp = fallbackLastCheck.get(context);
logger.debug("Last check for context {} was {}", context, lastCheckTimestamp);
@ -190,16 +199,8 @@ public abstract class PersistenceBackendFactory {
}
public static PersistenceBackend getPersistenceBackend(String context) {
/*
if(context==null){
logger.warn("No Context available. FallbackPersistence will be used");
FallbackPersistenceBackend fallbackPersistenceBackend = createFallback(null);
persistenceBackends.put(null, fallbackPersistenceBackend);
return fallbackPersistenceBackend;
}
*/
context = sanitizeContext(context);
PersistenceBackend persistence = null;
logger.debug("Going to synchronized block in getPersistenceBackend");
synchronized (persistenceBackends) {
@ -229,6 +230,7 @@ public abstract class PersistenceBackendFactory {
}
public static void flush(String context, long timeout, TimeUnit timeUnit){
context = sanitizeContext(context);
PersistenceBackend apb = persistenceBackends.get(context);
try {
logger.debug("Flushing records in context {}", context);