View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu.operation.args;
18  
19  import java.util.logging.Logger;
20  import net.sourceforge.argparse4j.inf.Argument;
21  import net.sourceforge.argparse4j.inf.ArgumentParser;
22  import net.sourceforge.argparse4j.inf.Namespace;
23  import org.apache.commons.lang3.StringUtils;
24  
25  public class PasswordArgs implements ArgsConfiguration {
26  
27      private static final Logger logger = Logger.getLogger(PasswordArgs.class.getName());
28  
29      private final String title;
30  
31      public Argument passwordArgument;
32      public Argument environmentVariableArgument;
33  
34      private String password;
35  
36      public PasswordArgs(String title) {
37          assert title != null;
38          this.title = title;
39      }
40  
41      public String getPassword() {
42          return password;
43      }
44  
45      public char[] getPasswordCharArray() {
46          if (password == null) {
47              return null;
48          }
49          return password.toCharArray();
50      }
51  
52      @Deprecated
53      @Override
54      public void addArguments(ArgumentParser parser) {
55          finalizeArguments();
56      }
57  
58      public void finalizeArguments() {
59          assert passwordArgument != null;
60          passwordArgument.type(String.class);
61  
62          assert environmentVariableArgument != null;
63          environmentVariableArgument.type(String.class);
64      }
65  
66      @Override
67      public void setFromNamespace(Namespace namespace) {
68          assert title != null;
69          assert passwordArgument != null;
70          password = namespace.getString(passwordArgument.getDest());
71          if (password == null) {
72              // Load the password from an environment variable
73              assert environmentVariableArgument != null;
74              String envVar = namespace.getString(environmentVariableArgument.getDest());
75              assert envVar != null; // The argument has a default value
76              logger.info(String.format("%s environment variable: %s", StringUtils.capitalize(title), envVar));
77              password = System.getenv(envVar);
78              if (password != null) {
79                  logger.info(String.format("%s loaded from the environment variable %s.", StringUtils.capitalize(title), envVar));
80              } else {
81                  logger.info(String.format("%s was not set.", StringUtils.capitalize(title)));
82              }
83          } else {
84              logger.info(String.format("%s loaded from the command line.", StringUtils.capitalize(title)));
85          }
86      }
87  
88  }