001    /**
002     * Copyright (c) 2000-2011 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.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                    if (_portalLifecyclesInit == null) {
052                            portalLifecycle.portalInit();
053                    }
054                    else {
055                            _portalLifecyclesInit.add(portalLifecycle);
056                    }
057    
058                    _portalLifecyclesDestroy.add(portalLifecycle);
059            }
060    
061            public static synchronized void removeDestroy(
062                    PortalLifecycle portalLifecycle) {
063    
064                    if (!_inFlushDestroys) {
065                            _portalLifecyclesDestroy.remove(portalLifecycle);
066                    }
067            }
068    
069            private static boolean _inFlushDestroys;
070            private static List<PortalLifecycle> _portalLifecyclesDestroy =
071                    new Vector<PortalLifecycle>();
072            private static List<PortalLifecycle> _portalLifecyclesInit =
073                    new Vector<PortalLifecycle>();
074    
075    }