/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2012-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.osgi;

import com.vaadin.ui.UI;
import org.opennms.vaadin.extender.ApplicationFactory;
import org.osgi.service.blueprint.container.BlueprintContainer;

import java.util.Collections;
import java.util.Map;

/**
 * Creates an instance of a Vaadin UI object from the underlying OSGI container.
 * It is necessary that a bean-configuration of {@link #m_uiBeanName} exists inside the blueprint.xml.
 *
 * @author Markus von Rüden
 */
public class OnmsVaadinUIFactory implements ApplicationFactory {

    private final BlueprintContainer m_blueprintContainer;
    private final String m_uiBeanName;
    private final Class<? extends UI> m_uiClass;

    public OnmsVaadinUIFactory(Class<? extends UI> uiClass, BlueprintContainer blueprintContainer, String uiBeanName) {
        m_blueprintContainer = blueprintContainer;
        m_uiClass = uiClass;
        m_uiBeanName = uiBeanName;
    }

    @Override
    public Map<String, String> getAdditionalHeaders() {
        return Collections.emptyMap();
    }

    @Override
    public UI createUI() {
        UI ui = (UI) m_blueprintContainer.getComponentInstance(m_uiBeanName);
        return ui;
    }

    @Override
    public Class<? extends UI> getUIClass() {
        return m_uiClass;
    }
}

@@ -1238,25 +1238,79 @@
 ort 
-com.vaadin.ui.UI;
+java.util.Collections;%0Aimport java.util.Map;%0Aimport java.util.Objects;%0A
 %0Aimp
@@ -1429,59 +1429,149 @@
 er;%0A
-%0A
 import 
-java.util.Collections;%0Aimport java.util.Map
+org.osgi.service.blueprint.reflect.BeanMetadata;%0Aimport org.osgi.service.blueprint.reflect.ComponentMetadata;%0A%0Aimport com.vaadin.ui.UI
 ;%0A%0A/
@@ -1699,18 +1699,43 @@
 uration 
-of
+with a id which matches the
  %7B@link 
@@ -1743,20 +1743,18 @@
 m_uiBean
-Name
+Id
 %7D exists
@@ -1778,16 +1778,61 @@
 rint.xml
+ represented by %7B@link #m_blueprintContainer%7D
 .%0A *%0A * 
@@ -2014,28 +2014,26 @@
 ing m_uiBean
-Name
+Id
 ;%0A    privat
@@ -2184,20 +2184,18 @@
 g uiBean
-Name
+Id
 ) %7B%0A    
@@ -2221,16 +2221,39 @@
 ainer = 
+Objects.requireNonNull(
 blueprin
@@ -2258,24 +2258,25 @@
 intContainer
+)
 ;%0A        m_
@@ -2285,23 +2285,47 @@
 Class = 
+Objects.requireNonNull(
 uiClass
+)
 ;%0A      
@@ -2338,25 +2338,65 @@
 Bean
-Name = uiBeanName
+Id = Objects.requireNonNull(uiBeanId);%0A        validate()
 ;%0A  
@@ -2632,12 +2632,10 @@
 Bean
-Name
+Id
 );%0A 
@@ -2750,11 +2750,1089 @@
 s;%0A    %7D
+%0A%0A    /**%0A     * Verify that the current UIFactory is set up correctly.%0A     */%0A    private void validate() %7B%0A        // Verify that the uiBean is a subclass of UI%0A        final Object instance = m_blueprintContainer.getComponentInstance(m_uiBeanId);%0A        if (!(instance instanceof UI)) %7B%0A            throw new IllegalStateException(%22The bean with id %22 + m_uiBeanId + %22 must be of type %22 + com.vaadin.ui.UI.class);%0A        %7D%0A%0A        // Verify that the scope is prototype and NOT singleton%0A        final ComponentMetadata componentMetadata = Objects.requireNonNull(m_blueprintContainer.getComponentMetadata(m_uiBeanId));%0A        if (!(componentMetadata instanceof BeanMetadata)) %7B%0A            throw new IllegalStateException(%22The referenced id is not a bean%22);%0A        %7D%0A%0A        if (!BeanMetadata.SCOPE_PROTOTYPE.equals(((BeanMetadata) componentMetadata).getScope())) %7B%0A            throw new IllegalStateException(%22The scope of the defined bean with id %22 + m_uiBeanId + %22 must be %22 + BeanMetadata.SCOPE_PROTOTYPE + %22 but is %22 + BeanMetadata.SCOPE_SINGLETON);%0A        %7D%0A    %7D
 %0A%7D%0A

Add validation to be more responsive if the referenced UI bean is not defined correctly (e.g. prototype vs singleton scope)