1 19 20 package org.netbeans.modules.loadgenerator.spi; 21 22 import java.awt.Image ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 33 public abstract class Engine { 34 public static final String STATE = Engine.class.getName() + "#STATE"; public static final String INSTANCE = Engine.class.getName() + "#INSTANCE"; 37 private boolean lastReadyState = true; 38 39 final private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 40 41 final private ProcessInstanceListener pil = new ProcessInstanceListener() { 42 public void generatorStarted(ProcessInstance provider) { 43 setLastReadyState(false); 44 } 45 46 public void generatorStarted(ProcessInstance provider, String logPath) { 47 setLastReadyState(false); 48 } 49 50 public void generatorStopped(ProcessInstance provider) { 51 setLastReadyState(true); 52 } 53 54 public void instanceInvalidated(ProcessInstance instance) { 55 processes.remove(instance); 56 } 57 }; 58 59 final private List <ProcessInstance> processes; 60 61 public Engine() { 62 processes = new ArrayList <ProcessInstance>(); 63 } 64 65 73 public ProcessInstance createProcess(final String scriptName) { 74 ProcessInstance process = getProcessByName(scriptName); 76 if (process == null) { 77 process = prepareInstance(scriptName); 78 process.setCurrentScript(scriptName); 79 process.addListener(pil); 80 if (process.isNew()) { 81 registerProcess(process); 82 } 83 process.touch(); 84 } 85 86 return process; 87 } 88 89 92 public List <? extends ProcessInstance> getProcesses() { 93 return processes; 94 } 95 96 100 public ProcessInstance getProcessByName(final String scriptName) { 101 for(ProcessInstance instance : processes) { 102 if (instance.getCurrentScript() != null && instance.getCurrentScript().equals(scriptName)) { 103 return instance; 104 } 105 } 106 return null; 107 } 108 109 112 public void cleanup() { 113 if (!isReady()) 114 return; 115 116 for(Iterator <ProcessInstance> iter = processes.iterator();iter.hasNext();) { 117 ProcessInstance process = iter.next(); 118 if (!process.isRunning()) { 119 process.detachFactory(); 120 iter.remove(); 121 } 122 } 123 124 pcs.firePropertyChange(INSTANCE, true, false); 125 } 126 127 public boolean canCleanup() { 128 if (!isReady()) 129 return false; 130 131 for(ProcessInstance process : processes) { 132 if (!process.isRunning()) { 133 return true; 134 } 135 } 136 137 return false; 138 } 139 140 public void addPropertyChangeListener(final PropertyChangeListener pcl) { 142 pcs.addPropertyChangeListener(pcl); 143 } 144 145 public void addPropertyChangeListener(final String propertyName, final PropertyChangeListener pcl) { 146 pcs.addPropertyChangeListener(propertyName, pcl); 147 } 148 149 public void removePropertyChangeListener(final PropertyChangeListener pcl) { 150 pcs.removePropertyChangeListener(pcl); 151 } 152 153 public void removePropertyChangeListener(final String propertyName, final PropertyChangeListener pcl) { 154 pcs.removePropertyChangeListener(propertyName, pcl); 155 } 156 158 162 public abstract boolean isReady(); 163 164 168 public abstract Image getIcon(); 169 170 175 public abstract String getDisplayName(); 176 177 180 public abstract Collection <String > getSupportedExtensions(); 181 182 186 protected abstract ProcessInstance prepareInstance(final String scriptName); 187 188 191 private void registerProcess(final ProcessInstance instance) { 192 processes.add(instance); 194 pcs.firePropertyChange(INSTANCE, false, true); 195 } 196 197 200 private void fireReadyStateChanged(final boolean oldValue, final boolean newValue) { 201 pcs.firePropertyChange(STATE, oldValue, newValue); 202 } 203 204 private void setLastReadyState(final boolean value) { 205 fireReadyStateChanged(lastReadyState, value); 206 lastReadyState = value; 207 } 208 } 209 | Popular Tags |