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  
21  package org.waarp.vitam.common;
22  
23  import fr.gouv.vitam.access.external.client.AdminExternalClient;
24  import fr.gouv.vitam.access.external.client.AdminExternalClientFactory;
25  import fr.gouv.vitam.access.external.client.VitamPoolingClient;
26  import fr.gouv.vitam.common.exception.InvalidParseOperationException;
27  import fr.gouv.vitam.common.exception.VitamClientException;
28  import fr.gouv.vitam.common.exception.VitamException;
29  import fr.gouv.vitam.common.json.JsonHandler;
30  import org.waarp.common.logging.WaarpLogger;
31  import org.waarp.common.logging.WaarpLoggerFactory;
32  import org.waarp.vitam.dip.DipRequest;
33  
34  import java.io.File;
35  import java.util.concurrent.TimeUnit;
36  
37  /**
38   * Class that pool the result from an asynchrone Vitam operation
39   */
40  public class OperationCheck {
41    /**
42     * Internal Logger
43     */
44    private static final WaarpLogger logger =
45        WaarpLoggerFactory.getLogger(OperationCheck.class);
46  
47    private static int retry = 3;
48    private static int delay = 100;
49    private static boolean result;
50    private final AdminExternalClient client;
51  
52    public OperationCheck(AdminExternalClient client) {
53      this.client = client;
54    }
55  
56    /**
57     * Change the default retry and delay for pooling of operation
58     *
59     * @param retryToSet
60     * @param delayToSet
61     */
62    public static void setRetry(int retryToSet, int delayToSet) {
63      retry = retryToSet;
64      delay = delayToSet;
65    }
66  
67    /**
68     * @return the number of retries for pooling of operation
69     */
70    public static int getRetry() {
71      return retry;
72    }
73  
74    /**
75     * @return the current delay between 2 retries for pooling of operation
76     */
77    public static int getDelay() {
78      return delay;
79    }
80  
81    public static boolean getResult() {
82      return result;
83    }
84  
85    public static void main(String[] args) {
86      if (args.length < 1) {
87        logger.error("{} needs 1 argument: json_request_file",
88                     OperationCheck.class.getSimpleName());
89        return;
90      }
91      File file = new File(args[0]);
92      if (!file.canRead()) {
93        logger.error("{} needs 1 valid argument: json_request_file",
94                     OperationCheck.class.getSimpleName());
95        result = false;
96        return;
97      }
98      DipRequest dipRequest;
99      try {
100       dipRequest = JsonHandler.getFromFile(file, DipRequest.class);
101     } catch (InvalidParseOperationException e) {
102       logger.error("{} needs 1 valid argument: json_request_file",
103                    OperationCheck.class.getSimpleName());
104       result = false;
105       return;
106     }
107     try (AdminExternalClient client = AdminExternalClientFactory.getInstance()
108                                                                 .getClient()) {
109       OperationCheckperationCheck">OperationCheck operationCheck = new OperationCheck(client);
110       if (operationCheck.checkAvailabilityAtr(dipRequest.getTenantId(),
111                                               dipRequest.getRequestId())) {
112         logger.warn("Operation {} for tenant {} is finished",
113                     dipRequest.getRequestId(), dipRequest.getTenantId());
114         result = true;
115       } else {
116         logger.warn("Operation {} for tenant {} is started or unknown",
117                     dipRequest.getRequestId(), dipRequest.getTenantId());
118         result = false;
119       }
120     }
121   }
122 
123   /**
124    * Check if the corresponding operation is done
125    *
126    * @param tenantId the tenantId associated with the operation
127    * @param requestId the operation Id
128    *
129    * @return True if done
130    */
131   public boolean checkAvailabilityAtr(int tenantId, String requestId) {
132     try {
133       VitamPoolingClient vitamPoolingClient = new VitamPoolingClient(client);
134       return vitamPoolingClient
135           .wait(tenantId, requestId, retry, delay, TimeUnit.MILLISECONDS);
136     } catch (VitamClientException e) {
137       logger.warn(e);
138       return false;
139     } catch (VitamException e) {
140       logger.info(e);
141       return false;
142     }
143   }
144 }