Package test.stress

Source Code of test.stress.TestConfig

/*
* TestConfig.java
*
* Created on April 15, 2007, 9:15 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package test.stress;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.atomojo.app.Main;
import org.atomojo.app.ServerConfiguration;
import org.atomojo.app.StorageFactory;
import org.atomojo.app.WebComponent;
import org.atomojo.app.db.ConfigurationException;
import org.atomojo.app.db.DB;
import org.atomojo.app.storage.xmldb.XMLDBStorageFactory;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.WriterItemDestination;
import org.restlet.Server;
import org.restlet.data.Reference;

/**
*
* @author alex
*/
public class TestConfig
{

   Logger log = Logger.getLogger(TestConfig.class.getName());
   Reference location;
   File testDir;
   WebComponent www;
   Map<String,DB> dbList;
   ServerConfiguration conf;
  
   public static File getTestDir(String dirName)
   {
      String stestDir = System.getProperty("atomojo.test.dir");
      if (stestDir==null) {
         stestDir = "test.out";
      }
      File parentDir = new File(stestDir);
      if (!parentDir.exists()) {
         parentDir.mkdir();
      }
      File dir = new File(parentDir,dirName);
      if (!dir.exists()) {
         dir.mkdir();
      }
      return dir;
   }
   /** Creates a new instance of TestConfig */
   public TestConfig(String dirName,String hostname,String ipAddress,int port)
      throws IOException,ConfigurationException,XMLException
   {
      String slocation = System.getProperty("atomojo.test.location");
      if (slocation==null) {
         slocation = "http://localhost:8080/";
      }
      location = new Reference(slocation);

      testDir = getTestDir(dirName);
     
      dbList = DB.getDatabases(log,testDir);
      if (dbList.isEmpty()) {
         DB db = DB.createDB(log,testDir,"data");
         dbList.put(db.getName(),db);           
         try {
            DB.writeList(testDir,dbList);
         } catch (Exception ex) {
            ex.printStackTrace();
            return;
         }
      }
     
      conf = new ServerConfiguration();
     
      File serverConfFile = new File(testDir,"server.conf");
      File keystoreFile = new File(testDir,"keystore");
      File logFile = new File(testDir,"host.log");
      if (serverConfFile.exists()) {
         conf.load(serverConfFile.toURI());
      } else {
         ServerConfiguration.Host host = new ServerConfiguration.Host(conf,"data",hostname,null,port,false,false,logFile);
         conf.getInterfaces().add(new ServerConfiguration.Interface(ipAddress,port,false));
         conf.getHosts().put(host.getName(),host);
         conf.setKeystoreFile(keystoreFile);
         conf.setKeystorePassword("atomojo");
         conf.setKeyPassword("atomojo");
         Writer out = new OutputStreamWriter(new FileOutputStream(serverConfFile),"UTF-8");
         WriterItemDestination dest = new WriterItemDestination(out,"UTF-8");
         conf.store(serverConfFile.toURI(),dest);
         out.flush();
         out.close();
         if (!keystoreFile.exists()) {
            Main.copyResource("/org/atomojo/app/db/conf/keystore",keystoreFile);
         }
      }
   }
  
   public File getDatabaseDirectory() {
      return testDir;
   }
  
   public Reference getServerLocation() {
      return location;
   }
  
   public void startServer()
      throws Exception
   {
      dbList.get("data").connect();
      StorageFactory storageFactory = new XMLDBStorageFactory();
      www = new WebComponent(getDatabaseDirectory(),dbList,storageFactory,conf);
      www.start();
   }
  
   public void stopServer()
      throws Exception
   {
      www.stop();
      for (Server server : www.getServers()) {
         if (server.isStarted()) {
            server.stop();
         }
      }
   }

   public static boolean deltree(File dir)
   {
      final List<File> queue = new ArrayList<File>();
      queue.add(dir);
      boolean ok = true;
      int mark = -1;
      while (ok && queue.size()>0) {
         File target = queue.remove(queue.size()-1);
         if (target.isDirectory()) {
            if (mark==queue.size()) {
               ok = target.delete();
               mark = -1;
            } else {
               mark = queue.size();
               queue.add(target);
               target.listFiles(new FileFilter() {
                  public boolean accept(File f)
                  {
                     queue.add(f);
                     return false;
                  }
               });
            }
         } else {
            ok = target.delete();
         }
      }
      return ok;
   }
  
}
TOP

Related Classes of test.stress.TestConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.