/*
 * Copyright 2005-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.control;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * 
 * 
 * author Peter Lin
 * @version $Revision$
 */
public class IncludeController extends GenericController implements ReplaceableController {
	private static final Logger log = LoggingManager.getLoggerForClass();

    public static final String INCLUDE_PATH = "IncludeController.includepath";

    private static  final String prefix;
    static {
    	prefix=JMeterUtils.getPropDefault("includecontroller.prefix", "");
    }
    private HashTree SUBTREE = null;
    private TestElement SUB = null;

	/**
	 * No-arg constructor
	 * 
	 * @see java.lang.Object#Object()
	 */
	public IncludeController() {
		super();
	}

	public Object clone() {
        this.loadIncludedElements();
		IncludeController clone = (IncludeController) super.clone();
        clone.setIncludePath(this.getIncludePath());
        if (this.SUBTREE != null) {
            if (this.SUBTREE.keySet().size() == 1) {
                Iterator itr = this.SUBTREE.keySet().iterator();
                while (itr.hasNext()) {
                    this.SUB = (TestElement)itr.next();
                }
            }
            clone.SUBTREE = (HashTree)this.SUBTREE.clone();
            clone.SUB = (TestElement)this.SUB.clone();
        }
		return clone;
	}

    /**
     * In the event an user wants to include an external JMX test plan
     * the GUI would call this.
     * @param jmxfile
     */
    public void setIncludePath(String jmxfile) {
        this.setProperty(INCLUDE_PATH,jmxfile);
    }
    
    /**
     * return the JMX file path.
     * @return
     */
    public String getIncludePath() {
        return this.getPropertyAsString(INCLUDE_PATH);
    }
    
    /**
     * The way ReplaceableController works is clone is called first,
     * followed by replace(HashTree) and finally getReplacement().
     */
    public HashTree getReplacementSubTree() {
        return SUBTREE;
    }

    /**
     * load the included elements using SaveService
     * @param node
     */
    protected HashTree loadIncludedElements() {
        // only try to load the JMX test plan if there is one
        final String includePath = getIncludePath();
        if (includePath != null && includePath.length() > 0) {
            try {
            	String file=prefix+includePath;
                log.info("loadIncludedElements -- try to load included module: "+file);
                InputStream reader = new FileInputStream(file);
                this.SUBTREE = SaveService.loadTree(reader);
                return this.SUBTREE;
            } catch (NoClassDefFoundError ex) // Allow for missing optional jars
            {
                String msg = ex.getMessage();
                if (msg == null) {
                    msg = "Missing jar file - see log for details";
                    log.warn("Missing jar file", ex);
                }
                JMeterUtils.reportErrorToUser(msg);
            } catch (Exception ex) {
                String msg = ex.getMessage();
                if (msg == null) {
                    msg = "Unexpected error - see log for details";
                    log.warn("Unexpected error", ex);
                }
            }
        }
        return this.SUBTREE;
    }
    
}

@@ -676,32 +676,98 @@
 ileInputStream;%0A
+import java.io.FileNotFoundException;%0Aimport java.io.IOException;%0A
 import java.io.I
@@ -3194,24 +3194,59 @@
 ludePath();%0A
+        InputStream reader = null;%0A
         if (
@@ -3459,36 +3459,24 @@
             
-InputStream 
 reader = new
@@ -3837,32 +3837,46 @@
 r details%22;%0A    
+            %7D%0A
                 
@@ -3917,33 +3917,162 @@
                 
-%7D
+JMeterUtils.reportErrorToUser(msg);%0A            %7D catch (FileNotFoundException ex) %7B%0A                String msg = ex.getMessage();
 %0A               
@@ -4100,32 +4100,63 @@
 rorToUser(msg);%0A
+                log.warn(msg);%0A
             %7D ca
@@ -4345,45 +4345,315 @@
     
-    log.warn(%22Unexpected error%22, ex);
+%7D%0A                JMeterUtils.reportErrorToUser(msg);%0A                log.warn(%22Unexpected error%22, ex);%0A            %7D%0A            finally%7B%0A                if (reader!=null)%7B%0A                    try %7B%0A                        reader.close();%0A                    %7D catch (IOException e) %7B%0A                    %7D
 %0A   

Improve error reporting. Close input file after use.