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.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.memory.FinalizeManager;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.kernel.util.ProxyUtil;
021    import com.liferay.portal.util.ClassLoaderUtil;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.jcr.RepositoryException;
028    import javax.jcr.Session;
029    
030    /**
031     * @author Michael Young
032     */
033    public class JCRFactoryUtil {
034    
035            public static void closeSession(Session session) {
036                    if (session != null) {
037                            session.logout();
038                    }
039            }
040    
041            public static Session createSession() throws RepositoryException {
042                    return createSession(null);
043            }
044    
045            public static Session createSession(String workspaceName)
046                    throws RepositoryException {
047    
048                    if (workspaceName == null) {
049                            workspaceName = JCRFactory.WORKSPACE_NAME;
050                    }
051    
052                    if (!PropsValues.JCR_WRAP_SESSION) {
053                            JCRFactory jcrFactory = getJCRFactory();
054    
055                            return jcrFactory.createSession(workspaceName);
056                    }
057    
058                    Map<String, Session> sessions = _sessions.get();
059    
060                    Session session = sessions.get(workspaceName);
061    
062                    if (session != null) {
063                            return session;
064                    }
065    
066                    JCRFactory jcrFactory = getJCRFactory();
067    
068                    Session jcrSession = jcrFactory.createSession(workspaceName);
069    
070                    JCRSessionInvocationHandler jcrSessionInvocationHandler =
071                            new JCRSessionInvocationHandler(jcrSession);
072    
073                    Object sessionProxy = ProxyUtil.newProxyInstance(
074                            ClassLoaderUtil.getPortalClassLoader(),
075                            new Class<?>[] {Map.class, Session.class},
076                            jcrSessionInvocationHandler);
077    
078                    FinalizeManager.register(sessionProxy, jcrSessionInvocationHandler);
079    
080                    session = (Session)sessionProxy;
081    
082                    sessions.put(workspaceName, session);
083    
084                    return session;
085            }
086    
087            public static JCRFactory getJCRFactory() {
088                    if (_jcrFactory == null) {
089                            _jcrFactory = (JCRFactory)PortalBeanLocatorUtil.locate(
090                                    JCRFactory.class.getName());
091                    }
092    
093                    return _jcrFactory;
094            }
095    
096            public static void initialize() throws RepositoryException {
097                    JCRFactory jcrFactory = getJCRFactory();
098    
099                    jcrFactory.initialize();
100            }
101    
102            public static void prepare() throws RepositoryException {
103                    JCRFactory jcrFactory = getJCRFactory();
104    
105                    jcrFactory.prepare();
106            }
107    
108            public static void shutdown() {
109                    JCRFactory jcrFactory = getJCRFactory();
110    
111                    jcrFactory.shutdown();
112            }
113    
114            private static JCRFactory _jcrFactory;
115            private static ThreadLocal<Map<String, Session>> _sessions =
116                    new AutoResetThreadLocal<Map<String, Session>>(
117                            JCRFactoryUtil.class + "._sessions",
118                            new HashMap<String, Session>());
119    
120    }