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    
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            @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    }