AbstractGriffonArtifact.java
001 /*
002  * Copyright 2008-2016 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 package org.codehaus.griffon.runtime.core.artifact;
017 
018 import griffon.core.GriffonApplication;
019 import griffon.core.artifact.GriffonArtifact;
020 import griffon.core.artifact.GriffonClass;
021 import griffon.core.artifact.GriffonController;
022 import griffon.core.artifact.GriffonModel;
023 import griffon.core.artifact.GriffonMvcArtifact;
024 import griffon.core.artifact.GriffonView;
025 import griffon.core.i18n.NoSuchMessageException;
026 import griffon.core.mvc.MVCFunction;
027 import griffon.core.mvc.MVCGroup;
028 import griffon.core.mvc.MVCGroupFunction;
029 import org.slf4j.Logger;
030 import org.slf4j.LoggerFactory;
031 
032 import javax.annotation.Nonnull;
033 import javax.annotation.Nullable;
034 import javax.annotation.concurrent.GuardedBy;
035 import javax.inject.Inject;
036 import java.io.InputStream;
037 import java.net.URL;
038 import java.util.List;
039 import java.util.Map;
040 import java.util.concurrent.Callable;
041 import java.util.concurrent.ExecutorService;
042 import java.util.concurrent.Future;
043 
044 /**
045  * Base implementation of the GriffonArtifact interface.
046  *
047  @author Andres Almiray
048  @since 2.0.0
049  */
050 public abstract class AbstractGriffonArtifact implements GriffonArtifact {
051     private final Logger log;
052     private final Object lock = new Object[0];
053     @Inject
054     protected GriffonApplication application;
055     @GuardedBy("lock")
056     private GriffonClass griffonClass;
057 
058     public AbstractGriffonArtifact() {
059         log = LoggerFactory.getLogger("griffon.app." + getArtifactType() "." + getClass().getName());
060     }
061 
062     /**
063      * Creates a new instance of this class.
064      *
065      @param application the GriffonApplication that holds this artifact.
066      @deprecated Griffon prefers field injection over constructor injector for artifacts as of 2.1.0
067      */
068     @Inject
069     @Deprecated
070     public AbstractGriffonArtifact(@Nonnull GriffonApplication application) {
071         this();
072         this.application = application;
073     }
074 
075     @Nonnull
076     public GriffonApplication getApplication() {
077         return application;
078     }
079 
080     @Nonnull
081     @Override
082     @SuppressWarnings("ConstantConditions")
083     public GriffonClass getGriffonClass() {
084         synchronized (lock) {
085             if (griffonClass == null) {
086                 griffonClass = application.getArtifactManager().findGriffonClass(getClass());
087             }
088             return griffonClass;
089         }
090     }
091 
092     @Nonnull
093     @Override
094     public Logger getLog() {
095         return log;
096     }
097 
098     @Nonnull
099     protected abstract String getArtifactType();
100 
101     @Override
102     public boolean isUIThread() {
103         return application.getUIThreadManager().isUIThread();
104     }
105 
106     @Nonnull
107     @Override
108     public <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable) {
109         return application.getUIThreadManager().runFuture(executorService, callable);
110     }
111 
112     @Nonnull
113     @Override
114     public <R> Future<R> runFuture(@Nonnull Callable<R> callable) {
115         return application.getUIThreadManager().runFuture(callable);
116     }
117 
118     @Override
119     public void runInsideUISync(@Nonnull Runnable runnable) {
120         application.getUIThreadManager().runInsideUISync(runnable);
121     }
122 
123     @Override
124     public void runOutsideUI(@Nonnull Runnable runnable) {
125         application.getUIThreadManager().runOutsideUI(runnable);
126     }
127 
128     @Override
129     public void runInsideUIAsync(@Nonnull Runnable runnable) {
130         application.getUIThreadManager().runInsideUIAsync(runnable);
131     }
132 
133     @Nullable
134     @Override
135     public <R> R runInsideUISync(@Nonnull Callable<R> callable) {
136         return application.getUIThreadManager().runInsideUISync(callable);
137     }
138 
139     @Nonnull
140     @Override
141     public ClassLoader classloader() {
142         return application.getResourceHandler().classloader();
143     }
144 
145     @Nullable
146     @Override
147     public URL getResourceAsURL(@Nonnull String name) {
148         return application.getResourceHandler().getResourceAsURL(name);
149     }
150 
151     @Nullable
152     @Override
153     public List<URL> getResources(@Nonnull String name) {
154         return application.getResourceHandler().getResources(name);
155     }
156 
157     @Nullable
158     @Override
159     public InputStream getResourceAsStream(@Nonnull String name) {
160         return application.getResourceHandler().getResourceAsStream(name);
161     }
162 
163     @Nonnull
164     @Override
165     public MVCGroup createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
166         return application.getMvcGroupManager().createMVCGroup(args, mvcType);
167     }
168 
169     @Nonnull
170     @Override
171     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType) {
172         return application.getMvcGroupManager().createMVC(mvcType);
173     }
174 
175     @Override
176     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCFunction<M, V, C> handler) {
177         application.getMvcGroupManager().withMVC(mvcType, mvcId, handler);
178     }
179 
180     @Nonnull
181     @Override
182     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull String mvcId) {
183         return application.getMvcGroupManager().createMVC(mvcType, mvcId);
184     }
185 
186     @Nonnull
187     @Override
188     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
189         return application.getMvcGroupManager().createMVC(args, mvcType, mvcId);
190     }
191 
192     @Override
193     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCFunction<M, V, C> handler) {
194         application.getMvcGroupManager().withMVC(args, mvcType, mvcId, handler);
195     }
196 
197     @Nonnull
198     @Override
199     public MVCGroup createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
200         return application.getMvcGroupManager().createMVCGroup(args, mvcType, mvcId);
201     }
202 
203     @Nonnull
204     @Override
205     public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
206         return application.getMvcGroupManager().createMVCGroup(mvcType, mvcId, args);
207     }
208 
209     @Nonnull
210     @Override
211     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
212         return application.getMvcGroupManager().createMVC(args, mvcType);
213     }
214 
215     @Override
216     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull MVCFunction<M, V, C> handler) {
217         application.getMvcGroupManager().withMVC(mvcType, handler);
218     }
219 
220     @Nonnull
221     @Override
222     public MVCGroup createMVCGroup(@Nonnull String mvcType) {
223         return application.getMvcGroupManager().createMVCGroup(mvcType);
224     }
225 
226     @Nonnull
227     @Override
228     public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId) {
229         return application.getMvcGroupManager().createMVCGroup(mvcType, mvcId);
230     }
231 
232     @Nonnull
233     @Override
234     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
235         return application.getMvcGroupManager().createMVC(mvcType, args);
236     }
237 
238     @Override
239     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull MVCFunction<M, V, C> handler) {
240         application.getMvcGroupManager().withMVC(args, mvcType, handler);
241     }
242 
243     @Override
244     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull Map<String, Object> args, @Nonnull MVCFunction<M, V, C> handler) {
245         application.getMvcGroupManager().withMVC(mvcType, args, handler);
246     }
247 
248     @Nonnull
249     @Override
250     public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
251         return application.getMvcGroupManager().createMVCGroup(mvcType, args);
252     }
253 
254     @Nonnull
255     @Override
256     public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
257         return application.getMvcGroupManager().createMVC(mvcType, mvcId, args);
258     }
259 
260     @Override
261     public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args, @Nonnull MVCFunction<M, V, C> handler) {
262         application.getMvcGroupManager().withMVC(mvcType, mvcId, args, handler);
263     }
264 
265     @Override
266     public void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull MVCGroupFunction handler) {
267         application.getMvcGroupManager().withMVCGroup(args, mvcType, handler);
268     }
269 
270     @Override
271     public void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCGroupFunction handler) {
272         application.getMvcGroupManager().withMVCGroup(args, mvcType, mvcId, handler);
273     }
274 
275     @Override
276     public void withMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args, @Nonnull MVCGroupFunction handler) {
277         application.getMvcGroupManager().withMVCGroup(mvcType, args, handler);
278     }
279 
280     @Override
281     public void withMVCGroup(@Nonnull String mvcType, @Nonnull MVCGroupFunction handler) {
282         application.getMvcGroupManager().withMVCGroup(mvcType, handler);
283     }
284 
285     @Override
286     public void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args, @Nonnull MVCGroupFunction handler) {
287         application.getMvcGroupManager().withMVCGroup(mvcType, mvcId, args, handler);
288     }
289 
290     @Override
291     public void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCGroupFunction handler) {
292         application.getMvcGroupManager().withMVCGroup(mvcType, mvcId, handler);
293     }
294 
295     @Override
296     public void destroyMVCGroup(@Nonnull String mvcId) {
297         application.getMvcGroupManager().destroyMVCGroup(mvcId);
298     }
299 
300     /**
301      * Try to resolve the message.
302      *
303      @param key Key to lookup, such as 'log4j.appenders.console'
304      @return The resolved message at the given key for the default locale
305      @throws NoSuchMessageException if no message is found
306      @since 2.4.0
307      */
308     @Nonnull
309     protected String msg(@Nonnull String keythrows NoSuchMessageException {
310         return getApplication().getMessageSource().getMessage(key);
311     }
312 
313     /**
314      * Try to resolve the message.
315      *
316      @param key  Key to lookup, such as 'log4j.appenders.console'
317      @param args Arguments that will be filled in for params within the message (params look like "{0}" within a
318      *             message, but this might differ between implementations), or null if none.
319      @return The resolved message at the given key for the default locale
320      @throws NoSuchMessageException if no message is found
321      @since 2.4.0
322      */
323     @Nonnull
324     protected String msg(@Nonnull String key, @Nonnull List<?> argsthrows NoSuchMessageException {
325         return getApplication().getMessageSource().getMessage(key, args);
326     }
327 
328     /**
329      * Try to resolve the message.
330      *
331      @param key  Key to lookup, such as 'log4j.appenders.console'
332      @param args Arguments that will be filled in for params within the message (params look like "{0}" within a
333      *             message, but this might differ between implementations), or null if none.
334      @return The resolved message at the given key for the default locale
335      @throws NoSuchMessageException if no message is found
336      @since 2.4.0
337      */
338     @Nonnull
339     protected String msg(@Nonnull String key, @Nonnull Object[] argsthrows NoSuchMessageException {
340         return getApplication().getMessageSource().getMessage(key, args);
341     }
342 
343     /**
344      * Try to resolve the message.
345      *
346      @param key  Key to lookup, such as 'log4j.appenders.console'
347      @param args Arguments that will be filled in for params within the message (params look like "{:key}"
348      *             within a message, but this might differ between implementations), or null if none.
349      @return The resolved message at the given key for the default locale
350      @throws NoSuchMessageException if no message is found
351      @since 2.4.0
352      */
353     @Nonnull
354     protected String msg(@Nonnull String key, @Nonnull Map<String, Object> argsthrows NoSuchMessageException {
355         return getApplication().getMessageSource().getMessage(key, args);
356     }
357 }