KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > loadgenerator > spi > ProcessInstance


1 package org.netbeans.modules.loadgenerator.spi;
2
3 import java.awt.Image JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.beans.PropertyChangeSupport JavaDoc;
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12 import org.netbeans.modules.loadgenerator.api.impl.ManagerOutputWindowRegistry;
13 import org.netbeans.modules.loadgenerator.utils.NullOutputWriter;
14 import org.openide.filesystems.FileAttributeEvent;
15 import org.openide.filesystems.FileChangeListener;
16 import org.openide.filesystems.FileEvent;
17 import org.openide.filesystems.FileObject;
18 import org.openide.filesystems.FileRenameEvent;
19 import org.openide.filesystems.FileUtil;
20 import org.openide.util.WeakListeners;
21 import org.openide.windows.OutputWriter;
22
23 /**
24  * Provides the basic scaffolding for a ILoadGeneratorInstance implementation
25  *
26  *
27  * @author Jaroslav Bachorik
28  */

29 public abstract class ProcessInstance {
30   public static final String JavaDoc FACTORY = ProcessInstance.class.getName() + "#FACTORY";
31   public final static String JavaDoc STATE = ProcessInstance.class.getName() + "#STATE";
32   
33   private static final OutputWriter NULLWRITER = new NullOutputWriter();
34   
35   
36   public ProcessInstance(final Engine factory) {
37     pcs = new PropertyChangeSupport JavaDoc(this);
38     listeners = new ArrayList JavaDoc<ProcessInstanceListener>();
39     listenerMap = Collections.synchronizedMap(new HashMap JavaDoc<ProcessInstanceListener, ProcessInstanceListener>());
40     
41     setFactory(factory);
42   }
43   
44   public synchronized boolean isNew() {
45     return isNewFlag;
46   }
47   
48   public synchronized void touch() {
49     isNewFlag = false;
50     isModifiedFlag = false;
51     isDeletedFlag = false;
52 }
53   
54   void attachFactory(final Engine factory) {
55     setFactory(factory);
56   }
57   
58   void detachFactory() {
59     setFactory(null);
60   }
61   
62   /**
63    * Registers a new listener
64    * @param listener The listener instance to register
65    */

66   public void addListener(final ProcessInstanceListener listener) {
67     ProcessInstanceListener weak = WeakListeners.create(ProcessInstanceListener.class, listener, this);
68     if (!listeners.contains(weak)) {
69       listeners.add(weak);
70       listenerMap.put(listener, weak);
71     }
72   }
73   
74   // <editor-fold defaultstate="collapsed" desc="PropertyChange support">
75
public void addPropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
76     pcs.addPropertyChangeListener(pcl);
77   }
78   
79   public void addPropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
80     pcs.addPropertyChangeListener(propertyName, pcl);
81   }
82   
83   public void removePropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
84     pcs.removePropertyChangeListener(pcl);
85   }
86   
87   public void removePropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
88     pcs.removePropertyChangeListener(propertyName, pcl);
89   }
90   // </editor-fold>
91

92   /**
93    * Unregisters a listener
94    * @param listener The listener to unregister
95    */

96   public void removeListener(final ProcessInstanceListener listener) {
97     ProcessInstanceListener weak = listenerMap.get(listener);
98     if (weak != null) {
99       listeners.remove(weak);
100       listenerMap.remove(listener);
101     }
102   }
103   
104   public Engine getFactory() {
105     return factory;
106   }
107   
108   public void start(final String JavaDoc scriptFileName) {
109     setCurrentScript(scriptFileName);
110     performStart(scriptFileName);
111   }
112   
113   public void start() {
114     if (currentScript != null) {
115       performStart(currentScript);
116     }
117   }
118   
119   public void stop(final boolean force) {
120     if (isRunning()) {
121       performStop(force);
122     } else {
123       publishStart(); // fake start; just to be sure we are stopping the engine in a consistent state
124
publishStop();
125     }
126   }
127   
128   public String JavaDoc getCurrentScript() {
129     return currentScript;
130   }
131   
132   public void setCurrentScript(final String JavaDoc value) {
133     if (currentScriptFile != null) {
134       currentScriptFile.removeFileChangeListener(fcl);
135       currentScriptFile = null;
136     }
137     if (value != null) {
138       currentScriptFile = FileUtil.toFileObject(new File JavaDoc(value));
139       currentScriptFile.addFileChangeListener(fcl);
140     }
141     currentScript = value;
142   }
143   
144   /**
145    * Attaches an OutputWriter instance to load generator
146    */

