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
020 import java.lang.reflect.InvocationHandler;
021 import java.lang.reflect.InvocationTargetException;
022 import java.lang.reflect.Method;
023
024 import java.util.HashMap;
025 import java.util.Map.Entry;
026 import java.util.Map;
027
028 import javax.jcr.Binary;
029 import javax.jcr.Session;
030
031
034 public class JCRSessionInvocationHandler implements InvocationHandler {
035
036 public JCRSessionInvocationHandler(Session session) {
037 _session = session;
038
039 if (_log.isDebugEnabled()) {
040 _log.debug("Starting session " + _session);
041 }
042 }
043
044 public Object invoke(Object proxy, Method method, Object[] arguments)
045 throws Throwable {
046
047 String methodName = method.getName();
048
049 if (methodName.equals("close")) {
050 if (_log.isDebugEnabled()) {
051 _log.debug("Closing session " + _session);
052 }
053
054 for (Entry<String, Binary> entry : _binaries.entrySet()) {
055 Binary binary = entry.getValue();
056
057 binary.dispose();
058 }
059
060 _session.logout();
061
062 return null;
063 }
064 else 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 catch (Exception e) {
092 throw e;
093 }
094 }
095
096 private static Log _log = LogFactoryUtil.getLog(
097 JCRSessionInvocationHandler.class);
098
099 private Map<String, Binary> _binaries = new HashMap<String, Binary>();
100 private Session _session;
101
102 }