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

File MailSenderInitializerListener.java

 

Coverage histogram

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

Code metrics

4
23
5
1
147
78
8
0.35
4.6
5
1.6

Classes

Class Line # Actions
MailSenderInitializerListener 50 23 0% 8 1
0.9687596.9%
 

Contributing tests

No tests hitting this source file were found.

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.mail.internal.thread;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32    import org.xwiki.bridge.event.ApplicationReadyEvent;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.manager.ComponentLifecycleException;
35    import org.xwiki.component.phase.Disposable;
36    import org.xwiki.observation.EventListener;
37    import org.xwiki.observation.event.Event;
38   
39    /**
40    * Used to trigger the mails Thread used to prepare and send mails. Note that we want application to be ready before
41    * starting the mail threads since sending mail require access to the configuration which is usually defined in wiki
42    * pages.
43    *
44    * @version $Id: 379e639e63e8e3056cdbf044308a82bc57631b92 $
45    * @since 7.4.5
46    */
47    @Component
48    @Named(MailSenderInitializerListener.LISTENER_NAME)
49    @Singleton
 
50    public class MailSenderInitializerListener implements EventListener, Disposable
51    {
52    /**
53    * The name of the listener.
54    */
55    public static final String LISTENER_NAME = "MailSenderInitializationEventListener";
56   
57    /**
58    * The events observed by this event listener.
59    */
60    private static final List<Event> EVENTS = new ArrayList<Event>(Arrays.asList(new ApplicationReadyEvent()));
61   
62    /**
63    * Logger to use to log shutdown information (opposite of initialization).
64    */
65    private static final Logger SHUTDOWN_LOGGER = LoggerFactory.getLogger("org.xwiki.shutdown");
66   
67    @Inject
68    @Named("prepare")
69    private MailRunnable prepareMailRunnable;
70   
71    @Inject
72    @Named("send")
73    private MailRunnable sendMailRunnable;
74   
75    private Thread prepareMailThread;
76   
77    private Thread sendMailThread;
78   
 
79  28 toggle @Override
80    public String getName()
81    {
82  28 return LISTENER_NAME;
83    }
84   
 
85  7 toggle @Override
86    public List<Event> getEvents()
87    {
88  7 return EVENTS;
89    }
90   
 
91  10 toggle @Override
92    public void onEvent(Event event, Object o, Object o1)
93    {
94    // Step 1: Start the Mail Prepare Thread
95  10 this.prepareMailThread = new Thread(this.prepareMailRunnable);
96  10 this.prepareMailThread.setName("Mail Prepare Thread");
97  10 this.prepareMailThread.setDaemon(true);
98  10 this.prepareMailThread.start();
99   
100    // Step 2: Start the Mail Sender Thread
101  10 this.sendMailThread = new Thread(this.sendMailRunnable);
102  10 this.sendMailThread.setName("Mail Sender Thread");
103  10 this.sendMailThread.setDaemon(true);
104  10 this.sendMailThread.start();
105   
106    }
107   
108    /**
109    * Stops the Mail Prepare and Sender threads. Should be called when the application is stopped for a clean shutdown.
110    *
111    * @throws InterruptedException if a thread fails to be stopped
112    */
 
113  19 toggle private void stopMailThreads() throws InterruptedException
114    {
115    // Step 1: Stop the Mail Sender Thread
116   
117  19 if (this.sendMailThread != null) {
118  16 this.sendMailRunnable.stopProcessing();
119    // Make sure the Thread goes out of sleep if it's sleeping so that it stops immediately.
120  16 this.sendMailThread.interrupt();
121    // Wait till the thread goes away
122  16 this.sendMailThread.join();
123  16 SHUTDOWN_LOGGER.debug(String.format("Mail Prepare Thread has been stopped"));
124    }
125   
126    // Step 2: Stop the Mail Prepare Thread
127   
128  19 if (this.prepareMailThread != null) {
129  16 this.prepareMailRunnable.stopProcessing();
130    // Make sure the Thread goes out of sleep if it's sleeping so that it stops immediately.
131  16 this.prepareMailThread.interrupt();
132    // Wait till the thread goes away
133  16 this.prepareMailThread.join();
134  16 SHUTDOWN_LOGGER.debug(String.format("Mail Sender Thread has been stopped"));
135    }
136    }
137   
 
138  19 toggle @Override
139    public void dispose() throws ComponentLifecycleException
140    {
141  19 try {
142  19 stopMailThreads();
143    } catch (InterruptedException e) {
144  0 SHUTDOWN_LOGGER.debug("Mail threads shutdown has been interruped", e);
145    }
146    }
147    }