Search in sources :

Example 6 with Subscription

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"));
}
Also used : ValueChangeEvent(com.canoo.platform.remoting.ValueChangeEvent) ValueChangeListener(com.canoo.platform.remoting.ValueChangeListener) SimpleAnnotatedTestModel(com.canoo.impl.server.util.SimpleAnnotatedTestModel) ServerModelStore(com.canoo.dp.impl.server.legacy.ServerModelStore) Subscription(com.canoo.platform.core.functional.Subscription) BeanManager(com.canoo.platform.remoting.BeanManager) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.impl.server.util.AbstractDolphinBasedTest)

Example 7 with Subscription

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));
}
Also used : SingleReferenceModel(com.canoo.impl.server.util.SingleReferenceModel) ValueChangeEvent(com.canoo.platform.remoting.ValueChangeEvent) ValueChangeListener(com.canoo.platform.remoting.ValueChangeListener) ServerModelStore(com.canoo.dp.impl.server.legacy.ServerModelStore) SimpleTestModel(com.canoo.impl.server.util.SimpleTestModel) Subscription(com.canoo.platform.core.functional.Subscription) BeanManager(com.canoo.platform.remoting.BeanManager) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.impl.server.util.AbstractDolphinBasedTest)

Example 8 with Subscription

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();
    }
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription)

Example 9 with Subscription

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;
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) Subscription(com.canoo.platform.core.functional.Subscription)

Example 10 with 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();
    }
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) Method(java.lang.reflect.Method) Subscription(com.canoo.platform.core.functional.Subscription) InvocationTargetException(java.lang.reflect.InvocationTargetException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ValueConverterException(com.canoo.platform.remoting.spi.converter.ValueConverterException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Subscription (com.canoo.platform.core.functional.Subscription)30 Test (org.testng.annotations.Test)10 BeanManager (com.canoo.platform.remoting.BeanManager)7 ValueChangeEvent (com.canoo.platform.remoting.ValueChangeEvent)6 ValueChangeListener (com.canoo.platform.remoting.ValueChangeListener)6 TransformedPropertyImpl (com.canoo.dp.impl.reactive.TransformedPropertyImpl)4 AbstractDolphinBasedTest (com.canoo.dolphin.client.util.AbstractDolphinBasedTest)3 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)3 BeanRepository (com.canoo.dp.impl.remoting.BeanRepository)3 EventDispatcher (com.canoo.dp.impl.remoting.EventDispatcher)3 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)3 AbstractDolphinBasedTest (com.canoo.impl.server.util.AbstractDolphinBasedTest)3 Binding (com.canoo.platform.core.functional.Binding)3 SimpleTestModel (com.canoo.dolphin.client.util.SimpleTestModel)2 Assert (com.canoo.dp.impl.platform.core.Assert)2 ContextManagerImpl (com.canoo.dp.impl.platform.core.context.ContextManagerImpl)2 ObservableArrayList (com.canoo.dp.impl.remoting.collections.ObservableArrayList)2 SimpleTestModel (com.canoo.impl.server.util.SimpleTestModel)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2