/*
 * Copyright 2011 Vaadin Ltd.
 * 
 * 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.vaadin.server;

import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import com.vaadin.LegacyApplication;

public class LegacyVaadinPortlet extends VaadinPortlet {

    private static final LegacyApplicationUIProvider provider = new LegacyApplicationUIProvider() {
        @Override
        protected LegacyApplication createApplication() {
            VaadinPortlet portlet = VaadinPortlet.getCurrent();
            if (portlet instanceof LegacyVaadinPortlet) {
                LegacyVaadinPortlet legacyPortlet = (LegacyVaadinPortlet) portlet;
                PortletRequest request = VaadinPortletService
                        .getCurrentPortletRequest();
                if (legacyPortlet.shouldCreateApplication(request)) {
                    try {
                        return legacyPortlet.getNewApplication(request);
                    } catch (PortletException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            return null;
        }
    };

    @Override
    public void init() throws PortletException {
        super.init();

        getService().addVaadinSessionInitializationListener(
                new VaadinSessionInitializationListener() {
                    @Override
                    public void vaadinSessionInitialized(
                            VaadinSessionInitializeEvent event)
                            throws ServiceException {
                        try {
                            onVaadinSessionStarted(VaadinPortletRequest
                                    .cast(event.getRequest()),
                                    (VaadinPortletSession) event
                                            .getSession());
                        } catch (PortletException e) {
                            throw new ServiceException(e);
                        }
                    }
                });
    }

    protected Class<? extends LegacyApplication> getApplicationClass()
            throws ClassNotFoundException {
        try {
            return ServletPortletHelper
                    .getLegacyApplicationClass(getService());
        } catch (ServiceException e) {
            throw new RuntimeException(e);
        }
    }

    protected LegacyApplication getNewApplication(PortletRequest request)
            throws PortletException {
        try {
            Class<? extends LegacyApplication> applicationClass = getApplicationClass();
            return applicationClass.newInstance();
        } catch (Exception e) {
            throw new PortletException(e);
        }
    }

    private void onVaadinSessionStarted(VaadinPortletRequest request,
            VaadinPortletSession session) throws PortletException {
        getService().addUIProvider(session, provider);
    }

    protected boolean shouldCreateApplication(PortletRequest request) {
        return true;
    }
}

@@ -616,16 +616,52 @@
 erver;%0A%0A
+import javax.portlet.PortletConfig;%0A
 import j
@@ -691,16 +691,16 @@
 eption;%0A
-
 import j
@@ -1728,16 +1728,43 @@
 id init(
+PortletConfig portletConfig
 ) throws
@@ -1802,16 +1802,29 @@
 er.init(
+portletConfig
 );%0A%0A    
@@ -2379,53 +2379,8 @@
 vent
-%0A                                            
 .get
@@ -2701,32 +2701,32 @@
 %7B%0A        try %7B%0A
+
             retu
@@ -2748,37 +2748,16 @@
 etHelper
-%0A                    
 .getLega

Use init(PortletConfig) to avoid running init before super (#9728)