KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > profiler > loadgen > LoadGenPluginImpl


1
2
3 package org.netbeans.modules.profiler.loadgen;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Set JavaDoc;
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 /**
28  *
29  * @author Jaroslav Bachorik
30  */

31 public class LoadGenPluginImpl implements LoadGenPlugin, ProcessInstanceListener {
32   Set JavaDoc<ProcessInstance> runningProcesses;
33   /** Creates a new instance of LoadGenPluginImpl */
34   public LoadGenPluginImpl() {
35     runningProcesses = new HashSet JavaDoc<ProcessInstance>();
36   }
37   
38   /**
39    *
40    * @param project
41    * @return
42    */

43   public Collection JavaDoc<FileObject> listScripts(Project project) {
44     Collection JavaDoc<FileObject> scripts = new ArrayList JavaDoc<FileObject>();
45     EngineManager manager = Lookup.getDefault().lookup(EngineManager.class);
46     Set JavaDoc<String JavaDoc> allExtensions = new HashSet JavaDoc<String JavaDoc>();
47     Collection JavaDoc<Engine> engines = manager.findEngines();
48     
49     for(Engine engine : engines) {
50       allExtensions.addAll(engine.getSupportedExtensions());
51     }
52     return findScripts(project, allExtensions);
53   }
54   
55   /**
56    *
57    * @param scriptPath
58    * @return
59    */

60   public boolean start(String JavaDoc scriptPath) {
61     EngineManager manager = Lookup.getDefault().lookup(EngineManager.class);
62     Collection JavaDoc<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   /**
71    *
72    * @return
73    */

74   public boolean isRunning() {
75     return !runningProcesses.isEmpty();
76   }
77   
78   /**
79    *
80    */

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   /**
89    *
90    * @param scriptPath
91    */

92   public void stop(String JavaDoc 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 JavaDoc<FileObject> findScripts(final Project project, final Set JavaDoc<String JavaDoc> extensions) {
102     Collection JavaDoc<FileObject> scripts = new ArrayList JavaDoc<FileObject>();
103     for(FileObject root : getSourceRoots(project)) {
104       Enumeration JavaDoc<? extends FileObject> children = root.getChildren(true);
105       while(children.hasMoreElements()) {
106         FileObject child = children.nextElement();
107         if (child.isData()) {
108           String JavaDoc extLow = child.getExt().toLowerCase();
109           String JavaDoc 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   /**
120    * Provides a list of source roots for the given project.
121    *
122    * @param project The project
123    * @return an array of FileObjects that are the source roots for this project
124    */

125   private static FileObject[] getSourceRoots(final Project project) {
126     return getSourceRoots(project, true);
127   }
128   
129   /**
130    * Provides a list of source roots for the given project.
131    *
132    * @param project The project
133    * @param traverse Include subprojects
134    * @return an array of FileObjects that are the source roots for this project
135    */

136   private static FileObject[] getSourceRoots(final Project project, final boolean traverse) {
137     Set JavaDoc<FileObject> set = new HashSet JavaDoc<FileObject>();
138     Set JavaDoc<Project> projects = new HashSet JavaDoc<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 JavaDoc<Project> projects, Set JavaDoc<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       // process possible subprojects
154
SubprojectProvider spp = (SubprojectProvider)project.getLookup().lookup(SubprojectProvider.class);
155       if (spp != null) {
156         for (Iterator JavaDoc 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 JavaDoc 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