001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019package org.apache.james.mailbox.jpa.mail; 020 021import javax.persistence.EntityManager; 022import javax.persistence.EntityManagerFactory; 023import javax.persistence.PersistenceException; 024 025import org.apache.james.mailbox.MailboxPathLocker; 026import org.apache.james.mailbox.MailboxSession; 027import org.apache.james.mailbox.exception.MailboxException; 028import org.apache.james.mailbox.jpa.mail.model.JPAMailbox; 029import org.apache.james.mailbox.store.mail.AbstractLockingUidProvider; 030import org.apache.james.mailbox.store.mail.model.Mailbox; 031 032public class JPAUidProvider extends AbstractLockingUidProvider<Long>{ 033 034 private EntityManagerFactory factory; 035 036 public JPAUidProvider(MailboxPathLocker locker, EntityManagerFactory factory) { 037 super(locker); 038 this.factory = factory; 039 } 040 041 042 @Override 043 public long lastUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException { 044 EntityManager manager = null; 045 try { 046 manager = factory.createEntityManager(); 047 manager.getTransaction().begin(); 048 long uid = (Long) manager.createNamedQuery("findLastUid").setParameter("idParam", mailbox.getMailboxId()).getSingleResult(); 049 manager.getTransaction().commit(); 050 return uid; 051 } catch (PersistenceException e) { 052 if (manager != null && manager.getTransaction().isActive()) { 053 manager.getTransaction().rollback(); 054 } 055 throw new MailboxException("Unable to get last uid for mailbox " + mailbox, e); 056 } finally { 057 if (manager != null) { 058 manager.close(); 059 } 060 } 061 } 062 063 @Override 064 protected long lockedNextUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException { 065 EntityManager manager = null; 066 try { 067 manager = factory.createEntityManager(); 068 manager.getTransaction().begin(); 069 JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId()); 070 long uid = m.consumeUid(); 071 manager.persist(m); 072 manager.getTransaction().commit(); 073 return uid; 074 } catch (PersistenceException e) { 075 if (manager != null && manager.getTransaction().isActive()) { 076 manager.getTransaction().rollback(); 077 } 078 throw new MailboxException("Unable to save next uid for mailbox " + mailbox, e); 079 } finally { 080 if (manager != null) { 081 manager.close(); 082 } 083 } 084 } 085 086}