From: <one...@us...> - 2003-02-02 00:29:10
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg In directory sc8-pr-cvs1:/tmp/cvs-serv18747/net/sf/hibernate/cfg Modified Files: Binder.java Configuration.java Mappings.java Log Message: added support for configuring caching in .cfg.xml much saner collection role naming improved logging during startup Index: Binder.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Binder.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Binder.java 28 Jan 2003 10:22:19 -0000 1.4 --- Binder.java 2 Feb 2003 00:29:06 -0000 1.5 *************** *** 103,106 **** --- 103,108 ---- model.setTable( model.getSuperclass().getTable() ); + + log.info("Mapping subclass: " + model.getName() + " -> " + model.getTable().getName() ); // properties *************** *** 129,132 **** --- 131,136 ---- model.setTable(mytable); + log.info("Mapping joined-subclass: " + model.getName() + " -> " + model.getTable().getName() ); + NodeList subnodes = node.getChildNodes(); for ( int i=0; i<subnodes.getLength(); i++ ) { *************** *** 134,138 **** Value key = new Value(mytable); model.setKey(key); ! bindValue( subnodes.item(i), key, false, Mappings.ROOT_ROLE_NAME ); } } --- 138,142 ---- Value key = new Value(mytable); model.setKey(key); ! bindValue( subnodes.item(i), key, false, model.getName() ); } } *************** *** 165,168 **** --- 169,174 ---- model.setTable(table); + log.info("Mapping class: " + model.getName() + " -> " + model.getTable().getName() ); + //PERSISTER Node persisterNode = atts.getNamedItem("persister"); *************** *** 192,196 **** Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = Property.getPropertyName(subnode); if ( "id".equals(name) ) { --- 198,202 ---- Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = getPropertyName(subnode); if ( "id".equals(name) ) { *************** *** 218,222 **** model.setIdentifier(id); if (propertyName==null) { ! bindComponent(subnode, id, null, null, false, mappings); model.setEmbeddedIdentifier( id.isEmbedded() ); model.setIdentifierProperty(null); --- 224,228 ---- model.setIdentifier(id); if (propertyName==null) { ! bindComponent(subnode, id, null, model.getName() + ".id", false, mappings); model.setEmbeddedIdentifier( id.isEmbedded() ); model.setIdentifierProperty(null); *************** *** 224,228 **** else { Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); ! bindComponent(subnode, id, reflectedClass, null, false, mappings); Property prop = new Property(id); bindProperty(subnode, prop, mappings); --- 230,234 ---- else { Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); ! bindComponent(subnode, id, reflectedClass, model.getName() + StringHelper.DOT + propertyName, false, mappings); Property prop = new Property(id); bindProperty(subnode, prop, mappings); *************** *** 253,257 **** } else if ( "jcs-cache".equals(name) ) { ! model.setCache( mappings.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getPersistentClass().getName(), --- 259,263 ---- } else if ( "jcs-cache".equals(name) ) { ! model.setCache( Configuration.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getPersistentClass().getName(), *************** *** 334,338 **** public static void bindProperty(Node node, Property model, Mappings mappings) throws MappingException { ! model.setName( Property.getPropertyName(node) ); Type type = model.getValue().getType(); if (type==null) throw new MappingException( "Could not determine a property type for: " + model.getName() ); --- 340,344 ---- public static void bindProperty(Node node, Property model, Mappings mappings) throws MappingException { ! model.setName( getPropertyName(node) ); Type type = model.getValue().getType(); if (type==null) throw new MappingException( "Could not determine a property type for: " + model.getName() ); *************** *** 361,367 **** NamedNodeMap atts = node.getAttributes(); //ROLENAME ! Node roleAtt = atts.getNamedItem("name"); ! String barerole = roleAtt.getNodeValue(); ! model.setRole( prefix + Mappings.ROLE_SEPERATOR + barerole ); Node inverseNode = atts.getNamedItem("inverse"); --- 367,373 ---- NamedNodeMap atts = node.getAttributes(); //ROLENAME ! Node nameAtt = atts.getNamedItem("name"); ! String propertyName = nameAtt.getNodeValue(); ! model.setRole( prefix + StringHelper.DOT + propertyName ); Node inverseNode = atts.getNamedItem("inverse"); *************** *** 398,406 **** } else { ! tableName = barerole; } Node schemaNode = atts.getNamedItem("schema"); String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getNodeValue(); model.setTable( mappings.addTable(schema, tableName) ); } //LAZINESS --- 404,414 ---- } else { ! tableName = propertyName; } Node schemaNode = atts.getNamedItem("schema"); String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getNodeValue(); model.setTable( mappings.addTable(schema, tableName) ); + + log.info("Mapping collection: " + model.getRole() + " -> " + model.getTable().getName() ); } //LAZINESS *************** *** 600,606 **** } - //component path - path += Mappings.ROLE_SEPERATOR + StringHelper.unqualify(className); - NodeList list = node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { --- 608,611 ---- *************** *** 608,612 **** Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = Property.getPropertyName(subnode); CollectionType collectType = CollectionType.collectionTypeFromString(name); --- 613,618 ---- Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = getPropertyName(subnode); ! String subpath = path + StringHelper.DOT + propertyName; CollectionType collectType = CollectionType.collectionTypeFromString(name); *************** *** 638,642 **** new Component( model.getOwner() ) : // a class component new Component( model.getTable() ); // a composite element ! bindComponent(subnode, (Component) value, subreflectedClass, path, isNullable, mappings); } else if ( "parent".equals(name) ) { --- 644,648 ---- new Component( model.getOwner() ) : // a class component new Component( model.getTable() ); // a composite element ! bindComponent(subnode, (Component) value, subreflectedClass, subpath, isNullable, mappings); } else if ( "parent".equals(name) ) { *************** *** 750,754 **** NodeList list = node.getChildNodes(); ! String path = StringHelper.unqualify( model.getName() ); Table table = model.getTable(); for ( int i=0; i<list.getLength(); i++ ) { --- 756,760 ---- NodeList list = node.getChildNodes(); ! String path = model.getName(); Table table = model.getTable(); for ( int i=0; i<list.getLength(); i++ ) { *************** *** 756,760 **** Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = Property.getPropertyName(subnode); CollectionType collectType = CollectionType.collectionTypeFromString(name); --- 762,766 ---- Node subnode = list.item(i); String name = subnode.getNodeName(); ! String propertyName = getPropertyName(subnode); CollectionType collectType = CollectionType.collectionTypeFromString(name); *************** *** 780,786 **** } else if ( "component".equals(name) || "dynabean".equals(name) ) { Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); value = new Component(model); ! bindComponent(subnode, (Component) value, reflectedClass, path, true, mappings); } else if ( "subclass".equals(name) ) { --- 786,793 ---- } else if ( "component".equals(name) || "dynabean".equals(name) ) { + String subpath = path + StringHelper.DOT + propertyName; Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); value = new Component(model); ! bindComponent(subnode, (Component) value, reflectedClass, subpath, true, mappings); } else if ( "subclass".equals(name) ) { *************** *** 862,866 **** else if ( "composite-index".equals(name) ) { Component component = new Component( model.getTable() ); ! bindComponent(subnode, component, null, Mappings.ROOT_ROLE_NAME, model.isOneToMany(), mappings); model.setIndex(component); } --- 869,873 ---- else if ( "composite-index".equals(name) ) { Component component = new Component( model.getTable() ); ! bindComponent(subnode, component, null, model.getRole() + ".index", model.isOneToMany(), mappings); model.setIndex(component); } *************** *** 884,887 **** --- 891,896 ---- ); model.setTable( persistentClass.getTable() ); + + log.info("Mapping collection: " + model.getRole() + " -> " + model.getTable().getName() ); } *************** *** 913,920 **** Component element = new Component( model.getTable() ); model.setElement(element); ! bindComponent(subnode, element, null, Mappings.ROOT_ROLE_NAME, true, mappings); } else if ( "jcs-cache".equals(name) ) { ! model.setCache( mappings.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getRole(), --- 922,929 ---- Component element = new Component( model.getTable() ); model.setElement(element); ! bindComponent(subnode, element, null, model.getRole() + ".element", true, mappings); } else if ( "jcs-cache".equals(name) ) { ! model.setCache( Configuration.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getRole(), *************** *** 951,955 **** RootClass rootclass = new RootClass(); Binder.bindRootClass(n, rootclass, model); - log.debug("Mapping class: " + rootclass.getName() + " -> " + rootclass.getTable().getName() ); model.addClass(rootclass); } --- 960,963 ---- *************** *** 963,966 **** --- 971,981 ---- } + } + + private static String getPropertyName(Node node) { + NamedNodeMap atts = node.getAttributes(); + if (atts==null) return null; + Node propertyNameNode = atts.getNamedItem("name"); + return (propertyNameNode==null) ? null : propertyNameNode.getNodeValue(); } Index: Configuration.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Configuration.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Configuration.java 31 Jan 2003 23:48:35 -0000 1.12 --- Configuration.java 2 Feb 2003 00:29:06 -0000 1.13 *************** *** 22,25 **** --- 22,26 ---- import net.sf.hibernate.util.ArrayHelper; + import net.sf.hibernate.util.ReflectHelper; import net.sf.hibernate.util.StringHelper; import net.sf.hibernate.util.XMLHelper; *************** *** 34,41 **** --- 35,50 ---- import net.sf.hibernate.id.PersistentIdentifierGenerator; import net.sf.hibernate.impl.SessionFactoryImpl; + import net.sf.hibernate.mapping.Collection; import net.sf.hibernate.mapping.ForeignKey; import net.sf.hibernate.mapping.Index; import net.sf.hibernate.mapping.PersistentClass; + import net.sf.hibernate.mapping.RootClass; import net.sf.hibernate.mapping.Table; + import net.sf.hibernate.cache.Cache; + import net.sf.hibernate.cache.CacheConcurrencyStrategy; + import net.sf.hibernate.cache.CacheException; + import net.sf.hibernate.cache.JCSCache; + import net.sf.hibernate.cache.ReadOnlyCache; + import net.sf.hibernate.cache.ReadWriteCache; import net.sf.hibernate.dialect.Dialect; import net.sf.hibernate.engine.Mapping; *************** *** 105,108 **** --- 114,126 ---- /** + * Get the mapping for a particular collection role + * @param role a collection role + * @return Collection + */ + public Collection getCollectionMapping(String role) { + return (Collection) collections.get(role); + } + + /** * Read mappings from a particular XML file * @param xmlFile a path to a file *************** *** 652,657 **** for ( int j=0; j<mapElements.getLength(); j++ ) { Node mapElement = mapElements.item(j); ! if ( mapElement.getNodeName().equals("mapping") ) { ! NamedNodeMap atts = mapElement.getAttributes(); Node rsrc = atts.getNamedItem("resource"); Node file = atts.getNamedItem("file"); --- 670,675 ---- for ( int j=0; j<mapElements.getLength(); j++ ) { Node mapElement = mapElements.item(j); ! NamedNodeMap atts = mapElement.getAttributes(); ! if ( "mapping".equals( mapElement.getNodeName() ) ) { Node rsrc = atts.getNamedItem("resource"); Node file = atts.getNamedItem("file"); *************** *** 671,679 **** } else { ! if (file==null) throw new HibernateException("<mapping> element in configuration specifies no attributes"); log.debug(name + "<-" + file); addFile( file.getNodeValue() ); } } } --- 689,726 ---- } else { ! if (file==null) throw new MappingException("<mapping> element in configuration specifies no attributes"); log.debug(name + "<-" + file); addFile( file.getNodeValue() ); } } + else if ( "jcs-class-cache".equals( mapElement.getNodeName() ) ) { + String className = atts.getNamedItem("class").getNodeValue(); + final Class clazz; + try { + clazz = ReflectHelper.classForName(className); + } + catch (ClassNotFoundException cnfe) { + throw new MappingException("Could not find class: " + className, cnfe); + } + RootClass pc; + try { + pc = (RootClass) getClassMapping(clazz); + } + catch (ClassCastException cce) { + throw new MappingException("You may only specify a cache for root <class> mappings"); + } + Node regionNode = atts.getNamedItem("region"); + String region = className; + if (regionNode!=null) region = regionNode.getNodeValue(); + pc.setCache( createJCSCache( atts.getNamedItem("usage").getNodeValue(), region, pc ) ); + } + else if ( "jcs-ccollection-cache".equals( mapElement.getNodeName() ) ) { + String role = atts.getNamedItem("collection").getNodeValue(); + Collection c = (Collection) getCollectionMapping(role); + Node regionNode = atts.getNamedItem("region"); + String region = role; + if (regionNode!=null) region = regionNode.getNodeValue(); + c.setCache( createJCSCache( atts.getNamedItem("usage").getNodeValue(), region, c.getOwner() ) ); + } } *************** *** 683,686 **** --- 730,764 ---- return this; + } + + static CacheConcurrencyStrategy createJCSCache(String usage, String name, PersistentClass owner) throws MappingException { + + log.info("creating JCS cache region: " + name + ", usage: " + usage); + + final Cache jcs; + try { + jcs = new JCSCache(); + } + catch (NoClassDefFoundError ncf) { + log.warn( "Could not instantiate cache - probably the JCS jar is missing", ncf ); + // continue with no cache + return null; + } + try { + jcs.setRegion(name); + } + catch (CacheException ce) { + throw new MappingException("Could not instantiate JCS",ce); + } + if ( usage.equals("read-only") ) { + if ( owner.isMutable() ) log.warn( "read-only cache configured for mutable: " + name ); + return new ReadOnlyCache(jcs); + } + else if ( usage.equals("read-write") ) { + return new ReadWriteCache(jcs); + } + else { + throw new MappingException("jcs-cache usage attribute should be read-write or read-only"); + } } Index: Mappings.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Mappings.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Mappings.java 27 Jan 2003 12:39:34 -0000 1.2 --- Mappings.java 2 Feb 2003 00:29:06 -0000 1.3 *************** *** 9,22 **** import net.sf.hibernate.MappingException; - import net.sf.hibernate.cache.Cache; - import net.sf.hibernate.cache.CacheConcurrencyStrategy; - import net.sf.hibernate.cache.CacheException; - import net.sf.hibernate.cache.JCSCache; - import net.sf.hibernate.cache.ReadOnlyCache; - import net.sf.hibernate.cache.ReadWriteCache; import net.sf.hibernate.mapping.Collection; import net.sf.hibernate.mapping.PersistentClass; import net.sf.hibernate.mapping.Table; - import net.sf.hibernate.util.StringHelper; /** --- 9,15 ---- *************** *** 27,33 **** public class Mappings { - public static final String ROOT_ROLE_NAME = StringHelper.EMPTY_STRING; - public static final char ROLE_SEPERATOR = '/'; - private static final Log log = LogFactory.getLog(Mappings.class); --- 20,23 ---- *************** *** 89,120 **** public String getDefaultCascade() { return defaultCascade; - } - - public CacheConcurrencyStrategy createJCSCache(String usage, String name, PersistentClass owner) throws MappingException { - final Cache jcs; - try { - jcs = new JCSCache(); - } - catch (NoClassDefFoundError ncf) { - log.warn( "Could not instantiate cache - probably the JCS jar is missing", ncf ); - // continue with no cache - return null; - } - try { - jcs.setClass(name); - } - catch (CacheException ce) { - throw new MappingException("Could not instantiate JCS",ce); - } - if ( usage.equals("read-only") ) { - if ( owner.isMutable() ) log.warn( "read-only cache configured for mutable: " + name ); - return new ReadOnlyCache(jcs); - } - else if ( usage.equals("read-write") ) { - return new ReadWriteCache(jcs); - } - else { - throw new MappingException("jcs-cache usage attribute should be read-write or read-only"); - } } --- 79,82 ---- |