001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.lockmanager.impl;
006:
007: import com.tc.object.gtx.ClientGlobalTransactionManager;
008: import com.tc.object.lockmanager.api.LockContext;
009: import com.tc.object.lockmanager.api.LockFlushCallback;
010: import com.tc.object.lockmanager.api.LockID;
011: import com.tc.object.lockmanager.api.LockLevel;
012: import com.tc.object.lockmanager.api.LockRequest;
013: import com.tc.object.lockmanager.api.RemoteLockManager;
014: import com.tc.object.lockmanager.api.ThreadID;
015: import com.tc.object.lockmanager.api.TryLockContext;
016: import com.tc.object.lockmanager.api.TryLockRequest;
017: import com.tc.object.lockmanager.api.WaitContext;
018: import com.tc.object.lockmanager.api.WaitLockRequest;
019: import com.tc.object.msg.LockRequestMessage;
020: import com.tc.object.msg.LockRequestMessageFactory;
021: import com.tc.object.tx.WaitInvocation;
022: import com.tc.util.Assert;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: /**
028: * Responsible for communicating to server to request/release locks
029: *
030: * @author steve
031: */
032: public class RemoteLockManagerImpl implements RemoteLockManager {
033:
034: private LockRequestMessageFactory lockRequestMessageFactory;
035: private final ClientGlobalTransactionManager gtxManager;
036:
037: public RemoteLockManagerImpl(LockRequestMessageFactory lrmf,
038: ClientGlobalTransactionManager gtxManager) {
039: this .lockRequestMessageFactory = lrmf;
040: this .gtxManager = gtxManager;
041: }
042:
043: public void requestLock(LockID lockID, ThreadID threadID,
044: int lockType) {
045: Assert.assertTrue(LockLevel.isDiscrete(lockType));
046: LockRequestMessage req = createRequest();
047: req.initializeObtainLock(lockID, threadID, lockType);
048: req.send();
049: }
050:
051: public void tryRequestLock(LockID lockID, ThreadID threadID,
052: WaitInvocation timeout, int lockType) {
053: Assert.assertTrue(LockLevel.isDiscrete(lockType));
054: LockRequestMessage req = createRequest();
055: req
056: .initializeTryObtainLock(lockID, threadID, timeout,
057: lockType);
058: req.send();
059: }
060:
061: public void releaseLock(LockID lockID, ThreadID threadID) {
062: LockRequestMessage req = createRequest();
063: req.initializeLockRelease(lockID, threadID);
064: req.send();
065: }
066:
067: public void releaseLockWait(LockID lockID, ThreadID threadID,
068: WaitInvocation call) {
069: LockRequestMessage req = createRequest();
070: req.initializeLockReleaseWait(lockID, threadID, call);
071: req.send();
072: }
073:
074: public void queryLock(LockID lockID, ThreadID threadID) {
075: LockRequestMessage req = createRequest();
076: req.initializeQueryLock(lockID, threadID);
077: req.send();
078: }
079:
080: public void interrruptWait(LockID lockID, ThreadID threadID) {
081: LockRequestMessage req = createRequest();
082: req.initializeInterruptWait(lockID, threadID);
083: req.send();
084: }
085:
086: private LockRequestMessage createRequest() {
087: // return (LockRequestMessage) channel.createMessage(TCMessageType.LOCK_REQUEST_MESSAGE);
088: return lockRequestMessageFactory.newLockRequestMessage();
089: }
090:
091: public void recallCommit(LockID lockID, Collection lockContext,
092: Collection waitContext, Collection pendingRequests,
093: Collection pendingTryLockRequests) {
094: LockRequestMessage req = createRequest();
095: req.initializeLockRecallCommit(lockID);
096: for (Iterator i = lockContext.iterator(); i.hasNext();) {
097: LockRequest request = (LockRequest) i.next();
098: LockContext ctxt = new LockContext(request.lockID(), req
099: .getClientID(), request.threadID(), request
100: .lockLevel());
101: req.addLockContext(ctxt);
102: }
103:
104: for (Iterator i = waitContext.iterator(); i.hasNext();) {
105: WaitLockRequest request = (WaitLockRequest) i.next();
106: WaitContext ctxt = new WaitContext(request.lockID(), req
107: .getClientID(), request.threadID(), request
108: .lockLevel(), request.getWaitInvocation());
109: req.addWaitContext(ctxt);
110: }
111:
112: for (Iterator i = pendingRequests.iterator(); i.hasNext();) {
113: LockRequest request = (LockRequest) i.next();
114: LockContext ctxt = new LockContext(request.lockID(), req
115: .getClientID(), request.threadID(), request
116: .lockLevel());
117: req.addPendingLockContext(ctxt);
118: }
119:
120: for (Iterator i = pendingTryLockRequests.iterator(); i
121: .hasNext();) {
122: TryLockRequest request = (TryLockRequest) i.next();
123: LockContext ctxt = new TryLockContext(request.lockID(), req
124: .getClientID(), request.threadID(), request
125: .lockLevel(), request.getWaitInvocation());
126: req.addPendingTryLockContext(ctxt);
127: }
128:
129: req.send();
130: }
131:
132: public void flush(LockID lockID) {
133: gtxManager.flush(lockID);
134: }
135:
136: public boolean isTransactionsForLockFlushed(LockID lockID,
137: LockFlushCallback callback) {
138: return gtxManager
139: .isTransactionsForLockFlushed(lockID, callback);
140: }
141: }
|