001/* An action to access a workspace.
002
003 Copyright (c) 2003-2005 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 @ProposedRating Green (eal)
028
029 */
030package ptolemy.kernel.util.test;
031
032import java.util.List;
033
034import ptolemy.kernel.util.InternalErrorException;
035import ptolemy.kernel.util.Workspace;
036
037public class AccessAction {
038    public AccessAction(Workspace workspace, long sleepBefore, char action,
039            long sleepAfter, Object lock, List record, String name) {
040        _workspace = workspace;
041        _sleepBefore = sleepBefore;
042        _action = action;
043        _sleepAfter = sleepAfter;
044        _lock = lock;
045        _record = record;
046        _name = name;
047    }
048
049    public void access() throws InterruptedException {
050        if (_sleepBefore > 0) {
051            Thread.sleep(_sleepBefore);
052        }
053
054        switch (_action) {
055        case 'R':
056
057            synchronized (_workspace) {
058                try {
059                    _workspace.getReadAccess();
060                    _record.add(_name + " got read access");
061                } catch (InternalErrorException ex) {
062                    _failed = true;
063                    _record.add(_name + " failed to get read access");
064                }
065            }
066
067            break;
068
069        case 'W':
070
071            synchronized (_workspace) {
072                try {
073                    _workspace.getWriteAccess();
074                    _record.add(_name + " got write access");
075                } catch (InternalErrorException ex) {
076                    _failed = true;
077                    _record.add(_name + " failed to get write access");
078                }
079            }
080
081            break;
082
083        case 'U':
084
085            //synchronized (_workspace) {
086            try {
087                _record.add(_name + " entered waiting on lock");
088                _workspace.wait(_lock);
089                _record.add(_name + " woke up from waiting");
090            } catch (InterruptedException ex) {
091                _record.add(_name + " interrupted while waiting");
092            }
093
094            //}
095            break;
096
097        default:
098
099            // no-op
100            break;
101        }
102
103        if (_sleepAfter > 0) {
104            Thread.sleep(_sleepAfter);
105        }
106    }
107
108    public void deAccess() throws InterruptedException {
109        if (_sleepAfter > 0) {
110            Thread.sleep(_sleepAfter);
111        }
112
113        switch (_action) {
114        case 'R':
115
116            synchronized (_workspace) {
117                _workspace.doneReading();
118
119                if (_failed) {
120                    _record.add(
121                            _name + " handled failure in getting read access");
122                } else {
123                    _record.add(_name + " released read access");
124                }
125            }
126
127            break;
128
129        case 'W':
130
131            synchronized (_workspace) {
132                _workspace.doneWriting();
133
134                if (_failed) {
135                    _record.add(
136                            _name + " handled failure in getting write access");
137                } else {
138                    _record.add(_name + " released write access");
139                }
140            }
141
142            break;
143
144        case 'U':
145
146            //_workspace.wait(_lock);
147            // no-op
148            break;
149
150        default:
151
152            // no-op
153            break;
154        }
155
156        if (_sleepBefore > 0) {
157            Thread.sleep(_sleepBefore);
158        }
159    }
160
161    private Workspace _workspace;
162
163    private long _sleepBefore;
164
165    private long _sleepAfter;
166
167    private char _action;
168
169    private Object _lock;
170
171    private List _record;
172
173    private String _name;
174
175    private boolean _failed = false;
176}