001 /*
002 * Copyright 2008-2014 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.*;
020 import griffon.core.mvc.MVCCallable;
021 import griffon.core.mvc.MVCGroup;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024
025 import javax.annotation.Nonnull;
026 import javax.annotation.Nullable;
027 import javax.annotation.concurrent.GuardedBy;
028 import java.io.InputStream;
029 import java.net.URL;
030 import java.util.List;
031 import java.util.Map;
032 import java.util.concurrent.Callable;
033 import java.util.concurrent.ExecutorService;
034 import java.util.concurrent.Future;
035
036 import static java.util.Objects.requireNonNull;
037
038 /**
039 * Base implementation of the GriffonArtifact interface.
040 *
041 * @author Andres Almiray
042 * @since 2.0.0
043 */
044 public abstract class AbstractGriffonArtifact implements GriffonArtifact {
045 private final Logger log;
046 private final GriffonApplication application;
047 @GuardedBy("lock")
048 private GriffonClass griffonClass;
049 private final Object lock = new Object[0];
050
051 public AbstractGriffonArtifact(@Nonnull GriffonApplication application) {
052 this.application = requireNonNull(application, "Arguments 'application' must not be null");
053 log = LoggerFactory.getLogger("griffon.app." + getArtifactType() + "." + getClass().getName());
054 }
055
056 @Nonnull
057 public GriffonApplication getApplication() {
058 return application;
059 }
060
061 @Nonnull
062 @Override
063 @SuppressWarnings("ConstantConditions")
064 public GriffonClass getGriffonClass() {
065 synchronized (lock) {
066 if (griffonClass == null) {
067 griffonClass = application.getArtifactManager().findGriffonClass(getClass());
068 }
069 return griffonClass;
070 }
071 }
072
073 @Nonnull
074 @Override
075 public Logger getLog() {
076 return log;
077 }
078
079 @Nonnull
080 protected abstract String getArtifactType();
081
082 public boolean isUIThread() {
083 return application.getUIThreadManager().isUIThread();
084 }
085
086 @Nonnull
087 public <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable) {
088 return application.getUIThreadManager().runFuture(executorService, callable);
089 }
090
091 @Nonnull
092 public <R> Future<R> runFuture(@Nonnull Callable<R> callable) {
093 return application.getUIThreadManager().runFuture(callable);
094 }
095
096 public void runInsideUISync(@Nonnull Runnable runnable) {
097 application.getUIThreadManager().runInsideUISync(runnable);
098 }
099
100 public void runOutsideUI(@Nonnull Runnable runnable) {
101 application.getUIThreadManager().runOutsideUI(runnable);
102 }
103
104 public void runInsideUIAsync(@Nonnull Runnable runnable) {
105 application.getUIThreadManager().runInsideUIAsync(runnable);
106 }
107
108 @Nonnull
109 public ClassLoader classloader() {
110 return application.getResourceHandler().classloader();
111 }
112
113 @Nullable
114 public URL getResourceAsURL(@Nonnull String name) {
115 return application.getResourceHandler().getResourceAsURL(name);
116 }
117
118 @Nullable
119 public List<URL> getResources(@Nonnull String name) {
120 return application.getResourceHandler().getResources(name);
121 }
122
123 @Nullable
124 public InputStream getResourceAsStream(@Nonnull String name) {
125 return application.getResourceHandler().getResourceAsStream(name);
126 }
127
128 @Nonnull
129 public MVCGroup buildMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
130 return application.getMvcGroupManager().buildMVCGroup(args, mvcType);
131 }
132
133 @Nonnull
134 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull String mvcType) {
135 return application.getMvcGroupManager().createMVCGroup(mvcType);
136 }
137
138 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCCallable<M, V, C> handler) {
139 application.getMvcGroupManager().withMVCGroup(mvcType, mvcId, handler);
140 }
141
142 @Nonnull
143 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId) {
144 return application.getMvcGroupManager().createMVCGroup(mvcType, mvcId);
145 }
146
147 @Nonnull
148 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
149 return application.getMvcGroupManager().createMVCGroup(args, mvcType, mvcId);
150 }
151
152 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCCallable<M, V, C> handler) {
153 application.getMvcGroupManager().withMVCGroup(args, mvcType, mvcId, handler);
154 }
155
156 @Nonnull
157 public MVCGroup buildMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
158 return application.getMvcGroupManager().buildMVCGroup(args, mvcType, mvcId);
159 }
160
161 @Nonnull
162 public MVCGroup buildMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
163 return application.getMvcGroupManager().buildMVCGroup(mvcType, mvcId, args);
164 }
165
166 @Nonnull
167 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
168 return application.getMvcGroupManager().createMVCGroup(args, mvcType);
169 }
170
171 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull String mvcType, @Nonnull MVCCallable<M, V, C> handler) {
172 application.getMvcGroupManager().withMVCGroup(mvcType, handler);
173 }
174
175 @Nonnull
176 public MVCGroup buildMVCGroup(@Nonnull String mvcType) {
177 return application.getMvcGroupManager().buildMVCGroup(mvcType);
178 }
179
180 @Nonnull
181 public MVCGroup buildMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId) {
182 return application.getMvcGroupManager().buildMVCGroup(mvcType, mvcId);
183 }
184
185 @Nonnull
186 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
187 return application.getMvcGroupManager().createMVCGroup(mvcType, args);
188 }
189
190 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull MVCCallable<M, V, C> handler) {
191 application.getMvcGroupManager().withMVCGroup(args, mvcType, handler);
192 }
193
194 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args, @Nonnull MVCCallable<M, V, C> handler) {
195 application.getMvcGroupManager().withMVCGroup(mvcType, args, handler);
196 }
197
198 @Nonnull
199 public MVCGroup buildMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
200 return application.getMvcGroupManager().buildMVCGroup(mvcType, args);
201 }
202
203 @Nonnull
204 public List<? extends GriffonMvcArtifact> createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
205 return application.getMvcGroupManager().createMVCGroup(mvcType, mvcId, args);
206 }
207
208 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args, @Nonnull MVCCallable<M, V, C> handler) {
209 application.getMvcGroupManager().withMVCGroup(mvcType, mvcId, args, handler);
210 }
211
212 public void destroyMVCGroup(@Nonnull String mvcId) {
213 application.getMvcGroupManager().destroyMVCGroup(mvcId);
214 }
215 }
|