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

File LocalEventListener.java

 

Coverage histogram

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

Code metrics

4
15
3
1
121
64
6
0.4
5
3
2

Classes

Class Line # Actions
LocalEventListener 51 15 0% 6 1
0.9545454495.5%
 

Contributing tests

This file is covered by 59 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.remote.internal;
21   
22    import java.util.Collections;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.observation.EventListener;
34    import org.xwiki.observation.ObservationManager;
35    import org.xwiki.observation.event.AllEvent;
36    import org.xwiki.observation.event.Event;
37    import org.xwiki.observation.remote.LocalEventData;
38    import org.xwiki.observation.remote.RemoteObservationManager;
39    import org.xwiki.observation.remote.RemoteObservationManagerConfiguration;
40   
41    /**
42    * Register to {@link org.xwiki.observation.ObservationManager} for all events and send them to
43    * {@link RemoteObservationManager}.
44    *
45    * @version $Id: 27d2a0168f2b2fa19af33c745a7ff387febaa74f $
46    * @since 2.0M3
47    */
48    @Component
49    @Named("observation.remote")
50    @Singleton
 
51    public class LocalEventListener implements EventListener
52    {
53    /**
54    * The name of the listener.
55    */
56    private static final String NAME = "observation.remote";
57   
58    /**
59    * Used to know if remote observation manager is enabled.
60    */
61    @Inject
62    private RemoteObservationManagerConfiguration configuration;
63   
64    /**
65    * Used to lookup for {@link RemoteObservationManager} if it's enabled. To avoid initializing it if it's not needed.
66    */
67    @Inject
68    private ComponentManager componentManager;
69   
70    /**
71    * The logger to log.
72    */
73    @Inject
74    private Logger logger;
75   
76    /**
77    * We don't inject {@link RemoteObservationManager} automatically to load it only when necessary (when remote
78    * observation manager is enabled).
79    */
80    private RemoteObservationManager remoteObservationManager;
81   
 
82  192 toggle @Override
83    public List<Event> getEvents()
84    {
85  192 List<Event> events;
86   
87  192 if (this.configuration.isEnabled()) {
88  10 events = Collections.<Event>singletonList(AllEvent.ALLEVENT);
89    } else {
90  182 events = Collections.emptyList();
91    }
92   
93  192 return events;
94    }
95   
 
96  409 toggle @Override
97    public String getName()
98    {
99  409 return NAME;
100    }
101   
 
102  1085 toggle @Override
103    public void onEvent(Event event, Object source, Object data)
104    {
105  1083 if (this.remoteObservationManager == null) {
106  5 try {
107    // Make sure to not receive events until RemoteObservationManager is ready
108  5 ObservationManager om = this.componentManager.getInstance(ObservationManager.class);
109  5 om.removeListener(getName());
110  5 this.remoteObservationManager = this.componentManager.getInstance(RemoteObservationManager.class);
111  5 om.addListener(this);
112   
113  5 this.remoteObservationManager.notify(new LocalEventData(event, source, data));
114    } catch (ComponentLookupException e) {
115  0 this.logger.error("Failed to initialize the Remote Observation Manager", e);
116    }
117    } else {
118  1077 this.remoteObservationManager.notify(new LocalEventData(event, source, data));
119    }
120    }
121    }