Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ircclouds.irc.cloudserver.account;
- import java.io.*;
- import java.util.*;
- import org.apache.commons.logging.*;
- import com.ircclouds.irc.cloudserver.domain.*;
- import com.ircclouds.irc.cloudserver.exception.*;
- import com.thoughtworks.xstream.*;
- import com.thoughtworks.xstream.converters.basic.*;
- /**
- *
- * @author
- *
- */
- public final class UserAccountManager
- {
- private static Log LOG = LogFactory.getLog(UserAccountManager.class);
- /** Tweaking cache **/
- private static final Map<String, UserAccount> USER_CACHE = new HashMap<String, UserAccount>();
- private static final XStream X_STREAM;
- private static final String ACCOUNTS_DIR = "accounts/";
- static
- {
- X_STREAM = new XStream();
- X_STREAM.registerConverter(new ServerDescriptorConverter());
- X_STREAM.registerConverter(new UserAccountConverter());
- X_STREAM.registerConverter(new DateConverter());
- X_STREAM.alias("account", UserAccount.class);
- }
- static
- {
- File _accDir = new File(ACCOUNTS_DIR);
- if (!_accDir.exists() && _accDir.mkdir())
- {
- try
- {
- register(new UserAccount("test1", "", new Date()));
- register(new UserAccount("test2", "", new Date()));
- }
- catch (RegisterException aExc)
- {
- LOG.error("Registring of default user failed...");
- }
- }
- }
- /**
- *
- */
- private UserAccountManager()
- {
- }
- public static void save(UserAccount aUserAccount)
- {
- if (USER_CACHE.containsKey(aUserAccount.getId()))
- {
- USER_CACHE.remove(aUserAccount.getId());
- }
- try
- {
- register(aUserAccount);
- }
- catch (RegisterException aExc)
- {
- LOG.error(aExc);
- }
- }
- public static boolean delete(UserAccount aUserAccount)
- {
- USER_CACHE.remove(aUserAccount.getId());
- return new File(ACCOUNTS_DIR + aUserAccount.getId() + ".xml").delete();
- }
- /**
- * Login
- *
- * @param ua
- * @return
- */
- public static boolean hasAccess(String aUsername, String aPassword)
- {
- if (aUsername == null || aPassword == null)
- {
- return false;
- }
- boolean res = false;
- FileInputStream in = null;
- try
- {
- UserAccount rua = null;
- if (USER_CACHE.containsKey(aUsername))
- {
- rua = USER_CACHE.get(aUsername);
- }
- else
- {
- in = new FileInputStream(ACCOUNTS_DIR + aUsername + ".xml");
- rua = (UserAccount) X_STREAM.fromXML(in);
- USER_CACHE.put(aUsername, rua);
- }
- res = rua.getPassword().equals(aPassword);
- }
- catch (FileNotFoundException e)
- {
- res = false;
- }
- finally
- {
- if (in != null)
- {
- try
- {
- in.close();
- }
- catch (IOException aExc)
- {
- LOG.error(aExc);
- }
- }
- }
- return res;
- }
- /**
- * Login
- *
- * @param aUserAccount
- * @return
- * @throws Exception
- */
- public static void register(UserAccount aUserAccount) throws RegisterException
- {
- FileOutputStream _fos = null;
- try
- {
- String _id = aUserAccount.getId();
- if (USER_CACHE.containsKey(_id))
- {
- throw new RegisterException("User already exists");
- }
- else
- {
- File _file = new File(ACCOUNTS_DIR + _id + ".xml");
- _fos = new FileOutputStream(_file);
- X_STREAM.toXML(aUserAccount, _fos);
- }
- }
- catch (FileNotFoundException aExc)
- {
- throw new RegisterException("Impossible to create account file for : " + aUserAccount, aExc);
- }
- finally
- {
- if (_fos != null)
- {
- try
- {
- _fos.close();
- }
- catch (IOException aExc)
- {
- LOG.error(aExc);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement