001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.server.platform;
021    
022    import org.slf4j.LoggerFactory;
023    import org.sonar.api.ServerComponent;
024    import org.sonar.api.platform.ServerStartHandler;
025    import org.sonar.api.platform.ServerStopHandler;
026    import org.sonar.api.platform.Server;
027    
028    /**
029     * @since 2.2
030     */
031    public class ServerLifecycleNotifier implements ServerComponent {
032    
033      private ServerStartHandler[] startHandlers;
034      private ServerStopHandler[] stopHandlers;
035      private Server server;
036    
037      public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers, ServerStopHandler[] stopHandlers) {
038        this.startHandlers = startHandlers;
039        this.stopHandlers = stopHandlers;
040        this.server = server;
041      }
042    
043      public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers) {
044        this(server, startHandlers, new ServerStopHandler[0]);
045      }
046    
047      public ServerLifecycleNotifier(Server server, ServerStopHandler[] stopHandlers) {
048        this(server, new ServerStartHandler[0], stopHandlers);
049      }
050    
051      public ServerLifecycleNotifier(Server server) {
052        this(server, new ServerStartHandler[0], new ServerStopHandler[0]);
053      }
054    
055      public void start() {
056        /* IMPORTANT :
057         we want to be sure that handlers are notified when all other services are started.
058         That's why the class Platform explicitely executes the method notifyStart(), instead of letting picocontainer
059         choose the startup order.
060         */
061      }
062    
063      public void notifyStart() {
064        LoggerFactory.getLogger(ServerLifecycleNotifier.class).debug("Notify " + ServerStartHandler.class.getSimpleName() + " handlers...");
065        for (ServerStartHandler handler : startHandlers) {
066          handler.onServerStart(server);
067        }
068      }
069    
070      public void stop() {
071        LoggerFactory.getLogger(ServerLifecycleNotifier.class).debug("Notify " + ServerStopHandler.class.getSimpleName() + " handlers...");
072        for (ServerStopHandler handler : stopHandlers) {
073          handler.onServerStop(server);
074        }
075      }
076    }