~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jgdi/test/com/sun/grid/jgdi/event/WaitForEventThread.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.jgdi.event;
 
33
 
 
34
import java.util.LinkedList;
 
35
import java.util.logging.Logger;
 
36
 
 
37
/**
 
38
 * Helper class for event test cases.
 
39
 *
 
40
 */
 
41
public class WaitForEventThread extends Thread implements EventListener {
 
42
 
 
43
    private Logger log = Logger.getLogger("com.sun.grid.jgdi.event");
 
44
    private Object object;
 
45
    private LinkedList<Event> events = new LinkedList<Event>();
 
46
 
 
47
    public WaitForEventThread(Object object) {
 
48
        this.object = object;
 
49
    }
 
50
 
 
51
    /**
 
52
     *
 
53
     * @param evt the event
 
54
     */
 
55
    public void eventOccured(Event evt) {
 
56
        log.entering(getClass().getName(), "eventOccured", evt.getClass());
 
57
        synchronized (events) {
 
58
            events.add(evt);
 
59
            events.notifyAll();
 
60
        }
 
61
        log.exiting(getClass().getName(), "eventOccured");
 
62
    }
 
63
    private Object addEventSync = new Object();
 
64
    private boolean hasAddEvent = false;
 
65
 
 
66
    public boolean waitForAddEvent(long timeout) throws InterruptedException {
 
67
        log.entering(getClass().getName(), "waitForAddEvent", timeout);
 
68
        synchronized (addEventSync) {
 
69
            if (!hasAddEvent) {
 
70
                addEventSync.wait(timeout);
 
71
            }
 
72
        }
 
73
        log.exiting(getClass().getName(), "waitForAddEvent", hasAddEvent);
 
74
        return hasAddEvent;
 
75
    }
 
76
    private Object modEventSync = new Object();
 
77
    private boolean hasModEvent = false;
 
78
 
 
79
    public boolean waitForModEvent(long timeout) throws InterruptedException {
 
80
        log.entering(getClass().getName(), "waitForModEvent", timeout);
 
81
        synchronized (modEventSync) {
 
82
            if (!hasModEvent) {
 
83
                modEventSync.wait(timeout);
 
84
            }
 
85
        }
 
86
        log.exiting(getClass().getName(), "waitForModEvent", hasModEvent);
 
87
        return hasModEvent;
 
88
    }
 
89
    private Object delEventSync = new Object();
 
90
    private boolean hasDelEvent = false;
 
91
 
 
92
    public boolean waitForDelEvent(long timeout) throws InterruptedException {
 
93
        log.entering(getClass().getName(), "waitForDelEvent", timeout);
 
94
        synchronized (delEventSync) {
 
95
            if (!hasDelEvent) {
 
96
                delEventSync.wait(timeout);
 
97
            }
 
98
        }
 
99
        log.exiting(getClass().getName(), "waitForDelEvent", hasDelEvent);
 
100
        return hasDelEvent;
 
101
    }
 
102
 
 
103
    public void run() {
 
104
        log.entering(getClass().getName(), "run");
 
105
        try {
 
106
            while (true) {
 
107
                Event evt = null;
 
108
                synchronized (events) {
 
109
                    while (evt == null) {
 
110
                        while (events.isEmpty()) {
 
111
                            events.wait();
 
112
                        }
 
113
                        evt = events.removeFirst();
 
114
                    }
 
115
                }
 
116
 
 
117
                if (evt.getType().equals(EventType.SGE_EMA_ADD)) {
 
118
                    log.fine("got add event" + evt);
 
119
                    AddEvent addEvt = (AddEvent) evt;
 
120
                    if (this.object.equals(addEvt.getChangedObject())) {
 
121
                        synchronized (addEventSync) {
 
122
                            hasAddEvent = true;
 
123
                            addEventSync.notifyAll();
 
124
                        }
 
125
                    }
 
126
                } else if (evt.getType().equals(EventType.SGE_EMA_MOD)) {
 
127
                    log.fine("got modifiy event" + evt);
 
128
                    ModEvent modEvt = (ModEvent) evt;
 
129
                    if (this.object.equals(modEvt.getChangedObject())) {
 
130
                        synchronized (modEventSync) {
 
131
                            hasModEvent = true;
 
132
                            modEventSync.notifyAll();
 
133
                        }
 
134
                    }
 
135
 
 
136
                } else if (evt.getType().equals(EventType.SGE_EMA_DEL)) {
 
137
                    log.fine("got delete event" + evt);
 
138
                    DelEvent delEvt = (DelEvent) evt;
 
139
                    if (delEvt.hasDeletedObject(object)) {
 
140
                        synchronized (delEventSync) {
 
141
                            hasDelEvent = true;
 
142
                            delEventSync.notifyAll();
 
143
                        }
 
144
                    }
 
145
                }
 
146
                evt = null;
 
147
            }
 
148
        } catch (InterruptedException ire) {
 
149
            log.fine("wait thread has been interrupted");
 
150
        }
 
151
        log.exiting(getClass().getName(), "run");
 
152
    }
 
153
}