001 /*
002 * Copyright 2010-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.codehaus.griffon.runtime.core;
018
019 import griffon.core.*;
020 import groovy.lang.*;
021 import org.codehaus.griffon.runtime.builder.UberInterceptorMetaClass;
022 import org.codehaus.griffon.runtime.util.GriffonApplicationHelper;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025
026 import java.io.InputStream;
027 import java.net.URL;
028 import java.util.Collections;
029 import java.util.List;
030 import java.util.Map;
031 import java.util.concurrent.Callable;
032 import java.util.concurrent.ExecutorService;
033 import java.util.concurrent.Future;
034
035 /**
036 * Base implementation of the GriffonArtifact interface for Script based artifacts.
037 *
038 * @author Andres Almiray
039 * @since 0.9.4
040 */
041 public abstract class AbstractGriffonArtifactScript extends Script implements GriffonArtifact {
042 private GriffonApplication app;
043 private final Logger log;
044 private MetaClass myMetaClass;
045 private final ResourceLocator resourceLocator = new ResourceLocator();
046
047 public AbstractGriffonArtifactScript(String type) {
048 log = LoggerFactory.getLogger("griffon.app." + type + "." + getClass().getName());
049 }
050
051 public GriffonApplication getApp() {
052 return app;
053 }
054
055 public void setApp(GriffonApplication app) {
056 this.app = app;
057 }
058
059 public Object newInstance(Class clazz, String type) {
060 return GriffonApplicationHelper.newInstance(app, clazz, type);
061 }
062
063 public MetaClass getMetaClass() {
064 if (myMetaClass == null) {
065 Class clazz = getClass();
066 myMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(clazz);
067 if (!(myMetaClass instanceof ExpandoMetaClass) || !(myMetaClass instanceof UberInterceptorMetaClass)) {
068 myMetaClass = new ExpandoMetaClass(clazz, true, true);
069 log.debug("Upgrading MetaClass to " + myMetaClass);
070 myMetaClass.initialize();
071 GroovySystem.getMetaClassRegistry().setMetaClass(clazz, myMetaClass);
072 }
073 }
074 return myMetaClass;
075 }
076
077 public void setMetaClass(MetaClass metaClass) {
078 myMetaClass = metaClass;
079 GroovySystem.getMetaClassRegistry().setMetaClass(getClass(), metaClass);
080 }
081
082 public GriffonClass getGriffonClass() {
083 return app.getArtifactManager().findGriffonClass(getClass());
084 }
085
086 public boolean isUIThread() {
087 return UIThreadManager.getInstance().isUIThread();
088 }
089
090 public void execInsideUIAsync(Runnable runnable) {
091 UIThreadManager.getInstance().executeAsync(runnable);
092 }
093
094 public void execInsideUISync(Runnable runnable) {
095 UIThreadManager.getInstance().executeSync(runnable);
096 }
097
098 public void execOutsideUI(Runnable runnable) {
099 UIThreadManager.getInstance().executeOutside(runnable);
100 }
101
102 public <R> Future<R> execFuture(ExecutorService executorService, Closure<R> closure) {
103 return UIThreadManager.getInstance().executeFuture(executorService, closure);
104 }
105
106 public <R> Future<R> execFuture(Closure<R> closure) {
107 return UIThreadManager.getInstance().executeFuture(closure);
108 }
109
110 public <R> Future<R> execFuture(ExecutorService executorService, Callable<R> callable) {
111 return UIThreadManager.getInstance().executeFuture(executorService, callable);
112 }
113
114 public <R> Future<R> execFuture(Callable<R> callable) {
115 return UIThreadManager.getInstance().executeFuture(callable);
116 }
117
118 public Logger getLog() {
119 return log;
120 }
121
122 public MVCGroup buildMVCGroup(String mvcType) {
123 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
124 }
125
126 public MVCGroup buildMVCGroup(String mvcType, String mvcName) {
127 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
128 }
129
130 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType) {
131 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
132 }
133
134 public MVCGroup buildMVCGroup(String mvcType, Map<String, Object> args) {
135 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
136 }
137
138 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
139 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
140 }
141
142 public MVCGroup buildMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
143 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
144 }
145
146 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType) {
147 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
148 }
149
150 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType) {
151 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
152 }
153
154 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, Map<String, Object> args) {
155 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
156 }
157
158 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName) {
159 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
160 }
161
162 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
163 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
164 }
165
166 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
167 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
168 }
169
170 public void destroyMVCGroup(String mvcName) {
171 getApp().getMvcGroupManager().destroyMVCGroup(mvcName);
172 }
173
174 public void withMVCGroup(String mvcType, Closure handler) {
175 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
176 }
177
178 public void withMVCGroup(String mvcType, String mvcName, Closure handler) {
179 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
180 }
181
182 public void withMVCGroup(String mvcType, Map<String, Object> args, Closure handler) {
183 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
184 }
185
186 public void withMVCGroup(Map<String, Object> args, String mvcType, Closure handler) {
187 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
188 }
189
190 public void withMVCGroup(String mvcType, String mvcName, Map<String, Object> args, Closure handler) {
191 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
192 }
193
194 public void withMVCGroup(Map<String, Object> args, String mvcType, String mvcName, Closure handler) {
195 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
196 }
197
198 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, MVCClosure<M, V, C> handler) {
199 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
200 }
201
202 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, String mvcName, MVCClosure<M, V, C> handler) {
203 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
204 }
205
206 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, Map<String, Object> args, MVCClosure<M, V, C> handler) {
207 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
208 }
209
210 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(Map<String, Object> args, String mvcType, MVCClosure<M, V, C> handler) {
211 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
212 }
213
214 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, String mvcName, Map<String, Object> args, MVCClosure<M, V, C> handler) {
215 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
216 }
217
218 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(Map<String, Object> args, String mvcType, String mvcName, MVCClosure<M, V, C> handler) {
219 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
220 }
221
222 public InputStream getResourceAsStream(String name) {
223 return resourceLocator.getResourceAsStream(name);
224 }
225
226 public URL getResourceAsURL(String name) {
227 return resourceLocator.getResourceAsURL(name);
228 }
229
230 public List<URL> getResources(String name) {
231 return resourceLocator.getResources(name);
232 }
233 }
|