/*
 * Created on Jun 13, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.apache.jmeter.save;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;

import org.apache.jmeter.save.converters.BooleanPropertyConverter;
import org.apache.jmeter.save.converters.HashTreeConverter;
import org.apache.jmeter.save.converters.IntegerPropertyConverter;
import org.apache.jmeter.save.converters.LongPropertyConverter;
import org.apache.jmeter.save.converters.MultiPropertyConverter;
import org.apache.jmeter.save.converters.StringPropertyConverter;
import org.apache.jmeter.save.converters.TestElementConverter;
import org.apache.jmeter.save.converters.TestElementPropertyConverter;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.IntegerProperty;
import org.apache.jmeter.testelement.property.LongProperty;
import org.apache.jmeter.testelement.property.MapProperty;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import com.thoughtworks.xstream.XStream;

/**
 * @author mstover
 *
 * @author     Mike Stover
 * @author     <a href="mailto:kcassell&#X0040;apache.org">Keith Cassell</a>
 */
public class SaveService
{
   private static XStream saver = new XStream();
   private static Logger log = LoggingManager.getLoggerForClass();
   
   static
   {
      saver.alias("stringProp",StringProperty.class);
      saver.alias("intProp",IntegerProperty.class);
      saver.alias("longProp",LongProperty.class);
      saver.alias("collectionProp",CollectionProperty.class);
      saver.alias("mapProp",MapProperty.class);
      saver.alias("elementProp",TestElementProperty.class);
      saver.alias("boolProp",BooleanProperty.class);
      saver.alias("hashTree",ListedHashTree.class);
      saver.alias("jmeterTestPlan",ScriptWrapper.class);
      saver.registerConverter(new StringPropertyConverter());
      saver.registerConverter(new BooleanPropertyConverter());
      saver.registerConverter(new IntegerPropertyConverter());
      saver.registerConverter(new LongPropertyConverter());
      saver.registerConverter(new TestElementConverter(saver.getClassMapper(),"class"));
      saver.registerConverter(new MultiPropertyConverter(saver.getClassMapper(),"class"));
      saver.registerConverter(new TestElementPropertyConverter(saver.getClassMapper(),"class"));
      saver.registerConverter(new HashTreeConverter(saver.getClassMapper(),"class"));
      saver.registerConverter(new ScriptWrapperConverter(saver.getClassMapper()));
   }

   public static void saveTree(HashTree tree,Writer writer) throws Exception
   {
      ScriptWrapper wrapper = new ScriptWrapper();
      wrapper.testPlan = tree;
      saver.toXML(wrapper,writer);
   }
   
   public static HashTree loadTree(InputStream reader) throws Exception
   {
      if(!reader.markSupported())
      {
         reader = new BufferedInputStream(reader);
      }
      reader.mark(Integer.MAX_VALUE);
      ScriptWrapper wrapper = null;
      try
      {
         wrapper = (ScriptWrapper)saver.fromXML(new InputStreamReader(reader));
         return wrapper.testPlan;
      }
      catch (RuntimeException e)
      {
         log.warn("Problem loading new style: ",e);
         reader.reset();
         return OldSaveService.loadSubTree(reader);
      }
   }
}

@@ -216,32 +216,64 @@
 redInputStream;%0A
+import java.io.FileInputStream;%0A
 import java.io.I
@@ -340,16 +340,94 @@
 .Writer;
+%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -1375,32 +1375,75 @@
 lementProperty;%0A
+import org.apache.jmeter.util.JMeterUtils;%0A
 import org.apach
@@ -1939,19 +1939,960 @@
 %0A   
-static%0A   %7B
+// Helper method to simplify alias creation from properties%0A   private static void makeAlias(String alias,String clazz)%0A   %7B%0A   %09try %7B%0A%09%09saver.alias(alias,Class.forName(clazz));%0A%09%7D catch (ClassNotFoundException e) %7B%0A%09%09log.warn(%22Could not set up alias %22+alias+%22 %22+e.toString());%0A%09%7D%0A   %7D%0A   %0A   static%0A   %7B%0A   %09%0A      // Load the alias properties%0A%09  Properties nameMap = new Properties();%0A%09  try%0A%09  %7B%0A%09     nameMap.load(%0A%09         new FileInputStream(%0A%09             JMeterUtils.getJMeterHome()%0A%09                 + JMeterUtils.getPropDefault(%0A%09                     %22saveservice_properties%22,%0A%09                     %22/bin/saveservice.properties%22)));%0A%09     // now create the aliases%0A%09%09 Iterator it = nameMap.entrySet().iterator();%0A%09%09 while (it.hasNext())%0A%09%09 %7B%0A%09%09 %09Map.Entry me = (Map.Entry) it.next();%0A%09%09  %09makeAlias((String)me.getKey(),(String)me.getValue());%0A%09%09 %7D%0A%09  %7D%0A%09  catch (Exception e)%0A%09  %7B%0A%09     log.error(%22Bad saveservice properties file%22,e);%0A%09  %7D%0A%09
 %0A   

Use saveservice properties file