[51] | 1 | package geniusweb.logconverter;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedWriter;
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.FileWriter;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.util.Arrays;
|
---|
| 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.logging.Level;
|
---|
| 11 |
|
---|
| 12 | import org.apache.commons.cli.BasicParser;
|
---|
| 13 | import org.apache.commons.cli.CommandLine;
|
---|
| 14 | import org.apache.commons.cli.CommandLineParser;
|
---|
| 15 | import org.apache.commons.cli.HelpFormatter;
|
---|
| 16 | import org.apache.commons.cli.Option;
|
---|
| 17 | import org.apache.commons.cli.Options;
|
---|
| 18 | import org.apache.commons.cli.ParseException;
|
---|
| 19 |
|
---|
| 20 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 21 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 22 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 23 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 24 |
|
---|
| 25 | import geniusweb.actions.PartyId;
|
---|
| 26 | import geniusweb.protocol.NegoState;
|
---|
| 27 | import geniusweb.protocol.session.SessionResult;
|
---|
| 28 | import tudelft.utilities.logging.Reporter;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Utility class with command line options and main program. Converts a
|
---|
| 32 | * NegoState into a file (CSV, JSON) with utilities for each session. Contacts
|
---|
| 33 | * the actual profileservers/profile files to do so.
|
---|
| 34 | */
|
---|
| 35 | public class Convert {
|
---|
| 36 | private static final String JSON = ".json";
|
---|
| 37 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
| 38 | private final Reporter reporter;
|
---|
| 39 | private final File source;
|
---|
| 40 | private final File output;
|
---|
| 41 | private final Format format;
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * How {@link SessionResultWithUtils} is written to the output
|
---|
| 45 | */
|
---|
| 46 | enum Format {
|
---|
| 47 | CSV, // for each party write sessionnr, partyname, profilename, final
|
---|
| 48 | // util
|
---|
| 49 | JSON // jackson serializes it
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * @param sourcedir a directory or file to convert. If directory, converts
|
---|
| 54 | * all json files in the dir.
|
---|
| 55 | * @param level the debug {@link Level}. Debug messages at or above this
|
---|
| 56 | * level go to stderr.
|
---|
| 57 | * @param outputf the output file or directory to write results to. null=
|
---|
| 58 | * write to stdout. If output=file then input must be also
|
---|
| 59 | * a single file.
|
---|
| 60 | * @throws IOException
|
---|
| 61 | * @throws JsonMappingException
|
---|
| 62 | * @throws JsonParseException
|
---|
| 63 | */
|
---|
| 64 | public Convert(File source, Level level, File outputf, Format format,
|
---|
| 65 | Reporter reporter)
|
---|
| 66 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 67 | this.source = source;
|
---|
| 68 | this.format = format;
|
---|
| 69 | this.output = outputf;
|
---|
| 70 | this.reporter = reporter;
|
---|
| 71 | if (outputf != null) {
|
---|
| 72 | if (outputf.exists()) {
|
---|
| 73 | if (!outputf.isDirectory())
|
---|
| 74 | throw new IOException("Output " + outputf
|
---|
| 75 | + " exists but is not a directory");
|
---|
| 76 | if (!outputf.canWrite())
|
---|
| 77 | throw new IOException("" + outputf + " is write protected");
|
---|
| 78 | }
|
---|
| 79 | if (source.isDirectory() && !outputf.isDirectory())
|
---|
| 80 | throw new IOException(
|
---|
| 81 | "Source is a directory, output must also be a directory but got "
|
---|
| 82 | + output);
|
---|
| 83 | }
|
---|
| 84 | if (source.isDirectory()) {
|
---|
| 85 | File filesList[] = source.listFiles();
|
---|
| 86 | for (File file : filesList) {
|
---|
| 87 | if (file.getName().endsWith(JSON)) {
|
---|
| 88 | output(file, convertFile(file));
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | } else {
|
---|
| 92 | output(source, convertFile(source));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Writes converted file to the {@link #output}, or stdout if null. The
|
---|
| 99 | * destination file will match the source file. Destination file must not
|
---|
| 100 | * exist.
|
---|
| 101 | *
|
---|
| 102 | * If {@link #output} is a dir , the name of the source file is used for the
|
---|
| 103 | * destination file. The extension is replaced with {@link #format}.
|
---|
| 104 | *
|
---|
| 105 | * @param src the source {@link File}. May be used to generate output
|
---|
| 106 | * filename.
|
---|
| 107 | * @param res the result to write to the output.
|
---|
| 108 | */
|
---|
| 109 | private void output(File src, List<SessionResultWithUtils> res) {
|
---|
| 110 | String text = "";
|
---|
| 111 | switch (format) {
|
---|
| 112 | case CSV:
|
---|
| 113 | text = "session,party,profile,utility\n";
|
---|
| 114 | long sessionnr = 0;
|
---|
| 115 | for (SessionResultWithUtils r : res) {
|
---|
| 116 | for (PartyId party : r.getParticipants().keySet()) {
|
---|
| 117 | text = text + "" + sessionnr + ","
|
---|
| 118 | + r.getParticipants().get(party).getParty()
|
---|
| 119 | .getPartyRef().getURI()
|
---|
| 120 | + ","
|
---|
| 121 | + r.getParticipants().get(party).getProfile()
|
---|
| 122 | .getURI()
|
---|
| 123 | + "," + r.getUtilities().get(party) + "\n";
|
---|
| 124 | }
|
---|
| 125 | sessionnr++;
|
---|
| 126 | }
|
---|
| 127 | break;
|
---|
| 128 | case JSON:
|
---|
| 129 | try {
|
---|
| 130 | text = jackson.writeValueAsString(res);
|
---|
| 131 | } catch (JsonProcessingException e) {
|
---|
| 132 | reporter.log(Level.SEVERE,
|
---|
| 133 | fullMessage("Failed to convert result to json:", e));
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (output == null) {
|
---|
| 138 | System.out.println(text);
|
---|
| 139 | } else {
|
---|
| 140 | File outfile;
|
---|
| 141 | if (output.exists()) {
|
---|
| 142 | // must be dir
|
---|
| 143 | String destfilename = src.getName();
|
---|
| 144 | if (destfilename.endsWith(JSON)) {
|
---|
| 145 | destfilename = destfilename.substring(0,
|
---|
| 146 | destfilename.length() - 4) + format;
|
---|
| 147 | }
|
---|
| 148 | outfile = new File(output.getAbsolutePath(), destfilename);
|
---|
| 149 |
|
---|
| 150 | } else {
|
---|
| 151 | // we assume output is just the file to write to.
|
---|
| 152 | outfile = output;
|
---|
| 153 | }
|
---|
| 154 | try {
|
---|
| 155 | if (outfile.exists())
|
---|
| 156 | throw new IOException(
|
---|
| 157 | "destination file already exists:" + outfile);
|
---|
| 158 | BufferedWriter writer = new BufferedWriter(
|
---|
| 159 | new FileWriter(outfile));
|
---|
| 160 | writer.write(text);
|
---|
| 161 | writer.close();
|
---|
| 162 | } catch (IOException e) {
|
---|
| 163 | reporter.log(Level.SEVERE, fullMessage(
|
---|
| 164 | "Failed to write result to file " + outfile, e));
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /** @return all messages from stacktraces */
|
---|
| 171 | private String fullMessage(String msg, Throwable e) {
|
---|
| 172 | while (e != null) {
|
---|
| 173 | msg = msg + ":" + e.getMessage();
|
---|
| 174 | e = e.getCause();
|
---|
| 175 | }
|
---|
| 176 | return msg;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | private List<SessionResultWithUtils> convertFile(File file)
|
---|
| 180 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 181 | NegoState state = jackson.readValue(file, NegoState.class);
|
---|
| 182 | List<SessionResultWithUtils> results = new LinkedList<>();
|
---|
| 183 | for (SessionResult res : state.getResults()) {
|
---|
| 184 | results.add(SessionResultWithUtils.create(res, reporter));
|
---|
| 185 | }
|
---|
| 186 | return results;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | *
|
---|
| 191 | * @param args first arg must be folder containing .json files containing
|
---|
| 192 | * NegoState objects. All objects are parsed, getResults() is
|
---|
| 193 | * called, and all results are converted to
|
---|
| 194 | * SessionResultWithUtils.
|
---|
| 195 | */
|
---|
| 196 | public static void main(String[] args) {
|
---|
| 197 |
|
---|
| 198 | CommandLineParser parser = new BasicParser();
|
---|
| 199 | // use to read Command Line Arguments
|
---|
| 200 | HelpFormatter formatter = new HelpFormatter(); // // Use to Format
|
---|
| 201 | CommandLine cmd = null;
|
---|
| 202 | Options options = getOpts();
|
---|
| 203 |
|
---|
| 204 | try {
|
---|
| 205 | cmd = parser.parse(options, args); // it will parse according to the
|
---|
| 206 | // options and parse option
|
---|
| 207 | // value
|
---|
| 208 | String source = cmd.getOptionValue("source");
|
---|
| 209 | String output = cmd.getOptionValue("output");
|
---|
| 210 | Level level = Level.parse(cmd.getOptionValue("level", "WARNING"));
|
---|
| 211 | Format format = Format
|
---|
| 212 | .valueOf(cmd.getOptionValue("format", "JSON"));
|
---|
| 213 | new Convert(new File(source), level,
|
---|
| 214 | output == null ? null : new File(output), format,
|
---|
| 215 | new StdOutReporter(level));
|
---|
| 216 | } catch (ParseException e) {
|
---|
| 217 | System.out.println(e.getMessage());
|
---|
| 218 | formatter.printHelp("Convert", options);
|
---|
| 219 | } catch (IOException e) {
|
---|
| 220 | e.printStackTrace();
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | private static Options getOpts() {
|
---|
| 225 | Options options = new Options(); // Options Arguments which are
|
---|
| 226 | // Acceptable By Program.
|
---|
| 227 | Option source = new Option("s", "source", true,
|
---|
| 228 | "source file path. If dir, all json files in the dir are converted");
|
---|
| 229 | source.setRequired(true);
|
---|
| 230 | options.addOption(source);
|
---|
| 231 | Option destination = new Option("o", "output", true,
|
---|
| 232 | "Destination file/dir (must exist and be writable; must be dir if source is dir)");
|
---|
| 233 | options.addOption(destination);
|
---|
| 234 | Option debuglevel = new Option("l", "debuglevel", true,
|
---|
| 235 | "Debug level (WARNING, SEVERE, INFO, FINE, FINEST) ");
|
---|
| 236 | options.addOption(debuglevel);
|
---|
| 237 | Option format = new Option("f", "format", true,
|
---|
| 238 | "output format " + Arrays.toString(Format.values()));
|
---|
| 239 | options.addOption(format);
|
---|
| 240 |
|
---|
| 241 | return options;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | class StdOutReporter implements Reporter {
|
---|
| 246 | private final Level level;
|
---|
| 247 |
|
---|
| 248 | public StdOutReporter(Level level) {
|
---|
| 249 | this.level = level;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | @Override
|
---|
| 253 | public void log(Level level, String msg) {
|
---|
| 254 | if (level.intValue() >= this.level.intValue())
|
---|
| 255 | System.err.println(level + ":" + msg);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | @Override
|
---|
| 259 | public void log(Level arg0, String msg, Throwable arg2) {
|
---|
| 260 | log(arg0, msg);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|