KickJava   Java API By Example, From Geeks To Geeks.

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


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.spi;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 /**
30  *
31  * @author Jaroslav Bachorik
32  */

33 public abstract class Engine {
34   public static final String JavaDoc STATE = Engine.class.getName() + "#STATE"; // STATE property
35
public static final String JavaDoc INSTANCE = Engine.class.getName() + "#INSTANCE"; // INSTANCE property
36

37   private boolean lastReadyState = true;
38   
39   final private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
40     
41   final private ProcessInstanceListener pil = new ProcessInstanceListener() {
42     public void generatorStarted(ProcessInstance provider) {
43       setLastReadyState(false);
44     }
45     
46     public void generatorStarted(ProcessInstance provider, String JavaDoc logPath) {
47       setLastReadyState(false);
48     }
49     
50     public void generatorStopped(ProcessInstance provider) {
51       setLastReadyState(true);
52     }
53     
54     public void instanceInvalidated(ProcessInstance instance) {
55       processes.remove(instance);
56     }
57   };
58   
59   final private List JavaDoc<ProcessInstance> processes;
60   
61   public Engine() {
62     processes = new ArrayList JavaDoc<ProcessInstance>();
63   }
64   
65   /**
66    * Must return a valid process (@see org.netbeans.modules.loadgenerator.spi.ProcessInstance)
67    * The implementation may use the script name to return a previously created and cached process
68    *
69    *
70    * @param scriptName A textual identification of the process to be returned (e.g. a path to the underlying script)
71    * @return Returns a valid process
72    */

73   public ProcessInstance createProcess(final String JavaDoc scriptName) {
74     // check for an already created process
75
ProcessInstance process = getProcessByName(scriptName);
76     if (process == null) {
77       process = prepareInstance(scriptName);
78       process.setCurrentScript(scriptName);
79       process.addListener(pil);
80       if (process.isNew()) {
81         registerProcess(process);
82       }
83       process.touch();
84     }
85     
86     return process;
87   }
88   
89   /**
90    * Returns a list of all processes attached to the particular load generator engine
91    */

92   public List JavaDoc<? extends ProcessInstance> getProcesses() {
93     return processes;
94   }
95   
96   /**
97    * Finds and returns an instance created for the given script
98    * May return NULL if such an instance doesn't exist
99    */

100   public ProcessInstance getProcessByName(final String JavaDoc scriptName) {
101     for(ProcessInstance instance : processes) {
102       if (instance.getCurrentScript() != null && instance.getCurrentScript().equals(scriptName)) {
103         return instance;
104       }
105     }
106     return null;
107   }
108   
109   /**
110    * Removes all non-running processes
111    */

112   public void cleanup() {
113     if (!isReady())
114       return;
115     
116     for(Iterator JavaDoc<ProcessInstance> iter = processes.iterator();iter.hasNext();) {
117       ProcessInstance process = iter.next();
118       if (!process.isRunning()) {
119         process.detachFactory();
120         iter.remove();
121       }
122     }
123     
124     pcs.firePropertyChange(INSTANCE, true, false);
125   }
126   
127   public boolean canCleanup() {
128     if (!isReady())
129       return false;
130     
131     for(ProcessInstance process : processes) {
132       if (!process.isRunning()) {
133         return true;
134       }
135     }
136     
137     return false;
138   }
139   
140   // <editor-fold defaultstate="collapsed" desc="Property Change Support">
141
public void addPropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
142     pcs.addPropertyChangeListener(pcl);
143   }
144   
145   public void addPropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
146     pcs.addPropertyChangeListener(propertyName, pcl);
147   }
148   
149   public void removePropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
150     pcs.removePropertyChangeListener(pcl);
151   }
152   
153   public void removePropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
154     pcs.removePropertyChangeListener(propertyName, pcl);
155   }
156   // </editor-fold>
157

158   /**
159    * Returns the actual state of the load generator engine
160    * @returns TRUE if the load generator can run another instance; FALSE otherwise
161    */

162   public abstract boolean isReady();
163   
164   /**
165    * Returns the icon representing the load generator engine if it exists
166    * @return Returns the icon representing the load generator or null
167    */

168   public abstract Image JavaDoc getIcon();
169   
170   /**
171    * Returns a descriptive name for the particular load generator engine
172    *
173    * @return Returns a descriptive name
174    */

175   public abstract String JavaDoc getDisplayName();
176   
177   /**
178    * Returns the list of all supported file extensions
179    */

180   public abstract Collection JavaDoc<String JavaDoc> getSupportedExtensions();
181   
182   /**
183    * The actual implementation of the instance creating strategy
184    * To be provided by a subclass
185    */

186   protected abstract ProcessInstance prepareInstance(final String JavaDoc scriptName);
187   
188   /**
189    * This method is to be called when a new ProcessInstance object was created
190    */

191   private void registerProcess(final ProcessInstance instance) {
192     // System.out.println("AbstractLoadGenerator: processing new instance event");
193
processes.add(instance);
194     pcs.firePropertyChange(INSTANCE, false, true);
195   }
196   
197   /**
198    * To be called when the state of the load generator has been changed (ready/not ready)
199    */

200   private void fireReadyStateChanged(final boolean oldValue, final boolean newValue) {
201     pcs.firePropertyChange(STATE, oldValue, newValue);
202   }
203   
204   private void setLastReadyState(final boolean value) {
205     fireReadyStateChanged(lastReadyState, value);
206     lastReadyState = value;
207   }
208 }
209
Popular Tags