Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package com.ircclouds.irc.cloudserver.account;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. import org.apache.commons.logging.*;
  7.  
  8. import com.ircclouds.irc.cloudserver.domain.*;
  9. import com.ircclouds.irc.cloudserver.exception.*;
  10. import com.thoughtworks.xstream.*;
  11. import com.thoughtworks.xstream.converters.basic.*;
  12.  
  13. /**
  14. *
  15. * @author
  16. *
  17. */
  18. public final class UserAccountManager
  19. {
  20. private static Log LOG = LogFactory.getLog(UserAccountManager.class);
  21.  
  22. /** Tweaking cache **/
  23. private static final Map<String, UserAccount> USER_CACHE = new HashMap<String, UserAccount>();
  24. private static final XStream X_STREAM;
  25. private static final String ACCOUNTS_DIR = "accounts/";
  26.  
  27. static
  28. {
  29. X_STREAM = new XStream();
  30. X_STREAM.registerConverter(new ServerDescriptorConverter());
  31. X_STREAM.registerConverter(new UserAccountConverter());
  32. X_STREAM.registerConverter(new DateConverter());
  33. X_STREAM.alias("account", UserAccount.class);
  34. }
  35.  
  36. static
  37. {
  38. File _accDir = new File(ACCOUNTS_DIR);
  39. if (!_accDir.exists() && _accDir.mkdir())
  40. {
  41. try
  42. {
  43. register(new UserAccount("test1", "", new Date()));
  44. register(new UserAccount("test2", "", new Date()));
  45. }
  46. catch (RegisterException aExc)
  47. {
  48. LOG.error("Registring of default user failed...");
  49. }
  50. }
  51.  
  52. }
  53.  
  54. /**
  55. *
  56. */
  57. private UserAccountManager()
  58. {
  59.  
  60. }
  61.  
  62. public static void save(UserAccount aUserAccount)
  63. {
  64. if (USER_CACHE.containsKey(aUserAccount.getId()))
  65. {
  66. USER_CACHE.remove(aUserAccount.getId());
  67. }
  68.  
  69. try
  70. {
  71. register(aUserAccount);
  72. }
  73. catch (RegisterException aExc)
  74. {
  75. LOG.error(aExc);
  76. }
  77. }
  78.  
  79. public static boolean delete(UserAccount aUserAccount)
  80. {
  81. USER_CACHE.remove(aUserAccount.getId());
  82.  
  83. return new File(ACCOUNTS_DIR + aUserAccount.getId() + ".xml").delete();
  84. }
  85.  
  86. /**
  87. * Login
  88. *
  89. * @param ua
  90. * @return
  91. */
  92. public static boolean hasAccess(String aUsername, String aPassword)
  93. {
  94. if (aUsername == null || aPassword == null)
  95. {
  96. return false;
  97. }
  98.  
  99. boolean res = false;
  100. FileInputStream in = null;
  101. try
  102. {
  103. UserAccount rua = null;
  104. if (USER_CACHE.containsKey(aUsername))
  105. {
  106. rua = USER_CACHE.get(aUsername);
  107. }
  108. else
  109. {
  110. in = new FileInputStream(ACCOUNTS_DIR + aUsername + ".xml");
  111. rua = (UserAccount) X_STREAM.fromXML(in);
  112. USER_CACHE.put(aUsername, rua);
  113. }
  114. res = rua.getPassword().equals(aPassword);
  115. }
  116. catch (FileNotFoundException e)
  117. {
  118. res = false;
  119. }
  120. finally
  121. {
  122. if (in != null)
  123. {
  124. try
  125. {
  126. in.close();
  127. }
  128. catch (IOException aExc)
  129. {
  130. LOG.error(aExc);
  131. }
  132. }
  133. }
  134. return res;
  135. }
  136.  
  137. /**
  138. * Login
  139. *
  140. * @param aUserAccount
  141. * @return
  142. * @throws Exception
  143. */
  144. public static void register(UserAccount aUserAccount) throws RegisterException
  145. {
  146. FileOutputStream _fos = null;
  147. try
  148. {
  149. String _id = aUserAccount.getId();
  150. if (USER_CACHE.containsKey(_id))
  151. {
  152. throw new RegisterException("User already exists");
  153. }
  154. else
  155. {
  156. File _file = new File(ACCOUNTS_DIR + _id + ".xml");
  157. _fos = new FileOutputStream(_file);
  158. X_STREAM.toXML(aUserAccount, _fos);
  159. }
  160. }
  161. catch (FileNotFoundException aExc)
  162. {
  163. throw new RegisterException("Impossible to create account file for : " + aUserAccount, aExc);
  164. }
  165. finally
  166. {
  167. if (_fos != null)
  168. {
  169. try
  170. {
  171. _fos.close();
  172. }
  173. catch (IOException aExc)
  174. {
  175. LOG.error(aExc);
  176. }
  177. }
  178. }
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement