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.kernel.util;
016    
017    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class PortalLifecycleUtil {
027    
028            public static void flushDestroys() {
029                    _inFlushDestroys = true;
030    
031                    for (int i = _portalLifecyclesDestroy.size() - 1; i >= 0; i--) {
032                            PortalLifecycle portalLifecycle = _portalLifecyclesDestroy.get(i);
033    
034                            portalLifecycle.portalDestroy();
035                    }
036    
037                    _portalLifecyclesDestroy.clear();
038    
039                    _inFlushDestroys = false;
040            }
041    
042            @SuppressWarnings("deprecation")
043            public static void flushInits() {
044                    if (_portalLifecyclesInit != null) {
045                            List<PortalLifecycle> portalLifecyclesInit = _portalLifecyclesInit;
046    
047                            _portalLifecyclesInit = null;
048    
049                            for (PortalLifecycle portalLifecycle : portalLifecyclesInit) {
050                                    portalLifecycle.portalInit();
051                            }
052                    }
053    
054                    PortalInitableUtil.flushInitables();
055            }
056    
057            public static void register(PortalLifecycle portalLifecycle) {
058                    register(portalLifecycle, PortalLifecycle.METHOD_ALL);
059            }
060    
061            public static void register(PortalLifecycle portalLifecycle, int method) {
062                    if ((method == PortalLifecycle.METHOD_ALL) ||
063                            (method == PortalLifecycle.METHOD_INIT)) {
064    
065                            if (_portalLifecyclesInit == null) {
066                                    Thread currentThread = Thread.currentThread();
067    
068                                    String servletContextName = ClassLoaderPool.getContextName(
069                                            currentThread.getContextClassLoader());
070    
071                                    if (!HotDeployUtil.registerDependentPortalLifecycle(
072                                                    servletContextName, portalLifecycle)) {
073    
074                                            portalLifecycle.portalInit();
075                                    }
076                            }
077                            else {
078                                    _portalLifecyclesInit.add(portalLifecycle);
079                            }
080                    }
081    
082                    if ((method == PortalLifecycle.METHOD_ALL) ||
083                            (method == PortalLifecycle.METHOD_DESTROY)) {
084    
085                            _portalLifecyclesDestroy.add(portalLifecycle);
086                    }
087            }
088    
089            public static void removeDestroy(PortalLifecycle portalLifecycle) {
090                    if (!_inFlushDestroys) {
091                            _portalLifecyclesDestroy.remove(portalLifecycle);
092                    }
093            }
094    
095            public static void reset() {
096                    _inFlushDestroys = false;
097    
098                    if (_portalLifecyclesInit == null) {
099                            _portalLifecyclesInit = Collections.synchronizedList(
100                                    new ArrayList<PortalLifecycle>());
101                    }
102                    else {
103                            _portalLifecyclesInit.clear();
104                    }
105    
106                    _portalLifecyclesDestroy.clear();
107            }
108    
109            private static boolean _inFlushDestroys;
110            private static List<PortalLifecycle> _portalLifecyclesDestroy =
111                    Collections.synchronizedList(new ArrayList<PortalLifecycle>());
112            private static List<PortalLifecycle> _portalLifecyclesInit =
113                    Collections.synchronizedList(new ArrayList<PortalLifecycle>());
114    
115    }