1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.observation.internal

File ObservationContextListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
20
4
1
119
63
11
0.55
5
4
2.75

Classes

Class Line # Actions
ObservationContextListener 47 20 0% 11 4
0.888888988.9%
 

Contributing tests

This file is covered by 246 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.observation.internal;
21   
22    import java.util.Stack;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.context.ExecutionContext;
32    import org.xwiki.observation.AbstractEventListener;
33    import org.xwiki.observation.event.AllEvent;
34    import org.xwiki.observation.event.BeginEvent;
35    import org.xwiki.observation.event.EndEvent;
36    import org.xwiki.observation.event.Event;
37   
38    /**
39    * Listen to all events and stack {@link BeginEvent}.
40    *
41    * @version $Id: cca3661379223cfa5364c6428d1ea253e6776a00 $
42    * @since 3.2M1
43    */
44    @Component
45    @Singleton
46    @Named("ObservationContextListener")
 
47    public class ObservationContextListener extends AbstractEventListener
48    {
49    /**
50    * The execution.
51    */
52    @Inject
53    private Execution execution;
54   
55    /**
56    * The logger to log.
57    */
58    @Inject
59    private Logger logger;
60   
61    /**
62    * Setup event listener.
63    */
 
64  322 toggle public ObservationContextListener()
65    {
66  322 super("ObservationContextListener", AllEvent.ALLEVENT);
67    }
68   
69    /**
70    * @return the events stacked in the execution context
71    */
 
72  11398 toggle private Stack<BeginEvent> getCurrentEvents()
73    {
74  11398 Stack<BeginEvent> events = null;
75   
76  11396 ExecutionContext context = this.execution.getContext();
77  11401 if (context != null) {
78  11403 events = (Stack<BeginEvent>) context.getProperty(DefaultObservationContext.KEY_EVENTS);
79    }
80   
81  11405 return events;
82    }
83   
84    /**
85    * @param event the event stack
86    */
 
87  11384 toggle private void pushCurrentEvent(BeginEvent event)
88    {
89  11388 ExecutionContext context = this.execution.getContext();
90  11398 if (context != null) {
91  11399 Stack<BeginEvent> events = (Stack<BeginEvent>) context.getProperty(DefaultObservationContext.KEY_EVENTS);
92   
93  11397 if (events == null) {
94  9892 events = new Stack<BeginEvent>();
95  9895 context.setProperty(DefaultObservationContext.KEY_EVENTS, events);
96    }
97   
98  11409 events.push(event);
99    }
100    }
101   
102    // EventListener
103   
 
104  494102 toggle @Override
105    public void onEvent(Event event, Object source, Object data)
106    {
107  494046 if (event instanceof BeginEvent) {
108  11402 pushCurrentEvent((BeginEvent) event);
109  482649 } else if (event instanceof EndEvent) {
110  11401 Stack<BeginEvent> events = getCurrentEvents();
111   
112  11400 if (events != null && !events.isEmpty()) {
113  11398 events.pop();
114    } else {
115  0 this.logger.error("Can't find any begin event corresponding to [{}]", event);
116    }
117    }
118    }
119    }