Skip to content

Commit

Permalink
Complete the refactoring of SWTRipper and SWTReplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick committed Apr 17, 2011
1 parent 64fca63 commit 1e1b4db
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 214 deletions.
78 changes: 78 additions & 0 deletions src/edu/umd/cs/guitar/ripper/SWTApplicationRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package edu.umd.cs.guitar.ripper;

import edu.umd.cs.guitar.model.SWTApplicationStartException;
import edu.umd.cs.guitar.util.GUITARLog;

/**
* This class handles running the GUI and {@link SWTGuitarExecutor}. As
* SWTGuitar uses a fairly complicated threading model, clients should make use
* of this class instead of trying to run {@link SWTRipper} or
* {@link SWTReplayer} directly.
*
* @author Gabe Gorelick
*
*/
public class SWTApplicationRunner {

private final SWTGuitarExecutor executor;
private final Thread executorThread;

/**
* The default name of the {@link Thread} that the
* <code>SWTGuitarExecutor</code> will run on. This can be overridden with
* {@link #setThreadName(String)}.
*/
public static final String THREAD_NAME = "SWTGuitarExecutor";

public SWTApplicationRunner(SWTGuitarExecutor exec) {
this.executor = exec;
executorThread = new Thread(new Runnable() {
@Override
public void run() {
executor.execute();
}
});

// set thread name to ease debugging
executorThread.setName(THREAD_NAME);
}

/**
* <p>
* Start the GUI and the {@link SWTGuitarExecutor}.
* </p>
* <p>
* Because of inherent limitations in SWT, the GUI must be started on the
* <code>main</code> thread. But once the GUI starts, it will block until it
* is closed by the user. To avoid this, this method starts the
* <code>SWTGuitarExecutor</code> first on another thread and then starts
* the GUI on the <code>main</code> thread. It is the responsibility of the
* <code>SWTGuitarExecutor</code> to wait until the GUI is ready.
* </p>
*/
public void run() {
executorThread.start();
try {
executor.getApplication().startGUI();

// prevent the main thread closing (and thus the JVM) before
// the executor is done
executorThread.join();
} catch (SWTApplicationStartException e) {
GUITARLog.log.error(e);
} catch (InterruptedException e) {
GUITARLog.log.error(e);
}
}

/**
* Set the name of the {@link Thread} that the
* <code>SWTGuitarExecutor</code> will run on. By default, this is {@link SWTApplicationRunner#THREAD_NAME}
*
* @param name
* the name of the thread
*/
public void setThreadName(String name) {
executorThread.setName(name);
}
}
161 changes: 161 additions & 0 deletions src/edu/umd/cs/guitar/ripper/SWTGuitarExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package edu.umd.cs.guitar.ripper;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import edu.umd.cs.guitar.model.GUITARConstants;
import edu.umd.cs.guitar.model.IO;
import edu.umd.cs.guitar.model.SWTApplication;
import edu.umd.cs.guitar.model.SWTConstants;
import edu.umd.cs.guitar.model.data.AttributesType;
import edu.umd.cs.guitar.model.data.ComponentListType;
import edu.umd.cs.guitar.model.data.ComponentType;
import edu.umd.cs.guitar.model.data.Configuration;
import edu.umd.cs.guitar.model.data.FullComponentType;
import edu.umd.cs.guitar.model.wrapper.AttributesTypeWrapper;
import edu.umd.cs.guitar.model.wrapper.ComponentTypeWrapper;
import edu.umd.cs.guitar.util.DefaultFactory;
import edu.umd.cs.guitar.util.GUITARLog;

public abstract class SWTGuitarExecutor {

private final SWTGuitarConfiguration config;
private final SWTApplication application;

private final Configuration xmlConfig;

private long startTime;

protected SWTGuitarExecutor(SWTGuitarConfiguration config) {
this(config, Thread.currentThread());
}

protected SWTGuitarExecutor(SWTGuitarConfiguration config, Thread guiThread) {
if (config == null) {
config = new SWTRipperConfiguration();
}

this.config = config;
this.application = new SWTApplication(config.getMainClass(), guiThread);

this.xmlConfig = loadXmlConfig();

System.setProperty(GUITARLog.LOGFILE_NAME_SYSTEM_PROPERTY, config.getLogFile());
}

private Configuration loadXmlConfig() {
Configuration conf = null;

try {
conf = (Configuration) IO.readObjFromFile(config.getConfigFile(),
Configuration.class);

if (conf == null) {
InputStream in = getClass().getClassLoader()
.getResourceAsStream(config.getConfigFile());
conf = (Configuration) IO.readObjFromFile(in,
Configuration.class);
}

} catch (Exception e) {
// this space left intentionally blank
}

if (conf == null) {
GUITARLog.log.info("No configuration file. Using an empty one...");
DefaultFactory df = new DefaultFactory();
conf = df.createDefaultConfiguration();
}

return conf;
}

protected SWTGuitarConfiguration getConfig() {
return config;
}

public SWTApplication getApplication() {
return application;
}

protected long getStartTime() {
return startTime;
}

protected Configuration getXmlConfig() {
return xmlConfig;
}

/**
* Called directly before the {@link #execute() execute} method.
*/
protected void onBeforeExecute() {
initTerminalComponents();
initIgnoredComponents();

beginTiming();
}

public void execute() {
onBeforeExecute();
onExecute();
onAfterExecute();
}

protected abstract void onExecute();

protected void onAfterExecute() {
endTiming();
}

protected void beginTiming() {
startTime = System.currentTimeMillis();
}

protected void endTiming() {
long nEndTime = System.currentTimeMillis();
long nDuration = nEndTime - getStartTime();
DateFormat df = new SimpleDateFormat("HH : mm : ss: SS");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
GUITARLog.log.info("Time Elapsed: " + df.format(nDuration));
}

protected void initTerminalComponents() {
List<FullComponentType> cTerminalList = getXmlConfig().getTerminalComponents().getFullComponent();

for (FullComponentType cTermWidget : cTerminalList) {
ComponentType component = cTermWidget.getComponent();
AttributesType attributes = component.getAttributes();
if (attributes != null) {
SWTConstants.sTerminalWidgetSignature.add(new AttributesTypeWrapper(component.getAttributes()));
}
}
}

protected void initIgnoredComponents() {
List<FullComponentType> lIgnoredComps = new ArrayList<FullComponentType>();
ComponentListType ignoredAll = getXmlConfig().getIgnoredComponents();

if (ignoredAll != null)
for (FullComponentType fullComp : ignoredAll.getFullComponent()) {
ComponentType comp = fullComp.getComponent();

if (comp == null) {
ComponentType win = fullComp.getWindow();
ComponentTypeWrapper winAdapter = new ComponentTypeWrapper(win);
String ID = winAdapter.getFirstValueByName(GUITARConstants.ID_TAG_NAME);
if (ID != null) {
SWTConstants.sIgnoredWins.add(ID);
}

} else {
lIgnoredComps.add(fullComp);
}
}
}

}
Loading

0 comments on commit 1e1b4db

Please sign in to comment.