001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.action;
020:
021: import java.awt.event.ActionEvent;
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.Set;
028:
029: import javax.swing.JFileChooser;
030: import javax.swing.JOptionPane;
031:
032: import org.apache.commons.io.FilenameUtils;
033: import org.apache.jmeter.exceptions.IllegalUserActionException;
034: import org.apache.jmeter.gui.GuiPackage;
035: import org.apache.jmeter.gui.tree.JMeterTreeNode;
036: import org.apache.jmeter.gui.util.FileDialoger;
037: import org.apache.jmeter.save.OldSaveService;
038: import org.apache.jmeter.save.SaveService;
039: import org.apache.jmeter.testelement.TestElement;
040: import org.apache.jmeter.util.JMeterUtils;
041: import org.apache.jorphan.collections.HashTree;
042: import org.apache.jorphan.logging.LoggingManager;
043: import org.apache.jorphan.util.JOrphanUtils;
044: import org.apache.log.Logger;
045:
046: /**
047: * Save the current test plan; implements:
048: * Save
049: * Save TestPlan As
050: * Save (Selection) As
051: */
052: public class Save implements Command {
053: private static final Logger log = LoggingManager
054: .getLoggerForClass();
055:
056: public final static String JMX_FILE_EXTENSION = ".jmx"; // $NON-NLS-1$
057:
058: private static Set commands = new HashSet();
059: static {
060: commands.add(ActionNames.SAVE_AS); // Save (Selection) As
061: commands.add(ActionNames.SAVE_ALL_AS); // Save TestPlan As
062: commands.add(ActionNames.SAVE); // Save
063: }
064:
065: /**
066: * Constructor for the Save object.
067: */
068: public Save() {
069: }
070:
071: /**
072: * Gets the ActionNames attribute of the Save object.
073: *
074: * @return the ActionNames value
075: */
076: public Set getActionNames() {
077: return commands;
078: }
079:
080: public void doAction(ActionEvent e)
081: throws IllegalUserActionException {
082: HashTree subTree = null;
083: if (!commands.contains(e.getActionCommand())) {
084: throw new IllegalUserActionException(
085: "Invalid user command:" + e.getActionCommand());
086: }
087: if (e.getActionCommand().equals(ActionNames.SAVE_AS)) {
088: JMeterTreeNode[] nodes = GuiPackage.getInstance()
089: .getTreeListener().getSelectedNodes();
090: if (nodes.length > 1) {
091: JMeterUtils.reportErrorToUser(JMeterUtils
092: .getResString("save_as_error"), // $NON-NLS-1$
093: JMeterUtils.getResString("save_as")); // $NON-NLS-1$
094: return;
095: }
096: subTree = GuiPackage.getInstance().getCurrentSubTree();
097: } else {
098: subTree = GuiPackage.getInstance().getTreeModel()
099: .getTestPlan();
100: }
101:
102: String updateFile = GuiPackage.getInstance().getTestPlanFile();
103: if (!ActionNames.SAVE.equals(e.getActionCommand())
104: || updateFile == null) {
105: JFileChooser chooser = FileDialoger
106: .promptToSaveFile(GuiPackage.getInstance()
107: .getTreeListener().getCurrentNode()
108: .getName()
109: + JMX_FILE_EXTENSION);
110: if (chooser == null) {
111: return;
112: }
113: updateFile = chooser.getSelectedFile().getAbsolutePath();
114: // Make sure the file ends with proper extension
115: if (FilenameUtils.getExtension(updateFile).equals("")) {
116: updateFile = updateFile + JMX_FILE_EXTENSION;
117: }
118: // Check if the user is trying to save to an existing file
119: if (!e.getActionCommand().equals(ActionNames.SAVE)) {//so it must be a SAVE_AS action
120: File f = new File(updateFile);
121: if (f.exists()) {
122: int response = JOptionPane
123: .showConfirmDialog(
124: GuiPackage.getInstance()
125: .getMainFrame(),
126: JMeterUtils
127: .getResString("save_overwrite_existing_file"), // $NON-NLS-1$
128: JMeterUtils.getResString("save?"), // $NON-NLS-1$
129: JOptionPane.YES_NO_OPTION,
130: JOptionPane.QUESTION_MESSAGE);
131: if (response == JOptionPane.CLOSED_OPTION
132: || response == JOptionPane.NO_OPTION) {
133: return; // Do not save, user does not want to overwrite
134: }
135: }
136: }
137:
138: if (!e.getActionCommand().equals(ActionNames.SAVE_AS)) {
139: GuiPackage.getInstance().setTestPlanFile(updateFile);
140: }
141: }
142: // TODO: doesn't putting this here mark the tree as
143: // saved even though a failure may occur later?
144:
145: ActionRouter.getInstance().doActionNow(
146: new ActionEvent(subTree, e.getID(),
147: ActionNames.SUB_TREE_SAVED));
148: try {
149: convertSubTree(subTree);
150: } catch (Exception err) {
151: }
152: FileOutputStream ostream = null;
153: try {
154: ostream = new FileOutputStream(updateFile);
155: if (SaveService.isSaveTestPlanFormat20()) {
156: OldSaveService.saveSubTree(subTree, ostream);
157: } else {
158: SaveService.saveTree(subTree, ostream);
159: }
160: } catch (Throwable ex) {
161: GuiPackage.getInstance().setTestPlanFile(null);
162: log.error("", ex);
163: throw new IllegalUserActionException(
164: "Couldn't save test plan to file: " + updateFile);
165: } finally {
166: JOrphanUtils.closeQuietly(ostream);
167: }
168: GuiPackage.getInstance().updateCurrentGui();
169: }
170:
171: // package protected to all for separate test code
172: void convertSubTree(HashTree tree) {
173: Iterator iter = new LinkedList(tree.list()).iterator();
174: while (iter.hasNext()) {
175: JMeterTreeNode item = (JMeterTreeNode) iter.next();
176: convertSubTree(tree.getTree(item));
177: TestElement testElement = item.getTestElement();
178: tree.replace(item, testElement);
179: }
180: }
181: }
|