PageRenderTime 173ms CodeModel.GetById 40ms RepoModel.GetById 17ms app.codeStats 2ms

/NameNode/src/mdfs/namenode/repositories/UserDataRepository.java

https://bitbucket.org/crholm/mdfs
Java | 200 lines | 115 code | 25 blank | 60 comment | 7 complexity | 4d76fe506da61849e1628fd6f6945d37 MD5 | raw file
  1. package mdfs.namenode.repositories;
  2. import java.util.concurrent.locks.ReentrantLock;
  3. import mdfs.namenode.sql.MySQLFetch;
  4. import mdfs.namenode.sql.MySQLUpdater;
  5. import mdfs.utils.Hashing;
  6. import mdfs.utils.SplayTree;
  7. import mdfs.utils.Time;
  8. /**
  9. * UserDataRepository stores MDFS user data that correlates to the files, authentication and so on.
  10. * It is a Singelton since it is a shared recorce and are synchronized via {@link ReentrantLock}
  11. * for allowing multiple threads. When a lock is acquired all other operations on the UserDataRepository
  12. * are locked. If the thread that holds the lock fails/craches, The lock will be unlocked.
  13. * @author Rasmus Holm
  14. *
  15. */
  16. public class UserDataRepository {
  17. private SplayTree<String, UserDataRepositoryNode> repository = new SplayTree<String, UserDataRepositoryNode>();
  18. private ReentrantLock lock = new ReentrantLock(true);
  19. private static UserDataRepository instance= null;
  20. private UserDataRepository(){
  21. lock.lock();
  22. try{
  23. load();
  24. }finally{
  25. lock.unlock();
  26. }
  27. }
  28. /**
  29. *
  30. * @return the instance of the UserDataRepository
  31. */
  32. public static UserDataRepository getInstance(){
  33. if(instance == null){
  34. instance = new UserDataRepository();
  35. }
  36. return instance;
  37. }
  38. /**
  39. * 1. Adds a user in the form a {@link UserDataRepositoryNode} to the Repository where the key is the username.
  40. * 2. Creates a home dir for the new user.
  41. * 3. Updates permanent storage with new user.
  42. * @param node the UserDataRepository node to add to repositori
  43. * @return true if successful, false if it is preexisting
  44. */
  45. public boolean addUser(UserDataRepositoryNode node){
  46. lock.lock();
  47. boolean result = false;
  48. try{
  49. //Makes sure the user dose not exist
  50. if(!repository.containsKey(node.getKey())){
  51. //Adds user to repo
  52. repository.put(node.getKey(), node);
  53. //Creates the home dir for the new user
  54. MetaDataRepositoryNode homeDir = new MetaDataRepositoryNode();
  55. homeDir.setFilePath("/" + node.getName());
  56. homeDir.setFileType(DataTypeEnum.DIR);
  57. String time = Time.getTimeStamp();
  58. homeDir.setCreated(time);
  59. homeDir.setLastEdited(time);
  60. homeDir.setOwner(node.getName());
  61. homeDir.setGroup(node.getName());
  62. //Adds the new dir to the repo
  63. MetaDataRepository.getInstance().add(homeDir.getKey(), homeDir);
  64. //Writes the new user to permanent storage
  65. MySQLUpdater.getInstance().updateUserData(node);
  66. result = true;
  67. }
  68. }finally{
  69. lock.unlock();
  70. }
  71. return result;
  72. }
  73. /**
  74. * Removes a user from the repository.
  75. * @param name the name of the user to be removed
  76. * @return the removed users
  77. */
  78. public UserDataRepositoryNode removeUser(String name){
  79. lock.lock();
  80. UserDataRepositoryNode result;
  81. try{
  82. result = repository.remove(name);
  83. if(result != null)
  84. MySQLUpdater.getInstance().removeUserData(result);
  85. }finally{
  86. lock.unlock();
  87. }
  88. return result;
  89. }
  90. /**
  91. * 1. Adds a user in the form a {@link UserDataRepositoryNode} but generated from name and pass to the Repository where the key is the username
  92. * 2. Creates a home dir for the new user.
  93. * 3. Updates permanent storage with new user.
  94. * @param name the username of the new user
  95. * @param pass the cleartext password of the new user
  96. * @return true if successful, false if it is preexisting
  97. */
  98. public boolean addUser(String name, String pass){
  99. lock.lock();
  100. boolean result = false;
  101. try{
  102. UserDataRepositoryNode node = new UserDataRepositoryNode(name);
  103. node.setPwdHash(Hashing.hash(node.getHashType(), pass));
  104. result = addUser(node);
  105. }finally{
  106. lock.unlock();
  107. }
  108. return result;
  109. }
  110. /**
  111. * Fetches the user from the repository
  112. * @param name the username of the user that is requested
  113. * @return the user, null if it dose not exist
  114. */
  115. public UserDataRepositoryNode getUser(String name){
  116. lock.lock();
  117. UserDataRepositoryNode node = null;
  118. try{
  119. node = repository.get(name);
  120. }finally{
  121. lock.unlock();
  122. }
  123. return node;
  124. }
  125. /**
  126. * Authenticates a user
  127. * @param name username as a String
  128. * @param password clear text password
  129. * @return true if valid, false if wrong password
  130. */
  131. public boolean authUser(String name, String password){
  132. lock.lock();
  133. boolean result = false;
  134. try{
  135. UserDataRepositoryNode user = getUser(name);
  136. result = authUser(user, password);
  137. }finally{
  138. lock.unlock();
  139. }
  140. return result;
  141. }
  142. /**
  143. * Authenticates a user
  144. * @param user the node that the password is checked against
  145. * @param password
  146. * @return true if valid, false if wrong password
  147. */
  148. public boolean authUser(UserDataRepositoryNode user, String password){
  149. lock.lock();
  150. boolean result = false;
  151. try{
  152. if(Hashing.hash(user.getHashType(), password).equals(user.getPwdHash())){
  153. result = true;
  154. }
  155. }finally{
  156. lock.unlock();
  157. }
  158. return result;
  159. }
  160. /**
  161. * Loads user data repository from permanent storage
  162. */
  163. public void load(){
  164. MySQLFetch sql = new MySQLFetch();
  165. UserDataRepositoryNode[] nodes = sql.getUserDataRepositoryNodes();
  166. for (UserDataRepositoryNode node : nodes) {
  167. repository.put(node.getKey(), node);
  168. }
  169. }
  170. /**
  171. * Saves the repository to permanent storage
  172. */
  173. public void save(){
  174. }
  175. }