Subversion Repositories general

Rev

Blame | Last modification | View Log | RSS feed

package ak.hostadmiral.core.listener.file;

import java.util.Collection;
import java.util.Iterator;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.core.model.*;

import org.apache.log4j.Logger;

public class FileListener
    implements
        UserCreatedListener,
        UserModifiedListener,
        UserDeletedListener
{
        private static final Logger logger = Logger.getLogger(FileListener.class);

        private static String fileName;
        protected static Object lock = new Object();

    public static final String PROTOCOL_NAME    = "HostAdmiral_FileListener";
    public static final String PROTOCOL_VERSION = "0.1";

        public static String getFileName()
        {
                return fileName;
        }

        public static void setFileName(String fileName_)
        {
                fileName = fileName_;
        }

    protected static String escape(String s)
    {
        // FIXME: any other problem characters? optimize it?
        s = s.replaceAll("\0",   "\\\\0");
        s = s.replaceAll("\\\\", "\\\\\\\\");
        s = s.replaceAll("\t",   "\\\\t");
        s = s.replaceAll("\n",   "\\\\n");
        return s;
    }

        protected static void send(String message)
                throws ModelException
        {
                synchronized(lock) {
                        try {
                                Writer out = new BufferedWriter(new FileWriter(fileName));
                                if(PROTOCOL_NAME != null) {
                                        out.write(PROTOCOL_NAME);
                                        out.write(" ");
                                }
                                if(PROTOCOL_VERSION != null) {
                                        out.write(PROTOCOL_VERSION);
                                        out.write("\n");
                                }
                                out.write(message);
                                out.write("\n\n");
                                out.close();
                        }
                        catch(Exception ex) {
                                logger.error("Cannot save message to file", ex);
                                throw new ModelException("Cannot save message to file:" + ex.getMessage());
                                // FIMXE: or just throw "internal server error" message?
                        }
                }
        }

    //=== user ====================================================================================

        public void userCreated(User editor, User user)
                throws ModelException
        {
            send("user\tcreate\t" + escape(user.getLogin()) + "\t"
                + escape(/* FIXME user.getPassword() */ "") + "\t"
                + user.getEnabled() + "\t"
                + escape(user.getComment()));
        }

        public void userModified(User editor, User user, User oldUser)
                throws ModelException
        {
            send("user\tmodify\t" + escape(oldUser.getLogin()) + "\t"
                + escape(user.getLogin()) + "\t"
                + escape(/* FIXME user.getPassword() */ "") + "\t"
                + user.getEnabled() + "\t"
                + escape(user.getComment()));
        }

        public void userDeleted(User editor, User user)
                throws ModelException
        {
            send("user\tdelete\t" + escape(user.getLogin()));
        }

    //=== inet domain =============================================================================

        public void inetDomainCreated(User editor, InetDomain domain)
                throws ModelException
        {
            send("inetDomain\tcreate\t" + escape(domain.getName()) + "\t"
                + domain.getEnabled() + "\t"
                + escape(domain.getComment()));
        }

        public void inetDomainModified(User editor, InetDomain domain, InetDomain oldDomain)
                throws ModelException
        {
            send("inetDomain\tmodify\t" + escape(oldDomain.getName()) + "\t"
                + escape(domain.getName()) + "\t"
                + domain.getEnabled() + "\t"
                + escape(domain.getComment()));
        }

        public void inetDomainDeleted(User editor, InetDomain domain)
                throws ModelException
        {
            send("inetDomain\tdelete\t" + escape(domain.getName()));
        }

    //=== system user =============================================================================

        public void systemUserCreated(User editor, SystemUser systemUser)
                throws ModelException
        {
            send("systemUser\tcreate\t" + systemUser.getUid() + "\t"
                + escape(systemUser.getName()) + "\t"
                + systemUser.getEnabled() + "\t"
                + escape(systemUser.getComment()));
        }

        public void systemUserModified(User editor, SystemUser systemUser, SystemUser oldSystemUser)
                throws ModelException
        {
            send("systemUser\tmodify\t" + oldSystemUser.getUid() + "\t"
                + escape(oldSystemUser.getName()) + "\t"
                + systemUser.getUid() + "\t"
                + escape(systemUser.getName()) + "\t"
                + systemUser.getEnabled() + "\t"
                + escape(systemUser.getComment()));
        }

        public void systemUserDeleted(User editor, SystemUser systemUser)
                throws ModelException
        {
            send("systemUser\tdelete\t" + systemUser.getUid() + "\t" + escape(systemUser.getName()));
        }

    //=== mailbox =================================================================================

        public void mailboxCreated(User editor, Mailbox mailbox)
                throws ModelException
        {
            send("mailbox\tcreate\t" + escape(mailbox.getLogin()) + "\t"
                + escape(/* FIXME user.getPassword() */ "") + "\t"
                + escape(mailbox.getDomain().getName()) + "\t"
                + mailbox.getVirusCheck() + "\t"
                + mailbox.getSpamCheck() + "\t"
                + mailbox.getSystemUser().getUid() + "\t"
                + mailbox.getEnabled() + "\t"
                + escape(mailbox.getComment()));
        }

        public void mailboxModified(User editor, Mailbox mailbox, Mailbox oldMailbox)
                throws ModelException
        {
            send("mailbox\tmodify\t" + escape(oldMailbox.getLogin()) + "\t"
                + escape(oldMailbox.getDomain().getName()) + "\t"
                + escape(mailbox.getLogin()) + "\t"
                + escape(/* FIXME user.getPassword() */ "") + "\t"
                + escape(mailbox.getDomain().getName()) + "\t"
                + mailbox.getVirusCheck() + "\t"
                + mailbox.getSpamCheck() + "\t"
                + mailbox.getSystemUser().getUid() + "\t"
                + mailbox.getEnabled() + "\t"
                + escape(mailbox.getComment()));
        }

        public void mailboxDeleted(User editor, Mailbox mailbox)
                throws ModelException
        {
            send("mailbox\tdelete\t" + escape(mailbox.getLogin()) + "\t"
                + escape(mailbox.getDomain().getName()));
        }

    //=== mail alias ==============================================================================

        private String formMailAliasDestinations(User editor, MailAlias mailAlias)
                throws ModelException
        {
                StringBuffer b = new StringBuffer();

                Collection dests = mailAlias.getDestinations(editor);
                if(dests != null) {
                        for(Iterator i = dests.iterator(); i.hasNext(); ) {
                                MailAliasDestination d = (MailAliasDestination)i.next();
                                b.append("\n\t");
                                if(d.getMailbox() != null)
                                        b.append(escape(d.getMailbox().getLogin())).append("@").append(escape(d.getMailbox().getDomain().getName()));
                                else
                                        b.append(escape(d.getEmail()));
                        }
                }

                return b.toString();
        }

        public void mailAliasCreated(User editor, MailAlias mailAlias)
                throws ModelException
        {
            send(" mailAlias\tcreate\t" + escape(mailAlias.getAddress()) + "\t"
                + escape(mailAlias.getDomain().getName()) + "\t"
                + mailAlias.getEnabled() + "\t"
                + escape(mailAlias.getComment())
                + formMailAliasDestinations(editor, mailAlias));
        }

        public void mailAliasModified(User editor, MailAlias mailAlias, MailAlias oldMailAlias)
                throws ModelException
        {
            send(" mailAlias\tmodify\t" + escape(oldMailAlias.getAddress()) + "\t"
                + escape(oldMailAlias.getDomain().getName()) + "\t"
                + escape(mailAlias.getAddress()) + "\t"
                + escape(mailAlias.getDomain().getName()) + "\t"
                + mailAlias.getEnabled() + "\t"
                + escape(mailAlias.getComment())
                + formMailAliasDestinations(editor, mailAlias));
        }

        public void mailAliasDeleted(User editor, MailAlias mailAlias)
                throws ModelException
        {
            send(" mailAlias\tdelete\t" + escape(mailAlias.getAddress())+ "\t"
                + escape(mailAlias.getDomain().getName()));
        }
}