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

File ObservationContextListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% 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 0
1.0100%
 

Contributing tests

This file is covered by 261 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  357 toggle public ObservationContextListener()
65    {
66  357 super("ObservationContextListener", AllEvent.ALLEVENT);
67    }
68   
69    /**
70    * @return the events stacked in the execution context
71    */
 
72  70745 toggle private Stack<BeginEvent> getCurrentEvents()
73    {
74  70759 Stack<BeginEvent> events = null;
75   
76  70771 ExecutionContext context = this.execution.getContext();
77  70781 if (context != null) {
78  70758 events = (Stack<BeginEvent>) context.getProperty(DefaultObservationContext.KEY_EVENTS);
79    }
80   
81  70773 return events;
82    }
83   
84    /**
85    * @param event the event stack
86    */
 
87  70534 toggle private void pushCurrentEvent(BeginEvent event)
88    {
89  70574 ExecutionContext context = this.execution.getContext();
90  70587 if (context != null) {
91  70556 Stack<BeginEvent> events = (Stack<BeginEvent>) context.getProperty(DefaultObservationContext.KEY_EVENTS);
92   
93  70579 if (events == null) {
94  66177 events = new Stack<BeginEvent>();
95  66186 context.setProperty(DefaultObservationContext.KEY_EVENTS, events);
96    }
97   
98  70575 events.push(event);
99    }
100    }
101   
102    // EventListener
103   
 
104  6950331 toggle @Override
105    public void onEvent(Event event, Object source, Object data)
106    {
107  6952229 if (event instanceof BeginEvent) {
108  70561 pushCurrentEvent((BeginEvent) event);
109  6882871 } else if (event instanceof EndEvent) {
110  70759 Stack<BeginEvent> events = getCurrentEvents();
111   
112  70773 if (events != null && !events.isEmpty()) {
113  70584 events.pop();
114    } else {
115  186 this.logger.error("Can't find any begin event corresponding to [{}]", event);
116    }
117    }
118    }
119    }