1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.commands.misc;
|
---|
16 |
|
---|
17 | import java.io.FileOutputStream;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.io.ObjectOutputStream;
|
---|
20 | import java.util.List;
|
---|
21 |
|
---|
22 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
23 | import de.ugoe.cs.util.console.Command;
|
---|
24 | import de.ugoe.cs.util.console.Console;
|
---|
25 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * Command that saves an object contained in the {@link GlobalDataContainer}
|
---|
30 | * through serialization.
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @author Steffen Herbold
|
---|
34 | * @version 1.0
|
---|
35 | */
|
---|
36 | public class CMDsaveObject implements Command {
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * (non-Javadoc)
|
---|
40 | *
|
---|
41 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
42 | */
|
---|
43 | @Override
|
---|
44 | public void run(List<Object> parameters) {
|
---|
45 | String filename;
|
---|
46 | String objectName;
|
---|
47 | try {
|
---|
48 | filename = (String) parameters.get(0);
|
---|
49 | objectName = (String) parameters.get(1);
|
---|
50 | } catch (Exception e) {
|
---|
51 | throw new IllegalArgumentException();
|
---|
52 | }
|
---|
53 |
|
---|
54 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
55 | objectName);
|
---|
56 | if (dataObject == null) {
|
---|
57 | CommandHelpers.objectNotFoundMessage(objectName);
|
---|
58 | return;
|
---|
59 | }
|
---|
60 |
|
---|
61 | FileOutputStream fos = null;
|
---|
62 | ObjectOutputStream out = null;
|
---|
63 | try {
|
---|
64 | fos = new FileOutputStream(filename);
|
---|
65 | out = new ObjectOutputStream(fos);
|
---|
66 | out.writeObject(dataObject);
|
---|
67 | out.close();
|
---|
68 | } catch (IOException ex) {
|
---|
69 | Console.logException(ex);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * (non-Javadoc)
|
---|
75 | *
|
---|
76 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
77 | */
|
---|
78 | @Override
|
---|
79 | public String help() {
|
---|
80 | return "saveObject <filename> <objectName>";
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|