/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.domain.hibernate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import org.alfresco.util.GUID;
import org.hibernate.Session;
import org.hibernate.engine.EntityKey;
/**
* Support to (optionally) listen to hibernate events generated by a hibernate session. The tracking is bound to a
* transaction resource
*
* @author andyh
*/
public class HibernateSessionHelperResource implements HibernateSessionHelperResourceProvider
{
LinkedHashMap> marks = new LinkedHashMap>();
String currentMark = null;
HibernateSessionHelperResource()
{
}
public String getCurrentMark()
{
return currentMark;
}
public List getMarks(Session session)
{
ArrayList answer = new ArrayList(marks.size());
for (String key : marks.keySet())
{
answer.add(key);
}
return answer;
}
public void mark(Session session)
{
String guid = GUID.generate();
mark(session, guid);
}
@SuppressWarnings("unchecked")
public void mark(Session session, String label)
{
if (label == null)
{
throw new HibernateSessionHelperResourceException("Null key is not supported");
}
if (marks.containsKey(label))
{
throw new HibernateSessionHelperResourceException("Key already exists - " + label);
}
if (marks.size() == 0)
{
SessionSizeResourceManager.setDisableInTransaction();
}
HashSet mark = new HashSet((Set) session.getStatistics().getEntityKeys());
// If the mark is too large, then the flush process will be excessive.
if (mark.size() > 1000)
{
// The session is to big. Use the mark to as a basis for cleaning out the session.
if (currentMark == null)
{
// The session is just too big
SessionSizeResourceManager.clear(session);
}
else
{
reset(session);
}
// Get the mark list again
mark = new HashSet((Set) session.getStatistics().getEntityKeys());
}
marks.put(label, mark);
currentMark = label;
}
public void removeMark(Session session)
{
if (currentMark != null)
{
removeMark(session, currentMark);
}
else
{
throw new HibernateSessionHelperResourceException("No current mark");
}
}
public void removeMark(Session session, String label)
{
if (label == null)
{
throw new HibernateSessionHelperResourceException("Null key is not supported");
}
if (!marks.containsKey(label))
{
throw new HibernateSessionHelperResourceException("Key does not exist - " + label);
}
if (marks.size() > 0)
{
marks.remove(label);
if (label.equals(currentMark))
{
currentMark = getLastMarkOrNull();
}
}
if (marks.size() == 0)
{
SessionSizeResourceManager.setEnableInTransaction();
}
}
public void reset(Session session)
{
if (currentMark != null)
{
doResetAndRemove(session, currentMark, false);
}
else
{
throw new HibernateSessionHelperResourceException("No current mark");
}
}
public void reset(Session session, String label)
{
doResetAndRemove(session, label, false);
}
public void resetAndRemoveMark(Session session)
{
if (currentMark != null)
{
doResetAndRemove(session, currentMark, true);
}
else
{
throw new HibernateSessionHelperResourceException("No current mark");
}
}
public void resetAndRemoveMark(Session session, String label)
{
doResetAndRemove(session, label, true);
}
@SuppressWarnings("unchecked")
private void doResetAndRemove(Session session, String label, boolean remove)
{
if (label == null)
{
throw new HibernateSessionHelperResourceException("Null key is not supported");
}
if (!marks.containsKey(label))
{
throw new HibernateSessionHelperResourceException("Key does not exist - " + label);
}
if (marks.size() > 0)
{
session.flush();
Set check = marks.get(label);
Set current = new HashSet((Set) session.getStatistics().getEntityKeys());
Set