View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core.messaging.persistence;
16  
17  import org.figure8.join.core.persistence.HibernateObjectDao;
18  import org.figure8.join.core.messaging.JMSConsumerBeanInfo;
19  import org.figure8.join.core.InfrastructureException;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  import org.springframework.orm.hibernate.HibernateCallback;
24  import net.sf.hibernate.LockMode;
25  import net.sf.hibernate.Hibernate;
26  import net.sf.hibernate.Session;
27  import net.sf.hibernate.HibernateException;
28  /**
29   * Implementation of JMSConsumerBeanInfoDao using Hibernate ORM system.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.3 $
32   */
33  public class HibernateJMSConsumerBeanInfoDao extends HibernateObjectDao
34          implements JMSConsumerBeanInfoDao{
35  
36     // Static -------------------------------------------------------------------
37  
38     /** Get a commons logger. */
39     private static final Log log = LogUtil.getLog(HibernateJMSConsumerBeanInfoDao.class);
40  
41  
42     // Constructors -------------------------------------------------------------
43  
44     /** Creates a new HibernateJMSConsumerBeanInfoDao. */
45     public HibernateJMSConsumerBeanInfoDao(){
46     }
47  
48  
49     // Implementation of JMSConsumerBeanInfoDao ---------------------------------
50  
51     /**
52      * Retrieve JMSConsumerBean info using its name
53      * @param name The name of the consumer info to retrieve
54      * @return The consumer info or null if no consumer exists with this name
55      */
56     public JMSConsumerBeanInfo getJMSConsumerBeanInfo(String name){
57        // Result should be a single object.
58        JMSConsumerBeanInfo consumer = (JMSConsumerBeanInfo)findSingleObject(findNamedQueryStringParam(
59                "join.consumer_findByName", "consumerName", name));
60        if (log.isDebugEnabled())
61           log.debug("Found a consumer with name '" + name + "' ? " + (consumer!=null ? "Yes !" : "No !"));
62        return consumer;
63     }
64  
65     /**
66      * Initialize the informations on parameters of a JMSConsumerBeanInfo
67      * (this because parameters may be lazy-loaded from persistence store)
68      * @param info The bean wrapping information on JMS consumer to get parameters for
69      */
70     public void initializeConsumerParameterInfos(final JMSConsumerBeanInfo info){
71        try{
72           // Execute initialization after having reattaching object in same session.
73           getHibernateTemplate().execute(new HibernateCallback(){
74              public Object doInHibernate(Session session) throws HibernateException{
75                 if (!Hibernate.isInitialized(info.getConsumerParameterInfos())){
76                    session.lock(info, LockMode.READ);
77                    Hibernate.initialize(info.getConsumerParameterInfos());
78                 }
79                 return null;
80              }
81           });
82        }
83        catch (Exception e){
84           log.error("Exception in initialize()!", e);
85           throw new InfrastructureException(e);
86        }
87     }
88  
89  
90     // Implementation of ObjectDao ----------------------------------------------
91  
92     /** @return org.figure8.join.core.messaging.JMSConsumerBeanInfo class */
93     public Class getPersistentClass(){
94        return org.figure8.join.core.messaging.JMSConsumerBeanInfo.class;
95     }
96  }