com.vaadin.portlet.LegacyVaadinPortlet Maven / Gradle / Ivy
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import com.vaadin.server.LegacyApplication;
import com.vaadin.server.LegacyApplicationUIProvider;
import com.vaadin.server.ServiceException;
import com.vaadin.server.ServletPortletHelper;
import com.vaadin.server.SessionInitEvent;
import com.vaadin.util.ReflectTools;
@SuppressWarnings("deprecation")
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(PortletConfig portletConfig) throws PortletException {
super.init(portletConfig);
getService().addSessionInitListener((SessionInitEvent event) -> {
try {
onVaadinSessionStarted(
(VaadinPortletRequest) 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 ReflectTools.createInstance(applicationClass);
} catch (Exception e) {
throw new PortletException(e);
}
}
private void onVaadinSessionStarted(VaadinPortletRequest request,
VaadinPortletSession session) throws PortletException {
session.addUIProvider(PROVIDER);
}
protected boolean shouldCreateApplication(PortletRequest request) {
return true;
}
}