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;
027 import java.util.Map.Entry;
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 @Override
048 public void doFinalize() {
049 for (Entry<String, Binary> entry : _binaries.entrySet()) {
050 Binary binary = entry.getValue();
051
052 binary.dispose();
053 }
054
055 _session.logout();
056 }
057
058 @Override
059 public Object invoke(Object proxy, Method method, Object[] arguments)
060 throws Throwable {
061
062 String methodName = method.getName();
063
064 if (methodName.equals("logout")) {
065 if (_log.isDebugEnabled()) {
066 _log.debug("Skipping logout for session " + _session);
067 }
068
069 return null;
070 }
071 else if (methodName.equals("put")) {
072 String key = (String)arguments[0];
073 Binary binary = (Binary)arguments[1];
074
075 if (_log.isDebugEnabled()) {
076 _log.debug(
077 "Tracking binary " + key + " for session " + _session);
078 }
079
080 _binaries.put(key, binary);
081
082 return null;
083 }
084
085 try {
086 return method.invoke(_session, arguments);
087 }
088 catch (InvocationTargetException ite) {
089 throw ite.getCause();
090 }
091 }
092
093 private static Log _log = LogFactoryUtil.getLog(
094 JCRSessionInvocationHandler.class);
095
096 private Map<String, Binary> _binaries = new HashMap<String, Binary>();
097 private Session _session;
098
099 }