Search in sources :

Example 1 with MailboxData

use of com.zimbra.cs.mailbox.Mailbox.MailboxData in project zm-mailbox by Zimbra.

the class DesktopMailboxTest method init.

@BeforeClass
public static void init() throws Exception {
    //TODO: allow paths to be specified so we can run tests outside of ZimbraServer
    MailboxTestUtil.initServer();
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            //mock the behaviors we need to test in DesktopMailbox
            return new Mailbox(data) {

                @Override
                protected boolean needRedo(OperationContext octxt, RedoableOp recorder) {
                    return false;
                }
            };
        }
    });
    Provisioning prov = Provisioning.getInstance();
    prov.createAccount("test@zimbra.com", "secret", new HashMap<String, Object>());
}
Also used : RedoableOp(com.zimbra.cs.redolog.op.RedoableOp) MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) Provisioning(com.zimbra.cs.account.Provisioning) BeforeClass(org.junit.BeforeClass)

Example 2 with MailboxData

use of com.zimbra.cs.mailbox.Mailbox.MailboxData in project zm-mailbox by Zimbra.

the class SendShareNotificationTest method init.

@BeforeClass
public static void init() throws Exception {
    MailboxTestUtil.initServer();
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = Maps.newHashMap();
    prov.createDomain("zimbra.com", attrs);
    attrs = Maps.newHashMap();
    prov.createAccount("test@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test2@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test3@zimbra.com", "secret", attrs);
    // this MailboxManager does everything except actually send mail
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            return new Mailbox(data) {

                @Override
                public MailSender getMailSender() {
                    return new MailSender() {

                        @Override
                        protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) throws SafeMessagingException, IOException {
                            try {
                                return Arrays.asList(getRecipients(mm));
                            } catch (Exception e) {
                                return Collections.emptyList();
                            }
                        }
                    };
                }
            };
        }
    });
    L10nUtil.setMsgClassLoader("conf/msgs");
}
Also used : MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) BeforeClass(org.junit.BeforeClass)

Example 3 with MailboxData

use of com.zimbra.cs.mailbox.Mailbox.MailboxData in project zm-mailbox by Zimbra.

the class GetMsgTest method setUp.

/**
     * @throws java.lang.Exception
     */
@Before
public void setUp() throws Exception {
    MailboxTestUtil.initServer();
    MailboxTestUtil.clearData();
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = Maps.newHashMap();
    prov.createDomain("zimbra.com", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test2@zimbra.com", "secret", attrs);
    // this MailboxManager does everything except actually send mail
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            return new Mailbox(data) {

                @Override
                public MailSender getMailSender() {
                    return new MailSender() {

                        @Override
                        protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) {
                            try {
                                return Arrays.asList(getRecipients(mm));
                            } catch (Exception e) {
                                return Collections.emptyList();
                            }
                        }
                    };
                }
            };
        }
    });
}
Also used : MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) MailSender(com.zimbra.cs.mailbox.MailSender) Provisioning(com.zimbra.cs.account.Provisioning) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) Before(org.junit.Before)

Example 4 with MailboxData

use of com.zimbra.cs.mailbox.Mailbox.MailboxData in project zm-mailbox by Zimbra.

the class MailboxManager method getMailboxById.

protected Mailbox getMailboxById(int mailboxId, FetchMode fetchMode, boolean skipMailHostCheck) throws ServiceException {
    // we need the Mailbox instantiation code to run w/o the lock held.
    assert (fetchMode == FetchMode.ONLY_IF_CACHED || !Thread.holdsLock(this));
    if (mailboxId <= 0)
        throw MailServiceException.NO_SUCH_MBOX(mailboxId);
    long startTime = ZimbraPerf.STOPWATCH_MBOX_GET.start();
    Mailbox mbox = null;
    synchronized (this) {
        // check to see if the mailbox has already been cached
        Object cached = retrieveFromCache(mailboxId, true);
        if (cached instanceof Mailbox) {
            ZimbraPerf.COUNTER_MBOX_CACHE.increment(100);
            mbox = (Mailbox) cached;
        }
    }
    if (fetchMode == FetchMode.ONLY_IF_CACHED && (mbox == null || !mbox.isOpen())) {
        // if the mailbox is in the middle of opening, deem it not cached rather than waiting.
        return null;
    }
    if (mbox == null) {
        // not found in cache
        ZimbraPerf.COUNTER_MBOX_CACHE.increment(0);
        MailboxData data;
        DbConnection conn = DbPool.getConnection();
        try {
            // fetch the Mailbox data from the database
            data = DbMailbox.getMailboxStats(conn, mailboxId);
            if (data == null) {
                throw MailServiceException.NO_SUCH_MBOX(mailboxId);
            }
        } finally {
            conn.closeQuietly();
        }
        mbox = instantiateMailbox(data);
        Account account = mbox.getAccount();
        boolean isGalSyncAccount = AccountUtil.isGalSyncAccount(account);
        mbox.setGalSyncMailbox(isGalSyncAccount);
        if (!skipMailHostCheck) {
            // server.
            if (!Provisioning.onLocalServer(account))
                throw ServiceException.WRONG_HOST(account.getMailHost(), null);
        }
        synchronized (this) {
            // avoid the race condition by re-checking the cache and using that data (if any)
            Object cached = retrieveFromCache(mailboxId, false);
            if (cached instanceof Mailbox) {
                mbox = (Mailbox) cached;
            } else {
                // cache the newly-created Mailbox object
                if (cached instanceof MailboxMaintenance) {
                    ((MailboxMaintenance) cached).setMailbox(mbox);
                } else {
                    cacheMailbox(mbox);
                }
            }
        }
    }
    // and other longer operations don't block the system.
    if (mbox.open()) {
        // if TRUE, then the mailbox is actually opened, so we need to notify listeners of the mailbox being loaded
        notifyMailboxLoaded(mbox);
    }
    ZimbraPerf.STOPWATCH_MBOX_GET.stop(startTime);
    if (maintenanceLocks.containsKey(mbox.getAccountId()) && mbox.getMaintenance() == null) {
        //case here where mailbox was unloaded (due to memory pressure or similar) but
        //it was in maintenance before so needs to be in maintenance now that it is reloaded
        MailboxMaintenance oldMaint = maintenanceLocks.get(mbox.getAccountId());
        MailboxMaintenance maint = null;
        synchronized (mbox) {
            maint = mbox.beginMaintenance();
            synchronized (this) {
                cache.put(mailboxId, maint);
            }
        }
        if (oldMaint.isNestedAllowed()) {
            maint.setNestedAllowed(true);
        }
        maint.removeAllowedThread(Thread.currentThread());
        maintenanceLocks.put(mbox.getAccountId(), maint);
    }
    return mbox;
}
Also used : Account(com.zimbra.cs.account.Account) DbMailbox(com.zimbra.cs.db.DbMailbox) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 5 with MailboxData

