AbstractGriffonArtifact.java
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 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 == nullreturn 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 == nullapp = ApplicationHolder.getApplication();
082         return app.getArtifactManager().findGriffonClass(getClass());
083     }
084 
085     public boolean isUIThread() {
086         return UIThreadManager.getInstance().isUIThread();
087     }
088 
089     public void execInsideUIAsync(Runnable runnable) {
090         UIThreadManager.getInstance().executeAsync(runnable);
091     }
092 
093     public void execInsideUISync(Runnable runnable) {
094         UIThreadManager.getInstance().executeSync(runnable);
095     }
096 
097     public void execOutsideUI(Runnable runnable) {
098         UIThreadManager.getInstance().executeOutside(runnable);
099     }
100 
101     public <R> Future<R> execFuture(ExecutorService executorService, Closure<R> closure) {
102         return UIThreadManager.getInstance().executeFuture(executorService, closure);
103     }
104 
105     public <R> Future<R> execFuture(Closure<R> closure) {
106         return UIThreadManager.getInstance().executeFuture(closure);
107     }
108 
109     public <R> Future<R> execFuture(ExecutorService executorService, Callable<R> callable) {
110         return UIThreadManager.getInstance().executeFuture(executorService, callable);
111     }
112 
113     public <R> Future<R> execFuture(Callable<R> callable) {
114         return UIThreadManager.getInstance().executeFuture(callable);
115     }
116 
117     public Logger getLog() {
118         return log;
119     }
120 
121     public MVCGroup buildMVCGroup(String mvcType) {
122         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
123     }
124 
125     public MVCGroup buildMVCGroup(String mvcType, String mvcName) {
126         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
127     }
128 
129     public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType) {
130         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
131     }
132 
133     public MVCGroup buildMVCGroup(String mvcType, Map<String, Object> args) {
134         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args);
135     }
136 
137     public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
138         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
139     }
140 
141     public MVCGroup buildMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
142         return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args);
143     }
144 
145     public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType) {
146         return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, Collections.<String, Object>emptyMap());
147     }
148 
149     public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType) {
150         return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
151     }
152 
153     public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, Map<String, Object> args) {
154         return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args);
155     }
156 
157     public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName) {
158         return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap());
159     }
160 
161     public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType, String mvcName) {
162         return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
163     }
164 
165     public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName, Map<String, Object> args) {
166         return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args);
167     }
168 
169     public void destroyMVCGroup(String mvcName) {
170         getApp().getMvcGroupManager().destroyMVCGroup(mvcName);
171     }
172 
173     public void withMVCGroup(String mvcType, Closure handler) {
174         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
175     }
176 
177     public void withMVCGroup(String mvcType, String mvcName, Closure handler) {
178         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
179     }
180 
181     public void withMVCGroup(String mvcType, Map<String, Object> args, Closure handler) {
182         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
183     }
184 
185     public void withMVCGroup(Map<String, Object> args, String mvcType, Closure handler) {
186         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
187     }
188 
189     public void withMVCGroup(String mvcType, String mvcName, Map<String, Object> args, Closure handler) {
190         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
191     }
192 
193     public void withMVCGroup(Map<String, Object> args, String mvcType, String mvcName, Closure handler) {
194         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
195     }
196 
197     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, MVCClosure<M, V, C> handler) {
198         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler);
199     }
200 
201     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, String mvcName, MVCClosure<M, V, C> handler) {
202         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler);
203     }
204 
205     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, Map<String, Object> args, MVCClosure<M, V, C> handler) {
206         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
207     }
208 
209     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(Map<String, Object> args, String mvcType, MVCClosure<M, V, C> handler) {
210         getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler);
211     }
212 
213     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) {
214         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
215     }
216 
217     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) {
218         getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler);
219     }
220 
221     public InputStream getResourceAsStream(String name) {
222         return resourceLocator.getResourceAsStream(name);
223     }
224 
225     public URL getResourceAsURL(String name) {
226         return resourceLocator.getResourceAsURL(name);
227     }
228 
229     public List<URL> getResources(String name) {
230         return resourceLocator.getResources(name);
231     }
232 }