    001// Copyright 2011 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.jpa;
    016
    017import java.util.Collections;
    018import java.util.Map;
    019import java.util.Map.Entry;
    020
    021import javax.persistence.EntityManager;
    022import javax.persistence.spi.PersistenceUnitInfo;
    023
    024import org.apache.tapestry5.commons.util.CollectionFactory;
    025import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
    026import org.apache.tapestry5.jpa.EntityManagerManager;
    027import org.apache.tapestry5.jpa.EntityManagerSource;
    028import org.slf4j.Logger;
    029
    030public class EntityManagerManagerImpl implements EntityManagerManager, ThreadCleanupListener
    031{
    032    private final EntityManagerSource entityManagerSource;
    033
    034    private final Logger logger;
    035
    036    private final Map<String, EntityManager> entityManagers = CollectionFactory.newMap();
    037
    038    public EntityManagerManagerImpl(final EntityManagerSource entityManagerSource,
    039            final Logger logger)
    040    {
    041        super();
    042        this.entityManagerSource = entityManagerSource;
    043        this.logger = logger;
    044    }
    045
    046    /**
    047     * {@inheritDoc}
    048     */
    049    @Override
    050    public EntityManager getEntityManager(final String persistenceUnitName)
    051    {
    052        return getOrCreateEntityManager(persistenceUnitName);
    053    }
    054
    055    /**
    056     * {@inheritDoc}
    057     */
    058    @Override
    059    public Map<String, EntityManager> getEntityManagers()
    060    {
    061        createAllEntityManagers();
    062
    063        return Collections.unmodifiableMap(entityManagers);
    064    }
    065
    066    private void createAllEntityManagers()
    067    {
    068        for (final PersistenceUnitInfo info : entityManagerSource.getPersistenceUnitInfos())
    069        {
    070            getOrCreateEntityManager(info.getPersistenceUnitName());
    071        }
    072    }
    073
    074    private EntityManager getOrCreateEntityManager(final String persistenceUnitName)
    075    {
    076        EntityManager em = entityManagers.get(persistenceUnitName);
    077
    078        if (em == null)
    079        {
    080            em = entityManagerSource.create(persistenceUnitName);
    081
    082            entityManagers.put(persistenceUnitName, em);
    083        }
    084
    085        return em;
    086    }
    087
    088    @Override
    089    public void threadDidCleanup()
    090    {
    091        for (final Entry<String, EntityManager> next : entityManagers.entrySet())
    092        {
    093            try
    094            {
    095                final EntityManager em = next.getValue();
    096
    097                if (em.isOpen())
    098                {
    099                    em.close();
    100                }
    101            }
    102            catch (final Exception e)
    103            {
    104                logger.info(String.format(
    105                        "Failed to close EntityManager for persistence unit '%s'", next.getKey()));
    106            }
    107        }
    108
    109        entityManagers.clear();
    110
    111    }
    112
    113}