View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.event.PluginEventListener;
4   import com.atlassian.plugin.event.PluginEventManager;
5   import com.atlassian.plugin.event.events.PluginFrameworkShutdownEvent;
6   import com.atlassian.plugin.event.events.PluginFrameworkStartedEvent;
7   import com.atlassian.plugin.event.events.PluginModuleDisabledEvent;
8   import com.atlassian.plugin.event.events.PluginModuleEnabledEvent;
9   
10  import java.util.concurrent.atomic.AtomicLong;
11  
12  /**
13   * A ready-to-use implementation of batching state counter. Ready to be plugged into {@link WebResourceIntegration}.
14   *
15   * @since 2.11.0
16   */
17  public class WebResourceBatchingStateCounterImpl implements WebResourceBatchingStateCounter
18  {
19      private final PluginEventManager pluginEventManager;
20      private final AtomicLong counter;
21      private volatile boolean active = false;
22  
23      public WebResourceBatchingStateCounterImpl(PluginEventManager pluginEventManager)
24      {
25          this.pluginEventManager = pluginEventManager;
26          this.counter = new AtomicLong(0L);
27  
28          pluginEventManager.register(this);
29      }
30  
31      /**
32       * Closes the instance.
33       */
34      public void close()
35      {
36          pluginEventManager.unregister(this);
37      }
38  
39      @PluginEventListener
40      public void onPluginFrameworkStarted(final PluginFrameworkStartedEvent event)
41      {
42          active = true;
43          incrementCounterIfActive();
44      }
45  
46      @PluginEventListener
47      public void onPluginFrameworkPluginFrameworkShutdown(final PluginFrameworkShutdownEvent event)
48      {
49          active = false;
50      }
51  
52      @PluginEventListener
53      public void onPluginModuleEnabled(final PluginModuleEnabledEvent event)
54      {
55          incrementCounterIfActive();
56      }
57  
58      @PluginEventListener
59      public void onPluginModuleDisabled(final PluginModuleDisabledEvent event)
60      {
61          incrementCounterIfActive();
62      }
63  
64      @Override
65      public long getBatchingStateCounter()
66      {
67          return counter.get();
68      }
69  
70      @Override
71      public void incrementCounter()
72      {
73          incrementCounterIfActive();
74      }
75  
76      private void incrementCounterIfActive()
77      {
78          if (active)
79          {
80              counter.incrementAndGet();
81          }
82      }
83  }