KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > action > Save


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/action/Save.java,v 1.22 2004/02/20 01:03:07 jsalvata Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.action;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.swing.JFileChooser JavaDoc;
30
31 import org.apache.jmeter.config.Arguments;
32 import org.apache.jmeter.exceptions.IllegalUserActionException;
33 import org.apache.jmeter.gui.GuiPackage;
34 import org.apache.jmeter.gui.tree.JMeterTreeNode;
35 import org.apache.jmeter.gui.util.FileDialoger;
36 import org.apache.jmeter.save.SaveService;
37 import org.apache.jmeter.testelement.TestElement;
38 import org.apache.jmeter.util.JMeterUtils;
39 import org.apache.jorphan.collections.HashTree;
40 import org.apache.jorphan.collections.ListedHashTree;
41 import org.apache.jorphan.logging.LoggingManager;
42 import org.apache.log.Logger;
43
44 /**
45  * @author Michael Stover
46  * @author <a HREF="mailto:klancast@swbell.net">Keith Lancaster</a>
47  * @version $Revision: 1.22 $ updated on $Date: 2004/02/20 01:03:07 $
48  */

49 public class Save implements Command
50 {
51     transient private static Logger log = LoggingManager.getLoggerForClass();
52     public final static String JavaDoc SAVE_ALL_AS = "save_all_as";
53     public final static String JavaDoc SAVE_AS = "save_as";
54     public final static String JavaDoc SAVE = "save";
55     //NOTUSED private String chosenFile;
56
private String JavaDoc testPlanFile;
57
58     private static Set JavaDoc commands = new HashSet JavaDoc();
59     static {
60         commands.add(SAVE_AS);
61         commands.add(SAVE_ALL_AS);
62         commands.add(SAVE);
63     }
64
65     /**
66      * Constructor for the Save object.
67      */

68     public Save()
69     {
70     }
71
72     /**
73      * Gets the ActionNames attribute of the Save object.
74      *
75      * @return the ActionNames value
76      */

77     public Set JavaDoc getActionNames()
78     {
79         return commands;
80     }
81
82     public void setTestPlanFile(String JavaDoc f)
83     {
84         testPlanFile = f;
85         GuiPackage.getInstance().getMainFrame().setTitle(JMeterUtils.getExtendedFrameTitle(testPlanFile));
86     }
87
88     public void doAction(ActionEvent JavaDoc e) throws IllegalUserActionException
89     {
90         HashTree subTree = null;
91         if (!commands.contains(e.getActionCommand()))
92         {
93             throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
94         }
95         if (e.getActionCommand().equals(SAVE_AS))
96         {
97             subTree = GuiPackage.getInstance().getCurrentSubTree();
98         }
99         else
100         {
101             subTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
102         }
103
104         String JavaDoc updateFile = testPlanFile;
105         if (!SAVE.equals(e.getActionCommand())
106             || testPlanFile == null)
107         {
108             JFileChooser JavaDoc chooser =
109                 FileDialoger.promptToSaveFile(
110                     GuiPackage
111                         .getInstance()
112                         .getTreeListener()
113                         .getCurrentNode()
114                         .getName()
115                         + ".jmx");
116             if (chooser == null)
117             {
118                 return;
119             }
120             updateFile = chooser.getSelectedFile().getAbsolutePath();
121             if (!e.getActionCommand().equals(SAVE_AS))
122             {
123                 testPlanFile = updateFile;
124             }
125         }
126         // TODO: doesn't putting this here mark the tree as
127
// saved even though a failure may occur later?
128

129         ActionRouter.getInstance().doActionNow(
130             new ActionEvent JavaDoc(subTree, e.getID(), CheckDirty.SUB_TREE_SAVED));
131         try
132         {
133             convertSubTree(subTree);
134         }
135         catch (Exception JavaDoc err)
136         {
137         }
138         OutputStream JavaDoc writer = null;
139         try
140         {
141             writer = new FileOutputStream JavaDoc(updateFile);
142             SaveService.saveSubTree(subTree, writer);
143         }
144         catch (Throwable JavaDoc ex)
145         {
146             testPlanFile = null;
147             log.error("", ex);
148             throw new IllegalUserActionException(
149                 "Couldn't save test plan to file: " + updateFile);
150         }
151         finally
152         {
153             closeWriter(writer);
154             if(testPlanFile != null)
155             {
156                 GuiPackage.getInstance().getMainFrame().setTitle(JMeterUtils.getExtendedFrameTitle(testPlanFile));
157             }
158             GuiPackage.getInstance().getMainFrame().repaint();
159         }
160     }
161
162     private void convertSubTree(HashTree tree)
163     {
164         Iterator JavaDoc iter = new LinkedList JavaDoc(tree.list()).iterator();
165         while (iter.hasNext())
166         {
167             JMeterTreeNode item = (JMeterTreeNode) iter.next();
168             convertSubTree(tree.getTree(item));
169             TestElement testElement = item.getTestElement();
170             tree.replace(item, testElement);
171         }
172     }
173
174     public static class Test extends junit.framework.TestCase
175     {
176         Save save;
177         public Test(String JavaDoc name)
178         {
179             super(name);
180         }
181
182         public void setUp()
183         {
184             save = new Save();
185         }
186
187         public void testTreeConversion() throws Exception JavaDoc
188         {
189             HashTree tree = new ListedHashTree();
190             JMeterTreeNode root = new JMeterTreeNode(new Arguments(), null);
191             tree.add(root, root);
192             tree.getTree(root).add(root, root);
193             save.convertSubTree(tree);
194             assertEquals(
195                 tree.getArray()[0].getClass().getName(),
196                 root.getTestElement().getClass().getName());
197             tree = tree.getTree(tree.getArray()[0]);
198             assertEquals(
199                 tree.getArray()[0].getClass().getName(),
200                 root.getTestElement().getClass().getName());
201             assertEquals(
202                 tree
203                     .getTree(tree.getArray()[0])
204                     .getArray()[0]
205                     .getClass()
206                     .getName(),
207                 root.getTestElement().getClass().getName());
208         }
209     }
210
211     private void closeWriter(OutputStream JavaDoc writer)
212     {
213         if (writer != null)
214         {
215             try
216             {
217                 writer.close();
218             }
219             catch (Exception JavaDoc ex)
220             {
221                 log.error("", ex);
222             }
223         }
224     }
225 }
226
Popular Tags