Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.control;
   20   
   21   import java.io.FileInputStream;
   22   import java.io.FileNotFoundException;
   23   import java.io.InputStream;
   24   import java.util.Iterator;
   25   import java.util.LinkedList;
   26   
   27   import org.apache.jmeter.gui.tree.JMeterTreeNode;
   28   import org.apache.jmeter.save.SaveService;
   29   import org.apache.jmeter.testelement.TestElement;
   30   import org.apache.jmeter.util.JMeterUtils;
   31   import org.apache.jorphan.collections.HashTree;
   32   import org.apache.jorphan.logging.LoggingManager;
   33   import org.apache.jorphan.util.JOrphanUtils;
   34   import org.apache.log.Logger;
   35   
   36   public class IncludeController extends GenericController implements ReplaceableController {
   37       private static final Logger log = LoggingManager.getLoggerForClass();
   38   
   39       private static final String INCLUDE_PATH = "IncludeController.includepath"; //$NON-NLS-1$
   40   
   41       private static  final String prefix =
   42           JMeterUtils.getPropDefault(
   43                   "includecontroller.prefix", //$NON-NLS-1$
   44                   ""); //$NON-NLS-1$
   45   
   46       private HashTree SUBTREE = null;
   47       private TestElement SUB = null;
   48   
   49       /**
   50        * No-arg constructor
   51        *
   52        * @see java.lang.Object#Object()
   53        */
   54       public IncludeController() {
   55           super();
   56       }
   57   
   58       public Object clone() {
   59           // TODO - fix so that this is only called once per test, instead of at every clone
   60           // Perhaps save previous filename, and only load if it has changed?
   61           this.resolveReplacementSubTree(null);
   62           IncludeController clone = (IncludeController) super.clone();
   63           clone.setIncludePath(this.getIncludePath());
   64           if (this.SUBTREE != null) {
   65               if (this.SUBTREE.keySet().size() == 1) {
   66                   Iterator itr = this.SUBTREE.keySet().iterator();
   67                   while (itr.hasNext()) {
   68                       this.SUB = (TestElement)itr.next();
   69                   }
   70               }
   71               clone.SUBTREE = (HashTree)this.SUBTREE.clone();
   72               clone.SUB = (TestElement)this.SUB.clone();
   73           }
   74           return clone;
   75       }
   76   
   77       /**
   78        * In the event an user wants to include an external JMX test plan
   79        * the GUI would call this.
   80        * @param jmxfile
   81        */
   82       public void setIncludePath(String jmxfile) {
   83           this.setProperty(INCLUDE_PATH,jmxfile);
   84       }
   85   
   86       /**
   87        * return the JMX file path.
   88        * @return the JMX file path
   89        */
   90       public String getIncludePath() {
   91           return this.getPropertyAsString(INCLUDE_PATH);
   92       }
   93   
   94       /**
   95        * The way ReplaceableController works is clone is called first,
   96        * followed by replace(HashTree) and finally getReplacement().
   97        */
   98       public HashTree getReplacementSubTree() {
   99           return SUBTREE;
  100       }
  101   
  102       public void resolveReplacementSubTree(JMeterTreeNode context) {
  103           this.SUBTREE = this.loadIncludedElements();
  104       }
  105   
  106       /**
  107        * load the included elements using SaveService
  108        */
  109       protected HashTree loadIncludedElements() {
  110           // only try to load the JMX test plan if there is one
  111           final String includePath = getIncludePath();
  112           InputStream reader = null;
  113           HashTree tree = null;
  114           if (includePath != null && includePath.length() > 0) {
  115               try {
  116                   String file=prefix+includePath;
  117                   log.info("loadIncludedElements -- try to load included module: "+file);
  118                   reader = new FileInputStream(file);
  119                   tree = SaveService.loadTree(reader);
  120                   removeDisabledItems(tree);
  121                   return tree;
  122               } catch (NoClassDefFoundError ex) // Allow for missing optional jars
  123               {
  124                   String msg = ex.getMessage();
  125                   if (msg == null) {
  126                       msg = "Missing jar file - see log for details";
  127                   }
  128                   log.warn("Missing jar file", ex);
  129                   JMeterUtils.reportErrorToUser(msg);
  130               } catch (FileNotFoundException ex) {
  131                   String msg = ex.getMessage();
  132                   JMeterUtils.reportErrorToUser(msg);
  133                   log.warn(msg);
  134               } catch (Exception ex) {
  135                   String msg = ex.getMessage();
  136                   if (msg == null) {
  137                       msg = "Unexpected error - see log for details";
  138                   }
  139                   JMeterUtils.reportErrorToUser(msg);
  140                   log.warn("Unexpected error", ex);
  141               }
  142               finally{
  143                   JOrphanUtils.closeQuietly(reader);
  144               }
  145           }
  146           return tree;
  147       }
  148   
  149       private void removeDisabledItems(HashTree tree) {
  150           Iterator iter = new LinkedList(tree.list()).iterator();
  151           while (iter.hasNext()) {
  152               TestElement item = (TestElement) iter.next();
  153               if (!item.isEnabled()) {
  154                   //log.info("Removing "+item.toString());
  155                   tree.remove(item);
  156               } else {
  157                   //log.info("Keeping "+item.toString());
  158                   removeDisabledItems(tree.getTree(item));// Recursive call
  159               }
  160           }
  161       }
  162   
  163   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]