001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.loadgenerator.spi;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: /**
053: *
054: * @author Jaroslav Bachorik
055: */
056: public abstract class Engine {
057: public static final String STATE = Engine.class.getName()
058: + "#STATE"; // STATE property
059: public static final String INSTANCE = Engine.class.getName()
060: + "#INSTANCE"; // INSTANCE property
061:
062: private boolean lastReadyState = true;
063:
064: final private PropertyChangeSupport pcs = new PropertyChangeSupport(
065: this );
066:
067: final private ProcessInstanceListener pil = new ProcessInstanceListener() {
068: public void generatorStarted(ProcessInstance provider) {
069: setLastReadyState(false);
070: }
071:
072: public void generatorStarted(ProcessInstance provider,
073: String logPath) {
074: setLastReadyState(false);
075: }
076:
077: public void generatorStopped(ProcessInstance provider) {
078: setLastReadyState(true);
079: }
080:
081: public void instanceInvalidated(ProcessInstance instance) {
082: processes.remove(instance);
083: }
084: };
085:
086: final private List<ProcessInstance> processes;
087:
088: public Engine() {
089: processes = new ArrayList<ProcessInstance>();
090: }
091:
092: /**
093: * Must return a valid process (@see org.netbeans.modules.loadgenerator.spi.ProcessInstance)
094: * The implementation may use the script name to return a previously created and cached process
095: *
096: *
097: * @param scriptName A textual identification of the process to be returned (e.g. a path to the underlying script)
098: * @return Returns a valid process
099: */
100: public ProcessInstance createProcess(final String scriptName) {
101: // check for an already created process
102: ProcessInstance process = getProcessByName(scriptName);
103: if (process == null) {
104: process = prepareInstance(scriptName);
105: process.setCurrentScript(scriptName);
106: process.addListener(pil);
107: if (process.isNew()) {
108: registerProcess(process);
109: }
110: process.touch();
111: }
112:
113: return process;
114: }
115:
116: /**
117: * Returns a list of all processes attached to the particular load generator engine
118: */
119: public List<? extends ProcessInstance> getProcesses() {
120: return processes;
121: }
122:
123: /**
124: * Finds and returns an instance created for the given script
125: * May return NULL if such an instance doesn't exist
126: */
127: public ProcessInstance getProcessByName(final String scriptName) {
128: for (ProcessInstance instance : processes) {
129: if (instance.getCurrentScript() != null
130: && instance.getCurrentScript().equals(scriptName)) {
131: return instance;
132: }
133: }
134: return null;
135: }
136:
137: /**
138: * Removes all non-running processes
139: */
140: public void cleanup() {
141: if (!isReady())
142: return;
143:
144: for (Iterator<ProcessInstance> iter = processes.iterator(); iter
145: .hasNext();) {
146: ProcessInstance process = iter.next();
147: if (!process.isRunning()) {
148: process.detachFactory();
149: iter.remove();
150: }
151: }
152:
153: pcs.firePropertyChange(INSTANCE, true, false);
154: }
155:
156: public boolean canCleanup() {
157: if (!isReady())
158: return false;
159:
160: for (ProcessInstance process : processes) {
161: if (!process.isRunning()) {
162: return true;
163: }
164: }
165:
166: return false;
167: }
168:
169: // <editor-fold defaultstate="collapsed" desc="Property Change Support">
170: public void addPropertyChangeListener(
171: final PropertyChangeListener pcl) {
172: pcs.addPropertyChangeListener(pcl);
173: }
174:
175: public void addPropertyChangeListener(final String propertyName,
176: final PropertyChangeListener pcl) {
177: pcs.addPropertyChangeListener(propertyName, pcl);
178: }
179:
180: public void removePropertyChangeListener(
181: final PropertyChangeListener pcl) {
182: pcs.removePropertyChangeListener(pcl);
183: }
184:
185: public void removePropertyChangeListener(final String propertyName,
186: final PropertyChangeListener pcl) {
187: pcs.removePropertyChangeListener(propertyName, pcl);
188: }
189:
190: // </editor-fold>
191:
192: /**
193: * Returns the actual state of the load generator engine
194: * @returns TRUE if the load generator can run another instance; FALSE otherwise
195: */
196: public abstract boolean isReady();
197:
198: /**
199: * Returns the icon representing the load generator engine if it exists
200: * @return Returns the icon representing the load generator or null
201: */
202: public abstract Image getIcon();
203:
204: /**
205: * Returns a descriptive name for the particular load generator engine
206: *
207: * @return Returns a descriptive name
208: */
209: public abstract String getDisplayName();
210:
211: /**
212: * Returns the list of all supported file extensions
213: */
214: public abstract Collection<String> getSupportedExtensions();
215:
216: /**
217: * The actual implementation of the instance creating strategy
218: * To be provided by a subclass
219: */
220: protected abstract ProcessInstance prepareInstance(
221: final String scriptName);
222:
223: /**
224: * This method is to be called when a new ProcessInstance object was created
225: */
226: private void registerProcess(final ProcessInstance instance) {
227: // System.out.println("AbstractLoadGenerator: processing new instance event");
228: processes.add(instance);
229: pcs.firePropertyChange(INSTANCE, false, true);
230: }
231:
232: /**
233: * To be called when the state of the load generator has been changed (ready/not ready)
234: */
235: private void fireReadyStateChanged(final boolean oldValue,
236: final boolean newValue) {
237: pcs.firePropertyChange(STATE, oldValue, newValue);
238: }
239:
240: private void setLastReadyState(final boolean value) {
241: fireReadyStateChanged(lastReadyState, value);
242: lastReadyState = value;
243: }
244: }
|