| 1 | package de.uka.ipd.sdq.prototype.framework.utils; |
| 2 | |
| 3 | import java.io.FileInputStream; |
| 4 | import java.io.FileNotFoundException; |
| 5 | import java.io.IOException; |
| 6 | import java.util.Properties; |
| 7 | |
| 8 | import org.apache.commons.cli.CommandLine; |
| 9 | import org.apache.commons.cli.Options; |
| 10 | import org.apache.log4j.Logger; |
| 11 | |
| 12 | public class RunProperties |
| 13 | { |
| 14 | private static final long serialVersionUID = -5663835008494398378L; |
| 15 | |
| 16 | private CommandLine cmdLine; |
| 17 | private Options options; |
| 18 | private Properties propertyFile = null; |
| 19 | |
| 20 | private static Logger logger = org.apache.log4j.Logger.getRootLogger(); |
| 21 | |
| 22 | public RunProperties(CommandLine cmdLine, Options options) |
| 23 | { |
| 24 | super(); |
| 25 | this.cmdLine = cmdLine; |
| 26 | this.options = options; |
| 27 | |
| 28 | if(cmdLine.hasOption("f")) |
| 29 | { |
| 30 | propertyFile = new Properties(); |
| 31 | try { |
| 32 | propertyFile.load(new FileInputStream(cmdLine.getOptionValue("f"))); |
| 33 | } catch (FileNotFoundException e) { |
| 34 | logger.error("Property file could not be found.",e); |
| 35 | propertyFile = null; |
| 36 | } catch (IOException e) { |
| 37 | logger.debug("Error reading property file.",e); |
| 38 | propertyFile = null; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private String getPropertyFromFile(String key) |
| 44 | { |
| 45 | if(propertyFile != null) |
| 46 | { |
| 47 | return propertyFile.getProperty(key); |
| 48 | } |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | private boolean hasPropertyInFile(String key) |
| 53 | { |
| 54 | if(propertyFile != null) |
| 55 | { |
| 56 | if(propertyFile.getProperty(key) == null) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | private String getLongOpt(String opt) |
| 69 | { |
| 70 | String longOpt = opt; |
| 71 | |
| 72 | if(options.hasOption(opt)) |
| 73 | { |
| 74 | longOpt = options.getOption(opt).getLongOpt(); |
| 75 | } |
| 76 | |
| 77 | return longOpt; |
| 78 | } |
| 79 | |
| 80 | public String getOptionValue(char opt) |
| 81 | { |
| 82 | String sOpt = ""+opt; |
| 83 | return getOptionValue(sOpt); |
| 84 | } |
| 85 | |
| 86 | public String getOptionValue(String opt) |
| 87 | { |
| 88 | if(cmdLine.hasOption(opt)) |
| 89 | { |
| 90 | return cmdLine.getOptionValue(opt); |
| 91 | } |
| 92 | else // look in file |
| 93 | { |
| 94 | String longOpt = getLongOpt(opt); |
| 95 | |
| 96 | if(hasPropertyInFile(longOpt)) |
| 97 | { |
| 98 | return getPropertyFromFile(longOpt); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | return cmdLine.getOptionValue(opt); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public boolean hasOption(char opt) |
| 108 | { |
| 109 | String sOpt = ""+opt; |
| 110 | return hasOption(sOpt); |
| 111 | } |
| 112 | |
| 113 | public boolean hasOption(String opt) |
| 114 | { |
| 115 | if(hasPropertyInFile(getLongOpt(opt))) return true; |
| 116 | return cmdLine.hasOption(opt); |
| 117 | } |
| 118 | |
| 119 | public Object getOptionObject(char opt) |
| 120 | { |
| 121 | String sOpt = ""+opt; |
| 122 | return getOptionObject(sOpt); |
| 123 | } |
| 124 | |
| 125 | public Object getOptionObject(String opt) |
| 126 | { |
| 127 | return cmdLine.getOptionObject(opt); |
| 128 | } |
| 129 | } |