source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDsaveObject.java @ 196

Last change on this file since 196 was 171, checked in by sherbold, 13 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 1.6 KB
Line 
1package de.ugoe.cs.eventbench.commands;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.ObjectOutputStream;
6import java.security.InvalidParameterException;
7import java.util.List;
8
9import de.ugoe.cs.eventbench.data.GlobalDataContainer;
10import de.ugoe.cs.util.console.Command;
11import 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 */
22public 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}
Note: See TracBrowser for help on using the repository browser.