1 /* Copyright 2004-2005 the original author or authors.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package org
.codehaus
.groovy
.grails
.orm
.hibernate
.cfg
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
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
;
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
35 public class DefaultGrailsDomainConfiguration
extends Configuration
implements GrailsDomainConfiguration
{
37 //private static final Log LOG = LogFactory.getLog(DefaultGrailsDomainConfiguration.class);
41 private static final long serialVersionUID
= -7115087342689305517L;
42 private GrailsApplication grailsApplication
;
43 private Set domainClasses
;
44 private boolean configLocked
;
48 public DefaultGrailsDomainConfiguration() {
50 this.domainClasses
= new HashSet();
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
);
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
]);
77 * Overrides the default behaviour to including binding of Grails
80 protected void secondPassCompile() throws MappingException
{
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());
95 super.secondPassCompile();
96 this.configLocked
= true;