GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / persistence / org / codehaus / groovy / grails / orm / hibernate / cfg / DefaultGrailsDomainConfiguration.java
blobd095b02730366e9a1c09eccb6110ab4c82c379f0
1 /* Copyright 2004-2005 the original author or authors.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.codehaus.groovy.grails.orm.hibernate.cfg;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.Set;
21 import org.codehaus.groovy.grails.commons.GrailsApplication;
22 import org.codehaus.groovy.grails.commons.GrailsDomainClass;
23 import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler;
24 import org.codehaus.groovy.grails.commons.GrailsClass;
25 import org.hibernate.MappingException;
26 import org.hibernate.cfg.Configuration;
28 /**
29 * Creates runtime configuration mapping for the Grails domain classes
30 * based on the work done in the Hibernate Annotations project
32 * @author Graeme Rocher
33 * @since 06-Jul-2005
35 public class DefaultGrailsDomainConfiguration extends Configuration implements GrailsDomainConfiguration {
37 //private static final Log LOG = LogFactory.getLog(DefaultGrailsDomainConfiguration.class);
38 /**
41 private static final long serialVersionUID = -7115087342689305517L;
42 private GrailsApplication grailsApplication;
43 private Set domainClasses;
44 private boolean configLocked;
45 /**
48 public DefaultGrailsDomainConfiguration() {
49 super();
50 this.domainClasses = new HashSet();
53 /* (non-Javadoc)
54 * @see org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainConfiguration#addDomainClass(org.codehaus.groovy.grails.commons.GrailsDomainClass)
56 public GrailsDomainConfiguration addDomainClass( GrailsDomainClass domainClass ) {
57 if(domainClass.getMappingStrategy().equalsIgnoreCase( GrailsDomainClass.GORM )) {
58 this.domainClasses.add(domainClass);
61 return this;
63 /* (non-Javadoc)
64 * @see org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainConfiguration#setGrailsApplication(org.codehaus.groovy.grails.commons.GrailsApplication)
66 public void setGrailsApplication(GrailsApplication application) {
67 this.grailsApplication = application;
68 if(this.grailsApplication != null) {
69 GrailsClass[] existingDomainClasses = this.grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE);
70 for(int i = 0; i < existingDomainClasses.length;i++) {
71 addDomainClass((GrailsDomainClass)existingDomainClasses[i]);
76 /**
77 * Overrides the default behaviour to including binding of Grails
78 * domain classes
80 protected void secondPassCompile() throws MappingException {
81 if (configLocked) {
82 return;
84 // set the class loader to load Groovy classes
85 if(this.grailsApplication != null)
86 Thread.currentThread().setContextClassLoader( this.grailsApplication.getClassLoader() );
87 // do Grails class configuration
88 for(Iterator i = this.domainClasses.iterator();i.hasNext();) {
89 GrailsDomainClass domainClass = (GrailsDomainClass)i.next();
91 GrailsDomainBinder.bindClass(domainClass, super.createMappings());
94 // call super
95 super.secondPassCompile();
96 this.configLocked = true;