org/codehaus/griffon/runtime/core/AbstractGriffonArtifactScript.java

`AbstractGriffonArtifactScript.java`
`001 /* 002 * Copyright 2010-2012 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 groovy.lang.*; 021 import org.codehaus.griffon.runtime.builder.UberInterceptorMetaClass; 022 import org.codehaus.griffon.runtime.util.GriffonApplicationHelper; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 026 import java.io.InputStream; 027 import java.net.URL; 028 import java.util.Collections; 029 import java.util.List; 030 import java.util.Map; 031 import java.util.concurrent.Callable; 032 import java.util.concurrent.ExecutorService; 033 import java.util.concurrent.Future; 034 035 /** 036 * Base implementation of the GriffonArtifact interface for Script based artifacts. 037 * 038 * @author Andres Almiray 039 * @since 0.9.4 040 */ 041 public abstract class AbstractGriffonArtifactScript extends Script implements GriffonArtifact { 042 private GriffonApplication app; 043 private final Logger log; 044 private MetaClass myMetaClass; 045 private final ResourceLocator resourceLocator = new ResourceLocator(); 046 047 public AbstractGriffonArtifactScript(String type) { 048 log = LoggerFactory.getLogger("griffon.app." + type + "." + getClass().getName()); 049 } 050 051 public GriffonApplication getApp() { 052 return app; 053 } 054 055 public void setApp(GriffonApplication app) { 056 this.app = app; 057 } 058 059 public Object newInstance(Class clazz, String type) { 060 return GriffonApplicationHelper.newInstance(app, clazz, type); 061 } 062 063 public MetaClass getMetaClass() { 064 if (myMetaClass == null) { 065 Class clazz = getClass(); 066 myMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(clazz); 067 if (!(myMetaClass instanceof ExpandoMetaClass) || !(myMetaClass instanceof UberInterceptorMetaClass)) { 068 myMetaClass = new ExpandoMetaClass(clazz, true, true); 069 log.debug("Upgrading MetaClass to " + myMetaClass); 070 myMetaClass.initialize(); 071 GroovySystem.getMetaClassRegistry().setMetaClass(clazz, myMetaClass); 072 } 073 } 074 return myMetaClass; 075 } 076 077 public void setMetaClass(MetaClass metaClass) { 078 myMetaClass = metaClass; 079 GroovySystem.getMetaClassRegistry().setMetaClass(getClass(), metaClass); 080 } 081 082 public GriffonClass getGriffonClass() { 083 return app.getArtifactManager().findGriffonClass(getClass()); 084 } 085 086 public boolean isUIThread() { 087 return UIThreadManager.getInstance().isUIThread(); 088 } 089 090 // TODO @deprecated - remove before 1.0 091 public void execAsync(Runnable runnable) { 092 execInsideUIAsync(runnable); 093 } 094 095 // TODO @deprecated - remove before 1.0 096 public void execSync(Runnable runnable) { 097 execInsideUISync(runnable); 098 } 099 100 // TODO @deprecated - remove before 1.0 101 public void execOutside(Runnable runnable) { 102 execOutsideUI(runnable); 103 } 104 105 public void execInsideUIAsync(Runnable runnable) { 106 UIThreadManager.getInstance().executeAsync(runnable); 107 } 108 109 public void execInsideUISync(Runnable runnable) { 110 UIThreadManager.getInstance().executeSync(runnable); 111 } 112 113 public void execOutsideUI(Runnable runnable) { 114 UIThreadManager.getInstance().executeOutside(runnable); 115 } 116 117 public Future execFuture(ExecutorService executorService, Closure closure) { 118 return UIThreadManager.getInstance().executeFuture(executorService, closure); 119 } 120 121 public Future execFuture(Closure closure) { 122 return UIThreadManager.getInstance().executeFuture(closure); 123 } 124 125 public Future execFuture(ExecutorService executorService, Callable callable) { 126 return UIThreadManager.getInstance().executeFuture(executorService, callable); 127 } 128 129 public Future execFuture(Callable callable) { 130 return UIThreadManager.getInstance().executeFuture(callable); 131 } 132 133 public Logger getLog() { 134 return log; 135 } 136 137 public MVCGroup buildMVCGroup(String mvcType) { 138 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, Collections.<String, Object>emptyMap()); 139 } 140 141 public MVCGroup buildMVCGroup(String mvcType, String mvcName) { 142 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap()); 143 } 144 145 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType) { 146 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args); 147 } 148 149 public MVCGroup buildMVCGroup(String mvcType, Map<String, Object> args) { 150 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, null, args); 151 } 152 153 public MVCGroup buildMVCGroup(Map<String, Object> args, String mvcType, String mvcName) { 154 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args); 155 } 156 157 public MVCGroup buildMVCGroup(String mvcType, String mvcName, Map<String, Object> args) { 158 return getApp().getMvcGroupManager().buildMVCGroup(mvcType, mvcName, args); 159 } 160 161 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType) { 162 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, Collections.<String, Object>emptyMap()); 163 } 164 165 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType) { 166 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args); 167 } 168 169 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, Map<String, Object> args) { 170 return getApp().getMvcGroupManager().createMVCGroup(mvcType, null, args); 171 } 172 173 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName) { 174 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap()); 175 } 176 177 public List<? extends GriffonMvcArtifact> createMVCGroup(Map<String, Object> args, String mvcType, String mvcName) { 178 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args); 179 } 180 181 public List<? extends GriffonMvcArtifact> createMVCGroup(String mvcType, String mvcName, Map<String, Object> args) { 182 return getApp().getMvcGroupManager().createMVCGroup(mvcType, mvcName, args); 183 } 184 185 public void destroyMVCGroup(String mvcName) { 186 getApp().getMvcGroupManager().destroyMVCGroup(mvcName); 187 } 188 189 public void withMVCGroup(String mvcType, Closure handler) { 190 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler); 191 } 192 193 public void withMVCGroup(String mvcType, String mvcName, Closure handler) { 194 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler); 195 } 196 197 public void withMVCGroup(String mvcType, Map<String, Object> args, Closure handler) { 198 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler); 199 } 200 201 public void withMVCGroup(Map<String, Object> args, String mvcType, Closure handler) { 202 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler); 203 } 204 205 public void withMVCGroup(String mvcType, String mvcName, Map<String, Object> args, Closure handler) { 206 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler); 207 } 208 209 public void withMVCGroup(Map<String, Object> args, String mvcType, String mvcName, Closure handler) { 210 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler); 211 } 212 213 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, MVCClosure<M, V, C> handler) { 214 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, Collections.<String, Object>emptyMap(), handler); 215 } 216 217 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, String mvcName, MVCClosure<M, V, C> handler) { 218 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, Collections.<String, Object>emptyMap(), handler); 219 } 220 221 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(String mvcType, Map<String, Object> args, MVCClosure<M, V, C> handler) { 222 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler); 223 } 224 225 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVCGroup(Map<String, Object> args, String mvcType, MVCClosure<M, V, C> handler) { 226 getApp().getMvcGroupManager().withMVCGroup(mvcType, null, args, handler); 227 } 228 229 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) { 230 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler); 231 } 232 233 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) { 234 getApp().getMvcGroupManager().withMVCGroup(mvcType, mvcName, args, handler); 235 } 236 237 public InputStream getResourceAsStream(String name) { 238 return resourceLocator.getResourceAsStream(name); 239 } 240 241 public URL getResourceAsURL(String name) { 242 return resourceLocator.getResourceAsURL(name); 243 } 244 245 public List<URL> getResources(String name) { 246 return resourceLocator.getResources(name); 247 } 248 }`