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;
027    import java.util.Map.Entry;
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            @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    }