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.data.GlobalDataContainer;
|
---|
10 | import de.ugoe.cs.util.console.Command;
|
---|
11 | import de.ugoe.cs.util.console.Console;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * <p>
|
---|
15 | * Command that saves an object contained in the {@link GlobalDataContainer}
|
---|
16 | * through serialization.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | * @version
|
---|
21 | */
|
---|
22 | public class CMDsaveObject implements Command {
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * (non-Javadoc)
|
---|
26 | *
|
---|
27 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
28 | */
|
---|
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);
|
---|
36 | } catch (Exception e) {
|
---|
37 | throw new InvalidParameterException();
|
---|
38 | }
|
---|
39 |
|
---|
40 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
41 | objectName);
|
---|
42 | if (dataObject == null) {
|
---|
43 | Console.println("Object " + objectName + " not found in storage.");
|
---|
44 | }
|
---|
45 |
|
---|
46 | FileOutputStream fos = null;
|
---|
47 | ObjectOutputStream out = null;
|
---|
48 | try {
|
---|
49 | fos = new FileOutputStream(filename);
|
---|
50 | out = new ObjectOutputStream(fos);
|
---|
51 | out.writeObject(dataObject);
|
---|
52 | out.close();
|
---|
53 | } catch (IOException ex) {
|
---|
54 | ex.printStackTrace();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * (non-Javadoc)
|
---|
60 | *
|
---|
61 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
62 | */
|
---|
63 | @Override
|
---|
64 | public void help() {
|
---|
65 | Console.println("Usage: saveObject <filename> <objectName>");
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|