Skip to content

Commit

Permalink
Fix dynamic route changes. (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
caalador authored and Denis committed Dec 13, 2018
1 parent e39aa02 commit 5b0e328
Showing 1 changed file with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import com.vaadin.flow.router.internal.RouteUtil;
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.server.startup.AbstractAnnotationValidator;
import com.vaadin.flow.server.startup.AbstractRouteRegistryInitializer;
import com.vaadin.flow.server.startup.AnnotationValidator;
import com.vaadin.flow.server.startup.RouteRegistry;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
import com.vaadin.flow.server.startup.ServletVerifier;
import com.vaadin.flow.spring.VaadinScanPackagesRegistrar.VaadinScanPackages;

Expand Down Expand Up @@ -79,25 +81,22 @@ private class RouteServletContextListener extends
@SuppressWarnings("unchecked")
@Override
public void contextInitialized(ServletContextEvent event) {
RouteRegistry registry = RouteRegistry
ApplicationRouteRegistry registry = ApplicationRouteRegistry
.getInstance(event.getServletContext());
if (registry.navigationTargetsInitialized()) {
return;
}

try {
List<Class<?>> routeClasses =
findByAnnotation(getRoutePackages(), Route.class,
RouteAlias.class).collect(Collectors.toList());
if (registry.getRegisteredRoutes().isEmpty()) {
try {
List<Class<?>> routeClasses = findByAnnotation(
getRoutePackages(), Route.class, RouteAlias.class).collect(Collectors.toList());

Set<Class<? extends Component>> navigationTargets =
validateRouteClasses(routeClasses.stream());
Set<Class<? extends Component>> navigationTargets = validateRouteClasses(
routeClasses.stream());

registry.setNavigationTargets(navigationTargets);
registry.setPwaConfigurationClass(
validatePwaClass(routeClasses.stream()));
} catch (InvalidRouteConfigurationException e) {
throw new IllegalStateException(e);
RouteUtil.setNavigationTargets(navigationTargets, registry);
registry.setPwaConfigurationClass(validatePwaClass(routeClasses.stream()));
} catch (InvalidRouteConfigurationException e) {
throw new IllegalStateException(e);
}
}
}

Expand All @@ -114,11 +113,8 @@ private class ErrorParameterServletContextListener
@Override
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
RouteRegistry registry = RouteRegistry
ApplicationRouteRegistry registry = ApplicationRouteRegistry
.getInstance(event.getServletContext());
if (registry.errorNavigationTargetsInitialized()) {
return;
}

Stream<Class<? extends Component>> hasErrorComponents = findBySuperType(
getErrorParameterPackages(), HasErrorParameter.class)
Expand Down Expand Up @@ -178,10 +174,11 @@ public void onStartup(ServletContext servletContext)
// Verify servlet version also for SpringBoot.
ServletVerifier.verifyServletVersion();

RouteRegistry registry = RouteRegistry.getInstance(servletContext);
ApplicationRouteRegistry registry = ApplicationRouteRegistry
.getInstance(servletContext);
// If the registry is already initialized then RouteRegistryInitializer
// has done its job already, skip the custom routes search
if (!registry.navigationTargetsInitialized()) {
if (registry.getRegisteredRoutes().isEmpty()) {
/*
* Don't rely on RouteRegistry.isInitialized() negative return value
* here because it's not known whether RouteRegistryInitializer has
Expand All @@ -194,12 +191,7 @@ public void onStartup(ServletContext servletContext)
servletContext.addListener(new RouteServletContextListener());
}

if (!registry.errorNavigationTargetsInitialized()) {
// Same thing: don't rely on hasNavigationTargets() negative return
// value
servletContext
.addListener(new ErrorParameterServletContextListener());
}
servletContext.addListener(new ErrorParameterServletContextListener());

servletContext
.addListener(new AnnotationValidatorServletContextListener());
Expand Down

0 comments on commit 5b0e328

Please sign in to comment.