2013-07-27 18:33:20 +02:00
|
|
|
package com.sismics.util.context;
|
|
|
|
|
2016-08-26 21:22:27 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2017-11-06 16:45:47 +01:00
|
|
|
import com.sismics.docs.core.event.TemporaryFileCleanupAsyncEvent;
|
2016-08-26 21:22:27 +02:00
|
|
|
import com.sismics.docs.core.model.context.AppContext;
|
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
import javax.persistence.EntityManager;
|
2017-11-06 16:45:47 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2018-03-05 11:57:56 +01:00
|
|
|
import java.util.Iterator;
|
2016-08-26 21:22:27 +02:00
|
|
|
import java.util.List;
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Context associated to a user request, and stored in a ThreadLocal.
|
|
|
|
*
|
|
|
|
* @author jtremeaux
|
|
|
|
*/
|
|
|
|
public class ThreadLocalContext {
|
|
|
|
/**
|
|
|
|
* ThreadLocal to store the context.
|
|
|
|
*/
|
2016-04-18 00:00:46 +02:00
|
|
|
private static final ThreadLocal<ThreadLocalContext> threadLocalContext = new ThreadLocal<>();
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity manager.
|
|
|
|
*/
|
|
|
|
private EntityManager entityManager;
|
2016-08-26 21:22:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of async events posted during this request.
|
|
|
|
*/
|
|
|
|
private List<Object> asyncEventList = Lists.newArrayList();
|
|
|
|
|
2017-11-06 16:45:47 +01:00
|
|
|
/**
|
|
|
|
* List of temporary files created during this request.
|
|
|
|
* They are deleted at the end of each request.
|
|
|
|
*/
|
|
|
|
private List<Path> temporaryFileList = Lists.newArrayList();
|
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
/**
|
|
|
|
* Private constructor.
|
|
|
|
*/
|
|
|
|
private ThreadLocalContext() {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of this thread context.
|
|
|
|
*
|
|
|
|
* @return Thread local context
|
|
|
|
*/
|
|
|
|
public static ThreadLocalContext get() {
|
|
|
|
ThreadLocalContext context = threadLocalContext.get();
|
|
|
|
if (context == null) {
|
|
|
|
context = new ThreadLocalContext();
|
|
|
|
threadLocalContext.set(context);
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up the instance of this thread context.
|
|
|
|
*/
|
|
|
|
public static void cleanup() {
|
|
|
|
threadLocalContext.set(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter of entityManager.
|
|
|
|
*
|
|
|
|
* @return entityManager
|
|
|
|
*/
|
|
|
|
public EntityManager getEntityManager() {
|
2018-02-01 18:01:11 +01:00
|
|
|
if (entityManager != null && entityManager.isOpen()) {
|
|
|
|
// This disables the L1 cache
|
|
|
|
entityManager.flush();
|
|
|
|
entityManager.clear();
|
|
|
|
}
|
2013-07-27 18:33:20 +02:00
|
|
|
return entityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter of entityManager.
|
|
|
|
*
|
|
|
|
* @param entityManager entityManager
|
|
|
|
*/
|
|
|
|
public void setEntityManager(EntityManager entityManager) {
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
}
|
2016-08-26 21:22:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an async event to the queue to be fired after the current request.
|
|
|
|
*
|
|
|
|
* @param asyncEvent Async event
|
|
|
|
*/
|
|
|
|
public void addAsyncEvent(Object asyncEvent) {
|
|
|
|
asyncEventList.add(asyncEvent);
|
|
|
|
}
|
|
|
|
|
2017-11-06 16:45:47 +01:00
|
|
|
/**
|
|
|
|
* Create a temporary file linked to the request.
|
|
|
|
*
|
|
|
|
* @return New temporary file
|
|
|
|
*/
|
|
|
|
public Path createTemporaryFile() throws IOException {
|
|
|
|
Path path = Files.createTempFile("sismics_docs", null);
|
|
|
|
temporaryFileList.add(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-08-26 21:22:27 +02:00
|
|
|
/**
|
|
|
|
* Fire all pending async events.
|
|
|
|
*/
|
|
|
|
public void fireAllAsyncEvents() {
|
2018-03-05 11:57:56 +01:00
|
|
|
Iterator<Object> iterator = asyncEventList.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Object asyncEvent = iterator.next();
|
|
|
|
iterator.remove();
|
2016-08-26 21:22:27 +02:00
|
|
|
AppContext.getInstance().getAsyncEventBus().post(asyncEvent);
|
|
|
|
}
|
2017-11-06 16:45:47 +01:00
|
|
|
|
|
|
|
if (!temporaryFileList.isEmpty()) {
|
|
|
|
// Some files were created during this request, add a cleanup event to the queue
|
|
|
|
// It works because we are using a one thread executor
|
2018-03-05 11:57:56 +01:00
|
|
|
AppContext.getInstance().getAsyncEventBus().post(
|
|
|
|
new TemporaryFileCleanupAsyncEvent(Lists.newArrayList(temporaryFileList)));
|
|
|
|
temporaryFileList.clear();
|
2017-11-06 16:45:47 +01:00
|
|
|
}
|
2016-08-26 21:22:27 +02:00
|
|
|
}
|
2013-07-27 18:33:20 +02:00
|
|
|
}
|