use of com.zimbra.cs.mailbox.Mailbox.MailboxData in project zm-mailbox by Zimbra.

the class MailboxManager method createMailboxInternal.

private synchronized Mailbox createMailboxInternal(OperationContext octxt, Account account, boolean isGalSyncAccount) throws ServiceException {
    CreateMailbox redoRecorder = new CreateMailbox(account.getId());
    Mailbox mbox = null;
    boolean success = false;
    DbConnection conn = DbPool.getConnection();
    try {
        CreateMailbox redoPlayer = (octxt == null ? null : (CreateMailbox) octxt.getPlayer());
        int id = (redoPlayer == null ? Mailbox.ID_AUTO_INCREMENT : redoPlayer.getMailboxId());
        // create the mailbox row and the mailbox database
        MailboxData data;
        boolean created = false;
        try {
            data = DbMailbox.createMailbox(conn, id, account.getId(), account.getName(), -1);
            ZimbraLog.mailbox.info("Creating mailbox with id %d and group id %d for %s.", data.id, data.schemaGroupId, account.getName());
            created = true;
        } catch (ServiceException se) {
            if (MailServiceException.ALREADY_EXISTS.equals(se.getCode())) {
                // mailbox for the account may be created by other server, re-fetch now.
                id = DbMailbox.getMailboxId(conn, account.getId());
                if (id > 0) {
                    data = DbMailbox.getMailboxStats(conn, id);
                } else {
                    throw ServiceException.FAILURE("could not create mailbox", se);
                }
            } else {
                throw se;
            }
        }
        mbox = account.isIsExternalVirtualAccount() ? instantiateExternalVirtualMailbox(data) : instantiateMailbox(data);
        mbox.setGalSyncMailbox(isGalSyncAccount);
        // the existing Connection is used for the rest of this transaction...
        mbox.beginTransaction("createMailbox", octxt, redoRecorder, conn);
        if (created) {
            // create the default folders
            mbox.initialize();
        }
        // cache the accountID-to-mailboxID and mailboxID-to-Mailbox relationships
        cacheAccount(data.accountId, data.id);
        cacheMailbox(mbox);
        redoRecorder.setMailboxId(mbox.getId());
        success = true;
    } catch (ServiceException e) {
        // Log exception here, just in case.  If badness happens during rollback
        // the original exception will be lost.
        ZimbraLog.mailbox.error("Error during mailbox creation", e);
        throw e;
    } catch (OutOfMemoryError e) {
        Zimbra.halt("out of memory", e);
    } catch (Throwable t) {
        ZimbraLog.mailbox.error("Error during mailbox creation", t);
        throw ServiceException.FAILURE("createMailbox", t);
    } finally {
        try {
            if (mbox != null) {
                mbox.endTransaction(success);
            } else {
                conn.rollback();
            }
        } finally {
            conn.closeQuietly();
        }
    }
    return mbox;
}
Also used : DbMailbox(com.zimbra.cs.db.DbMailbox) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

MailboxData (com.zimbra.cs.mailbox.Mailbox.MailboxData)5 Provisioning (com.zimbra.cs.account.Provisioning)3 ServiceException (com.zimbra.common.service.ServiceException)2 DbMailbox (com.zimbra.cs.db.DbMailbox)2 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)2 MailSender (com.zimbra.cs.mailbox.MailSender)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 MailboxManager (com.zimbra.cs.mailbox.MailboxManager)2 CreateMailbox (com.zimbra.cs.redolog.op.CreateMailbox)2 Collection (java.util.Collection)2 MimeMessage (javax.mail.internet.MimeMessage)2 BeforeClass (org.junit.BeforeClass)2 Account (com.zimbra.cs.account.Account)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 RedoableOp (com.zimbra.cs.redolog.op.RedoableOp)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 Before (org.junit.Before)1