[927] | 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 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.commands.misc;
|
---|
[87] | 16 |
|
---|
| 17 | import java.io.FileOutputStream;
|
---|
| 18 | import java.io.IOException;
|
---|
| 19 | import java.io.ObjectOutputStream;
|
---|
| 20 | import java.util.List;
|
---|
| 21 |
|
---|
[922] | 22 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
[87] | 23 | import de.ugoe.cs.util.console.Command;
|
---|
| 24 | import de.ugoe.cs.util.console.Console;
|
---|
[667] | 25 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
[87] | 26 |
|
---|
[171] | 27 | /**
|
---|
| 28 | * <p>
|
---|
| 29 | * Command that saves an object contained in the {@link GlobalDataContainer}
|
---|
| 30 | * through serialization.
|
---|
| 31 | * </p>
|
---|
| 32 | *
|
---|
| 33 | * @author Steffen Herbold
|
---|
[223] | 34 | * @version 1.0
|
---|
[171] | 35 | */
|
---|
[87] | 36 | public class CMDsaveObject implements Command {
|
---|
| 37 |
|
---|
[171] | 38 | /*
|
---|
| 39 | * (non-Javadoc)
|
---|
| 40 | *
|
---|
| 41 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 42 | */
|
---|
[87] | 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);
|
---|
[171] | 50 | } catch (Exception e) {
|
---|
[766] | 51 | throw new IllegalArgumentException();
|
---|
[87] | 52 | }
|
---|
[171] | 53 |
|
---|
| 54 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
| 55 | objectName);
|
---|
| 56 | if (dataObject == null) {
|
---|
[240] | 57 | CommandHelpers.objectNotFoundMessage(objectName);
|
---|
| 58 | return;
|
---|
[87] | 59 | }
|
---|
[171] | 60 |
|
---|
[87] | 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) {
|
---|
[211] | 69 | Console.logException(ex);
|
---|
[87] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[171] | 73 | /*
|
---|
| 74 | * (non-Javadoc)
|
---|
| 75 | *
|
---|
| 76 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 77 | */
|
---|
[87] | 78 | @Override
|
---|
[664] | 79 | public String help() {
|
---|
| 80 | return "saveObject <filename> <objectName>";
|
---|
[87] | 81 | }
|
---|
| 82 |
|
---|
| 83 | }
|
---|