001 /*
002 * Copyright 2009-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
020 import griffon.core.*;
021 import groovy.lang.Script;
022 import groovy.util.FactoryBuilderSupport;
023
024 import java.util.LinkedHashMap;
025 import java.util.Map;
026 import java.util.UUID;
027
028 import static java.util.Collections.unmodifiableMap;
029
030 /**
031 * Base implementation of the {@code MVCGroup} interface
032 *
033 * @author Andres Almiray
034 * @since 0.9.4
035 */
036 public abstract class AbstractMVCGroup implements MVCGroup {
037 protected final GriffonApplication app;
038 protected final MVCGroupConfiguration configuration;
039 protected final String mvcId;
040 protected final Map<String, Object> members = new LinkedHashMap<String, Object>();
041 private boolean alive;
042 private final Object[] lock = new Object[0];
043 protected final Map<String, Object> scriptResults = new LinkedHashMap<String, Object>();
044
045 public AbstractMVCGroup(GriffonApplication app, MVCGroupConfiguration configuration, String mvcId, Map<String, Object> members) {
046 this.app = app;
047 this.configuration = configuration;
048 this.mvcId = mvcId == null ? configuration.getMvcType() + "-" + UUID.randomUUID().toString() : mvcId;
049 this.members.putAll(members);
050 this.alive = true;
051 }
052
053 public GriffonApplication getApp() {
054 return app;
055 }
056
057 public MVCGroupConfiguration getConfiguration() {
058 return configuration;
059 }
060
061 public String getMvcType() {
062 return configuration.getMvcType();
063 }
064
065 public String getMvcId() {
066 return mvcId;
067 }
068
069 public GriffonModel getModel() {
070 return (GriffonModel) getMember(GriffonModelClass.TYPE);
071 }
072
073 public GriffonView getView() {
074 return (GriffonView) getMember(GriffonViewClass.TYPE);
075 }
076
077 public GriffonController getController() {
078 return (GriffonController) getMember(GriffonControllerClass.TYPE);
079 }
080
081 public FactoryBuilderSupport getBuilder() {
082 return (FactoryBuilderSupport) getMember("builder");
083 }
084
085 public Object getMember(String name) {
086 checkIfAlive();
087 return members.get(name);
088 }
089
090 public Map<String, Object> getMembers() {
091 checkIfAlive();
092 return unmodifiableMap(members);
093 }
094
095 public void destroy() {
096 checkIfAlive();
097 app.getMvcGroupManager().destroyMVCGroup(mvcId);
098 members.clear();
099 synchronized (lock) {
100 alive = false;
101 }
102 }
103
104 public boolean isAlive() {
105 synchronized (lock) {
106 return alive;
107 }
108 }
109
110 protected void checkIfAlive() {
111 if (!isAlive()) {
112 throw new IllegalStateException("Group " + getMvcType() + ":" + mvcId + " has been destroyed already.");
113 }
114 }
115
116 public Object getScriptResult(String name) {
117 return scriptResults.get(name);
118 }
119
120 public void buildScriptMember(final String name) {
121 Object member = members.get(name);
122 if (!(member instanceof Script)) return;
123 final Script script = (Script) member;
124
125 // special case: view gets executed in the UI thread always
126 if ("view".equals(name)) {
127 UIThreadManager.getInstance().executeSync(new Runnable() {
128 public void run() {
129 scriptResults.put(name, getBuilder().build(script));
130 }
131 });
132 } else {
133 scriptResults.put(name, getBuilder().build(script));
134 }
135 }
136 }
|