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 c;pWARRANTIES 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.
17 import grails
.util
.GrailsUtil
;
18 import groovy
.lang
.Binding
;
19 import groovy
.lang
.Closure
;
21 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
;
22 import org
.codehaus
.groovy
.grails
.support
.PersistenceContextInterceptor
;
23 import org
.springframework
.context
.ApplicationContext
;
26 import java
.util
.Iterator
;
30 * Extends regular Groovy console and bootstraps Grails environment before launch
31 * to allow interaction with the Grails domain model
34 * @author Graeme Rocher
38 * First Created: 02-Jun-2006
39 * Last Updated: $Date$
42 public class Console
extends groovy
.ui
.Console
{
44 public Console(ClassLoader parent
, Binding binding
) {
45 super(parent
, binding
);
51 public static void main(String
[] args
) {
52 final ApplicationContext ctx
= GrailsUtil
.bootstrapGrailsFromClassPath();
53 GrailsApplication app
= (GrailsApplication
)ctx
.getBean(GrailsApplication
.APPLICATION_ID
);
55 Binding b
= new Binding();
56 b
.setVariable(GrailsApplication
.APPLICATION_ID
, app
);
57 b
.setVariable("ctx", ctx
);
58 Console c
= new Console(app
.getClassLoader(), b
);
59 c
.setBeforeExecution(new Closure(c
) {
60 public Object
doCall() {
61 Map beans
= ctx
.getBeansOfType(PersistenceContextInterceptor
.class);
62 for (Iterator i
= beans
.values().iterator(); i
.hasNext();) {
63 PersistenceContextInterceptor interceptor
= (PersistenceContextInterceptor
) i
.next();
69 c
.setAfterExecution(new Closure(c
) {
70 public Object
doCall() {
71 Map beans
= ctx
.getBeansOfType(PersistenceContextInterceptor
.class);
72 for (Iterator i
= beans
.values().iterator(); i
.hasNext();) {
73 PersistenceContextInterceptor interceptor
= (PersistenceContextInterceptor
) i
.next();
75 interceptor
.destroy();
80 public Object
call() {
84 public Object
call(Object
[] args
) {
88 public Object
call(Object arguments
) {