Update of
/cvsroot/xdoclet/xdoclet/modules/hibernate/src/xdoclet/modules/hibernate/resources
In directory
sc8-pr-cvs1:/tmp/cvs-serv1190/modules/hibernate/src/xdoclet/modules/hibernate/resources
Added Files:
hibernate-factory.xdt
Log Message:
First draft session factory class generator. Does two things: first it generates
inline all of the persistent class names that are used in a project. IMHO it's better
than a properties file because the table generator call can optionally be included
right there. Second, it forms a junction at which things can easily be switched to
using something like the JBoss MBean without changing a lot of client code.
This code needs review, please send comments!
--- NEW FILE: hibernate-factory.xdt ---
/*
* <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages"
resource="do_not_edit"/>
*/
package <XDtPackage:packageOf><XDtHibernate:factoryClass/></XDtPackage:packageOf>;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import javax.naming.NamingException;
import java.util.Properties;
public class <XDtClass:classOf><XDtHibernate:factoryClass/></XDtClass:classOf> {
// default is set from <hibernateservice/> subtask
<XDtHibernate:ifHasJndiName>
public static String jndiFactoryName = "<XDtHibernate:jndiName/>";
</XDtHibernate:ifHasJndiName>
<XDtHibernate:ifNotHasJndiName>
public static String jndiFactoryName = "java:/hibernate/SessionFactory";
</XDtHibernate:ifNotHasJndiName>
private static SessionFactory cachedSessionFactory = null;
private static Properties propertyCache = new Properties();
public static int LOCAL_SESSION_FACTORY = 0;
public static int JNDI_SESSION_FACTORY = 1;
private static int sessionFactoryType = LOCAL_SESSION_FACTORY;
public static int getSessionFactoryType() {
return sessionFactoryType;
}
public static void setSessionFactoryType(int sessionFactoryType) {
<XDtClass:classOf><XDtHibernate:factoryClass/></XDtClass:classOf>.sessionFactoryType =
sessionFactoryType;
}
public static SessionFactory getSessionFactory() throws HibernateException {
return getSessionFactory(propertyCache, false);
}
public static SessionFactory getSessionFactory(Properties props, boolean
createTables) throws HibernateException {
if (sessionFactoryType == LOCAL_SESSION_FACTORY) {
return getLocalSessionFactory(props, createTables);
} else if (sessionFactoryType == JNDI_SESSION_FACTORY)
return getJNDISessionFactory();
else {
throw new HibernateException(new UnsupportedOperationException("factory
type '" + sessionFactoryType + "' is not supported"));
}
}
public static String getJndiFactoryName() {
return jndiFactoryName;
}
public static void setJndiFactoryName(String jndiFactoryName) {
<XDtClass:classOf><XDtHibernate:factoryClass/></XDtClass:classOf>.jndiFactoryName =
jndiFactoryName;
}
public static SessionFactory getJNDISessionFactory() throws HibernateException {
// Obtain initial context
javax.naming.InitialContext initialContext = null;
try {
initialContext = new javax.naming.InitialContext();
java.lang.Object objRef = initialContext.lookup(jndiFactoryName);
return (SessionFactory) javax.rmi.PortableRemoteObject.narrow(objRef,
SessionFactory.class);
} catch (NamingException ne) {
throw new HibernateException(ne);
} finally {
try {
initialContext.close();
} catch (Exception e) {
}
}
}
private static SessionFactory getLocalSessionFactory(Properties props, boolean
createTables) throws HibernateException {
if (cachedSessionFactory == null) {
Configuration cfg = new Configuration()
<XDtHibernate:forAllPersistentClasses>
.addClass(<XDtClass:fullClassName/>.class)
</XDtHibernate:forAllPersistentClasses>
.setProperties(props);
if (createTables) {
new net.sf.hibernate.tool.hbm2ddl.SchemaExport(cfg).create(true, true);
}
cachedSessionFactory = cfg.buildSessionFactory();
}
return cachedSessionFactory;
}
public static Properties getPropertyCache() {
<XDtHibernate:ifGeneratePropertyCache>
if (propertyCache == null) {
propertyCache.put("hibernate.dialect", "<XDtHibernate:dialect/>");
propertyCache.put("hibernate.connection.driver_class",
"<XDtHibernate:driver/>");
propertyCache.put("hibernate.connection.url",
"<XDtHibernate:dataSource/>");
propertyCache.put("hibernate.connection.username",
"<XDtHibernate:username/>");
propertyCache.put("hibernate.connection.password",
"<XDtHibernate:password/>");
}
</XDtHibernate:ifGeneratePropertyCache>
return propertyCache;
}
public static void setPropertyCache(Properties propertyCache) {
<XDtClass:classOf><XDtHibernate:factoryClass/></XDtClass:classOf>.propertyCache =
propertyCache;
}
}
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel