1 2 3 package org.netbeans.modules.profiler.loadgen; 4 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Enumeration ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.Set ; 11 import org.netbeans.api.project.Project; 12 import org.netbeans.api.project.ProjectUtils; 13 import org.netbeans.api.project.SourceGroup; 14 import org.netbeans.api.project.Sources; 15 import org.netbeans.modules.profiler.spi.LoadGenPlugin; 16 import org.openide.filesystems.FileObject; 17 import org.netbeans.modules.loadgenerator.api.EngineManager; 18 import org.netbeans.modules.loadgenerator.spi.Engine; 19 import org.netbeans.spi.project.SubprojectProvider; 20 import org.netbeans.api.java.project.JavaProjectConstants; 21 import org.netbeans.modules.loadgenerator.spi.ProcessInstance; 22 import org.netbeans.modules.loadgenerator.spi.ProcessInstanceListener; 23 import org.openide.filesystems.FileUtil; 24 25 import org.openide.util.Lookup; 26 27 31 public class LoadGenPluginImpl implements LoadGenPlugin, ProcessInstanceListener { 32 Set <ProcessInstance> runningProcesses; 33 34 public LoadGenPluginImpl() { 35 runningProcesses = new HashSet <ProcessInstance>(); 36 } 37 38 43 public Collection <FileObject> listScripts(Project project) { 44 Collection <FileObject> scripts = new ArrayList <FileObject>(); 45 EngineManager manager = Lookup.getDefault().lookup(EngineManager.class); 46 Set <String > allExtensions = new HashSet <String >(); 47 Collection <Engine> engines = manager.findEngines(); 48 49 for(Engine engine : engines) { 50 allExtensions.addAll(engine.getSupportedExtensions()); 51 } 52 return findScripts(project, allExtensions); 53 } 54 55 60 public boolean start(String scriptPath) { 61 EngineManager manager = Lookup.getDefault().lookup(EngineManager.class); 62 Collection <Engine> engines = manager.findEngines(FileUtil.getExtension(scriptPath)); 63 if (engines.isEmpty()) return false; 64 ProcessInstance newProcess = engines.iterator().next().createProcess(scriptPath); 65 newProcess.addListener(this); 66 manager.startProcess(newProcess); 67 return true; 68 } 69 70 74 public boolean isRunning() { 75 return !runningProcesses.isEmpty(); 76 } 77 78 81 public void stop() { 82 EngineManager manager = Lookup.getDefault().lookup(EngineManager.class); 83 for(ProcessInstance process : runningProcesses) { 84 manager.stopProcess(process, true); 85 } 86 } 87 88 92 public void stop(String scriptPath) { 93 EngineManager manager = Lookup.getDefault().lookup(EngineManager.class); 94 for(ProcessInstance process : runningProcesses) { 95 if (process.getCurrentScript().equalsIgnoreCase(scriptPath)) { 96 manager.stopProcess(process, true); 97 } 98 } 99 } 100 101 private static Collection <FileObject> findScripts(final Project project, final Set <String > extensions) { 102 Collection <FileObject> scripts = new ArrayList <FileObject>(); 103 for(FileObject root : getSourceRoots(project)) { 104 Enumeration <? extends FileObject> children = root.getChildren(true); 105 while(children.hasMoreElements()) { 106 FileObject child = children.nextElement(); 107 if (child.isData()) { 108 String extLow = child.getExt().toLowerCase(); 109 String extUp = extLow.toUpperCase(); 110 if (extensions.contains(extLow) || extensions.contains(extUp)) { 111 scripts.add(child); 112 } 113 }; 114 } 115 } 116 return scripts; 117 } 118 119 125 private static FileObject[] getSourceRoots(final Project project) { 126 return getSourceRoots(project, true); 127 } 128 129 136 private static FileObject[] getSourceRoots(final Project project, final boolean traverse) { 137 Set <FileObject> set = new HashSet <FileObject>(); 138 Set <Project> projects = new HashSet <Project>(); 139 140 projects.add(project); 141 getSourceRoots(project,traverse,projects,set); 142 return (FileObject[])set.toArray(new FileObject[0]); 143 } 144 145 private static void getSourceRoots(final Project project, final boolean traverse, Set <Project> projects, Set <FileObject> roots) { 146 final Sources sources = ProjectUtils.getSources(project); 147 final SourceGroup[] sgs = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 148 for (int i = 0; i < sgs.length; i++) { 149 roots.add(sgs[i].getRootFolder()); 150 } 151 152 if (traverse) { 153 SubprojectProvider spp = (SubprojectProvider)project.getLookup().lookup(SubprojectProvider.class); 155 if (spp != null) { 156 for (Iterator it = spp.getSubprojects().iterator(); it.hasNext();) { 157 Project p = (Project)it.next(); 158 if (projects.add(p)) { 159 getSourceRoots(p, traverse,projects,roots); 160 } 161 } 162 } 163 } 164 } 165 166 public void generatorStarted(ProcessInstance provider) { 167 runningProcesses.add(provider); 168 } 169 170 public void generatorStarted(ProcessInstance provider, String logPath) { 171 runningProcesses.add(provider); 172 } 173 174 public void generatorStopped(ProcessInstance provider) { 175 provider.removeListener(this); 176 runningProcesses.remove(provider); 177 } 178 179 public void instanceInvalidated(ProcessInstance instance) { 180 instance.removeListener(this); 181 runningProcesses.remove(instance); 182 } 183 } 184 | Popular Tags |