001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal.event;
011:
012: import java.io.DataInputStream;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.jdi.internal.MirrorImpl;
020: import org.eclipse.jdi.internal.VirtualMachineImpl;
021: import org.eclipse.jdi.internal.request.EventRequestImpl;
022:
023: import com.sun.jdi.InternalException;
024: import com.sun.jdi.ThreadReference;
025: import com.sun.jdi.event.EventIterator;
026: import com.sun.jdi.event.EventSet;
027: import com.sun.jdi.request.EventRequest;
028:
029: /**
030: * this class implements the corresponding interfaces
031: * declared by the JDI specification. See the com.sun.jdi package
032: * for more information.
033: *
034: */
035: public class EventSetImpl extends MirrorImpl implements EventSet {
036: /** Set that is used to store events. */
037: private List fEvents;
038: /** Which threads were suspended by this composite event. */
039: private byte fSuspendPolicy;
040:
041: /**
042: * Creates new EventSetImpl.
043: */
044: private EventSetImpl(VirtualMachineImpl vmImpl) {
045: super ("EventSet", vmImpl); //$NON-NLS-1$
046: }
047:
048: /**
049: * Creates new EventSetImpl with events in a given array.
050: */
051: public EventSetImpl(VirtualMachineImpl vmImpl, EventImpl[] events) {
052: this (vmImpl);
053: fEvents = new ArrayList(events.length);
054: for (int i = 0; i < events.length; i++)
055: fEvents.add(events[i]);
056: }
057:
058: /**
059: * Creates new EventSetImpl with given event.
060: */
061: public EventSetImpl(VirtualMachineImpl vmImpl, EventImpl event) {
062: this (vmImpl);
063: fEvents = new ArrayList(1);
064: fEvents.add(event);
065: }
066:
067: /**
068: * @return Returns iterator over events.
069: */
070: public EventIterator eventIterator() {
071: return new EventIteratorImpl(fEvents.listIterator());
072: }
073:
074: /**
075: * @return Returns which threads were suspended by this composite event.
076: */
077: public int suspendPolicy() {
078: switch (fSuspendPolicy) {
079: case EventRequestImpl.SUSPENDPOL_NONE_JDWP:
080: return EventRequest.SUSPEND_NONE;
081: case EventRequestImpl.SUSPENDPOL_EVENT_THREAD_JDWP:
082: return EventRequest.SUSPEND_EVENT_THREAD;
083: case EventRequestImpl.SUSPENDPOL_ALL_JDWP:
084: return EventRequest.SUSPEND_ALL;
085: default:
086: throw new InternalException(
087: EventMessages.EventSetImpl_Invalid_suspend_policy_encountered___1
088: + fSuspendPolicy);
089: }
090: }
091:
092: /**
093: * Resumes threads that were suspended by this event set.
094: */
095: public void resume() {
096: switch (fSuspendPolicy) {
097: case EventRequestImpl.SUSPENDPOL_NONE_JDWP:
098: break;
099: case EventRequestImpl.SUSPENDPOL_EVENT_THREAD_JDWP:
100: resumeThreads();
101: break;
102: case EventRequestImpl.SUSPENDPOL_ALL_JDWP:
103: virtualMachineImpl().resume();
104: break;
105: default:
106: throw new InternalException(
107: EventMessages.EventSetImpl_Invalid_suspend_policy_encountered___1
108: + fSuspendPolicy);
109: }
110: }
111:
112: /**
113: * Resumes threads that were suspended by this event set.
114: */
115: private void resumeThreads() {
116: if (fEvents.size() == 1) {
117: // Most event sets have only one event.
118: // Avoid expensive object creation.
119: ThreadReference ref = ((EventImpl) fEvents.get(0)).thread();
120: if (ref != null) {
121: ref.resume();
122: } else {
123: ((EventImpl) fEvents.get(0)).virtualMachine().resume();
124: }
125: return;
126: }
127: Iterator iter = fEvents.iterator();
128: List resumedThreads = new ArrayList(fEvents.size());
129: while (iter.hasNext()) {
130: EventImpl event = (EventImpl) iter.next();
131: ThreadReference thread = event.thread();
132: if (thread == null) {
133: event.virtualMachine().resume();
134: return;
135: }
136: if (!resumedThreads.contains(thread)) {
137: resumedThreads.add(thread);
138: }
139: }
140: Iterator resumeIter = resumedThreads.iterator();
141: while (resumeIter.hasNext()) {
142: ((ThreadReference) resumeIter.next()).resume();
143: }
144: }
145:
146: /**
147: * @return Returns EventSetImpl that was read from InputStream.
148: */
149: public static EventSetImpl read(MirrorImpl target,
150: DataInputStream in) throws IOException {
151: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
152: EventSetImpl eventSet = new EventSetImpl(vmImpl);
153:
154: // Read suspend policy.
155: eventSet.fSuspendPolicy = target
156: .readByte(
157: "suspendPolicy", EventRequestImpl.suspendPolicyMap(), in); //$NON-NLS-1$
158: // Read size.
159: int size = target.readInt("size", in); //$NON-NLS-1$
160: // Create event list.
161: eventSet.fEvents = new ArrayList(size);
162:
163: while (size-- > 0) {
164: EventImpl event = EventImpl.read(target, in);
165:
166: // If event == null than it is an event that must not be given to the application.
167: // See ClassPrepareEvent.
168: if (event == null)
169: continue;
170:
171: EventRequestImpl request = (EventRequestImpl) event
172: .request();
173:
174: // Check if the request corresponding to the event was not generated from inside this JDI implementation.
175: if (request == null || !request.isGeneratedInside())
176: eventSet.fEvents.add(event);
177:
178: }
179: return eventSet;
180: }
181:
182: /**
183: * @see java.util.Collection
184: */
185: public boolean contains(Object event) {
186: return fEvents.contains(event);
187: }
188:
189: /**
190: * @see java.util.Collection
191: */
192: public boolean containsAll(Collection events) {
193: return fEvents.containsAll(events);
194: }
195:
196: /**
197: * @see java.util.Collection
198: */
199: public boolean equals(Object object) {
200: return object != null
201: && object.getClass().equals(this .getClass())
202: && fEvents.equals(((EventSetImpl) object).fEvents);
203: }
204:
205: /**
206: * @see java.util.Collection
207: */
208: public int hashCode() {
209: return fEvents.hashCode();
210: }
211:
212: /**
213: * @see java.util.Collection
214: */
215: public boolean isEmpty() {
216: return fEvents.isEmpty();
217: }
218:
219: /**
220: * @see java.util.Collection#iterator()
221: */
222: public Iterator iterator() {
223: return fEvents.iterator();
224: }
225:
226: /**
227: * @see java.util.Collection#size()
228: */
229: public int size() {
230: return fEvents.size();
231: }
232:
233: /**
234: * @see java.util.Collection#toArray()
235: */
236: public Object[] toArray() {
237: return fEvents.toArray();
238: }
239:
240: /**
241: * @see java.util.Collection#toArray(Object[])
242: */
243: public Object[] toArray(Object[] events) {
244: return fEvents.toArray(events);
245: }
246:
247: /**
248: * @see java.util.Collection#add(Object).
249: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
250: */
251: public boolean add(Object arg1) {
252: throw new UnsupportedOperationException(
253: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
254: }
255:
256: /**
257: * @see java.util.Collection#addAll(Collection)
258: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
259: */
260: public boolean addAll(Collection arg1) {
261: throw new UnsupportedOperationException(
262: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
263: }
264:
265: /**
266: * @see java.util.Collection#clear()
267: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
268: */
269: public void clear() {
270: throw new UnsupportedOperationException(
271: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
272: }
273:
274: /**
275: * @see java.util.Collection#remove(Object)
276: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
277: */
278: public boolean remove(Object arg1) {
279: throw new UnsupportedOperationException(
280: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
281: }
282:
283: /**
284: * @see java.util.Collection#removeAll(Collection)
285: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
286: */
287: public boolean removeAll(Collection arg1) {
288: throw new UnsupportedOperationException(
289: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
290: }
291:
292: /**
293: * @see java.util.Collection#retainAll(Collection)
294: * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
295: */
296: public boolean retainAll(Collection arg1) {
297: throw new UnsupportedOperationException(
298: EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
299: }
300: }
|