KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > loadgenerator > api > impl > EngineManagerImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.loadgenerator.api.impl;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.JFileChooser JavaDoc;
30 import javax.swing.filechooser.FileFilter JavaDoc;
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 /**
43  *
44  * @author Jaroslav Bachorik
45  */

46 public class EngineManagerImpl implements EngineManager, ProcessInstanceListener {
47   private static EngineManager instance = null;
48   
49   final private Map JavaDoc<ProcessInstance, ProgressHandle> prgrsHandles = Collections.synchronizedMap(new HashMap JavaDoc<ProcessInstance, ProgressHandle>());
50   final private ManagerOutputWindowRegistry registry = ManagerOutputWindowRegistry.getDefault();
51   
52   private Collection JavaDoc<ProcessInstance> runningInstances;
53   private static String JavaDoc lastUsedScript = null;
54   
55   public void generatorStarted(ProcessInstance provider) {
56     try {
57       prgrsHandles.get(provider).finish();
58       prgrsHandles.remove(provider);
59     } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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   /**
116    * Creates a new instance of EngineManagerImpl
117    */

118   public EngineManagerImpl() {
119     runningInstances = new ArrayList JavaDoc<ProcessInstance>();
120   }
121   
122   public Collection JavaDoc<Engine> findEngines() {
123     Collection JavaDoc<Engine> providers = new ArrayList JavaDoc<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 JavaDoc<Engine> findEngines(final String JavaDoc extension) {
133     Collection JavaDoc<Engine> providers = new ArrayList JavaDoc<Engine>();
134     Collection JavaDoc<? 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 JavaDoc("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     // open the main management window
173
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 JavaDoc chooser = new JFileChooser JavaDoc();
183     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
184     chooser.setAcceptAllFileFilterUsed(false);
185     chooser.setFileFilter(new FileFilter JavaDoc() {
186       public boolean accept(File JavaDoc f) {
187         if (f.isDirectory())
188           return true;
189         return provider.getSupportedExtensions().contains(FileUtil.getExtension(f.getAbsolutePath()));
190       }
191       public String JavaDoc getDescription() {
192         return "Supported scripts";
193       }
194     });
195     if (lastUsedScript != null) {
196       chooser.setCurrentDirectory(new File JavaDoc(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 JavaDoc 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 JavaDoc("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