View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library 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.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web.config;
24  
25  import java.net.URL;
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  import javax.rmi.PortableRemoteObject;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.hibernate.SessionFactory;
32  import org.hibernate.cfg.Configuration;
33  import net.smartlab.config.Element;
34  import net.smartlab.config.XMLConfiguration;
35  import net.smartlab.web.BusinessObjectFactory;
36  import net.smartlab.web.Domain;
37  
38  /**
39   * The Hibernate configuration file used must reside into the
40   * <code>META-INF</code> directory of the topmost packaging archive and should
41   * be named <code>smartweb.jar.hcf</code> or have the name of the JAR file
42   * containing the subclass followed by the <code>.hcf</code> suffix.
43   * 
44   * @author rlogiacco
45   */
46  public class JNDIConfigurationStrategy implements FactoryConfigurationStrategy {
47  
48  	/**
49  	 * Provides logging capabilities to the strategy.
50  	 */
51  	protected final Log logger = LogFactory.getLog(JNDIConfigurationStrategy.class);
52  
53  
54  	/**
55  	 * @see net.smartlab.web.config.FactoryConfigurationStrategy#getSessionFactory(net.smartlab.web.BusinessObjectFactory)
56  	 */
57  	public SessionFactory getSessionFactory(BusinessObjectFactory factory) {
58  		synchronized (BusinessObjectFactory.class) {
59  			String archive = Domain.getLastArchiveName(factory.getClass());
60  			URL file = Domain.getResource(factory.getClass(), new String[] {"/META-INF/" + archive + ".hcf",
61  					"/META-INF/smartweb.jar.hcf", "/META-INF/hibernate.conf.xml"});
62  			try {
63  				XMLConfiguration config = new XMLConfiguration(file);
64  				Element sessionFactory = config.getElement("session-factory");
65  				String jndi = sessionFactory.getAttribute("name");
66  				try {
67  					// TODO check if remote references retrieves different objects as this behaviour can lead to caching problems
68  					return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
69  							SessionFactory.class);
70  				} catch (NamingException ne) {
71  					logger.info("getSessionFactory() - configure hibernate");
72  					try {
73  						// Session factory not yet configured
74  						Configuration hibernate = new Configuration().configure(file);
75  						hibernate.buildSessionFactory();
76  						return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
77  								SessionFactory.class);
78  					} catch (Exception e) {
79  						logger.error("getSessionFactory() - error", e);
80  					}
81  				}
82  			} catch (Exception e) {
83  				logger.error("getSessionFactory() - error", e);
84  			}
85  			return null;
86  		}
87  	}
88  }