    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
    015    package org.apache.tapestry5.internal.jpa;
    016
    017    import java.util.Collections;
    018    import java.util.Map;
    019    import java.util.Map.Entry;
    020
    021    import javax.persistence.EntityManager;
    022    import javax.persistence.spi.PersistenceUnitInfo;
    023
    024    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    025    import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
    026    import org.apache.tapestry5.jpa.EntityManagerManager;
    027    import org.apache.tapestry5.jpa.EntityManagerSource;
    028    import org.slf4j.Logger;
    029
    030    public 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        public EntityManager getEntityManager(final String persistenceUnitName)
    050        {
    051            return getOrCreateEntityManager(persistenceUnitName);
    052        }
    053
    054        /**
    055         * {@inheritDoc}
    056         */
    057        public Map<String, EntityManager> getEntityManagers()
    058        {
    059            createAllEntityManagers();
    060
    061            return Collections.unmodifiableMap(entityManagers);
    062        }
    063
    064        private void createAllEntityManagers()
    065        {
    066            for (final PersistenceUnitInfo info : entityManagerSource.getPersistenceUnitInfos())
    067            {
    068                getOrCreateEntityManager(info.getPersistenceUnitName());
    069            }
    070        }
    071
    072        private EntityManager getOrCreateEntityManager(final String persistenceUnitName)
    073        {
    074            EntityManager em = entityManagers.get(persistenceUnitName);
    075
    076            if (em == null)
    077            {
    078                em = entityManagerSource.create(persistenceUnitName);
    079
    080                entityManagers.put(persistenceUnitName, em);
    081            }
    082
    083            return em;
    084        }
    085
    086        public void threadDidCleanup()
    087        {
    088            for (final Entry<String, EntityManager> next : entityManagers.entrySet())
    089            {
    090                try
    091                {
    092                    final EntityManager em = next.getValue();
    093
    094                    if (em.isOpen())
    095                    {
    096                        em.close();
    097                    }
    098                }
    099                catch (final Exception e)
    100                {
    101                    logger.info(String.format(
    102                            "Failed to close EntityManager for persistence unit '%s'", next.getKey()));
    103                }
    104            }
    105
    106            entityManagers.clear();
    107
    108        }
    109
    110    }