001    /**
002     * Copyright (c) 2000-2012 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    
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    /**
032     * @author Raymond Augé
033     */
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    }