001 /*
002 * Copyright 2010-2012 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 griffon.util.ApplicationHolder;
021 import groovy.lang.Closure;
022 import groovy.lang.GroovyObjectSupport;
023 import groovy.lang.GroovySystem;
024 import groovy.lang.MetaClass;
025 import org.codehaus.griffon.runtime.util.GriffonApplicationHelper;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 import java.io.InputStream;
030 import java.net.URL;
031 import java.util.Collections;
032 import java.util.List;
033 import java.util.Map;
034 import java.util.concurrent.Callable;
035 import java.util.concurrent.ExecutorService;
036 import java.util.concurrent.Future;
037
038 /**
039 * Base implementation of the GriffonArtifact interface.
040 *
041 * @author Andres Almiray
042 * @since 0.9.1
043 */
044 public abstract class AbstractGriffonArtifact extends GroovyObjectSupport implements GriffonArtifact {
045 private GriffonApplication app;
046 private final Logger log;
047 private final ResourceLocator resourceLocator = new ResourceLocator();
048
049 public static MetaClass metaClassOf(GriffonArtifact artifact) {
050 if (artifact == null) return null;
051 return GriffonApplicationHelper.expandoMetaClassFor(artifact.getClass());
052 }
053
054 public AbstractGriffonArtifact() {
055 log = LoggerFactory.getLogger("griffon.app." + getArtifactType() + "." + getClass().getName());
056 }
057
058 protected abstract String getArtifactType();
059
060 public GriffonApplication getApp() {
061 return app;
062 }
063
064 public void setApp(GriffonApplication app) {
065 this.app = app;
066 }
067
068 public Object newInstance(Class clazz, String type) {
069 return GriffonApplicationHelper.newInstance(app, clazz, type);
070 }
071
072 public MetaClass getMetaClass() {
073 return metaClassOf(this);
074 }
075
076 public void setMetaClass(MetaClass metaClass) {
077 GroovySystem.getMetaClassRegistry().setMetaClass(getClass(), metaClass);
078 }
079
080 public GriffonClass getGriffonClass() {
081 if (app == null) app = ApplicationHolder.getApplication();
082 return app.getArtifactManager().findGriffonClass(getClass());
083 }
084
085 public boolean isUIThread() {
086 return UIThreadManager.getInstance().isUIThread();
087 }
088
089 // TODO @deprecated - remove before 1.0
090 public void execAsync(Runnable runnable) {
091 execInsideUIAsync(runnable);
092 }
093
094 // TODO @deprecated - remove before 1.0
095 public void execSync(Runnable runnable) {
096 execInsideUISync(runnable);
097 }
098
099 // TODO @deprecated - remove before 1.0
100 public void execOutside(Runnable runnable) {
101 execOutsideUI(runnable);
102 }
103
104 public void execInsideUIAsync(Runnable runnable) {
105 UIThreadManager.getInstance().executeAsync(runnable);
106 }
107
108 public void execInsideUISync(Runnable runnable) {
109 UIThreadManager.getInstance().executeSync(runnable);
110 }
111
112 public void execOutsideUI(Runnable runnable) {
113 UIThreadManager.getInstance().executeOutside(runnable);
114 }
115
116 public Future execFuture(ExecutorService executorService, Closure closure) {
117 return UIThreadManager.getInstance().executeFuture(executorService, closure);
118 }
119
120 public Future execFuture(Closure closure) {
121 return UIThreadManager.getInstance().executeFuture(closure);
122 }
123
124 public Future execFuture(ExecutorService executorService, Callable callable) {
125 return UIThreadManager.getInstance().executeFuture(executorService, callable);
126 }
127
128 public Future execFuture(Callable callable) {
129 return UIThreadManager.getInstance().executeFuture(callable);
130 }
131
132 public Logger getLog() {
133 return log;
134 }
135
136 public MVCGroup buildMVCGroup(String mvcType) {
137 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
138 }
139
140 public MVCGroup buildMVCGroup(String mvcType, String mvcName) {
141 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
142 }
143
144 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType) {
145 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
146 }
147
148 public MVCGroup buildMVCGroup(String mvcType, Map<String, Object> args) {
149 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
150 }
151
152 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
153 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
154 }
155
156 public MVCGroup buildMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
157 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
158 }
159
160 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType) {
161 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
162 }
163
164 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType) {
165 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
166 }
167
168 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, Map<String, Object> args) {
169 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
170 }
171
172 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName) {
173 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
174 }
175
176 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
177 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
178 }
179
180 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
181 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
182 }
183
184 public void destroyMVCGroup(String mvcName) {
185 getApp().getMvcGroupManager().destroyMVCGroup(mvcName);
186 }
187
188 public void withMVCGroup(String mvcType, Closure handler) {
189 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
190 }
191
192 public void withMVCGroup(String mvcType, String mvcName, Closure handler) {
193 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
194 }
195
196 public void withMVCGroup(String mvcType, Map<String, Object> args, Closure handler) {
197 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
198 }
199
200 public void withMVCGroup(Map<String, Object> args, String mvcType, Closure handler) {
201 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
202 }
203
204 public void withMVCGroup(String mvcType, String mvcName, Map<String, Object> args, Closure handler) {
205 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
206 }
207
208 public void withMVCGroup(Map<String, Object> args, String mvcType, String mvcName, Closure handler) {
209 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
210 }
211
212 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, MVCClosure<M, V, C> handler) {
213 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
214 }
215
216 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, String mvcName, MVCClosure<M, V, C> handler) {
217 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
218 }
219
220 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, Map<String, Object> args, MVCClosure<M, V, C> handler) {
221 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
222 }
223
224 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(Map<String, Object> args, String mvcType, MVCClosure<M, V, C> handler) {
225 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
226 }
227
228 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) {
229 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
230 }
231
232 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) {
233 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
234 }
235
236 public InputStream getResourceAsStream(String name) {
237 return resourceLocator.getResourceAsStream(name);
238 }
239
240 public URL getResourceAsURL(String name) {
241 return resourceLocator.getResourceAsURL(name);
242 }
243
244 public List<URL> getResources(String name) {
245 return resourceLocator.getResources(name);
246 }
247 }
|