I thought of something like this:

Index: UsersRepositoryAliasingForwarding.java
===================================================================
--- UsersRepositoryAliasingForwarding.java      (revision 726438)
+++ UsersRepositoryAliasingForwarding.java      (working copy)
@@ -28,6 +28,7 @@
 import org.apache.james.api.user.UsersStore;
 import org.apache.james.api.vut.ErrorMappingException;
 import org.apache.james.api.vut.VirtualUserTable;
+import org.apache.james.api.vut.VirtualUserTableStore;
 import org.apache.mailet.base.GenericMailet;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -64,6 +65,8 @@
      * inboxes on this server.
      */
     private UsersRepository usersRepository;
+    private VirtualUserTable vut;
+

     /**
      * Delivers a mail to a local mailbox.
@@ -165,70 +168,8 @@
         }

         if (usersRepository instanceof VirtualUserTable) {
-            Collection mappings;
-            try {
-                mappings = ((VirtualUserTable)
usersRepository).getMappings(recipient.getUser(),
recipient.getHost());
-            } catch (ErrorMappingException e) {
-                StringBuffer errorBuffer = new StringBuffer(128)
-                    .append("A problem as occoured trying to alias
and forward user ")
-                    .append(recipient)
-                    .append(": ")
-                    .append(e.getMessage());
-                    throw new MessagingException(errorBuffer.toString());
-            }
-
-            if (mappings != null) {
-                Iterator i = mappings.iterator();
-                Collection remoteRecipients = new ArrayList();
-                Collection localRecipients = new ArrayList();
-                while (i.hasNext()) {
-                    String rcpt = (String) i.next();
-
-                    if (rcpt.indexOf("@") < 0) {
-                        // the mapping contains no domain name, use
the default domain
-                        rcpt = rcpt + "@" +
getMailetContext().getAttribute(Constants.DEFAULT_DOMAIN);
-                    }
-
-                    MailAddress nextMap = new MailAddress(rcpt);
-                    if (getMailetContext().isLocalServer(nextMap.getHost())) {
-                        localRecipients.add(nextMap);
-                    } else {
-                        remoteRecipients.add(nextMap);
-                    }
-                }
-
-                if (remoteRecipients.size() > 0) {
-                    try {
-                        getMailetContext().sendMail(sender,
remoteRecipients, message);
-                        StringBuffer logBuffer = new StringBuffer(128).append(
-                                "Mail for ").append(recipient).append(
-                                " forwarded to ");
-                        for (Iterator j =
remoteRecipients.iterator(); j.hasNext(); ) {
-                            logBuffer.append(j.next());
-                            if (j.hasNext()) logBuffer.append(", ");
-                        }
-                        getMailetContext().log(logBuffer.toString());
-                        return null;
-                    } catch (MessagingException me) {
-                        StringBuffer logBuffer = new StringBuffer(128).append(
-                                "Error forwarding mail to ");
-                        for (Iterator j =
remoteRecipients.iterator(); j.hasNext(); ) {
-                            logBuffer.append(j.next());
-                            if (j.hasNext()) logBuffer.append(", ");
-                        }
-                        logBuffer.append("attempting local delivery");
-
-                        getMailetContext().log(logBuffer.toString());
-                        throw me;
-                    }
-                }
-
-                if (localRecipients.size() > 0) {
-                    return localRecipients;
-                } else {
-                    return null;
-                }
-            }
+            Collection newRcpts = handleVUT((VirtualUserTable)
usersRepository, sender,recipient,message);
+            if (newRcpts != null) return newRcpts;
         } else {
             StringBuffer errorBuffer = new StringBuffer(128)
                 .append("Warning: the repository ")
@@ -236,6 +177,11 @@
                 .append(" does not implement VirtualUserTable interface).");
             getMailetContext().log(errorBuffer.toString());
         }
+
+        if (vut != null) {
+            return handleVUT(vut, sender, recipient, message);
+        }
+
         String realName = usersRepository.getRealName(recipient.getUser());
         if (realName != null) {
             ArrayList ret = new ArrayList();
@@ -247,6 +193,74 @@
             return ret;
         }
     }
+
+    private Collection handleVUT(VirtualUserTable table, MailAddress
sender, MailAddress recipient,
+            MimeMessage message) throws MessagingException {
+        Collection mappings;
+        try {
+            mappings = table.getMappings(recipient.getUser(),
recipient.getHost());
+        } catch (ErrorMappingException e) {
+            StringBuffer errorBuffer = new StringBuffer(128)
+                .append("A problem as occoured trying to alias and
forward user ")
+                .append(recipient)
+                .append(": ")
+                .append(e.getMessage());
+                throw new MessagingException(errorBuffer.toString());
+        }
+
+        if (mappings != null) {
+            Iterator i = mappings.iterator();
+            Collection remoteRecipients = new ArrayList();
+            Collection localRecipients = new ArrayList();
+            while (i.hasNext()) {
+                String rcpt = (String) i.next();
+
+                if (rcpt.indexOf("@") < 0) {
+                    // the mapping contains no domain name, use the
default domain
+                    rcpt = rcpt + "@" +
getMailetContext().getAttribute(Constants.DEFAULT_DOMAIN);
+                }
+
+                MailAddress nextMap = new MailAddress(rcpt);
+                if (getMailetContext().isLocalServer(nextMap.getHost())) {
+                    localRecipients.add(nextMap);
+                } else {
+                    remoteRecipients.add(nextMap);
+                }
+            }
+
+            if (remoteRecipients.size() > 0) {
+                try {
+                    getMailetContext().sendMail(sender,
remoteRecipients, message);
+                    StringBuffer logBuffer = new StringBuffer(128).append(
+                            "Mail for ").append(recipient).append(
+                            " forwarded to ");
+                    for (Iterator j = remoteRecipients.iterator();
j.hasNext(); ) {
+                        logBuffer.append(j.next());
+                        if (j.hasNext()) logBuffer.append(", ");
+                    }
+                    getMailetContext().log(logBuffer.toString());
+                    return null;
+                } catch (MessagingException me) {
+                    StringBuffer logBuffer = new StringBuffer(128).append(
+                            "Error forwarding mail to ");
+                    for (Iterator j = remoteRecipients.iterator();
j.hasNext(); ) {
+                        logBuffer.append(j.next());
+                        if (j.hasNext()) logBuffer.append(", ");
+                    }
+                    logBuffer.append("attempting local delivery");
+
+                    getMailetContext().log(logBuffer.toString());
+                    throw me;
+                }
+            }
+
+            if (localRecipients.size() > 0) {
+                return localRecipients;
+            } else {
+                return null;
+            }
+        }
+    }

     /**
      * @see org.apache.mailet.GenericMailet#init()
@@ -270,11 +284,33 @@
                 UsersStore usersStore = (UsersStore)
compMgr.lookup(UsersStore.ROLE);
                 usersRepository = usersStore.getRepository(userRep);
             }
+
+

         } catch (ServiceException cnfe) {
             log("Failed to retrieve UsersStore component:" +
cnfe.getMessage());
         }
+
+        try {
+            String vutName = getInitParameter("virtualusertable-store");
+            if (vutName == null || vutName.length() == 0) {
+                try {
+                    vut = ((VirtualUserTableStore) compMgr
+
.lookup(VirtualUserTableStore.ROLE)).getTable(vutName);
+                } catch (ServiceException e) {
+                    log("Failed to retrieve VirtualUserTable component:"
+                            + e.getMessage());
+                }
+            } else {
+                vut = ((VirtualUserTableStore) compMgr
+
.lookup(VirtualUserTableStore.ROLE)).getTable("DefaultVirtualUserTable");
+            }
+
+

+        } catch (ServiceException cnfe) {
+            log("Failed to retrieve UsersStore component:" +
cnfe.getMessage());
+        }
     }

 }


Wdyt ?

Cheers,
Norman

2008/12/14 Robert Burrell Donkin <[email protected]>:
> On Sun, Dec 14, 2008 at 1:55 PM, Norman Maurer <[email protected]> wrote:
>> Ah ok now I see what makes you wonder... thats right. isLocalEmail
>> works only after the vut mappings was done. Thats prolly a problem in
>> some cases..
>
> it's the problem in my case :-)
>
> (the code's very messy since it's trying to ensure backwards compatibility.)
>
> - robert
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to