All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.smartlab.web.config.JNDIConfigurationStrategy Maven / Gradle / Ivy

Go to download

SmartWeb is a web application development meta framework based on Jakarta Struts, Hibernate and other open source frameworks and libraries.

There is a newer version: 1.2.13
Show newest version
/*
 * The SmartWeb Framework
 * Copyright (C) 2004-2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * For further informations on the SmartWeb Framework please visit
 *
 *                        http://smartweb.sourceforge.net
 */
package net.smartlab.web.config;

import java.net.URL;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import net.smartlab.config.Element;
import net.smartlab.config.XMLConfiguration;
import net.smartlab.web.BusinessObjectFactory;
import net.smartlab.web.Domain;

/**
 * The Hibernate configuration file used must reside into the
 * META-INF directory of the topmost packaging archive and should
 * be named smartweb.jar.hcf or have the name of the JAR file
 * containing the subclass followed by the .hcf suffix.
 * 
 * @author rlogiacco
 */
public class JNDIConfigurationStrategy implements FactoryConfigurationStrategy {

	/**
	 * Provides logging capabilities to the strategy.
	 */
	protected final Log logger = LogFactory.getLog(JNDIConfigurationStrategy.class);


	/**
	 * @see net.smartlab.web.config.FactoryConfigurationStrategy#getSessionFactory(net.smartlab.web.BusinessObjectFactory)
	 */
	public SessionFactory getSessionFactory(BusinessObjectFactory factory) {
		synchronized (BusinessObjectFactory.class) {
			String archive = Domain.getLastArchiveName(factory.getClass());
			URL file = Domain.getResource(factory.getClass(), new String[] {"/META-INF/" + archive + ".hcf",
					"/META-INF/smartweb.jar.hcf"});
			try {
				XMLConfiguration config = new XMLConfiguration(file);
				Element sessionFactory = config.getElement("session-factory");
				String jndi = sessionFactory.getAttribute("name");
				try {
					// TODO check if remote references retrieves different objects as this behaviour can lead to caching problems
					return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
							SessionFactory.class);
				} catch (NamingException ne) {
					logger.info("getSessionFactory() - configure hibernate");
					try {
						// Session factory not yet configured
						Configuration hibernate = new Configuration().configure(file);
						hibernate.buildSessionFactory();
						return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
								SessionFactory.class);
					} catch (Exception e) {
						logger.error("getSessionFactory() - error", e);
					}
				}
			} catch (Exception e) {
				logger.error("getSessionFactory() - error", e);
			}
			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy