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; 020 021import javax.persistence.EntityManager; 022import javax.persistence.EntityManagerFactory; 023 024import org.apache.james.mailbox.MailboxSession; 025import org.apache.james.mailbox.jpa.mail.JPAMailboxMapper; 026import org.apache.james.mailbox.jpa.mail.JPAMessageMapper; 027import org.apache.james.mailbox.jpa.user.JPASubscriptionMapper; 028import org.apache.james.mailbox.store.MailboxSessionMapperFactory; 029import org.apache.james.mailbox.store.mail.MailboxMapper; 030import org.apache.james.mailbox.store.mail.MessageMapper; 031import org.apache.james.mailbox.store.mail.ModSeqProvider; 032import org.apache.james.mailbox.store.mail.UidProvider; 033import org.apache.james.mailbox.store.user.SubscriptionMapper; 034 035/** 036 * JPA implementation of {@link MailboxSessionMapperFactory} 037 * 038 */ 039public class JPAMailboxSessionMapperFactory extends MailboxSessionMapperFactory<Long> { 040 041 private final EntityManagerFactory entityManagerFactory; 042 private final UidProvider<Long> uidProvider; 043 private final ModSeqProvider<Long> modSeqProvider; 044 045 public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory, UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider) { 046 this.entityManagerFactory = entityManagerFactory; 047 this.uidProvider = uidProvider; 048 this.modSeqProvider = modSeqProvider; 049 createEntityManager().close(); 050 } 051 052 @Override 053 public MailboxMapper<Long> createMailboxMapper(MailboxSession session) { 054 return new JPAMailboxMapper(entityManagerFactory); 055 } 056 057 @Override 058 public MessageMapper<Long> createMessageMapper(MailboxSession session) { 059 return new JPAMessageMapper(session, uidProvider, modSeqProvider, entityManagerFactory); 060 } 061 062 @Override 063 public SubscriptionMapper createSubscriptionMapper(MailboxSession session) { 064 return new JPASubscriptionMapper(entityManagerFactory); 065 } 066 067 /** 068 * Return a new {@link EntityManager} instance 069 * 070 * @return manager 071 */ 072 private EntityManager createEntityManager() { 073 return entityManagerFactory.createEntityManager(); 074 } 075 076}