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;
21  
22  import org.waarp.common.database.data.AbstractDbData;
23  import org.waarp.common.database.exception.WaarpDatabaseException;
24  import org.waarp.common.logging.SysErrLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
27  import org.waarp.common.utility.WaarpSystemUtil;
28  import org.waarp.openr66.client.utils.OutputFormat;
29  import org.waarp.openr66.context.ErrorCode;
30  import org.waarp.openr66.context.R66Result;
31  import org.waarp.openr66.database.data.DbTaskRunner;
32  import org.waarp.openr66.protocol.configuration.Messages;
33  import org.waarp.openr66.protocol.exception.OpenR66DatabaseGlobalException;
34  import org.waarp.openr66.protocol.utils.R66Future;
35  
36  import java.sql.Timestamp;
37  
38  import static org.waarp.common.database.DbConstant.*;
39  
40  /**
41   * Client to submit a transfer
42   */
43  public class SubmitTransfer extends AbstractTransfer {
44  
45    public SubmitTransfer(final R66Future future, final String remoteHost,
46                          final String filename, final String rulename,
47                          final String transferInfo, final boolean isMD5,
48                          final int blocksize, final long id,
49                          final Timestamp starttime) {
50      super(SubmitTransfer.class, future, filename, rulename, transferInfo, isMD5,
51            remoteHost, blocksize, id, starttime);
52    }
53  
54    @Override
55    public void run() {
56      if (logger == null) {
57        logger = WaarpLoggerFactory.getLogger(SubmitTransfer.class);
58      }
59      final DbTaskRunner taskRunner = initRequest();
60      if (taskRunner == null) {
61        logger.info("Cannot prepare task");
62        if (future.isFailed() && future.getResult() != null) {
63          return;
64        }
65        final R66Result result =
66            new R66Result(new OpenR66DatabaseGlobalException(), null, true,
67                          ErrorCode.Internal, null);
68        future.setResult(result);
69        future.setFailure(result.getException());
70        return;
71      }
72      if (transferArgs.getId() != ILLEGALVALUE) {
73        // Resubmit call, some checks are needed
74        if (!taskRunner.restart(true)) {
75          // cannot be done from there => must be done through IHM
76          logger.info("Cannot prepare task from there. IHM must be used");
77          final R66Result result = new R66Result(
78              new OpenR66DatabaseGlobalException(
79                  "Cannot prepare task from there. IHM must be used"), null, true,
80              ErrorCode.Internal, taskRunner);
81          future.setResult(result);
82          future.setFailure(result.getException());
83          return;
84        }
85      } else {
86        taskRunner.changeUpdatedInfo(AbstractDbData.UpdatedInfo.TOSUBMIT);
87      }
88      if (!taskRunner.forceSaveStatus()) {
89        try {
90          if (!taskRunner.specialSubmit()) {
91            logger.info("Cannot prepare task");
92            final R66Result result = new R66Result(
93                new OpenR66DatabaseGlobalException("Cannot prepare Task"), null,
94                true, ErrorCode.Internal, taskRunner);
95            future.setResult(result);
96            future.setFailure(result.getException());
97            return;
98          }
99        } catch (final WaarpDatabaseException e) {
100         logger.info("Cannot prepare task");
101         final R66Result result = new R66Result(
102             new OpenR66DatabaseGlobalException("Cannot prepare Task", e), null,
103             true, ErrorCode.Internal, taskRunner);
104         future.setResult(result);
105         future.setFailure(result.getException());
106         return;
107       }
108     }
109     final R66Result result =
110         new R66Result(null, false, ErrorCode.InitOk, taskRunner);
111     future.setResult(result);
112     future.setSuccess();
113   }
114 
115   /**
116    * @param args configuration file, the remoteHost Id, the file to
117    *     transfer, the rule, file transfer
118    *     information as arguments and optionally isMD5=1 for true or 0 for
119    *     false(default) and the
120    *     blocksize if different than default
121    */
122   public static void main(final String[] args) {
123     WaarpLoggerFactory.setDefaultFactoryIfNotSame(
124         new WaarpSlf4JLoggerFactory(null));
125     if (logger == null) {
126       logger = WaarpLoggerFactory.getLogger(SubmitTransfer.class);
127     }
128     if (!getParams(args, true)) {
129       logger.error(Messages.getString("Configuration.WrongInit")); //$NON-NLS-1$
130       if (!OutputFormat.isQuiet()) {
131         SysErrLogger.FAKE_LOGGER.sysout(
132             Messages.getString("Configuration.WrongInit")); //$NON-NLS-1$
133       }
134       if (admin != null) {
135         admin.close();
136       }
137       WaarpSystemUtil.systemExit(1);
138       return;
139     }
140     final R66Future future = new R66Future(true);
141     final SubmitTransfer transaction =
142         new SubmitTransfer(future, rhost, localFilename, rule, transferInfo,
143                            ismd5, block, idt, ttimestart);
144     transaction.normalInfoAsWarn = snormalInfoAsWarn;
145     transaction.run();
146     future.awaitOrInterruptible();
147     final DbTaskRunner runner = future.getResult().getRunner();
148     final OutputFormat outputFormat =
149         new OutputFormat(SubmitTransfer.class.getSimpleName(), args);
150     if (future.isSuccess()) {
151       prepareSubmitOkOutputFormat(runner, outputFormat);
152       if (transaction.normalInfoAsWarn) {
153         logger.warn(outputFormat.loggerOut());
154       } else if (logger.isInfoEnabled()) {
155         logger.info(outputFormat.loggerOut());
156       }
157       if (!OutputFormat.isQuiet()) {
158         outputFormat.sysout();
159       }
160     } else {
161       prepareSubmitKoOutputFormat(future, runner, outputFormat);
162       if (!OutputFormat.isQuiet()) {
163         outputFormat.sysout();
164       }
165       if (WaarpSystemUtil.isJunit()) {
166         return;
167       }
168       admin.close();
169       WaarpSystemUtil.systemExit(future.getResult().getCode().ordinal());
170       return;
171     }
172     if (!WaarpSystemUtil.isJunit()) {
173       admin.close();
174       WaarpSystemUtil.systemExit(0);
175     }
176   }
177 
178 }