001    /**
002     * Copyright (c) 2000-present 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.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.concurrent.ConcurrentReferenceKeyHashMap;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.memory.FinalizeManager;
021    import com.liferay.portal.kernel.util.ReflectionUtil;
022    
023    import java.lang.reflect.Field;
024    
025    import org.hibernate.intercept.FieldInterceptionHelper;
026    
027    /**
028     * This utility class collaborates with hibernate FieldInterceptionHelper patch
029     * from LPS-52218 to ensure the _instrumentedCache does not cause ClassLoader
030     * leaking for plugins
031     *
032     * @author Shuyang Zhou
033     */
034    public class FieldInterceptionHelperUtil {
035    
036            public static void initialize() {
037                    try {
038                            Field instrumentedCacheField = ReflectionUtil.getDeclaredField(
039                                    FieldInterceptionHelper.class, "_instrumentedCache");
040    
041                            instrumentedCacheField.set(
042                                    null,
043                                    new ConcurrentReferenceKeyHashMap<Class<?>, Boolean>(
044                                            FinalizeManager.WEAK_REFERENCE_FACTORY));
045                    }
046                    catch (NoSuchFieldException nsfe) {
047                            _log.error(
048                                    "Missing Hibernate FieldInterceptionHelper patch from " +
049                                            "LPS-52218");
050                    }
051                    catch (Exception e) {
052                            _log.error(e, e);
053                    }
054            }
055    
056            private static final Log _log = LogFactoryUtil.getLog(
057                    FieldInterceptionHelperUtil.class);
058    
059    }