2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.codehaus
.groovy
.grails
.orm
.hibernate
.metaclass
;
18 import org
.codehaus
.groovy
.grails
.commons
.metaclass
.AbstractDynamicMethodInvocation
;
19 import org
.hibernate
.SessionFactory
;
20 import org
.springframework
.orm
.hibernate3
.HibernateTemplate
;
21 import org
.springframework
.util
.Assert
;
23 import java
.util
.regex
.Pattern
;
28 * @author Steven Devijver
31 public abstract class AbstractDynamicPersistentMethod
extends
32 AbstractDynamicMethodInvocation
{
34 private SessionFactory sessionFactory
= null;
35 private ClassLoader classLoader
= null;
37 public AbstractDynamicPersistentMethod(Pattern pattern
, SessionFactory sessionFactory
, ClassLoader classLoader
) {
39 this.sessionFactory
= sessionFactory
;
40 this.classLoader
= classLoader
;
43 protected HibernateTemplate
getHibernateTemplate() {
44 Assert
.notNull(sessionFactory
, "Session factory is required!");
45 return new HibernateTemplate(this.sessionFactory
);
48 public Object
invoke(Object target
, String methodName
, Object
[] arguments
) {
49 ClassLoader originalClassLoader
= Thread
.currentThread().getContextClassLoader();
50 Thread
.currentThread().setContextClassLoader(this.classLoader
);
51 Object returnValue
= doInvokeInternal(target
, arguments
);
52 Thread
.currentThread().setContextClassLoader(originalClassLoader
);
56 protected abstract Object
doInvokeInternal(Object target
, Object
[] arguments
);