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