View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program 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
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.service;
20  
21  import davmail.BundleMessage;
22  import davmail.DavGateway;
23  import davmail.Settings;
24  import davmail.ui.tray.DavGatewayTray;
25  import org.boris.winrun4j.AbstractService;
26  import org.boris.winrun4j.ServiceException;
27  
28  /**
29   * WinRun4J DavMail service.
30   */
31  public class DavService extends AbstractService {
32  
33      protected boolean stopped;
34  
35      /**
36       * Perform a service request.
37       *
38       * @param control service control.
39       * @return return code.
40       * @throws ServiceException on error.
41       */
42      @Override
43      public int serviceRequest(int control) throws ServiceException {
44          switch (control) {
45              case SERVICE_CONTROL_STOP:
46              case SERVICE_CONTROL_SHUTDOWN:
47                  DavGatewayTray.debug(new BundleMessage("LOG_STOPPING_DAVMAIL"));
48                  DavGateway.stop();
49                  stopped = true;
50          }
51          return 0;
52      }
53  
54      /**
55       * Run the service.
56       *
57       * @param args command line arguments
58       * @return return code
59       * @throws ServiceException on error
60       */
61      public int serviceMain(String[] args) throws ServiceException {
62          if (args.length >= 1) {
63              Settings.setConfigFilePath(args[0]);
64          }
65  
66          Settings.load();
67          if (!Settings.getBooleanProperty("davmail.server")) {
68              Settings.setProperty("davmail.server", "true");
69              Settings.updateLoggingConfig();
70          }
71  
72          DavGateway.start();
73          DavGatewayTray.debug(new BundleMessage("LOG_DAVMAIL_STARTED"));
74  
75          // launch a non daemon thread
76          Thread shutdownListenerThread = new Thread("ShutDownListener") {
77              public void run() {
78                  try {
79                      while (!stopped) {
80                          Thread.sleep(1000);
81                      }
82                  } catch (InterruptedException e) {
83                      DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED"));
84                      DavGateway.stop();
85                      DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_STOP"));
86                  }
87              }
88          };
89          shutdownListenerThread.start();
90          return 0;
91      }
92  }