1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.internal

File StackingComponentEventManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
18
10
2
180
83
13
0.72
1.8
5
1.3

Classes

Class Line # Actions
StackingComponentEventManager 40 15 0% 12 0
1.0100%
StackingComponentEventManager.ComponentEventEntry 151 3 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 64 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.internal;
21   
22    import java.util.Stack;
23   
24    import org.xwiki.component.descriptor.ComponentDescriptor;
25    import org.xwiki.component.event.ComponentDescriptorAddedEvent;
26    import org.xwiki.component.event.ComponentDescriptorRemovedEvent;
27    import org.xwiki.component.manager.ComponentEventManager;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.observation.ObservationManager;
30    import org.xwiki.observation.event.Event;
31   
32    /**
33    * Allow stacking component events and flush them whenever the user of this class wants to. This is used for example at
34    * application initialization time when we don't want to send events before the Application Context has been initialized
35    * since components subscribing to these events may want to use the Application Context.
36    *
37    * @version $Id: df3d1d30dfccfa11d1151fdb47a93ee69faf152c $
38    * @since 2.0M1
39    */
 
40    public class StackingComponentEventManager implements ComponentEventManager
41    {
42    /**
43    * The wrapped observation manager.
44    */
45    private ObservationManager observationManager;
46   
47    /**
48    * The event stacked before been given the order to send them.
49    */
50    private Stack<ComponentEventEntry> events = new Stack<ComponentEventEntry>();
51   
52    /**
53    * Indicate if event should be retained to directly sent.
54    */
55    private boolean shouldStack = true;
56   
 
57  1 toggle @Override
58    public void notifyComponentRegistered(ComponentDescriptor<?> descriptor)
59    {
60  1 notifyComponentEvent(new ComponentDescriptorAddedEvent(descriptor.getRoleType(), descriptor.getRoleHint()),
61    descriptor, null);
62    }
63   
 
64  16304 toggle @Override
65    public void notifyComponentRegistered(ComponentDescriptor<?> descriptor, ComponentManager componentManager)
66    {
67  16304 notifyComponentEvent(new ComponentDescriptorAddedEvent(descriptor.getRoleType(), descriptor.getRoleHint()),
68    descriptor, componentManager);
69    }
70   
 
71  1 toggle @Override
72    public void notifyComponentUnregistered(ComponentDescriptor<?> descriptor)
73    {
74  1 notifyComponentEvent(new ComponentDescriptorRemovedEvent(descriptor.getRoleType(), descriptor.getRoleHint()),
75    descriptor, null);
76    }
77   
 
78  5806 toggle @Override
79    public void notifyComponentUnregistered(ComponentDescriptor<?> descriptor, ComponentManager componentManager)
80    {
81  5806 notifyComponentEvent(new ComponentDescriptorRemovedEvent(descriptor.getRoleType(), descriptor.getRoleHint()),
82    descriptor, componentManager);
83    }
84   
85    /**
86    * Force to send all stored events.
87    */
 
88  207 toggle public synchronized void flushEvents()
89    {
90  531 while (!this.events.isEmpty()) {
91  324 ComponentEventEntry entry = this.events.pop();
92  324 sendEvent(entry.event, entry.descriptor, entry.componentManager);
93    }
94    }
95   
96    /**
97    * @param shouldStack indicate of the events received should be stacked
98    */
 
99  417 toggle public void shouldStack(boolean shouldStack)
100    {
101  417 this.shouldStack = shouldStack;
102    }
103   
104    /**
105    * @param observationManager the wrapped observation manager
106    */
 
107  250 toggle public void setObservationManager(ObservationManager observationManager)
108    {
109  250 this.observationManager = observationManager;
110    }
111   
112    /**
113    * Send or stack the provided event dependening on the configuration.
114    *
115    * @param event the event send by the component manager
116    * @param descriptor the event related component descriptor.
117    * @param componentManager the event related component manager instance.
118    * @see #shouldStack(boolean)
119    */
 
120  22112 toggle private void notifyComponentEvent(Event event, ComponentDescriptor<?> descriptor,
121    ComponentManager componentManager)
122    {
123  22112 if (this.shouldStack) {
124  13910 synchronized (this) {
125  13910 this.events.push(new ComponentEventEntry(event, descriptor, componentManager));
126    }
127    } else {
128  8202 sendEvent(event, descriptor, componentManager);
129    }
130    }
131   
132    /**
133    * Send the event.
134    *
135    * @param event the event to send
136    * @param descriptor the event related component descriptor.
137    * @param componentManager the event related component manager instance.
138    */
 
139  8526 toggle private void sendEvent(Event event, ComponentDescriptor<?> descriptor, ComponentManager componentManager)
140    {
141  8526 if (this.observationManager != null) {
142  8388 this.observationManager.notify(event, componentManager, descriptor);
143    }
144    }
145   
146    /**
147    * Contains a stacked event.
148    *
149    * @version $Id: df3d1d30dfccfa11d1151fdb47a93ee69faf152c $
150    */
 
151    static class ComponentEventEntry
152    {
153    /**
154    * The stacked event.
155    */
156    public Event event;
157   
158    /**
159    * The event related component descriptor.
160    */
161    public ComponentDescriptor<?> descriptor;
162   
163    /**
164    * The event related component manager instance.
165    */
166    public ComponentManager componentManager;
167   
168    /**
169    * @param event the stacked event.
170    * @param descriptor the event related component descriptor.
171    * @param componentManager the event related component manager instance.
172    */
 
173  13910 toggle ComponentEventEntry(Event event, ComponentDescriptor<?> descriptor, ComponentManager componentManager)
174    {
175  13910 this.event = event;
176  13910 this.descriptor = descriptor;
177  13910 this.componentManager = componentManager;
178    }
179    }
180    }