001 /*
002 * Copyright 2008-2015 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.mvc;
017
018 import griffon.core.ApplicationClassLoader;
019 import griffon.core.GriffonApplication;
020 import griffon.core.artifact.GriffonArtifact;
021 import griffon.core.mvc.MVCGroup;
022 import griffon.util.BuilderCustomizer;
023 import griffon.util.CompositeBuilder;
024 import groovy.lang.Script;
025 import groovy.util.FactoryBuilderSupport;
026 import org.codehaus.griffon.runtime.core.mvc.DefaultMVCGroupManager;
027 import org.slf4j.Logger;
028 import org.slf4j.LoggerFactory;
029
030 import javax.annotation.Nonnull;
031 import javax.inject.Inject;
032 import java.util.Collection;
033 import java.util.Map;
034
035 import static griffon.core.GriffonExceptionHandler.sanitize;
036 import static griffon.util.AnnotationUtils.sortByDependencies;
037 import static org.codehaus.griffon.runtime.groovy.mvc.GroovyAwareMVCGroup.BUILDER;
038
039 /**
040 * @author Andres Almiray
041 */
042 public class GroovyAwareMVCGroupManager extends DefaultMVCGroupManager {
043 private static final Logger LOG = LoggerFactory.getLogger(DefaultMVCGroupManager.class);
044 private static final String BUILDER_CUSTOMIZER = "BuilderCustomizer";
045
046 @Inject
047 public GroovyAwareMVCGroupManager(@Nonnull GriffonApplication application, @Nonnull ApplicationClassLoader applicationClassLoader) {
048 super(application, applicationClassLoader);
049 }
050
051 @Nonnull
052 @Override
053 @SuppressWarnings("unchecked")
054 protected Map<String, Object> instantiateMembers(@Nonnull Map<String, ClassHolder> classMap, @Nonnull Map<String, Object> args) {
055 Map<String, Object> map = super.instantiateMembers(classMap, args);
056 FactoryBuilderSupport builder = createBuilder(getApplication());
057 map.put(BUILDER, builder);
058
059 for (Object member : map.values()) {
060 // all scripts get the builder as their binding
061 if (member instanceof Script) {
062 builder.getVariables().putAll(((Script) member).getBinding().getVariables());
063 ((Script) member).setBinding(builder);
064 }
065 }
066
067 return map;
068 }
069
070 @Nonnull
071 protected FactoryBuilderSupport createBuilder(@Nonnull GriffonApplication application) {
072 Collection<BuilderCustomizer> customizers = resolveBuilderCustomizers(application);
073 return new CompositeBuilder(customizers.toArray(new BuilderCustomizer[customizers.size()]));
074 }
075
076 @Override
077 @SuppressWarnings("ConstantConditions")
078 protected void adjustMvcArguments(@Nonnull MVCGroup group, @Nonnull Map<String, Object> args) {
079 super.adjustMvcArguments(group, args);
080 FactoryBuilderSupport builder = (FactoryBuilderSupport) group.getMember(BUILDER);
081 args.put(BUILDER, builder);
082 for (Map.Entry<String, Object> variable : args.entrySet()) {
083 builder.setVariable(variable.getKey(), variable.getValue());
084 }
085 }
086
087 @Override
088 @SuppressWarnings("unchecked")
089 protected void fillNonArtifactMemberProperties(@Nonnull String type, @Nonnull Object member, @Nonnull Map<String, Object> args) {
090 if (member instanceof Script) {
091 ((Script) member).getBinding().getVariables().putAll(args);
092 }
093 }
094
095 @Override
096 protected void initializeArtifactMember(@Nonnull MVCGroup group, @Nonnull String type, @Nonnull GriffonArtifact member, @Nonnull Map<String, Object> args) {
097 if (member instanceof Script) {
098 ((GroovyAwareMVCGroup) group).buildScriptMember(type);
099 } else {
100 super.initializeArtifactMember(group, type, member, args);
101 }
102 }
103
104 @Override
105 protected void initializeNonArtifactMember(@Nonnull MVCGroup group, @Nonnull String type, @Nonnull Object member, @Nonnull Map<String, Object> args) {
106 if (member instanceof Script) {
107 ((GroovyAwareMVCGroup) group).buildScriptMember(type);
108 } else {
109 super.initializeNonArtifactMember(group, type, member, args);
110 }
111 }
112
113 @Override
114 protected void destroyMembers(@Nonnull MVCGroup group, boolean fireDestructionEvents) {
115 super.destroyMembers(group, fireDestructionEvents);
116
117 try {
118 FactoryBuilderSupport builder = (FactoryBuilderSupport) group.getMember(BUILDER);
119 if (builder != null) {
120 builder.dispose();
121 builder.getVariables().clear();
122 }
123 } catch (Exception e) {
124 // TODO find out why this call breaks applet mode on shutdown
125 LOG.error("Application encountered an error while destroying group '" + group.getMvcId() + "'", sanitize(e));
126 }
127 }
128
129 @Nonnull
130 protected Collection<BuilderCustomizer> resolveBuilderCustomizers(@Nonnull GriffonApplication application) {
131 Collection<BuilderCustomizer> customizerInstances = application.getInjector().getInstances(BuilderCustomizer.class);
132 return sortByDependencies(customizerInstances, BUILDER_CUSTOMIZER, "customizer").values();
133 }
134 }
|