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