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.core.mvc;
017
018 import griffon.core.artifact.GriffonController;
019 import griffon.core.artifact.GriffonControllerClass;
020 import griffon.core.artifact.GriffonModel;
021 import griffon.core.artifact.GriffonModelClass;
022 import griffon.core.artifact.GriffonMvcArtifact;
023 import griffon.core.artifact.GriffonView;
024 import griffon.core.artifact.GriffonViewClass;
025 import griffon.core.mvc.MVCFunction;
026 import griffon.core.mvc.MVCGroup;
027 import griffon.core.mvc.MVCGroupFunction;
028 import griffon.core.mvc.MVCGroupConfiguration;
029 import griffon.core.mvc.MVCGroupManager;
030
031 import javax.annotation.Nonnull;
032 import javax.annotation.Nullable;
033 import java.util.LinkedHashMap;
034 import java.util.List;
035 import java.util.Map;
036 import java.util.UUID;
037
038 import static griffon.util.GriffonClassUtils.requireState;
039 import static griffon.util.GriffonNameUtils.isBlank;
040 import static griffon.util.GriffonNameUtils.requireNonBlank;
041 import static java.util.Collections.unmodifiableMap;
042 import static java.util.Objects.requireNonNull;
043
044 /**
045 * Base implementation of the {@code MVCGroup} interface
046 *
047 * @author Andres Almiray
048 * @since 2.0.0
049 */
050 public abstract class AbstractMVCGroup extends AbstractMVCHandler implements MVCGroup {
051 protected final MVCGroupConfiguration configuration;
052 protected final String mvcId;
053 protected final Map<String, Object> members = new LinkedHashMap<>();
054 private boolean alive;
055 private final Object[] lock = new Object[0];
056
057 public AbstractMVCGroup(@Nonnull MVCGroupManager mvcGroupManager, @Nonnull MVCGroupConfiguration configuration, @Nullable String mvcId, @Nonnull Map<String, Object> members) {
058 super(mvcGroupManager);
059 this.configuration = requireNonNull(configuration, "Argument 'configuration' must not be null");
060 this.mvcId = isBlank(mvcId) ? configuration.getMvcType() + "-" + UUID.randomUUID().toString() : mvcId;
061 this.members.putAll(requireNonNull(members, "Argument 'members' must not be null"));
062 this.alive = true;
063 }
064
065 @Nonnull
066 @Override
067 public MVCGroupConfiguration getConfiguration() {
068 return configuration;
069 }
070
071 @Nonnull
072 @Override
073 public String getMvcType() {
074 return configuration.getMvcType();
075 }
076
077 @Nonnull
078 @Override
079 public String getMvcId() {
080 return mvcId;
081 }
082
083 @Nullable
084 @Override
085 public GriffonModel getModel() {
086 return (GriffonModel) getMember(GriffonModelClass.TYPE);
087 }
088
089 @Nullable
090 @Override
091 public GriffonView getView() {
092 return (GriffonView) getMember(GriffonViewClass.TYPE);
093 }
094
095 @Nullable
096 @Override
097 public GriffonController getController() {
098 return (GriffonController) getMember(GriffonControllerClass.TYPE);
099 }
100
101 @Nullable
102 @Override
103 public Object getMember(@Nonnull String name) {
104 requireNonBlank(name, "Argument 'name' must not be blank");
105 checkIfAlive();
106 return members.get(name);
107 }
108
109 @Nonnull
110 @Override
111 public Map<String, Object> getMembers() {
112 checkIfAlive();
113 return unmodifiableMap(members);
114 }
115
116 @Override
117 public void destroy() {
118 if (isAlive()) {
119 getMvcGroupManager().destroyMVCGroup(mvcId);
120 members.clear();
121 synchronized (lock) {
122 alive = false;
123 }
124 }
125 }
126
127 @Override
128 public boolean isAlive() {
129 synchronized (lock) {
130 return alive;
131 }
132 }
133
134 protected void checkIfAlive() {
135 requireState(isAlive(), "Group " + getMvcType() + ":" + mvcId + " has been destroyed already.");
136 }
137
138 @Nonnull
139 @Override
140 public MVCGroup createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
141 return super.createMVCGroup(injectParentGroup(args), mvcType);
142 }
143
144 @Nonnull
145 @Override
146 public MVCGroup createMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
147 return super.createMVCGroup(injectParentGroup(args), mvcType, mvcId);
148 }
149
150 @Nonnull
151 @Override
152 public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
153 return super.createMVCGroup(mvcType, injectParentGroup(args));
154 }
155
156 @Nonnull
157 @Override
158 public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
159 return super.createMVCGroup(mvcType, mvcId, injectParentGroup(args));
160 }
161
162 @Nonnull
163 @Override
164 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType) {
165 return super.createMVC(injectParentGroup(args), mvcType);
166 }
167
168 @Nonnull
169 @Override
170 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId) {
171 return super.createMVC(injectParentGroup(args), mvcType, mvcId);
172 }
173
174 @Nonnull
175 @Override
176 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull Map<String, Object> args) {
177 return super.createMVC(mvcType, injectParentGroup(args));
178 }
179
180 @Nonnull
181 @Override
182 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args) {
183 return super.createMVC(mvcType, mvcId, injectParentGroup(args));
184 }
185
186 @Override
187 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull MVCFunction<M, V, C> handler) {
188 super.withMVC(injectParentGroup(args), mvcType, handler);
189 }
190
191 @Override
192 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCFunction<M, V, C> handler) {
193 super.withMVC(injectParentGroup(args), mvcType, mvcId, handler);
194 }
195
196 @Override
197 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull Map<String, Object> args, @Nonnull MVCFunction<M, V, C> handler) {
198 super.withMVC(mvcType, injectParentGroup(args), handler);
199 }
200
201 @Override
202 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args, @Nonnull MVCFunction<M, V, C> handler) {
203 super.withMVC(mvcType, mvcId, injectParentGroup(args), handler);
204 }
205
206 @Override
207 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull MVCFunction<M, V, C> handler) {
208 super.withMVC(mvcType, injectParentGroup(), handler);
209 }
210
211 @Override
212 public <M extends GriffonModel, V extends GriffonView, C extends GriffonController> void withMVC(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCFunction<M, V, C> handler) {
213 super.withMVC(mvcType, mvcId, injectParentGroup(), handler);
214 }
215
216 @Override
217 public void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull MVCGroupFunction handler) {
218 super.withMVCGroup(injectParentGroup(args), mvcType, handler);
219 }
220
221 @Override
222 public void withMVCGroup(@Nonnull Map<String, Object> args, @Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCGroupFunction handler) {
223 super.withMVCGroup(injectParentGroup(args), mvcType, mvcId, handler);
224 }
225
226 @Override
227 public void withMVCGroup(@Nonnull String mvcType, @Nonnull Map<String, Object> args, @Nonnull MVCGroupFunction handler) {
228 super.withMVCGroup(mvcType, injectParentGroup(args), handler);
229 }
230
231 @Override
232 public void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull Map<String, Object> args, @Nonnull MVCGroupFunction handler) {
233 super.withMVCGroup(mvcType, mvcId, injectParentGroup(args), handler);
234 }
235
236 @Override
237 public void withMVCGroup(@Nonnull String mvcType, @Nonnull MVCGroupFunction handler) {
238 super.withMVCGroup(mvcType, injectParentGroup(), handler);
239 }
240
241 @Override
242 public void withMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId, @Nonnull MVCGroupFunction handler) {
243 super.withMVCGroup(mvcType, mvcId, injectParentGroup(), handler);
244 }
245
246 @Nonnull
247 @Override
248 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType, @Nonnull String mvcId) {
249 return super.createMVC(mvcType, mvcId, injectParentGroup());
250 }
251
252 @Nonnull
253 @Override
254 public List<? extends GriffonMvcArtifact> createMVC(@Nonnull String mvcType) {
255 return super.createMVC(mvcType, injectParentGroup());
256 }
257
258 @Nonnull
259 @Override
260 public MVCGroup createMVCGroup(@Nonnull String mvcType, @Nonnull String mvcId) {
261 return super.createMVCGroup(mvcType, mvcId, injectParentGroup());
262 }
263
264 @Nonnull
265 @Override
266 public MVCGroup createMVCGroup(@Nonnull String mvcType) {
267 return super.createMVCGroup(mvcType, injectParentGroup());
268 }
269
270 @Nonnull
271 private Map<String, Object> injectParentGroup() {
272 return injectParentGroup(new LinkedHashMap<String, Object>());
273 }
274
275 @Nonnull
276 private Map<String, Object> injectParentGroup(@Nonnull Map<String, Object> args) {
277 Map<String, Object> map = new LinkedHashMap<>();
278 map.put("parentGroup", this);
279 map.putAll(args);
280 return map;
281 }
282 }
|