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

File DefaultRemoteObservationManager.java

 

Coverage histogram

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

Code metrics

10
31
6
1
203
115
15
0.48
5.17
6
2.5

Classes

Class Line # Actions
DefaultRemoteObservationManager 53 31 0% 15 7
0.8510638585.1%
 

Contributing tests

This file is covered by 1 test. .

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 javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.slf4j.Logger;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.component.phase.Initializable;
30    import org.xwiki.component.phase.InitializationException;
31    import org.xwiki.context.Execution;
32    import org.xwiki.context.ExecutionContext;
33    import org.xwiki.context.ExecutionContextManager;
34    import org.xwiki.observation.ObservationManager;
35    import org.xwiki.observation.event.ApplicationStoppedEvent;
36    import org.xwiki.observation.remote.LocalEventData;
37    import org.xwiki.observation.remote.NetworkAdapter;
38    import org.xwiki.observation.remote.RemoteEventData;
39    import org.xwiki.observation.remote.RemoteEventException;
40    import org.xwiki.observation.remote.RemoteObservationManager;
41    import org.xwiki.observation.remote.RemoteObservationManagerConfiguration;
42    import org.xwiki.observation.remote.RemoteObservationManagerContext;
43    import org.xwiki.observation.remote.converter.EventConverterManager;
44   
45    /**
46    * JGoups based {@link RemoteObservationManager}. It's also the default implementation for now.
47    *
48    * @version $Id: 223a2070281737243dada6d6618eff142fe8307d $
49    * @since 2.0M3
50    */
51    @Component
52    @Singleton
 
53    public class DefaultRemoteObservationManager implements RemoteObservationManager, Initializable
54    {
55    /**
56    * Access {@link RemoteObservationManager} configuration.
57    */
58    @Inject
59    private RemoteObservationManagerConfiguration configuration;
60   
61    /**
62    * Used to convert local event from and to remote event.
63    */
64    @Inject
65    private EventConverterManager eventConverterManager;
66   
67    /**
68    * Used to inject event coming from network.
69    */
70    @Inject
71    private ObservationManager observationManager;
72   
73    /**
74    * Used to set some extra information about the current event injected to the local {@link ObservationManager}.
75    */
76    @Inject
77    private RemoteObservationManagerContext remoteEventManagerContext;
78   
79    /**
80    * Used to initialize ExecutionContext for the remote->local thread.
81    */
82    @Inject
83    private Execution execution;
84   
85    /**
86    * Used to initialize ExecutionContext for the remote->local thread.
87    */
88    @Inject
89    private ExecutionContextManager executionContextManager;
90   
91    /**
92    * Used to lookup the network adapter.
93    */
94    @Inject
95    private ComponentManager componentManager;
96   
97    /**
98    * The logger to log.
99    */
100    @Inject
101    private Logger logger;
102   
103    /**
104    * The network adapter to use to actually send and receive network messages.
105    */
106    private NetworkAdapter networkAdapter;
107   
 
108  5 toggle @Override
109    public void initialize() throws InitializationException
110    {
111  5 try {
112  5 String networkAdapterHint = this.configuration.getNetworkAdapter();
113  5 this.networkAdapter = this.componentManager.getInstance(NetworkAdapter.class, networkAdapterHint);
114    } catch (ComponentLookupException e) {
115  0 throw new InitializationException("Failed to initialize network adapter ["
116    + this.configuration.getNetworkAdapter() + "]", e);
117    }
118   
119    // Start configured channels and register them against the JMX server
120  5 for (String channelId : this.configuration.getChannels()) {
121  4 try {
122  4 startChannel(channelId);
123    } catch (RemoteEventException e) {
124  0 this.logger.error("Failed to start channel [" + channelId + "]", e);
125    }
126    }
127    }
128   
 
129  1083 toggle @Override
130    public void notify(LocalEventData localEvent)
131    {
132  1085 if (this.remoteEventManagerContext.isRemoteState()) {
133    // the event is a remote event
134  24 return;
135    }
136   
137    // Convert local->remote
138  1061 RemoteEventData remoteEvent = this.eventConverterManager.createRemoteEventData(localEvent);
139   
140    // if remote event data is not filled it means the message should not be sent to the network
141  1060 if (remoteEvent != null) {
142  34 this.networkAdapter.send(remoteEvent);
143    }
144   
145  1062 if (localEvent.getEvent() instanceof ApplicationStoppedEvent) {
146  3 try {
147  3 this.networkAdapter.stopAllChannels();
148    } catch (RemoteEventException e) {
149  0 this.logger.error("Failed to stop channels", e);
150    }
151    }
152    }
153   
 
154  9 toggle @Override
155    public void notify(RemoteEventData remoteEvent)
156    {
157    // Make sure the Execution context is properly initialized
158  9 initializeContext();
159   
160  9 LocalEventData localEvent = this.eventConverterManager.createLocalEventData(remoteEvent);
161   
162    // send event
163  9 if (localEvent != null) {
164    // Indicate all the following events are remote events
165  9 this.remoteEventManagerContext.pushRemoteState();
166   
167  9 try {
168  9 this.observationManager.notify(localEvent.getEvent(), localEvent.getSource(), localEvent.getData());
169    } finally {
170    // Indicate all the following events are local events
171  9 this.remoteEventManagerContext.popRemoteState();
172    }
173    }
174    }
175   
 
176  5 toggle @Override
177    public void startChannel(String channelId) throws RemoteEventException
178    {
179  5 this.networkAdapter.startChannel(channelId);
180    }
181   
 
182  0 toggle @Override
183    public void stopChannel(String channelId) throws RemoteEventException
184    {
185  0 this.networkAdapter.stopChannel(channelId);
186    }
187   
188    /**
189    * Make sure an ExecutionContext initialized for remote->local thread.
190    */
 
191  9 toggle private void initializeContext()
192    {
193  9 if (this.execution.getContext() == null) {
194  4 ExecutionContext context = new ExecutionContext();
195   
196  4 try {
197  4 this.executionContextManager.initialize(context);
198    } catch (Exception e) {
199  0 this.logger.error("failed to initialize execution context", e);
200    }
201    }
202    }
203    }