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.report.gui.action;
020:
021: import java.awt.event.ActionEvent;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: import javax.swing.JFileChooser;
029: import javax.swing.tree.TreePath;
030:
031: import org.apache.jmeter.exceptions.IllegalUserActionException;
032: import org.apache.jmeter.gui.ReportGuiPackage;
033: import org.apache.jmeter.gui.action.Command;
034: import org.apache.jmeter.gui.util.ReportFileDialoger;
035: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
036: import org.apache.jmeter.save.SaveService;
037: import org.apache.jmeter.services.FileServer;
038: import org.apache.jmeter.testelement.ReportPlan;
039: import org.apache.jmeter.util.JMeterUtils;
040: import org.apache.jorphan.collections.HashTree;
041: import org.apache.jorphan.logging.LoggingManager;
042: import org.apache.jorphan.util.JOrphanUtils;
043: import org.apache.log.Logger;
044:
045: import com.thoughtworks.xstream.XStream;
046:
047: /**
048: * @author Peter Lin
049: * @version $Revision: 533867 $
050: */
051: public class ReportLoad implements Command {
052: transient private static Logger log = LoggingManager
053: .getLoggerForClass();
054:
055: XStream loadService = new XStream();
056:
057: private static Set commands = new HashSet();
058: static {
059: commands.add("open");
060: commands.add("merge");
061: }
062:
063: public ReportLoad() {
064: super ();
065: }
066:
067: public Set getActionNames() {
068: return commands;
069: }
070:
071: public void doAction(ActionEvent e) {
072: boolean merging = e.getActionCommand().equals("merge");
073:
074: if (!merging) {
075: ReportActionRouter.getInstance().doActionNow(
076: new ActionEvent(e.getSource(), e.getID(), "close"));
077: }
078:
079: JFileChooser chooser = ReportFileDialoger
080: .promptToOpenFile(new String[] { ".jmr" });
081: if (chooser == null) {
082: return;
083: }
084: boolean isTestPlan = false;
085: InputStream reader = null;
086: File f = null;
087: try {
088: f = chooser.getSelectedFile();
089: if (f != null) {
090: if (merging) {
091: log.info("Merging file: " + f);
092: } else {
093: log.info("Loading file: " + f);
094: FileServer.getFileServer().setBasedir(
095: f.getAbsolutePath());
096: }
097: reader = new FileInputStream(f);
098: HashTree tree = SaveService.loadTree(reader);
099: isTestPlan = insertLoadedTree(e.getID(), tree);
100: }
101: } catch (NoClassDefFoundError ex) // Allow for missing optional jars
102: {
103: String msg = ex.getMessage();
104: if (msg == null) {
105: msg = "Missing jar file - see log for details";
106: log.warn("Missing jar file", ex);
107: }
108: JMeterUtils.reportErrorToUser(msg);
109: } catch (Exception ex) {
110: String msg = ex.getMessage();
111: if (msg == null) {
112: msg = "Unexpected error - see log for details";
113: log.warn("Unexpected error", ex);
114: }
115: JMeterUtils.reportErrorToUser(msg);
116: } finally {
117: JOrphanUtils.closeQuietly(reader);
118: ReportGuiPackage.getInstance().updateCurrentGui();
119: ReportGuiPackage.getInstance().getMainFrame().repaint();
120: }
121: // don't change name if merging
122: if (!merging && isTestPlan && f != null) {
123: ReportGuiPackage.getInstance().setReportPlanFile(
124: f.getAbsolutePath());
125: }
126: }
127:
128: /**
129: * Returns a boolean indicating whether the loaded tree was a full test plan
130: */
131: public boolean insertLoadedTree(int id, HashTree tree)
132: throws Exception, IllegalUserActionException {
133: // convertTree(tree);
134: if (tree == null) {
135: throw new Exception("Error in TestPlan - see log file");
136: }
137: boolean isTestPlan = tree.getArray()[0] instanceof ReportPlan;
138: HashTree newTree = ReportGuiPackage.getInstance().addSubTree(
139: tree);
140: ReportGuiPackage.getInstance().updateCurrentGui();
141: ReportGuiPackage.getInstance().getMainFrame().getTree()
142: .setSelectionPath(
143: new TreePath(((ReportTreeNode) newTree
144: .getArray()[0]).getPath()));
145: tree = ReportGuiPackage.getInstance().getCurrentSubTree();
146: ReportActionRouter.getInstance().actionPerformed(
147: new ActionEvent(tree
148: .get(tree.getArray()[tree.size() - 1]), id,
149: ReportCheckDirty.SUB_TREE_LOADED));
150:
151: return isTestPlan;
152: }
153: }
|