DefaultGriffonAddon.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.GriffonApplication;
020 import groovy.lang.*;
021 import groovy.util.FactoryBuilderSupport;
022 import org.codehaus.groovy.runtime.InvokerHelper;
023 
024 import java.util.Collections;
025 import java.util.List;
026 import java.util.Map;
027 
028 /**
029  @author Andres Almiray
030  @since 0.9.2
031  */
032 public class DefaultGriffonAddon extends AbstractGriffonAddon {
033     private final Object addonDelegate;
034 
035     public DefaultGriffonAddon(GriffonApplication app, Object addonDelegate) {
036         super(app, "griffon.addon" + addonDelegate.getClass().getName());
037         this.addonDelegate = addonDelegate;
038     }
039 
040     public void setApp(GriffonApplication app) {
041         MetaClass metaClass = InvokerHelper.getMetaClass(addonDelegate);
042         if (metaClass instanceof ExpandoMetaClass) {
043             ExpandoMetaClass mc = (ExpandoMetaClassmetaClass;
044             mc.registerBeanProperty("app", app);
045         }
046     }
047 
048     public Map<String, Object> getFactories() {
049         try {
050             return (Map<String, Object>InvokerHelper.getProperty(addonDelegate, "factories");
051         catch (MissingPropertyException mpe) {
052             return Collections.emptyMap();
053         }
054     }
055 
056     public Map<String, Closure> getMethods() {
057         try {
058             return (Map<String, Closure>InvokerHelper.getProperty(addonDelegate, "methods");
059         catch (MissingPropertyException mpe) {
060             return Collections.emptyMap();
061         }
062     }
063 
064     public Map<String, Map<String, Closure>> getProps() {
065         try {
066             return (Map<String, Map<String, Closure>>InvokerHelper.getProperty(addonDelegate, "props");
067         catch (MissingPropertyException mpe) {
068             return Collections.emptyMap();
069         }
070     }
071 
072     public Map<String, Closure> getEvents() {
073         try {
074             return (Map<String, Closure>InvokerHelper.getProperty(addonDelegate, "events");
075         catch (MissingPropertyException mpe) {
076             return Collections.emptyMap();
077         }
078     }
079 
080     public Map<String, Map<String, Object>> getMvcGroups() {
081         try {
082             return (Map<String, Map<String, Object>>InvokerHelper.getProperty(addonDelegate, "mvcGroups");
083         catch (MissingPropertyException mpe) {
084             return Collections.emptyMap();
085         }
086     }
087 
088     public List<Closure> getAttributeDelegates() {
089         try {
090             return (List<Closure>InvokerHelper.getProperty(addonDelegate, "attributeDelegates");
091         catch (MissingPropertyException mpe) {
092             return Collections.emptyList();
093         }
094     }
095 
096     public List<Closure> getPreInstantiateDelegates() {
097         try {
098             return (List<Closure>InvokerHelper.getProperty(addonDelegate, "preInstantiateDelegates");
099         catch (MissingPropertyException mpe) {
100             return Collections.emptyList();
101         }
102     }
103 
104     public List<Closure> getPostInstantiateDelegates() {
105         try {
106             return (List<Closure>InvokerHelper.getProperty(addonDelegate, "postInstantiateDelegates");
107         catch (MissingPropertyException mpe) {
108             return Collections.emptyList();
109         }
110     }
111 
112     public List<Closure> getPostNodeCompletionDelegates() {
113         try {
114             return (List<Closure>InvokerHelper.getProperty(addonDelegate, "postNodeCompletionDelegates");
115         catch (MissingPropertyException mpe) {
116             return Collections.emptyList();
117         }
118     }
119 
120     public void addonInit(GriffonApplication app) {
121         try {
122             InvokerHelper.invokeMethod(addonDelegate, "addonInit", app);
123         catch (MissingMethodException mme) {
124             if (!mme.getMethod().equals("addonInit")) {
125                 throw mme;
126             }
127         }
128     }
129 
130     public void addonPostInit(GriffonApplication app) {
131         try {
132             InvokerHelper.invokeMethod(addonDelegate, "addonPostInit", app);
133         catch (MissingMethodException mme) {
134             if (!mme.getMethod().equals("addonPostInit")) {
135                 throw mme;
136             }
137         }
138     }
139 
140     public void addonBuilderInit(GriffonApplication app, FactoryBuilderSupport builder) {
141         try {
142             InvokerHelper.invokeMethod(addonDelegate, "addonBuilderInit"new Object[]{app, builder});
143         catch (MissingMethodException mme) {
144             if (!mme.getMethod().equals("addonBuilderInit")) {
145                 throw mme;
146             }
147         }
148     }
149 
150     public void addonBuilderPostInit(GriffonApplication app, FactoryBuilderSupport builder) {
151         try {
152             InvokerHelper.invokeMethod(addonDelegate, "addonBuilderPostInit"new Object[]{app, builder});
153         catch (MissingMethodException mme) {
154             if (!mme.getMethod().equals("addonBuilderPostInit")) {
155                 throw mme;
156             }
157         }
158     }
159 }