1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
73 assert environmentVariableArgument != null;
74 String envVar = namespace.getString(environmentVariableArgument.getDest());
75 assert envVar != null;
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 }