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