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  
27  /**
28   * WinRun4J DavMail service.
29   */
30  public class DavService extends AbstractService {
31  
32      private final Object LOCK = new Object();
33  
34      /**
35       * Perform a service request.
36       *
37       * @param control service control.
38       * @return return code.
39       */
40      @Override
41      public int serviceRequest(int control) {
42          switch (control) {
43              case SERVICE_CONTROL_STOP:
44              case SERVICE_CONTROL_SHUTDOWN:
45                  DavGatewayTray.debug(new BundleMessage("LOG_STOPPING_DAVMAIL"));
46                  DavGateway.stop();
47                  synchronized (LOCK) {
48                      LOCK.notifyAll();
49                  }
50          }
51          return 0;
52      }
53  
54      /**
55       * Run the service.
56       *
57       * @param args command line arguments
58       * @return return code
59       */
60      public int serviceMain(String[] args) {
61          if (args.length >= 1) {
62              Settings.setConfigFilePath(args[0]);
63          }
64  
65          Settings.load();
66          if (!Settings.getBooleanProperty("davmail.server")) {
67              Settings.setProperty("davmail.server", "true");
68              Settings.updateLoggingConfig();
69          }
70  
71          DavGateway.start();
72          DavGatewayTray.debug(new BundleMessage("LOG_DAVMAIL_STARTED"));
73  
74          synchronized (LOCK) {
75              try {
76                  LOCK.wait();
77              } catch (InterruptedException e) {
78                  DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED"));
79                  Thread.currentThread().interrupt();
80              }
81          }
82  
83          return 0;
84      }
85  }