예제 #1
0
 @Override
 public UI createInstance(UICreateEvent uiCreateEvent) {
   Class<? extends UI> type = uiCreateEvent.getUIClass();
   int uiId = uiCreateEvent.getUiId();
   VaadinRequest request = uiCreateEvent.getRequest();
   Bean<?> bean = scanForBeans(type);
   String uiMapping = "";
   if (bean == null) {
     if (type.isAnnotationPresent(CDIUI.class)) {
       uiMapping = parseUIMapping(request);
       bean = getUIBeanWithMapping(uiMapping);
     } else {
       throw new IllegalStateException(
           "UI class: "
               + type.getName()
               + " with mapping: "
               + uiMapping
               + " is not annotated with CDIUI!");
     }
   }
   UIBean uiBean = new UIBean(bean, uiId);
   try {
     // Make the UIBean available to UIScopedContext when creating nested
     // injected objects
     CurrentInstance.set(UIBean.class, uiBean);
     return (UI)
         getBeanManager()
             .getReference(uiBean, type, getBeanManager().createCreationalContext(bean));
   } finally {
     CurrentInstance.set(UIBean.class, null);
   }
 }
  // Generic method that creates necessary VaadinSessin mock environment to be
  // able to test the BeanStoreRetrievalStrategy class.
  // To use it, call this method and write your test logic inside the
  // test method implemented from BeanStoreRetrievalStrategyTest interface.
  private synchronized void beanStoreTest(
      BeanStoreRetrievalStrategyTest test, boolean openVaadinSession) {
    WrappedSession wrappedSession = mock(WrappedSession.class);
    VaadinService vaadinService = mock(VaadinService.class);

    VaadinSession session = mock(VaadinSession.class);
    if (openVaadinSession) {
      when(session.getState()).thenReturn(VaadinSession.State.OPEN);
    } else {
      when(session.getState()).thenReturn(VaadinSession.State.CLOSED);
    }
    when(session.getSession()).thenReturn(wrappedSession);
    when(session.getService()).thenReturn(vaadinService);
    when(session.getSession().getId()).thenReturn(TEST_SESSION_ID);

    UIID uiid = new UIID(TEST_UIID);
    BeanStore beanStore = new BeanStore(TEST_BEAN_NAME);

    UIStore uiStore = mock(UIStore.class);
    when(session.getAttribute(UIStore.class)).thenReturn(uiStore);
    when(uiStore.getBeanStore(uiid)).thenReturn(beanStore);

    try {
      CurrentInstance.set(VaadinSession.class, session);
      CurrentInstance.set(UIID.class, uiid);

      test.test(session, uiStore, uiid);
    } finally {
      CurrentInstance.clearAll();
    }
  }
 @Override
 public UI createInstance(UICreateEvent event) {
   final Class<VaadinUIIdentifier> key = VaadinUIIdentifier.class;
   final VaadinUIIdentifier identifier = new VaadinUIIdentifier(event);
   CurrentInstance.set(key, identifier);
   try {
     logger.debug(
         "Creating a new UI bean of class [{}] with identifier [{}]",
         event.getUIClass().getCanonicalName(),
         identifier);
     return webApplicationContext.getBean(event.getUIClass());
   } finally {
     CurrentInstance.set(key, null);
   }
 }