[87] | 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 |
|
---|
[240] | 9 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
[87] | 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 |
|
---|
[171] | 14 | /**
|
---|
| 15 | * <p>
|
---|
| 16 | * Command that saves an object contained in the {@link GlobalDataContainer}
|
---|
| 17 | * through serialization.
|
---|
| 18 | * </p>
|
---|
| 19 | *
|
---|
| 20 | * @author Steffen Herbold
|
---|
[223] | 21 | * @version 1.0
|
---|
[171] | 22 | */
|
---|
[87] | 23 | public class CMDsaveObject implements Command {
|
---|
| 24 |
|
---|
[171] | 25 | /*
|
---|
| 26 | * (non-Javadoc)
|
---|
| 27 | *
|
---|
| 28 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 29 | */
|
---|
[87] | 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);
|
---|
[171] | 37 | } catch (Exception e) {
|
---|
[87] | 38 | throw new InvalidParameterException();
|
---|
| 39 | }
|
---|
[171] | 40 |
|
---|
| 41 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
| 42 | objectName);
|
---|
| 43 | if (dataObject == null) {
|
---|
[240] | 44 | CommandHelpers.objectNotFoundMessage(objectName);
|
---|
| 45 | return;
|
---|
[87] | 46 | }
|
---|
[171] | 47 |
|
---|
[87] | 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) {
|
---|
[211] | 56 | Console.logException(ex);
|
---|
[87] | 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[171] | 60 | /*
|
---|
| 61 | * (non-Javadoc)
|
---|
| 62 | *
|
---|
| 63 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 64 | */
|
---|
[87] | 65 | @Override
|
---|
| 66 | public void help() {
|
---|
| 67 | Console.println("Usage: saveObject <filename> <objectName>");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | }
|
---|