View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.client.spooledService;
21  
22  import org.waarp.common.file.FileUtils;
23  import org.waarp.common.future.WaarpFuture;
24  import org.waarp.common.logging.SysErrLogger;
25  import org.waarp.common.logging.WaarpLogger;
26  import org.waarp.common.logging.WaarpLoggerFactory;
27  import org.waarp.common.service.EngineAbstract;
28  import org.waarp.common.utility.ParametersChecker;
29  import org.waarp.common.utility.SystemPropertyUtil;
30  import org.waarp.common.utility.WaarpShutdownHook;
31  import org.waarp.openr66.client.SpooledDirectoryTransfer;
32  import org.waarp.openr66.protocol.configuration.Configuration;
33  import org.waarp.openr66.protocol.configuration.Messages;
34  import org.waarp.openr66.protocol.configuration.R66SystemProperties;
35  import org.waarp.openr66.protocol.utils.ChannelUtils;
36  
37  import java.io.FileInputStream;
38  import java.util.ArrayList;
39  import java.util.Properties;
40  import java.util.concurrent.TimeUnit;
41  
42  /**
43   * Engine used to start and stop the SpooledDirectory service
44   */
45  public class SpooledEngine extends EngineAbstract {
46    /**
47     * Internal Logger
48     */
49    private static final WaarpLogger logger =
50        WaarpLoggerFactory.getLogger(SpooledEngine.class);
51  
52    static final WaarpFuture closeFuture = new WaarpFuture(true);
53    private static final String[] STRING_0_LENGTH = {};
54  
55    @Override
56    public void run() {
57      final String config =
58          SystemPropertyUtil.get(R66SystemProperties.OPENR66_CONFIGFILE);
59      if (config == null) {
60        logger.error("Cannot find " + R66SystemProperties.OPENR66_CONFIGFILE +
61                     " parameter for SpooledEngine");
62        closeFuture.cancel();
63        shutdown();
64        return;
65      }
66      Configuration.configuration.getShutdownConfiguration().serviceFuture =
67          closeFuture;
68      try {
69        final Properties prop = new Properties();
70        final FileInputStream in = new FileInputStream(config);
71        try {
72          prop.load(in);
73          final ArrayList<String> array = new ArrayList<String>();
74          for (final Object okey : prop.keySet()) {
75            final String key = (String) okey;
76            final String val = prop.getProperty(key);
77            if ("xmlfile".equals(key)) {
78              if (ParametersChecker.isNotEmpty(val)) {
79                array.add(0, val);
80              } else {
81                throw new Exception("Initialization in error: missing xmlfile");
82              }
83            } else {
84              array.add('-' + key);
85              if (ParametersChecker.isNotEmpty(val)) {
86                array.add(val);
87              }
88            }
89          }
90          if (!SpooledDirectoryTransfer.initialize(array.toArray(STRING_0_LENGTH),
91                                                   false)) {
92            throw new Exception("Initialization in error");
93          }
94        } finally {
95          FileUtils.close(in);
96        }
97      } catch (final Throwable e) {
98        logger.error("Cannot start SpooledDirectory", e);
99        closeFuture.cancel();
100       shutdown();
101       return;
102     }
103     logger.warn("SpooledDirectory Service started with " + config);
104   }
105 
106   @Override
107   public final void shutdown() {
108     WaarpShutdownHook.shutdownWillStart();
109     logger.info("Shutdown");
110     for (final SpooledDirectoryTransfer spooled : SpooledDirectoryTransfer.list) {
111       spooled.stop();
112     }
113     Configuration.configuration.setTimeoutCon(
114         Configuration.configuration.getTimeoutCon() / 10);
115     try {
116       while (!SpooledDirectoryTransfer.executorService.awaitTermination(
117           Configuration.configuration.getTimeoutCon(), TimeUnit.MILLISECONDS)) {
118         Thread.sleep(Configuration.configuration.getTimeoutCon());
119       }
120     } catch (final InterruptedException e) {//NOSONAR
121       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
122     }
123     for (final SpooledDirectoryTransfer spooledDirectoryTransfer : SpooledDirectoryTransfer.list) {
124       logger.warn(Messages.getString("SpooledDirectoryTransfer.58") +
125                   spooledDirectoryTransfer.name + ": " +
126                   spooledDirectoryTransfer.getSent() + " success, " +
127                   spooledDirectoryTransfer.getError() + Messages.getString(
128           "SpooledDirectoryTransfer.60")); //$NON-NLS-1$
129     }
130     SpooledDirectoryTransfer.list.clear();
131     logger.info("Shutdown network");
132     SpooledDirectoryTransfer.networkTransactionStatic.closeAll(false);
133     logger.info("All");
134     ChannelUtils.startShutdown();
135     closeFuture.setSuccess();
136     logger.info("SpooledDirectory Service stopped");
137   }
138 
139   @Override
140   public final boolean isShutdown() {
141     return closeFuture.isDone();
142   }
143 
144   @Override
145   public final boolean waitShutdown() {
146     closeFuture.awaitOrInterruptible();
147     logger.info("Shutdown on going: {}", closeFuture.isSuccess());
148     return closeFuture.isSuccess();
149   }
150 }