001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.debugger.bdiclient.impl;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.LinkedHashMap;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Vector;
031: import javax.xml.namespace.QName;
032: import org.netbeans.modules.bpel.debugger.api.ProcessInstance;
033: import org.netbeans.modules.bpel.debugger.api.ProcessInstancesModel;
034: import org.netbeans.modules.bpel.debuggerbdi.rmi.api.BPELProcessInstanceRef;
035: import org.netbeans.modules.bpel.debugger.BpelDebuggerImpl;
036: import org.netbeans.modules.bpel.debugger.api.BpelProcess;
037: import org.netbeans.modules.bpel.debuggerbdi.rmi.api.BPELProcessRef;
038: import org.netbeans.modules.bpel.debuggerbdi.rmi.api.VirtualBPELEngine;
039:
040: /**
041: * Model for maintaining the list of process instances.
042: * Supports notification of process state changes.
043: *
044: * @author Sun Microsystems
045: */
046: public class ProcessInstancesModelImpl implements ProcessInstancesModel {
047:
048: private final Vector<Listener> myListeners = new Vector<Listener>();
049: private final BpelDebuggerImpl myDebugger;
050:
051: private final Map<String, ProcessInstanceImpl> myProcessInstances = Collections
052: .synchronizedMap(new LinkedHashMap<String, ProcessInstanceImpl>());
053:
054: private final Map<QName, BpelProcessImpl> myProcesses = new HashMap<QName, BpelProcessImpl>();
055:
056: public ProcessInstancesModelImpl(final BpelDebuggerImpl debugger) {
057:
058: myDebugger = debugger;
059: }
060:
061: public BpelDebuggerImpl getDebugger() {
062: return myDebugger;
063: }
064:
065: public void clear() {
066: myProcessInstances.clear();
067: myListeners.clear();
068: }
069:
070: public ProcessInstanceImpl[] getProcessInstances() {
071: return myProcessInstances.values().toArray(
072: new ProcessInstanceImpl[myProcessInstances.size()]);
073: }
074:
075: public ProcessInstanceImpl[] getProcessInstances(
076: final BpelProcess process) {
077:
078: final List<ProcessInstance> filtered = new LinkedList<ProcessInstance>();
079:
080: for (ProcessInstance instance : myProcessInstances.values()) {
081: if (instance.getProcess().equals(process)) {
082: filtered.add(instance);
083: }
084: }
085:
086: return filtered
087: .toArray(new ProcessInstanceImpl[filtered.size()]);
088: }
089:
090: public BpelProcessImpl[] getProcesses() {
091: return myProcesses.values().toArray(
092: new BpelProcessImpl[myProcesses.size()]);
093: }
094:
095: public BpelProcessImpl getProcess(final BPELProcessRef processRef) {
096:
097: final QName processQName = makeProcessQName(processRef.uri());
098:
099: BpelProcessImpl process = myProcesses.get(processQName);
100:
101: if (process == null) {
102: process = new BpelProcessImpl(processRef, myDebugger);
103: myProcesses.put(processQName, process);
104: }
105:
106: return process;
107: }
108:
109: public void addListener(final Listener listener) {
110:
111: myListeners.add(listener);
112: }
113:
114: public void removeListener(final Listener listener) {
115:
116: myListeners.remove(listener);
117: }
118:
119: protected void processUndeployed(final String uri) {
120:
121: final QName processQName = makeProcessQName(uri);
122: for (ProcessInstanceImpl processInstance : getProcessInstances()) {
123: if (processInstance.getProcessQName().equals(processQName)) {
124: processInstance.setUndeployed(true);
125:
126: if (processInstance.getState() == ProcessInstance.STATE_SUSPENDED) {
127: processInstance.resume();
128: }
129:
130: myProcessInstances.remove(processInstance.getId());
131:
132: fireProcessInstanceRemoved(processInstance);
133: }
134: }
135:
136: myProcesses.remove(processQName);
137: }
138:
139: protected BDIDebugFrame frameCreated(final String id,
140: final String processInstanceId, final String parentFrameId,
141: final String bpelFile, final String uri) {
142:
143: ProcessInstanceImpl processInstance = myProcessInstances
144: .get(processInstanceId);
145:
146: // If no instance with this id is registered -- we should register
147: // it (apply for the BPELProcessInstanceRef). If it's not
148: // available -- ignore the event altogether.
149: if (processInstance == null) {
150: final VirtualBPELEngine engine = myDebugger
151: .getBDIDebugger().getVirtualBPELEngine();
152:
153: for (String procId : engine.allDeployedBPELs()) {
154: final BPELProcessRef processRef = engine
155: .getBPELProcess(procId);
156:
157: getProcess(processRef);
158:
159: final List<String> ids = Arrays.asList(processRef
160: .allProcessInstanceIDs());
161:
162: if (ids.contains(processInstanceId)) {
163: final BPELProcessInstanceRef instanceRef = processRef
164: .getProcessInstance(processInstanceId);
165:
166: processInstanceStarted(instanceRef);
167: break;
168: }
169: }
170:
171: processInstance = myProcessInstances.get(processInstanceId);
172:
173: // If it's still null -- ignore the event
174: if (processInstance == null) {
175: return null;
176: }
177: }
178:
179: final BDIDebugFrame frame = processInstance.addFrame(id,
180: parentFrameId);
181:
182: return frame;
183: }
184:
185: protected void processInstanceStarted(
186: final BPELProcessInstanceRef processInstanceRef) {
187:
188: final ProcessInstanceImpl processInstance = new ProcessInstanceImpl(
189: this , processInstanceRef);
190:
191: myProcessInstances
192: .put(processInstance.getId(), processInstance);
193: processInstance.onProcessInstanceStarted();
194:
195: fireProcessInstanceAdded(processInstance);
196: }
197:
198: protected void processInstanceDied(
199: final BPELProcessInstanceRef processInstanceRef) {
200:
201: final String processInstanceId = processInstanceRef.globalID();
202:
203: final ProcessInstanceImpl processInstance = myProcessInstances
204: .get(processInstanceId);
205:
206: processInstance.onProcessInstanceDied();
207: }
208:
209: protected void processInstanceCompleted(
210: final ProcessInstanceImpl processInstance) {
211:
212: myProcessInstances.remove(processInstance.getId());
213:
214: fireProcessInstanceRemoved(processInstance);
215: }
216:
217: protected void processInstanceStateChanged(
218: final ProcessInstanceImpl processInstance,
219: final int oldState, final int newState) {
220:
221: fireProcessInstanceStateChanged(processInstance, oldState,
222: newState);
223: }
224:
225: protected static QName makeProcessQName(String uri) {
226: final int pos = uri.lastIndexOf('/');
227:
228: return new QName(uri.substring(0, pos), uri.substring(pos + 1));
229: }
230:
231: // Private /////////////////////////////////////////////////////////////////
232: private void fireProcessInstanceAdded(
233: final ProcessInstanceImpl processInstance) {
234:
235: final Listener[] listeners = new Listener[myListeners.size()];
236: myListeners.copyInto(listeners);
237:
238: for (int i = 0; i < listeners.length; i++) {
239: listeners[i].processInstanceAdded(processInstance);
240: }
241: }
242:
243: private void fireProcessInstanceRemoved(
244: final ProcessInstanceImpl processInstance) {
245:
246: final Listener[] listeners = new Listener[myListeners.size()];
247: myListeners.copyInto(listeners);
248:
249: for (int i = 0; i < listeners.length; i++) {
250: listeners[i].processInstanceRemoved(processInstance);
251: }
252: }
253:
254: private void fireProcessInstanceStateChanged(
255: final ProcessInstanceImpl processInstance,
256: final int oldState, final int newState) {
257:
258: final Listener[] listeners = new Listener[myListeners.size()];
259: myListeners.copyInto(listeners);
260:
261: for (int i = 0; i < listeners.length; i++) {
262: listeners[i].processInstanceStateChanged(processInstance,
263: oldState, newState);
264: }
265: }
266: }
|