001
014
015 package com.liferay.portal.jcr;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.memory.FinalizeAction;
020
021 import java.lang.reflect.InvocationHandler;
022 import java.lang.reflect.InvocationTargetException;
023 import java.lang.reflect.Method;
024
025 import java.util.HashMap;
026 import java.util.Map.Entry;
027 import java.util.Map;
028
029 import javax.jcr.Binary;
030 import javax.jcr.Session;
031
032
036 public class JCRSessionInvocationHandler
037 implements FinalizeAction, InvocationHandler {
038
039 public JCRSessionInvocationHandler(Session session) {
040 _session = session;
041
042 if (_log.isDebugEnabled()) {
043 _log.debug("Starting session " + _session);
044 }
045 }
046
047 public void doFinalize() {
048 for (Entry<String, Binary> entry : _binaries.entrySet()) {
049 Binary binary = entry.getValue();
050
051 binary.dispose();
052 }
053
054 _session.logout();
055 }
056
057 public Object invoke(Object proxy, Method method, Object[] arguments)
058 throws Throwable {
059
060 String methodName = method.getName();
061
062 if (methodName.equals("logout")) {
063 if (_log.isDebugEnabled()) {
064 _log.debug("Skipping logout for session " + _session);
065 }
066
067 return null;
068 }
069 else if (methodName.equals("put")) {
070 String key = (String)arguments[0];
071 Binary binary = (Binary)arguments[1];
072
073 if (_log.isDebugEnabled()) {
074 _log.debug(
075 "Tracking binary " + key + " for session " + _session);
076 }
077
078 _binaries.put(key, binary);
079
080 return null;
081 }
082
083 try {
084 return method.invoke(_session, arguments);
085 }
086 catch (InvocationTargetException ite) {
087 throw ite.getCause();
088 }
089 }
090
091 private static Log _log = LogFactoryUtil.getLog(
092 JCRSessionInvocationHandler.class);
093
094 private Map<String, Binary> _binaries = new HashMap<String, Binary>();
095 private Session _session;
096
097 }