com.canoo.dolphin.server.controller.ControllerHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dolphin-platform-server Show documentation
Show all versions of dolphin-platform-server Show documentation
The Dolphin Platform is a framework that implements the presentation model pattern and provides amodern way to create enterprise applications. The Platform provides several client implementations that all canbe used in combination with a general sever API.
/*
* Copyright 2015 Canoo Engineering AG.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.canoo.dolphin.server.controller;
import com.canoo.dolphin.BeanManager;
import com.canoo.dolphin.impl.ReflectionHelper;
import com.canoo.dolphin.server.DolphinAction;
import com.canoo.dolphin.server.DolphinModel;
import com.canoo.dolphin.server.Param;
import com.canoo.dolphin.server.container.ContainerManager;
import com.canoo.dolphin.server.container.ModelInjector;
import com.canoo.dolphin.server.impl.ServerControllerActionCallBean;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class ControllerHandler {
private final Map controllers = new HashMap<>();
private final Map controllerClassMapping = new HashMap<>();
private final Map models = new HashMap<>();
private final ContainerManager containerManager;
private final BeanManager beanManager;
public ControllerHandler(ContainerManager containerManager, BeanManager beanManager) {
this.containerManager = containerManager;
this.beanManager = beanManager;
}
public Object getControllerModel(String id) {
return models.get(id);
}
public String createController(String name) {
Class> controllerClass = ControllerRepository.getControllerClassForName(name);
final String id = UUID.randomUUID().toString();
Object instance = containerManager.createManagedController(controllerClass, new ModelInjector() {
@Override
public void inject(Object controller) {
attachModel(id, controller);
}
});
controllers.put(id, instance);
controllerClassMapping.put(id, controllerClass);
return id;
}
private void attachModel(String controllerId, Object controller) {
List allFields = getInheritedDeclaredFields(controller.getClass());
Field modelField = null;
for (Field field : allFields) {
if (field.isAnnotationPresent(DolphinModel.class)) {
if (modelField != null) {
throw new RuntimeException("More than one Model was found for controller " + controller.getClass().getName());
}
modelField = field;
}
}
if (modelField != null) {
Object model = beanManager.create(modelField.getType());
ReflectionHelper.setPrivileged(modelField, controller, model);
models.put(controllerId, model);
}
}
public void invokeAction(ServerControllerActionCallBean bean) throws InvokeActionException {
try {
final Object controller = controllers.get(bean.getControllerId());
final Class controllerClass = controllerClassMapping.get(bean.getControllerId());
final Method actionMethod = getActionMethod(controller, controllerClass, bean.getActionName());
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy