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-2017 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.event.Subscription;
import com.canoo.dolphin.impl.ReflectionHelper;
import com.canoo.dolphin.internal.BeanRepository;
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.ServerBeanBuilder;
import com.canoo.dolphin.server.impl.ServerControllerActionCallBean;
import com.canoo.dolphin.server.mbean.DolphinContextMBeanRegistry;
import com.canoo.dolphin.server.mbean.beans.ModelProvider;
import com.canoo.dolphin.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* This class wrapps the complete Dolphin Platform controller handling.
* It defines the methods to create or destroy controllers and to interact with them.
*/
public class ControllerHandler {
private static final Logger LOG = LoggerFactory.getLogger(ControllerHandler.class);
private final Map controllers = new HashMap<>();
private final Map controllerClassMapping = new HashMap<>();
private final Map mBeanSubscriptions = new HashMap<>();
private final Map models = new HashMap<>();
private final ContainerManager containerManager;
private final ServerBeanBuilder beanBuilder;
private final ControllerRepository controllerRepository;
private final DolphinContextMBeanRegistry mBeanRegistry;
private final BeanRepository beanRepository;
public ControllerHandler(DolphinContextMBeanRegistry mBeanRegistry, ContainerManager containerManager, ServerBeanBuilder beanBuilder, BeanRepository beanRepository, ControllerRepository controllerRepository) {
this.mBeanRegistry = Assert.requireNonNull(mBeanRegistry, "mBeanRegistry");
this.containerManager = Assert.requireNonNull(containerManager, "containerManager");
this.beanBuilder = Assert.requireNonNull(beanBuilder, "beanBuilder");
this.controllerRepository = Assert.requireNonNull(controllerRepository, "controllerRepository");
this.beanRepository = Assert.requireNonNull(beanRepository, "beanRepository");
}
public Object getControllerModel(String id) {
return models.get(id);
}
public String createController(final String name) {
Assert.requireNonBlank(name, "name");
Class> controllerClass = controllerRepository.getControllerClassForName(name);
if(controllerClass == null) {
throw new ControllerCreationException("Can not find controller class for name " + 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);
mBeanSubscriptions.put(id, mBeanRegistry.registerController(controllerClass, id, new ModelProvider() {
@Override
public Object getModel() {
return models.get(id);
}
}));
LOG.trace("Created Controller of type %s and id %s for name %s", controllerClass.getName(), id, name);
return id;
}
public void destroyController(String id) {
Object controller = controllers.remove(id);
Class controllerClass = controllerClassMapping.remove(id);
containerManager.destroyController(controller, controllerClass);
Object model = models.remove(id);
if (model != null) {
beanRepository.delete(model);
}
Subscription subscription = mBeanSubscriptions.remove(id);
if(subscription != null) {
subscription.unsubscribe();
}
}
public void destroyAllControllers() {
List currentControllerIds = new ArrayList<>(getAllControllerIds());
for(String id : currentControllerIds) {
destroyController(id);
}
}
private void attachModel(String controllerId, Object controller) {
List allFields = ReflectionHelper.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 = beanBuilder.createRootModel(modelField.getType());
ReflectionHelper.setPrivileged(modelField, controller, model);
models.put(controllerId, model);
}
}
public void invokeAction(ServerControllerActionCallBean bean) throws InvokeActionException {
Assert.requireNonNull(bean, "bean");
Assert.requireNonBlank(bean.getControllerId(), "bean.getControllerId()");
Assert.requireNonBlank(bean.getActionName(), "bean.getActionName()");
try {
final Object controller = controllers.get(bean.getControllerId());
if(controller == null) {
throw new InvokeActionException("No controller for id " + bean.getControllerId() + " found");
}
final Class controllerClass = controllerClassMapping.get(bean.getControllerId());
if(controllerClass == null) {
throw new InvokeActionException("No controllerClass for id " + bean.getControllerId() + " found");
}
final Method actionMethod = getActionMethod(controllerClass, bean.getActionName());
if(actionMethod == null) {
throw new InvokeActionException("No actionMethod with name " + bean.getActionName() + " in controller class " + controllerClass.getName() + " found");
}
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy