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.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
25  import org.waarp.common.utility.WaarpSystemUtil;
26  import org.waarp.openr66.client.utils.OutputFormat;
27  import org.waarp.openr66.client.utils.OutputFormat.FIELDS;
28  import org.waarp.openr66.context.ErrorCode;
29  import org.waarp.openr66.protocol.configuration.Configuration;
30  import org.waarp.openr66.protocol.configuration.Messages;
31  import org.waarp.openr66.protocol.localhandler.packet.BusinessRequestPacket;
32  import org.waarp.openr66.protocol.networkhandler.NetworkTransaction;
33  import org.waarp.openr66.protocol.utils.R66Future;
34  
35  import static org.waarp.common.database.DbConstant.*;
36  
37  /**
38   * class for direct Business Request call
39   */
40  public class BusinessRequest extends AbstractBusinessRequest {
41    /**
42     * Internal Logger
43     */
44    private static WaarpLogger logger;
45    /**
46     * Default class
47     */
48    public static final String DEFAULT_CLASS =
49        "org.waarp.openr66.context.task.ExecBusinessTask";
50  
51    public BusinessRequest(final NetworkTransaction networkTransaction,
52                           final R66Future future, final String remoteHost,
53                           final BusinessRequestPacket packet) {
54      super(BusinessRequest.class, future, remoteHost, networkTransaction,
55            packet);
56    }
57  
58    public static void main(final String[] args) {
59      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
60          new WaarpSlf4JLoggerFactory(null));
61      if (logger == null) {
62        logger = WaarpLoggerFactory.getLogger(BusinessRequest.class);
63      }
64      if (args.length < 5) {
65        logger.error(Messages.getString("BusinessRequest.1") + //$NON-NLS-1$
66                     INFO_ARGS);
67        return;
68      }
69      classname = DEFAULT_CLASS;
70      if (!getParams(args) || classarg == null) {
71        logger.error(Messages.getString("Configuration.WrongInit")); //$NON-NLS-1$
72        if (admin != null) {
73          admin.close();
74        }
75        WaarpSystemUtil.systemExit(2);
76        return;
77      }
78      Configuration.configuration.pipelineInit();
79      final NetworkTransaction networkTransaction = new NetworkTransaction();
80      final R66Future future = new R66Future(true);
81  
82      logger.info("Start Test of Transaction");
83      final long time1 = System.currentTimeMillis();
84  
85      final BusinessRequestPacket packet =
86          new BusinessRequestPacket(classname + ' ' + classarg, 0);
87      final BusinessRequest transaction =
88          new BusinessRequest(networkTransaction, future, rhost, packet);
89      transaction.run();
90      future.awaitOrInterruptible();
91  
92      final long time2 = System.currentTimeMillis();
93      logger.debug("Finish Business Request: {}", future.isSuccess());
94      final long delay = time2 - time1;
95      final OutputFormat outputFormat =
96          new OutputFormat(BusinessRequest.class.getSimpleName(), args);
97      if (future.isSuccess()) {
98        outputFormat.setValue(FIELDS.status.name(), 0);
99        outputFormat.setValue(FIELDS.statusTxt.name(),
100                             Messages.getString("BusinessRequest.6") +
101                             Messages.getString(
102                                 "RequestInformation.Success")); //$NON-NLS-1$
103       outputFormat.setValue(FIELDS.remote.name(), rhost);
104       outputFormat.setValue("delay", delay);
105       if (logger.isInfoEnabled()) {
106         logger.info(outputFormat.loggerOut());
107       }
108       if (!OutputFormat.isQuiet()) {
109         outputFormat.sysout();
110       }
111     } else {
112       outputFormat.setValue(FIELDS.status.name(), 2);
113       outputFormat.setValue(FIELDS.statusTxt.name(),
114                             Messages.getString("BusinessRequest.6") +
115                             Messages.getString(
116                                 "RequestInformation.Failure")); //$NON-NLS-1$
117       outputFormat.setValue(FIELDS.remote.name(), rhost);
118       outputFormat.setValue("delay", delay);
119       logger.error(outputFormat.loggerOut(), future.getCause());
120       outputFormat.setValue(FIELDS.error.name(), future.getCause().toString());
121       if (!OutputFormat.isQuiet()) {
122         outputFormat.sysout();
123       }
124       networkTransaction.closeAll();
125       WaarpSystemUtil.systemExit(ErrorCode.Unknown.ordinal());
126       return;
127     }
128     networkTransaction.closeAll();
129     WaarpSystemUtil.systemExit(0);
130   }
131 
132 }