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.dip;
22  
23  import fr.gouv.vitam.access.external.client.AccessExternalClient;
24  import fr.gouv.vitam.common.exception.InvalidParseOperationException;
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.CommandLineParser;
27  import org.apache.commons.cli.DefaultParser;
28  import org.apache.commons.cli.HelpFormatter;
29  import org.apache.commons.cli.Options;
30  import org.apache.commons.cli.ParseException;
31  import org.waarp.common.logging.WaarpLogger;
32  import org.waarp.common.logging.WaarpLoggerFactory;
33  import org.waarp.common.utility.Version;
34  import org.waarp.openr66.configuration.FileBasedConfiguration;
35  import org.waarp.openr66.context.task.AbstractExecJavaTask;
36  import org.waarp.openr66.protocol.configuration.Configuration;
37  import org.waarp.vitam.common.WaarpCommon;
38  import org.waarp.vitam.common.WaarpCommon.TaskOption;
39  
40  import java.io.File;
41  
42  /**
43   * DipTask is a one shot command that will initiate one specific DIP export
44   * operation from a Waarp post task on reception.
45   */
46  public class DipTask {
47    /**
48     * Internal Logger
49     */
50    private static final WaarpLogger logger =
51        WaarpLoggerFactory.getLogger(DipTask.class);
52    private static File waarpConfigurationFile;
53    private static int statusMain;
54    private final TaskOption taskOption;
55    private final DipRequestFactory factory;
56    private final DipManager dipManager;
57  
58    /**
59     * Unique constructor
60     *
61     * @param taskOption
62     * @param factory
63     * @param dipManager
64     */
65    public DipTask(final TaskOption taskOption, final DipRequestFactory factory,
66                   final DipManager dipManager) {
67      this.taskOption = taskOption;
68      this.factory = factory;
69      this.dipManager = dipManager;
70    }
71  
72    /**
73     * Will try to start the DipTask according to arguments, else print
74     * the help message
75     *
76     * @param args
77     */
78    public static void main(String[] args) {
79      Options options = getOptions();
80  
81      if (args.length == 0 || WaarpCommon.checkHelp(args)) {
82        printHelp(options);
83        statusMain = 2;
84        return;
85      }
86      final DipTask dipTask;
87      try {
88        dipTask = getDipTask(options, args);
89      } catch (ParseException e) {
90        logger.error("Error while initializing " + DipTask.class.getSimpleName(),
91                     e);
92        printHelp(options);
93        statusMain = 2;
94        return;
95      }
96      if (!FileBasedConfiguration
97          .setSubmitClientConfigurationFromXml(Configuration.configuration,
98                                               waarpConfigurationFile
99                                                   .getAbsolutePath())) {
100       logger.error("Cannot load Waarp Configuration");
101       statusMain = 2;
102       return;
103     }
104     statusMain = dipTask.invoke();
105   }
106 
107   /**
108    * Define the options associated
109    *
110    * @return the Options
111    */
112   private static Options getOptions() {
113     Options options = new Options();
114     TaskOption.setStandardTaskOptions(options);
115     options.addOption(DipRequestFactory.getDirectoryOption());
116     return options;
117   }
118 
119   /**
120    * Helper to print help
121    *
122    * @param options
123    */
124   private static void printHelp(Options options) {
125     HelpFormatter formatter = new HelpFormatter();
126     formatter.printHelp(DipTask.class.getSimpleName(),
127                         "Version: " + Version.fullIdentifier(), options,
128                         WaarpCommon.FOR_MANDATORY_ARGUMENTS, true);
129   }
130 
131   /**
132    * Build the DipTask according to arguments
133    *
134    * @param options
135    * @param args
136    *
137    * @return the new DipTask
138    *
139    * @throws ParseException
140    */
141   private static DipTask getDipTask(Options options, String[] args)
142       throws ParseException {
143     CommandLineParser parser = new DefaultParser();
144     CommandLine cmd = parser.parse(options, args);
145     DipRequestFactory.parseDirectoryOption(cmd);
146     TaskOption taskOption = TaskOption.getTaskOption(cmd, args);
147     waarpConfigurationFile = new File(taskOption.getWaarpConfigurationFile());
148     return new DipTask(taskOption, DipRequestFactory.getInstance(),
149                        new DipManager());
150   }
151 
152   /**
153    * Launch the DipMonitor
154    *
155    * @return 0 if OK, 1 if Warning, 2 if error
156    */
157   public int invoke() {
158     try (AccessExternalClient client = factory.getClient()) {
159       DipRequestDipRequest">DipRequest dipRequest = new DipRequest(taskOption, factory);
160       return dipManager.select(factory, dipRequest, client);
161     } catch (InvalidParseOperationException e) {
162       logger.error("Issue since DipRequest cannot be saved", e);
163     }
164     return 2;
165   }
166 
167   /**
168    * Equivalent JavaTask
169    */
170   public static class JavaTask extends AbstractExecJavaTask {
171     @Override
172     public void run() {
173       String[] args;
174       if (!fullarg.contains("-s ") && !fullarg.contains("--session ")) {
175         try {
176           String key = this.session.getRunner().getKey();
177           args = BLANK.split(fullarg + " -s " + key);
178         } catch (Throwable e) {
179           args = BLANK.split(fullarg);
180         }
181       } else {
182         args = BLANK.split(fullarg);
183       }
184       main(args);
185       status = statusMain;
186     }
187   }
188 }