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

File OfficeServerLifecycleListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

6
13
5
1
118
64
10
0.77
2.6
5
2

Classes

Class Line # Actions
OfficeServerLifecycleListener 49 13 0% 10 6
0.7575%
 

Contributing tests

This file is covered by 58 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.officeimporter.internal.server;
21   
22    import java.util.Arrays;
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.observation.EventListener;
32    import org.xwiki.observation.event.ApplicationStartedEvent;
33    import org.xwiki.observation.event.ApplicationStoppedEvent;
34    import org.xwiki.observation.event.Event;
35    import org.xwiki.officeimporter.server.OfficeServer;
36    import org.xwiki.officeimporter.server.OfficeServerConfiguration;
37    import org.xwiki.officeimporter.server.OfficeServerException;
38   
39    /**
40    * Listens to application start and stop events in order to automatically start and stop an office server instance (if
41    * auto start/auto stop is configured).
42    *
43    * @version $Id: b9c3fcc46780aad8ece0c513fef10db340f3db4e $
44    * @since 2.0M1
45    */
46    @Component
47    @Named("OfficeServerLifecycleListener")
48    @Singleton
 
49    public class OfficeServerLifecycleListener implements EventListener
50    {
51    /**
52    * The configuration component.
53    */
54    @Inject
55    private OfficeServerConfiguration officeServerConfig;
56   
57    /**
58    * The office server component.
59    */
60    @Inject
61    private OfficeServer officeServer;
62   
63    /**
64    * The logger to log.
65    */
66    @Inject
67    private Logger logger;
68   
 
69  185 toggle @Override
70    public List<Event> getEvents()
71    {
72  185 return Arrays.asList(new ApplicationStartedEvent(), new ApplicationStoppedEvent());
73    }
74   
 
75  740 toggle @Override
76    public String getName()
77    {
78  740 return "OfficeServerLifecycleListener";
79    }
80   
 
81  64 toggle @Override
82    public void onEvent(Event event, Object source, Object data)
83    {
84  64 if (ApplicationStartedEvent.class.getName().equals(event.getClass().getName())) {
85  32 startOfficeServer();
86  32 } else if (ApplicationStoppedEvent.class.getName().equals(event.getClass().getName())) {
87  32 stopOfficeServer();
88    }
89    }
90   
91    /**
92    * Start the office server if the configuration says to start it automatically.
93    */
 
94  32 toggle private void startOfficeServer()
95    {
96  32 if (this.officeServerConfig.isAutoStart()) {
97  0 try {
98  0 this.officeServer.start();
99    } catch (OfficeServerException ex) {
100  0 this.logger.error(ex.getMessage(), ex);
101    }
102    }
103    }
104   
105    /**
106    * Stop the office server.
107    */
 
108  32 toggle private void stopOfficeServer()
109    {
110    // TODO: We shouldn't stop the office server if it hasn't been started automatically or if the configuration
111    // doesn't say to stop it automatically.
112  32 try {
113  32 this.officeServer.stop();
114    } catch (OfficeServerException ex) {
115  0 this.logger.error(ex.getMessage(), ex);
116    }
117    }
118    }