Skip to content
Snippets Groups Projects
Commit b55ab0f4 authored by srosse's avatar srosse
Browse files

OO-505: manage the cacheable mappers without session informations, try to...

OO-505: manage the cacheable mappers without session informations, try to retrieve them and update them, don't persist one for every user...
parent 3cef08b6
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,6 @@ import java.io.Serializable; ...@@ -23,7 +23,6 @@ import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.persistence.LockModeType;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.olat.core.commons.persistence.DB; import org.olat.core.commons.persistence.DB;
...@@ -57,25 +56,29 @@ public class MapperDAO { ...@@ -57,25 +56,29 @@ public class MapperDAO {
return m; return m;
} }
public void updateConfiguration(String mapperId, Serializable mapper) { public boolean updateConfiguration(String mapperId, Serializable mapper) {
PersistedMapper m = loadForUpdate(mapperId); PersistedMapper m = loadForUpdate(mapperId);
if(m == null) {
return false;
}
String configuration = XStreamHelper.createXStreamInstance().toXML(mapper); String configuration = XStreamHelper.createXStreamInstance().toXML(mapper);
m.setXmlConfiguration(configuration); m.setXmlConfiguration(configuration);
m.setLastModified(new Date()); m.setLastModified(new Date());
dbInstance.getCurrentEntityManager().merge(m); dbInstance.getCurrentEntityManager().merge(m);
return true;
} }
private PersistedMapper loadForUpdate(String mapperId) { private PersistedMapper loadForUpdate(String mapperId) {
StringBuilder q = new StringBuilder(); StringBuilder q = new StringBuilder();
q.append("select mapper from ").append(PersistedMapper.class.getName()).append(" as mapper ") q.append("select mapper from ").append(PersistedMapper.class.getName()).append(" as mapper ")
.append(" where mapper.mapperId=:mapperId"); .append(" where mapper.mapperId=:mapperId order by mapper.key");
List<PersistedMapper> mappers = dbInstance.getCurrentEntityManager() List<PersistedMapper> mappers = dbInstance.getCurrentEntityManager()
.createQuery(q.toString(), PersistedMapper.class) .createQuery(q.toString(), PersistedMapper.class)
.setParameter("mapperId", mapperId) .setParameter("mapperId", mapperId)
.setLockMode(LockModeType.PESSIMISTIC_WRITE) .setFirstResult(0)
.setMaxResults(1)
.getResultList(); .getResultList();
return mappers.isEmpty() ? null : mappers.get(0); return mappers.isEmpty() ? null : mappers.get(0);
} }
......
...@@ -30,6 +30,7 @@ import java.util.UUID; ...@@ -30,6 +30,7 @@ import java.util.UUID;
import org.olat.core.dispatcher.DispatcherAction; import org.olat.core.dispatcher.DispatcherAction;
import org.olat.core.dispatcher.mapper.Mapper; import org.olat.core.dispatcher.mapper.Mapper;
import org.olat.core.dispatcher.mapper.MapperService; import org.olat.core.dispatcher.mapper.MapperService;
import org.olat.core.dispatcher.mapper.model.PersistedMapper;
import org.olat.core.id.OLATResourceable; import org.olat.core.id.OLATResourceable;
import org.olat.core.util.Encoder; import org.olat.core.util.Encoder;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
...@@ -84,16 +85,7 @@ public class MapperServiceImpl implements MapperService { ...@@ -84,16 +85,7 @@ public class MapperServiceImpl implements MapperService {
@Override @Override
public String register(UserSession session, Mapper mapper) { public String register(UserSession session, Mapper mapper) {
String mapid = UUID.randomUUID().toString().replace("-", ""); String mapid = UUID.randomUUID().toString().replace("-", "");
return register(session, mapid, mapper); mapid = Encoder.encrypt(mapid);
}
@Override
public String register(UserSession session, String mapid, Mapper mapper) {
String saveMapperID = Encoder.encrypt(mapid);
return internRegister(session, saveMapperID, mapper);
}
private String internRegister(UserSession session, String mapid, Mapper mapper) {
mapperIdToMapper.put(mapid, mapper); mapperIdToMapper.put(mapid, mapper);
mapperToMapperId.put(mapper, mapid); mapperToMapperId.put(mapper, mapid);
if(session.getSessionInfo() == null) { if(session.getSessionInfo() == null) {
...@@ -113,6 +105,35 @@ public class MapperServiceImpl implements MapperService { ...@@ -113,6 +105,35 @@ public class MapperServiceImpl implements MapperService {
mapperDao.persistMapper(sessionId, mapid, (Serializable)mapper); mapperDao.persistMapper(sessionId, mapid, (Serializable)mapper);
} }
return WebappHelper.getServletContextPath() + DispatcherAction.PATH_MAPPED + mapid; return WebappHelper.getServletContextPath() + DispatcherAction.PATH_MAPPED + mapid;
}
/**
* Cacheable mapper, not session dependant
*/
@Override
public String register(UserSession session, String mapperId, Mapper mapper) {
String encryptedMapId = Encoder.encrypt(mapperId);
boolean alreadyLoaded = mapperIdToMapper.containsKey(encryptedMapId);
if(mapper instanceof Serializable) {
if(alreadyLoaded) {
if(!mapperDao.updateConfiguration(encryptedMapId, (Serializable)mapper)) {
mapperDao.persistMapper(null, encryptedMapId, (Serializable)mapper);
}
} else {
PersistedMapper persistedMapper = mapperDao.loadByMapperId(encryptedMapId);
if(persistedMapper == null) {
mapperDao.persistMapper(null, encryptedMapId, (Serializable)mapper);
} else {
mapperDao.updateConfiguration(encryptedMapId, (Serializable)mapper);
}
}
}
mapperIdToMapper.put(encryptedMapId, mapper);
mapperToMapperId.put(mapper, encryptedMapId);
if(session.getSessionInfo() == null) {
return WebappHelper.getServletContextPath() + DispatcherAction.PATH_MAPPED + encryptedMapId;
}
return WebappHelper.getServletContextPath() + DispatcherAction.PATH_MAPPED + encryptedMapId;
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment