View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  import ch.qos.logback.core.spi.LifeCycle;
20  
21  /**
22   * An object that manages a collection of components that implement the
23   * {@link LifeCycle} interface. Each component that is added to the manager will
24   * be stopped and removed from the manager when the manager is reset.
25   *
26   * @author Carl Harris
27   */
28  public class LifeCycleManager {
29  
30      private final Set<LifeCycle> components = new HashSet<LifeCycle>();
31  
32      /**
33       * Registers a component with this manager.
34       * <p>
35       * 
36       * @param component the component whose life cycle is to be managed
37       */
38      public void register(LifeCycle component) {
39          components.add(component);
40      }
41  
42      /**
43       * Resets this manager.
44       * <p>
45       * All registered components are stopped and removed from the manager.
46       */
47      public void reset() {
48          for (LifeCycle component : components) {
49              if (component.isStarted()) {
50                  component.stop();
51              }
52          }
53          components.clear();
54      }
55  
56  }