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.kernel.util;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class PortalLifecycleUtil {
024    
025            public static void flushDestroys() {
026                    _inFlushDestroys = true;
027    
028                    for (PortalLifecycle portalLifecycle : _portalLifecyclesDestroy) {
029                            portalLifecycle.portalDestroy();
030                    }
031    
032                    _portalLifecyclesDestroy.clear();
033    
034                    _inFlushDestroys = false;
035            }
036    
037            @SuppressWarnings("deprecation")
038            public static void flushInits() {
039                    if (_portalLifecyclesInit != null) {
040                            List<PortalLifecycle> portalLifecyclesInit = _portalLifecyclesInit;
041    
042                            _portalLifecyclesInit = null;
043    
044                            for (PortalLifecycle portalLifecycle : portalLifecyclesInit) {
045                                    portalLifecycle.portalInit();
046                            }
047                    }
048    
049                    PortalInitableUtil.flushInitables();
050            }
051    
052            public static void register(PortalLifecycle portalLifecycle) {
053                    register(portalLifecycle, PortalLifecycle.METHOD_ALL);
054            }
055    
056            public static void register(PortalLifecycle portalLifecycle, int method) {
057                    if ((method == PortalLifecycle.METHOD_ALL) ||
058                            (method == PortalLifecycle.METHOD_INIT)) {
059    
060                            if (_portalLifecyclesInit == null) {
061                                    portalLifecycle.portalInit();
062                            }
063                            else {
064                                    _portalLifecyclesInit.add(portalLifecycle);
065                            }
066                    }
067    
068                    if ((method == PortalLifecycle.METHOD_ALL) ||
069                            (method == PortalLifecycle.METHOD_DESTROY)) {
070    
071                            _portalLifecyclesDestroy.add(portalLifecycle);
072                    }
073            }
074    
075            public static void removeDestroy(PortalLifecycle portalLifecycle) {
076                    if (!_inFlushDestroys) {
077                            _portalLifecyclesDestroy.remove(portalLifecycle);
078                    }
079            }
080    
081            private static boolean _inFlushDestroys;
082            private static List<PortalLifecycle> _portalLifecyclesDestroy =
083                    new ArrayList<PortalLifecycle>();
084            private static List<PortalLifecycle> _portalLifecyclesInit =
085                    new ArrayList<PortalLifecycle>();
086    
087    }