147   public void attachWriter(final OutputWriter writer) {
148     this.writer = writer;
149   }
150   
151   /**
152    * Detaches the previously set OutputWriter
153    */

154   public void detachWriter() {
155     this.writer = null;
156   }
157   
158   public boolean isModified() {
159     return isModifiedFlag;
160   }
161   
162   public boolean isDeleted() {
163     return isDeletedFlag;
164   }
165   
166   public abstract void performStart(final String JavaDoc scriptFileName);
167   public abstract void performStop(final boolean force);
168   
169   /**
170    * Indicates the running status of a ProcessInstance instance
171    */

172   public abstract boolean isRunning();
173   
174   /**
175    * Returns a descriptive name for the particular process instance
176    * @return Returns a descriptive name
177    */

178   public abstract String JavaDoc getDisplayName();
179   
180   /**
181    * Returns the icon representing the load generator process if it exists
182    * @return Returns the icon representing the load generator or null
183    */

184   public abstract Image JavaDoc getIcon();
185   
186   /************* Private implementation ******************/
187   private Collection JavaDoc<ProcessInstanceListener> listeners;
188   private Map JavaDoc<ProcessInstanceListener, ProcessInstanceListener> listenerMap;
189   private PropertyChangeSupport JavaDoc pcs;
190   private String JavaDoc currentScript;
191   private FileObject currentScriptFile;
192   private OutputWriter writer;
193   private Engine factory;
194   private boolean isNewFlag = true, isModifiedFlag = false, isDeletedFlag = false;
195   
196   final private FileChangeListener fcl = new FileChangeListener() {
197     public void fileFolderCreated(FileEvent fe) {
198       // IGNORE
199
}
200     
201     public void fileDataCreated(FileEvent fe) {
202       // IGNORE
203
}
204     
205     public void fileChanged(FileEvent fe) {
206       setModified();
207     }
208     
209     public void fileDeleted(FileEvent fe) {
210       fe.getFile().removeFileChangeListener(this);
211       currentScriptFile = null;
212       currentScript = null;
213       if (!isRunning()) {
214         ManagerOutputWindowRegistry.getDefault().close(ProcessInstance.this);
215       } else {
216         setDeleted();
217       }
218     }
219     
220     public void fileRenamed(FileRenameEvent fe) {
221       currentScript = fe.getFile().getPath();
222       if (!isRunning()) {
223         ManagerOutputWindowRegistry.getDefault().close(ProcessInstance.this);
224         ManagerOutputWindowRegistry.getDefault().open(ProcessInstance.this);
225       } else {
226         setModified();
227       }
228     }
229     
230     public void fileAttributeChanged(FileAttributeEvent fe) {
231       // IGNORE
232
}
233   };
234   
235   
236   protected synchronized void publishStart() {
237     for(ProcessInstanceListener listener : listeners) {
238       listener.generatorStarted(this);
239     }
240     pcs.firePropertyChange(STATE, false, true);
241   }
242   
243   protected synchronized void publishStart(final String JavaDoc logPath) {
244     for(ProcessInstanceListener listener : listeners) {
245       listener.generatorStarted(this, logPath);
246     }
247     pcs.firePropertyChange(STATE, false, true);
248   }
249   
250   protected synchronized void publishStop() {
251     Collection JavaDoc<ProcessInstanceListener> immutableListeners = new ArrayList JavaDoc<ProcessInstanceListener>(listeners);
252     for(ProcessInstanceListener listener : immutableListeners) {
253       listener.generatorStopped(this);
254     }
255     pcs.firePropertyChange(STATE, true, false);
256   }
257   
258   protected synchronized void publishInvalidated() {
259     Collection JavaDoc<ProcessInstanceListener> immutableListeners = new ArrayList JavaDoc<ProcessInstanceListener>(listeners);
260     for(ProcessInstanceListener listener : immutableListeners) {
261       listener.instanceInvalidated(this);
262     }
263   }
264   
265   protected OutputWriter getWriter() {
266     return writer != null ? writer : NULLWRITER;
267   }
268   
269   private void setFactory(final Engine factory) {
270     pcs.firePropertyChange(FACTORY, this.factory, factory);
271     this.factory = factory;
272   }
273   
274   private void setModified() {
275     isModifiedFlag = true;
276     publishInvalidated();
277   }
278   
279   private void setDeleted() {
280     isDeletedFlag = true;
281     isModifiedFlag = false;
282     publishInvalidated();
283   }
284 }
285
Popular Tags