Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3e37b306dd1e452440e181e6350b9a4d2f3ce7de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.eclipse.net4j.util.lifecycle;

import org.eclipse.net4j.util.ReflectUtil;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Eike Stepper
 */
public abstract class AbstractLifecycle implements Lifecycle, LifecycleNotifier
{
  public static boolean DUMP_ON_ACTIVATE = false;

  public static boolean USE_LABEL = true;

  private boolean active;

  /**
   * Don't initialize lazily to circumvent synchronization!
   */
  private Queue<LifecycleListener> listeners = new ConcurrentLinkedQueue();

  protected AbstractLifecycle()
  {
  }

  public final void addLifecycleListener(LifecycleListener listener)
  {
    listeners.add(listener);
  }

  public final void removeLifecycleListener(LifecycleListener listener)
  {
    listeners.remove(listener);
  }

  public final synchronized void activate() throws Exception
  {
    if (!active)
    {
      System.out.println(toString() + ": Activating");
      onAccessBeforeActivate();
      onActivate();
      active = true;

      if (DUMP_ON_ACTIVATE)
      {
        ReflectUtil.dump(this, toString() + ": DUMP ");
      }

      fireLifecycleActivated();
    }
  }

  public final synchronized Exception deactivate()
  {
    if (active)
    {
      System.out.println(toString() + ": Deactivating");
      fireLifecycleDeactivating();

      try
      {
        onDeactivate();
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
        return ex;
      }
      finally
      {
        active = false;
      }
    }

    return null;
  }

  public final boolean isActive()
  {
    return active;
  }

  @Override
  public String toString()
  {
    if (USE_LABEL)
    {
      return ReflectUtil.getLabel(this);
    }
    else
    {
      return super.toString();
    }
  }

  protected void fireLifecycleActivated()
  {
    for (LifecycleListener listener : listeners)
    {
      try
      {
        listener.notifyLifecycleActivated(this);
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
      }
    }
  }

  protected void fireLifecycleDeactivating()
  {
    for (LifecycleListener listener : listeners)
    {
      try
      {
        listener.notifyLifecycleDeactivating(this);
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
      }
    }
  }

  protected void onAccessBeforeActivate() throws Exception
  {
  }

  protected void onActivate() throws Exception
  {
  }

  protected void onDeactivate() throws Exception
  {
  }
}

Back to the top