001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
034     * @author Raymond Aug??
035     * @author Shuyang Zhou
036     */
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    }