001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.List;
018    import java.util.Vector;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class PortalLifecycleUtil {
024    
025            public static synchronized 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 synchronized void flushInits() {
039                    if (_portalLifecyclesInit != null) {
040                            for (PortalLifecycle portalLifecycle : _portalLifecyclesInit) {
041                                    portalLifecycle.portalInit();
042                            }
043    
044                            _portalLifecyclesInit = null;
045                    }
046    
047                    PortalInitableUtil.flushInitables();
048            }
049    
050            public static synchronized void register(PortalLifecycle portalLifecycle) {
051                    register(portalLifecycle, PortalLifecycle.METHOD_ALL);
052            }
053    
054            public static synchronized void register(
055                    PortalLifecycle portalLifecycle, int method) {
056    
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 synchronized void removeDestroy(
076                    PortalLifecycle portalLifecycle) {
077    
078                    if (!_inFlushDestroys) {
079                            _portalLifecyclesDestroy.remove(portalLifecycle);
080                    }
081            }
082    
083            private static boolean _inFlushDestroys;
084            private static List<PortalLifecycle> _portalLifecyclesDestroy =
085                    new Vector<PortalLifecycle>();
086            private static List<PortalLifecycle> _portalLifecyclesInit =
087                    new Vector<PortalLifecycle>();
088    
089    }