/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.threads;
import java.util.*;
import org.apache.jmeter.timers.Timer;
import org.apache.jmeter.control.GenericController;
import org.apache.jmeter.control.SamplerController;
import org.apache.jmeter.samplers.RemoteSampleListener;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.gui.popup.*;
import org.apache.jmeter.gui.*;
import org.apache.jmeter.config.ConfigElement;
import org.apache.jmeter.save.Saveable;
/************************************************************
* Title: Apache JMeter Description: Copyright: Copyright (c) 2000 Company:
* Apache Foundation
*
*@author Michael Stover
*@created $Date: 2001/06/12 22:36:19 $
*@version 1.0
***********************************************************/
public class ThreadGroup implements SampleListener,JMeterComponentModel,Saveable
{
SamplerController control;
List listeners = new LinkedList();
List remoteListeners = new LinkedList();
int numThreads = 1;
List timers = new LinkedList();
private String name;
private SampleQueue queue;
private static List addableList;
/************************************************************
* !ToDo (Constructor description)
***********************************************************/
public ThreadGroup()
{
control = new GenericController();
if(addableList == null)
{
addableList = new LinkedList();
addableList.add(MenuFactory.getTimerMenu());
addableList.add(MenuFactory.getListenerMenu());
addableList.add(MenuFactory.getControllerMenu());
addableList.add(MenuFactory.getConfigElementMenu());
}
queue = new SampleQueue();
Thread thread = new Thread(queue);
//thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
public void uncompile()
{
control = new GenericController();
timers.clear();
this.clearListeners();
this.clearRemoteListeners();
}
/************************************************************
* !ToDo (Method aadescription)
*
*@param numThreads !ToDo (Parameter description)
***********************************************************/
public void setNumThreads(int numThreads)
{
this.numThreads = numThreads;
}
/************************************************************
* !ToDo (Method description)
*
*@param timer !ToDo (Parameter description)
***********************************************************/
public void addTimer(Timer timer)
{
this.timers.add(timer);
}
public Class getTagHandlerClass()
{
return org.apache.jmeter.save.handlers.ThreadGroupHandler.class;
}
/************************************************************
* !ToDo (Method description)
*
*@param newName !ToDo (Parameter description)
***********************************************************/
public void setName(String newName)
{
name = newName;
}
public Class getGuiClass()
{
return org.apache.jmeter.threads.gui.ThreadGroupGui.class;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public SamplerController getSamplerController()
{
return this.control;
}
public void setSamplerController(SamplerController c)
{
control = c;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public int getNumThreads()
{
return this.numThreads;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public List getTimers()
{
return this.timers;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public Collection getListeners()
{
return this.listeners;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public Collection getRemoteListeners()
{
return this.remoteListeners;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public String getName()
{
return name;
}
/************************************************************
* !ToDo
*
*@param control !ToDo
***********************************************************/
public void addSamplerController(SamplerController control)
{
this.control.addSamplerController(control);
}
public void addConfigElement(ConfigElement config)
{
this.control.addConfigElement(config);
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void clearListeners()
{
listeners.clear();
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void clearRemoteListeners()
{
remoteListeners.clear();
}
/************************************************************
* !ToDo
*
*@param listener !ToDo
***********************************************************/
public void addListener(SampleListener listener)
{
this.listeners.add(listener);
}
/************************************************************
* !ToDo
*
*@param listener !ToDo
***********************************************************/
public void addRemoteListener(RemoteSampleListener listener)
{
this.remoteListeners.add(listener);
}
/************************************************************
* !ToDo (Method description)
*
*@param listener !ToDo (Parameter description)
***********************************************************/
public void removeListener(SampleListener listener)
{
this.listeners.remove(listener);
}
/************************************************************
* !ToDo (Method description)
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleOccurred(SampleEvent e)
{
queue.sampleOccurred(e);
}
/************************************************************
* A sample has started.
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleStarted(SampleEvent e)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleStarted(e);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleStarted(e);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
/************************************************************
* A sample has stopped.
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleStopped(SampleEvent e)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleStopped(e);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleStopped(e);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public String getClassLabel()
{
return "ThreadGroup";
}
/**
* Indicates whether the item is editable. If so, the popup will include a
* "edit" choice which will display the item's gui screen in the main panel.
*/
public boolean isEditable()
{
return true;
}
/**
* Returns the list of object types that can be added to this item.
*/
public Collection getAddList()
{
return addableList;
}
/**
* Separate thread to deliver all SampleEvents. This ensures that sample listeners
* will get sample events 1 at a time, and can thus ignore thread issues.
*/
private class SampleQueue implements Runnable
{
List occurredQ = Collections.synchronizedList(new LinkedList());
public SampleQueue()
{
}
public synchronized void sampleOccurred(SampleEvent e)
{
occurredQ.add(e);
this.notify();
}
public void run()
{
SampleEvent event = null;
while(true)
{
try
{
event = (SampleEvent)occurredQ.remove(0);
}
catch (Exception ex)
{
waitForSamples();
continue;
}
try
{
if(event != null)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleOccurred(event);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleOccurred(event);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else
waitForSamples();
}
catch (Throwable ex)
{
ex.printStackTrace();
}
}
}
private synchronized void waitForSamples()
{
try
{
this.wait();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}