001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of the Granite Data Services Platform.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 * USA, or see <http://www.gnu.org/licenses/>.
021 */
022 package org.granite.tide.seam.lazy;
023
024 import static org.jboss.seam.annotations.Install.FRAMEWORK;
025
026 import javax.persistence.EntityManager;
027
028 import org.granite.tide.TidePersistenceManager;
029 import org.hibernate.Session;
030 import org.jboss.seam.Component;
031 import org.jboss.seam.ScopeType;
032 import org.jboss.seam.annotations.Install;
033 import org.jboss.seam.annotations.Name;
034 import org.jboss.seam.annotations.Scope;
035 import org.jboss.seam.annotations.intercept.BypassInterceptors;
036
037 /**
038 * Initializes a request for a passed in entity and a lazy property.
039
040 * @author CIngram,VDanda
041 */
042 @Name("org.granite.tide.seam.seamInitializer")
043 @Scope(ScopeType.CONVERSATION)
044 @Install(precedence=FRAMEWORK+1, classDependencies="org.hibernate.Session")
045 @BypassInterceptors
046 public class SeamHibernateInitializer extends SeamInitializer {
047
048 private static final long serialVersionUID = 1L;
049
050
051 /**
052 * Try to determine what type of persistence the application is using.
053 * If the EntityManager is stored under entityManager or if the Hibernate session is
054 * stored under session. Then the context will be found and used. This is only called if a
055 * ITidePersistenceManager is not found, probably because the query was not run in a conversation.
056 * @return The appropriate manager for the persistence context being used, if it can be determined
057 * otherwise a null is returned.
058 */
059 @Override
060 protected TidePersistenceManager tryToDetermineInitiailzer() {
061 EntityManager em = findEntityManager();
062 if (em != null)
063 return TideHibernatePersistenceFactory.createTidePersistence(null, em);
064
065 Session session = findHibernateSession();
066 if (session != null)
067 return TideHibernatePersistenceFactory.createTidePersistence(null, session);
068
069 return null;
070 }
071
072
073 /**
074 * Try to find the hibernateSession if possible. Assume that the session is stored under
075 * session.
076 * @return The Current Session
077 */
078 private Session findHibernateSession() {
079 return (Session) Component.getInstance("session");
080 }
081 }
082