GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / grails / ui / Console.java
blobf92244afc42f2351aa02398f49968a60142f3688
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.
15 package grails.ui;
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;
25 import java.util.Map;
26 import java.util.Iterator;
28 /**
30 * Extends regular Groovy console and bootstraps Grails environment before launch
31 * to allow interaction with the Grails domain model
34 * @author Graeme Rocher
35 * @since 0.2
37 * @version $Revision$
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();
64 interceptor.init();
66 return null;
68 });
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();
74 interceptor.flush();
75 interceptor.destroy();
77 return null;
80 public Object call() {
81 return doCall();
84 public Object call(Object[] args) {
85 return doCall();
88 public Object call(Object arguments) {
89 return doCall();
91 });
92 c.run();