1 19 20 package org.netbeans.modules.loadgenerator.api.impl; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import javax.swing.JFileChooser ; 30 import javax.swing.filechooser.FileFilter ; 31 import org.netbeans.api.progress.ProgressHandle; 32 import org.netbeans.api.progress.ProgressHandleFactory; 33 import org.netbeans.modules.loadgenerator.api.EngineManager; 34 import org.netbeans.modules.loadgenerator.spi.Engine; 35 import org.netbeans.modules.loadgenerator.spi.ProcessInstance; 36 import org.netbeans.modules.loadgenerator.spi.ProcessInstanceListener; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.Cancellable; 40 import org.openide.util.Lookup; 41 42 46 public class EngineManagerImpl implements EngineManager, ProcessInstanceListener { 47 private static EngineManager instance = null; 48 49 final private Map <ProcessInstance, ProgressHandle> prgrsHandles = Collections.synchronizedMap(new HashMap <ProcessInstance, ProgressHandle>()); 50 final private ManagerOutputWindowRegistry registry = ManagerOutputWindowRegistry.getDefault(); 51 52 private Collection <ProcessInstance> runningInstances; 53 private static String lastUsedScript = null; 54 55 public void generatorStarted(ProcessInstance provider) { 56 try { 57 prgrsHandles.get(provider).finish(); 58 prgrsHandles.remove(provider); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 63 if (!runningInstances.contains(provider)) { 64 runningInstances.add(provider); 65 } 66 67 } 68 public void generatorStarted(final ProcessInstance provider, final String logPath) { 69 try { 70 if (prgrsHandles == null || provider == null) return; 71 ProgressHandle phandle = prgrsHandles.get(provider); 72 if (phandle != null) { 73 phandle.finish(); 74 prgrsHandles.remove(provider); 75 } 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 80 if (!runningInstances.contains(provider)) { 81 runningInstances.add(provider); 82 } 83 } 84 85 public void generatorStopped(final ProcessInstance provider) { 86 try { 87 ProgressHandle ph = prgrsHandles.get(provider); 88 if (ph != null) { 89 ph.finish(); 90 prgrsHandles.remove(provider); 91 } 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 96 ((ProcessInstance)provider).removeListener(this); 97 runningInstances.remove(provider); 98 99 ManagerOutputWindow lgmgrWin = registry.find(provider); 100 if (lgmgrWin != null) { 101 lgmgrWin.detach(provider); 102 } 103 } 104 105 106 public void instanceInvalidated(ProcessInstance instance) { 107 if (lastUsedScript != null) { 108 lastUsedScript = instance.getCurrentScript(); 109 } 110 if (!instance.isRunning()) { 111 registry.close(instance); 112 } 113 } 114 115 118 public EngineManagerImpl() { 119 runningInstances = new ArrayList <ProcessInstance>(); 120 } 121 122 public Collection <Engine> findEngines() { 123 Collection <Engine> providers = new ArrayList <Engine>(); 124 Lookup.Result<Engine> result = Lookup.getDefault().lookupResult(Engine.class); 125 for(Engine provider : result.allInstances()) { 126 providers.add(provider); 127 } 128 129 return providers; 130 } 131 132 public Collection <Engine> findEngines(final String extension) { 133 Collection <Engine> providers = new ArrayList <Engine>(); 134 Collection <? extends Engine> result = Lookup.getDefault().lookupAll(Engine.class); 135 136 for(Engine provider : result) { 137 if (provider.getSupportedExtensions().contains(extension)) { 138 providers.add(provider); 139 } 140 } 141 142 return providers; 143 } 144 145 public void startProcess(final ProcessInstance instance) { 146 if (instance.isRunning()) { 147 ErrorManager.getDefault().notify(ErrorManager.ERROR, new Throwable ("Provider is busy")); 148 return; 149 } 150 151 ProgressHandle phandle = ProgressHandleFactory.createHandle("Starting load generator", new Cancellable() { 152 public boolean cancel() { 153 if (prgrsHandles == null || instance == null) return true; 154 ProgressHandle phandle = prgrsHandles.get(instance); 155 if (phandle != null) { 156 phandle.finish(); 157 stopProcess(instance, true); 158 return true; 159 } 160 return false; 161 } 162 }); 163 164 phandle.setInitialDelay(0); 165 phandle.start(); 166 phandle.switchToIndeterminate(); 167 168 prgrsHandles.put(instance, phandle); 169 170 ((ProcessInstance)instance).addListener(this); 171 172 ManagerOutputWindow mngrWin = registry.open(instance); 174 175 instance.start(); 176 lastUsedScript = instance.getCurrentScript(); 177 } 178 179 public ProcessInstance startNewProcess(final Engine provider) { 180 ProcessInstance runnableInstance = null; 181 182 final JFileChooser chooser = new JFileChooser (); 183 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 184 chooser.setAcceptAllFileFilterUsed(false); 185 chooser.setFileFilter(new FileFilter () { 186 public boolean accept(File f) { 187 if (f.isDirectory()) 188 return true; 189 return provider.getSupportedExtensions().contains(FileUtil.getExtension(f.getAbsolutePath())); 190 } 191 public String getDescription() { 192 return "Supported scripts"; 193 } 194 }); 195 if (lastUsedScript != null) { 196 chooser.setCurrentDirectory(new File (lastUsedScript)); 197 } 198 int retValue = chooser.showOpenDialog(null); 199 if (retValue == JFileChooser.APPROVE_OPTION) { 200 try { 201 runnableInstance = provider.createProcess(chooser.getSelectedFile().getCanonicalPath()); 202 startProcess(runnableInstance); 203 } catch (IOException ex) { 204 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 205 } 206 } 207 208 return runnableInstance; 209 } 210 211 public void stopProcess(final ProcessInstance provider, final boolean force) { 212 if (!provider.isRunning()) { 213 ErrorManager.getDefault().notify(ErrorManager.WARNING, new Throwable ("Stopping a non-running provider")); 214 } 215 216 ProgressHandle phandle = ProgressHandleFactory.createHandle("Stopping load generator"); 217 phandle.setInitialDelay(0); 218 phandle.start(); 219 phandle.switchToIndeterminate(); 220 221 prgrsHandles.put(provider, phandle); 222 223 provider.stop(force); 224 } 225 } 226 | Popular Tags |