4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
LockMaster.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: LockMaster.java
5  * Description: Class manages documents locks
6  */
7 
8 /**
9  * @file LockMaster.java
10  *
11  * @brief Class manages documents locks
12  */
13 
14 /**
15  * @package cz.vutbr.fit.knot.annotations.comet.documentLock
16  *
17  * @brief Classes responsible for synchronization of modifications of the document
18  */
19 package cz.vutbr.fit.knot.annotations.comet.documentLock;
20 
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 
24 /**
25  * Class manages documents locks.
26  *
27  * @brief Class manages documents locks.
28  * @author Martin Petr (xpetrm05)
29  */
30 public class LockMaster {
31 
32  /** Array of created locks for suggestions */
33  private ArrayList<Padlock> suggestionsLocks;
34 
35  /** Array of created locks for suggestions requests */
36  private ArrayList<Padlock> suggReqLocks;
37 
38  /** Array of created locks for changes */
39  private ArrayList<Padlock> changesDocLocks;
40 
41  /** Lock that locks sessions list for change */
43 
44  /**
45  * Constructor that inicialize locks arrays.
46  */
47  public LockMaster() {
48  suggestionsLocks = new ArrayList<Padlock>();
49  suggReqLocks = new ArrayList<Padlock>();
50  changesDocLocks = new ArrayList<Padlock>();
51  sessionsLock = null;
52  }
53 
54  /**
55  * The method creates a new lock, if the docuemnt id is found, method returns
56  * the appropriate lock.
57  *
58  * @param docId Id of document for which lock is made available
59  * @return new lock or appropriate lock
60  */
61  private synchronized Padlock makeSuggestionsPadlock(Integer docId){
62  Iterator<Padlock> padIt = suggestionsLocks.iterator();
63  while(padIt.hasNext()){
64  Padlock currentPadlock = padIt.next();
65  if(currentPadlock.getDocumentId().equals(docId)){
66  return currentPadlock;
67  }
68  }
69 
70  Padlock newPadlock = new Padlock(docId);
71  suggestionsLocks.add(newPadlock);
72  return newPadlock;
73  }
74 
75  /**
76  * Method which provides access to suggestions locks, when the lock for the
77  * document does not exist, method create it.
78  *
79  * @param docId Id of document for which lock is made available
80  * @return lock for suggestions for document with id in parameter
81  */
82  public synchronized Padlock getSuggestionLock(Integer docId){
83  Iterator<Padlock> padIt = suggestionsLocks.iterator();
84  while(padIt.hasNext()){
85  Padlock currentPadlock = padIt.next();
86  if(currentPadlock.getDocumentId().equals(docId)){
87  return currentPadlock;
88  }
89  }
90 
91  return makeSuggestionsPadlock(docId);
92  }
93 
94  /**
95  * The method creates a new lock, if the docuemnt id is found, method returns
96  * the appropriate lock.
97  *
98  * @param docId Id of document for which lock is made available
99  * @return new lock or appropriate lock
100  */
101  private synchronized Padlock makeSuggReqPadlock(Integer docId){
102  Iterator<Padlock> padIt = suggReqLocks.iterator();
103  while(padIt.hasNext()){
104  Padlock currentPadlock = padIt.next();
105  if(currentPadlock.getDocumentId().equals(docId)){
106  return currentPadlock;
107  }
108  }
109 
110  Padlock newPadlock = new Padlock(docId);
111  suggReqLocks.add(newPadlock);
112  return newPadlock;
113  }
114 
115  /**
116  * Method which provides access to suggestion request locks, when the lock for the
117  * document does not exist, method create it.
118  *
119  * @param docId Id of document for which lock is made available
120  * @return lock for suggestions requests for document with id in parameter
121  */
122  public synchronized Padlock getSuggReqLock(Integer docId){
123  Iterator<Padlock> padIt = suggReqLocks.iterator();
124  while(padIt.hasNext()){
125  Padlock currentPadlock = padIt.next();
126  if(currentPadlock.getDocumentId().equals(docId)){
127  return currentPadlock;
128  }
129  }
130 
131  return makeSuggReqPadlock(docId);
132  }
133 
134  /**
135  * The method creates a new lock, if the docuemnt id is found, method returns
136  * the appropriate lock.
137  *
138  * @param docId Id of document for which lock is made available
139  * @return new lock or appropriate lock
140  */
141  private synchronized Padlock makeDocumentPadlock(Integer docId){
142  Iterator<Padlock> padIt = changesDocLocks.iterator();
143  while(padIt.hasNext()){
144  Padlock currentPadlock = padIt.next();
145  if(currentPadlock.getDocumentId().equals(docId)){
146  return currentPadlock;
147  }
148  }
149 
150  Padlock newPadlock = new Padlock(docId);
151  changesDocLocks.add(newPadlock);
152  return newPadlock;
153  }
154 
155  /**
156  * Method which provides access to document locks, when the lock for the
157  * document does not exist, method create it.
158  *
159  * @param docId Id of document for which lock is made available
160  * @return lock for document for document with id in parameter
161  */
162  public synchronized Padlock getDocumentLock(Integer docId){
163  Iterator<Padlock> padIt = changesDocLocks.iterator();
164  while(padIt.hasNext()){
165  Padlock currentPadlock = padIt.next();
166  if(currentPadlock.getDocumentId().equals(docId)){
167  return currentPadlock;
168  }
169  }
170 
171  return makeDocumentPadlock(docId);
172  }
173 
174 
175  /**
176  * Method which provides access to dsessions lock, when the lock for the
177  * sessions does not exist, method create it.
178  *
179  * @return lock for sessions for document with id in parameter
180  */
182  if(sessionsLock == null){
183  sessionsLock = new Padlock(0);
184  }
185  return sessionsLock;
186  }
187 } // public class LockMaster
synchronized Padlock makeDocumentPadlock(Integer docId)
Class represents the document protection, and provides methods to lock.
Definition: Padlock.java:22
synchronized Padlock getSuggReqLock(Integer docId)
synchronized Padlock getSuggestionLock(Integer docId)
Definition: LockMaster.java:82
synchronized Padlock makeSuggestionsPadlock(Integer docId)
Definition: LockMaster.java:61
synchronized Padlock makeSuggReqPadlock(Integer docId)
synchronized Padlock getDocumentLock(Integer docId)