use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class TestPropertyChange method testWithAnnotatedSimpleModel.
@Test
public void testWithAnnotatedSimpleModel() {
ServerModelStore serverModelStore = createServerModelStore();
final BeanManager manager = createBeanManager(serverModelStore);
final SimpleAnnotatedTestModel model = manager.create(SimpleAnnotatedTestModel.class);
final ListerResults<String> results = new ListerResults<>();
ValueChangeListener<String> myListener = new ValueChangeListener<String>() {
@SuppressWarnings("unchecked")
@Override
public void valueChanged(ValueChangeEvent<? extends String> evt) {
assertThat((Property<String>) evt.getSource(), is(model.getMyProperty()));
results.newValue = evt.getNewValue();
results.oldValue = evt.getOldValue();
results.listenerCalls++;
}
};
final Subscription subscription = model.getMyProperty().onChanged(myListener);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, nullValue());
model.getMyProperty().set("Hallo Property");
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is("Hallo Property"));
assertThat(results.oldValue, nullValue());
results.listenerCalls = 0;
model.getMyProperty().set("Hallo Property2");
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is("Hallo Property2"));
assertThat(results.oldValue, is("Hallo Property"));
results.listenerCalls = 0;
subscription.unsubscribe();
model.getMyProperty().set("Hallo Property3");
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, is("Hallo Property2"));
assertThat(results.oldValue, is("Hallo Property"));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class TestPropertyChange method testWithSingleReferenceModel.
@Test
public void testWithSingleReferenceModel() {
ServerModelStore serverModelStore = createServerModelStore();
final BeanManager manager = createBeanManager(serverModelStore);
final SimpleTestModel ref1 = manager.create(SimpleTestModel.class);
final SimpleTestModel ref2 = manager.create(SimpleTestModel.class);
final SimpleTestModel ref3 = manager.create(SimpleTestModel.class);
final SingleReferenceModel model = manager.create(SingleReferenceModel.class);
final ListerResults<SimpleTestModel> results = new ListerResults<>();
final ValueChangeListener<SimpleTestModel> myListener = new ValueChangeListener<SimpleTestModel>() {
@SuppressWarnings("unchecked")
@Override
public void valueChanged(ValueChangeEvent<? extends SimpleTestModel> evt) {
assertThat((Property<SimpleTestModel>) evt.getSource(), is(model.getReferenceProperty()));
results.newValue = evt.getNewValue();
results.oldValue = evt.getOldValue();
results.listenerCalls++;
}
};
final Subscription subscription = model.getReferenceProperty().onChanged(myListener);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, nullValue());
model.getReferenceProperty().set(ref1);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is(ref1));
assertThat(results.oldValue, nullValue());
results.listenerCalls = 0;
model.getReferenceProperty().set(ref2);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is(ref2));
assertThat(results.oldValue, is(ref1));
results.listenerCalls = 0;
subscription.unsubscribe();
model.getReferenceProperty().set(ref3);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, is(ref2));
assertThat(results.oldValue, is(ref1));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class ControllerHandler method destroyController.
public void destroyController(final String id) {
Assert.requireNonBlank(id, "id");
final List<String> childControllerIds = parentChildRelations.remove(id);
if (childControllerIds != null && !childControllerIds.isEmpty()) {
for (final String childControllerId : childControllerIds) {
destroyController(childControllerId);
}
}
final Object controller = controllers.remove(id);
Assert.requireNonNull(controller, "controller");
final String parentControllerId = childToParentRelations.remove(id);
if (parentControllerId != null) {
final Object parentController = controllers.get(parentControllerId);
Assert.requireNonNull(parentController, "parentController");
firePreChildDestroyed(parentController, controller);
}
final Class controllerClass = controllerClassMapping.remove(id);
beanFactory.destroyDependentInstance(controller, controllerClass);
final Object model = models.remove(id);
if (model != null) {
beanRepository.delete(model);
}
final Subscription subscription = mBeanSubscriptions.remove(id);
if (subscription != null) {
subscription.unsubscribe();
}
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class AbstractEventBus method subscribe.
@Override
public <T extends Serializable> Subscription subscribe(final Topic<T> topic, final MessageListener<? super T> listener, final Predicate<MessageEventContext<T>> filter) {
checkInitialization();
Assert.requireNonNull(topic, "topic");
Assert.requireNonNull(listener, "listener");
final DolphinContext subscriptionContext = getCurrentContext();
if (subscriptionContext == null) {
throw new IllegalStateException("Subscription can only be done from Dolphin Context!");
}
final String subscriptionSessionId = subscriptionContext.getId();
LOG.trace("Adding subscription for topic {} in Dolphin Platform context {}", topic.getName(), subscriptionSessionId);
List<ListenerWithFilter<?>> listeners = topicToListenerMap.get(topic);
if (listeners == null) {
listeners = new CopyOnWriteArrayList<>();
topicToListenerMap.put(topic, listeners);
}
final ListenerWithFilter listenerWithFilter = new ListenerWithFilter(listener, filter);
listeners.add(listenerWithFilter);
listenerToSessionMap.put(listener, subscriptionSessionId);
final Subscription subscription = new Subscription() {
@Override
public void unsubscribe() {
LOG.trace("Removing subscription for topic {} in Dolphin Platform context {}", topic.getName(), subscriptionSessionId);
final List<ListenerWithFilter<?>> listeners = topicToListenerMap.get(topic);
if (listeners != null) {
listeners.remove(listenerWithFilter);
}
listenerToSessionMap.remove(listener);
removeSubscriptionForSession(this, subscriptionSessionId);
}
};
addSubscriptionForSession(subscription, subscriptionSessionId);
return subscription;
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class ControllerHandler method invokeAction.
public void invokeAction(final String controllerId, final String actionName, final Map<String, Object> params) throws InvokeActionException {
Assert.requireNonBlank(controllerId, "controllerId");
Assert.requireNonBlank(actionName, "actionName");
Assert.requireNonNull(params, "params");
final Object controller = controllers.get(controllerId);
final Class controllerClass = controllerClassMapping.get(controllerId);
final Subscription controllerContextSubscription = ContextManagerImpl.getInstance().addThreadContext(CONTROLLER_CONTEXT, Optional.ofNullable(controllerClass).map(c -> c.getSimpleName()).orElse(UNKNOWN_CONTROLLER_CONTEXT));
final Subscription controllerActionContextSubscription = ContextManagerImpl.getInstance().addThreadContext(CONTROLLER_ACTION_CONTEXT, actionName);
try {
if (controller == null) {
throw new InvokeActionException("No controller for id " + controllerId + " found");
}
if (controllerClass == null) {
throw new InvokeActionException("No controllerClass for id " + controllerId + " found");
}
final Method actionMethod = getActionMethod(controllerClass, actionName);
if (actionMethod == null) {
throw new InvokeActionException("No actionMethod with name " + actionName + " in controller class " + ControllerUtils.getControllerName(controllerClass) + " found");
}
final List<Object> args = getArgs(actionMethod, params);
LOG.debug("Will call {} action for controller {} ({}.{}) with {} params.", actionName, controllerId, controllerClass, ControllerUtils.getActionMethodName(actionMethod), args.size());
if (LOG.isTraceEnabled()) {
int index = 1;
for (final Object param : args) {
if (param != null) {
LOG.trace("Action param {}: {} with type {} is called with value \"{}\" and type {}", index, actionMethod.getParameters()[index - 1].getName(), actionMethod.getParameters()[index - 1].getType().getSimpleName(), param, param.getClass());
} else {
LOG.trace("Action param {}: {} with type {} is called with value null", index, actionMethod.getParameters()[index - 1].getName(), actionMethod.getParameters()[index - 1].getType().getSimpleName());
}
index++;
}
}
try {
ReflectionHelper.invokePrivileged(actionMethod, controller, args.toArray());
} catch (final DolphinRuntimeException e) {
if (e.getCause() instanceof InvocationTargetException) {
final InvocationTargetException invocationTargetException = (InvocationTargetException) e.getCause();
final Throwable internalException = invocationTargetException.getCause();
actionErrorHandler.handle(internalException, controller, ControllerUtils.getControllerName(controllerClass), actionName);
}
throw e;
}
} catch (final InvokeActionException e) {
throw e;
} catch (final Exception e) {
throw new InvokeActionException("Can not call action '" + actionName + "'", e);
} finally {
controllerContextSubscription.unsubscribe();
controllerActionContextSubscription.unsubscribe();
}
}
Aggregations