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 @Override
045 public Object invoke(Object proxy, Method method, Object[] arguments)
046 throws Throwable {
047
048 String methodName = method.getName();
049
050 if (methodName.equals("close")) {
051 if (_log.isDebugEnabled()) {
052 _log.debug("Closing session " + _session);
053 }
054
055 for (Entry<String, Binary> entry : _binaries.entrySet()) {
056 Binary binary = entry.getValue();
057
058 binary.dispose();
059 }
060
061 _session.logout();
062
063 return null;
064 }
065 else 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 catch (Exception e) {
093 throw e;
094 }
095 }
096
097 private static Log _log = LogFactoryUtil.getLog(
098 JCRSessionInvocationHandler.class);
099
100 private Map<String, Binary> _binaries = new HashMap<String, Binary>();
101 private Session _session;
102
103 }