0,0 → 1,299 |
package ak.hostadmiral.listener.tcp; |
|
import java.util.Map; |
import java.util.Collection; |
import java.util.Iterator; |
import java.io.Writer; |
import java.io.BufferedWriter; |
import java.io.OutputStreamWriter; |
import java.net.Socket; |
import ak.hostadmiral.util.ModelException; |
import ak.hostadmiral.util.ConfigInit; |
import ak.hostadmiral.core.model.*; |
|
import org.apache.log4j.Logger; |
|
public class TcpListener |
implements |
ConfigInit, |
UserCreatedListener, |
UserModifiedListener, |
UserDeletedListener, |
InetDomainCreatedListener, |
InetDomainModifiedListener, |
InetDomainDeletedListener, |
SystemUserCreatedListener, |
SystemUserModifiedListener, |
SystemUserDeletedListener, |
MailboxCreatedListener, |
MailboxModifiedListener, |
MailboxDeletedListener, |
MailAliasCreatedListener, |
MailAliasModifiedListener, |
MailAliasDeletedListener |
{ |
private static final Logger logger = Logger.getLogger(TcpListener.class); |
|
private static String hostname; |
private static int port; |
protected static Object lock = new Object(); |
|
public static final String PROTOCOL_NAME = "HostAdmiral_TcpListener"; |
public static final String PROTOCOL_VERSION = "0.1"; |
|
public void init(Map params) |
{ |
setHostname(((String[])params.get("hostname"))[0]); |
setPort(Integer.parseInt(((String[])params.get("port"))[0])); |
|
UserManager.getInstance().addCreatedListener(this); |
UserManager.getInstance().addModifiedListener(this); |
UserManager.getInstance().addDeletedListener(this); |
|
InetDomainManager.getInstance().addCreatedListener(this); |
InetDomainManager.getInstance().addModifiedListener(this); |
InetDomainManager.getInstance().addDeletedListener(this); |
|
SystemUserManager.getInstance().addCreatedListener(this); |
SystemUserManager.getInstance().addModifiedListener(this); |
SystemUserManager.getInstance().addDeletedListener(this); |
|
MailboxManager.getInstance().addCreatedListener(this); |
MailboxManager.getInstance().addModifiedListener(this); |
MailboxManager.getInstance().addDeletedListener(this); |
|
MailAliasManager.getInstance().addCreatedListener(this); |
MailAliasManager.getInstance().addModifiedListener(this); |
MailAliasManager.getInstance().addDeletedListener(this); |
} |
|
public static String getHostname() |
{ |
return hostname; |
} |
|
public static void setHostname(String hostname_) |
{ |
hostname = hostname_; |
} |
|
public static int getPort() |
{ |
return port; |
} |
|
public static void setPort(int port_) |
{ |
port = port_; |
} |
|
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"); |
s = s.replaceAll("\r", "\\\\r"); |
return s; |
} |
|
protected static void send(String message) |
throws ModelException |
{ |
synchronized(lock) { |
try { |
Socket socket = new Socket(hostname, port); |
Writer out = new BufferedWriter( |
new OutputStreamWriter(socket.getOutputStream())); |
|
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 send message over TCP", ex); |
throw new ModelException("Cannot send message over TCP:" + 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() == null ? "" : mailbox.getSystemUser().getUid().toString()) |
+ "\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() == null ? "" : mailbox.getSystemUser().getUid().toString()) |
+ "\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())); |
} |
} |