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