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