com.antonjohansson.managementcenter.core.web.ManagementCenterServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.antonjohansson.managementcenter.core.web.ManagementCenterServlet.java

Source

/**
 * Copyright (c) Anton Johansson <antoon.johansson@gmail.com>
 *
 * 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.antonjohansson.managementcenter.core.web;

import javax.servlet.ServletException;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.vaadin.server.UIClassSelectionEvent;
import com.vaadin.server.UICreateEvent;
import com.vaadin.server.UIProvider;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;

/**
 * The Vaadin Servlet for the management center.
 *
 * @author Anton Johansson
 */
public class ManagementCenterServlet extends VaadinServlet {
    private Injector injector;

    @Inject
    void setInjector(Injector injector) {
        this.injector = injector;
    }

    @Override
    protected void servletInitialized() throws ServletException {
        if (injector == null) {
            // Currently two servlets are created, one that is scanned, and one that is
            // properly created through ManagementCenterInitializer.java
            return;
        }

        UIProvider provider = new ManagementCenterUIProvider();
        getService().addSessionInitListener(e -> {
            e.getSession().addUIProvider(provider);
        });
    }

    /**
     * Provides the core {@link UI}.
     *
     * @author Anton Johansson
     */
    private class ManagementCenterUIProvider extends UIProvider {
        @Override
        public UI createInstance(UICreateEvent event) {
            return injector.getInstance(ManagementCenterUI.class);
        }

        @Override
        public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
            return ManagementCenterUI.class;
        }
    }
}