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