| 1 | package de.ugoe.cs.eventbench.commands;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.FileOutputStream;
|
|---|
| 4 | import java.io.IOException;
|
|---|
| 5 | import java.io.ObjectOutputStream;
|
|---|
| 6 | import java.security.InvalidParameterException;
|
|---|
| 7 | import java.util.List;
|
|---|
| 8 |
|
|---|
| 9 | import de.ugoe.cs.eventbench.CommandHelpers;
|
|---|
| 10 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
|---|
| 11 | import de.ugoe.cs.util.console.Command;
|
|---|
| 12 | import de.ugoe.cs.util.console.Console;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * <p>
|
|---|
| 16 | * Command that saves an object contained in the {@link GlobalDataContainer}
|
|---|
| 17 | * through serialization.
|
|---|
| 18 | * </p>
|
|---|
| 19 | *
|
|---|
| 20 | * @author Steffen Herbold
|
|---|
| 21 | * @version 1.0
|
|---|
| 22 | */
|
|---|
| 23 | public class CMDsaveObject implements Command {
|
|---|
| 24 |
|
|---|
| 25 | /*
|
|---|
| 26 | * (non-Javadoc)
|
|---|
| 27 | *
|
|---|
| 28 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
|---|
| 29 | */
|
|---|
| 30 | @Override
|
|---|
| 31 | public void run(List<Object> parameters) {
|
|---|
| 32 | String filename;
|
|---|
| 33 | String objectName;
|
|---|
| 34 | try {
|
|---|
| 35 | filename = (String) parameters.get(0);
|
|---|
| 36 | objectName = (String) parameters.get(1);
|
|---|
| 37 | } catch (Exception e) {
|
|---|
| 38 | throw new InvalidParameterException();
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
|---|
| 42 | objectName);
|
|---|
| 43 | if (dataObject == null) {
|
|---|
| 44 | CommandHelpers.objectNotFoundMessage(objectName);
|
|---|
| 45 | return;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | FileOutputStream fos = null;
|
|---|
| 49 | ObjectOutputStream out = null;
|
|---|
| 50 | try {
|
|---|
| 51 | fos = new FileOutputStream(filename);
|
|---|
| 52 | out = new ObjectOutputStream(fos);
|
|---|
| 53 | out.writeObject(dataObject);
|
|---|
| 54 | out.close();
|
|---|
| 55 | } catch (IOException ex) {
|
|---|
| 56 | Console.logException(ex);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /*
|
|---|
| 61 | * (non-Javadoc)
|
|---|
| 62 | *
|
|---|
| 63 | * @see de.ugoe.cs.util.console.Command#help()
|
|---|
| 64 | */
|
|---|
| 65 | @Override
|
|---|
| 66 | public void help() {
|
|---|
| 67 | Console.println("Usage: saveObject <filename> <objectName>");
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | }
|
|---|