use of com.vaadin.flow.server.PWA in project flow by vaadin.
the class VaadinAppShellInitializer method init.
/**
* Initializes the {@link AppShellRegistry} for the application.
*
* @param classes
* a set of classes that matches the {@link HandlesTypes} set in
* this class.
* @param context
* the {@link VaadinContext}.
*/
@SuppressWarnings("unchecked")
public static void init(Set<Class<?>> classes, VaadinContext context) {
ApplicationConfiguration config = ApplicationConfiguration.get(context);
if (config.useV14Bootstrap()) {
return;
}
boolean disregardOffendingAnnotations = config.getBooleanProperty(Constants.ALLOW_APPSHELL_ANNOTATIONS, false);
AppShellRegistry registry = AppShellRegistry.getInstance(context);
registry.reset();
if (classes == null || classes.isEmpty()) {
return;
}
List<String> offendingAnnotations = new ArrayList<>();
classes.stream().sorted((a, b) -> registry.isShell(a) ? -1 : registry.isShell(b) ? 1 : 0).forEach(clz -> {
if (registry.isShell(clz)) {
registry.setShell((Class<? extends AppShellConfigurator>) clz);
getLogger().debug("Using {} class for configuring `index.html` response", clz.getName());
} else {
String error = registry.validateClass(clz);
if (error != null) {
offendingAnnotations.add(error);
}
}
});
if (!offendingAnnotations.isEmpty()) {
if (disregardOffendingAnnotations) {
boolean hasPwa = offendingAnnotations.stream().anyMatch(err -> err.matches(".*@PWA.*"));
String message = String.format(hasPwa ? ERROR_HEADER_OFFENDING_PWA : ERROR_HEADER_NO_SHELL, String.join("\n ", offendingAnnotations));
getLogger().error(message);
} else {
String message = String.format(ERROR_HEADER_NO_SHELL, String.join("\n ", offendingAnnotations));
throw new InvalidApplicationConfigurationException(message);
}
}
List<String> classesImplementingPageConfigurator = classes.stream().filter(clz -> PageConfigurator.class.isAssignableFrom(clz)).map(Class::getName).collect(Collectors.toList());
if (!classesImplementingPageConfigurator.isEmpty()) {
String message = String.join("\n - ", classesImplementingPageConfigurator);
if (registry.getShell() != null) {
message = String.format(ERROR_HEADER_OFFENDING_CONFIGURATOR, registry.getShell().getName(), message);
throw new InvalidApplicationConfigurationException(message);
} else {
message = String.format(ERROR_HEADER_NO_APP_CONFIGURATOR, message);
getLogger().error(message);
}
}
}
use of com.vaadin.flow.server.PWA in project flow by vaadin.
the class FullDependenciesScanner method discoverPwa.
private PwaConfiguration discoverPwa() {
try {
Class<? extends Annotation> loadedPWAAnnotation = getFinder().loadClass(PWA.class.getName());
Set<Class<?>> annotatedClasses = getFinder().getAnnotatedClasses(loadedPWAAnnotation);
if (annotatedClasses.isEmpty()) {
return new PwaConfiguration(useV14Bootstrap);
} else if (annotatedClasses.size() != 1) {
throw new IllegalStateException(ERROR_INVALID_PWA_ANNOTATION);
}
Class<?> hopefullyAppShellClass = annotatedClasses.iterator().next();
if (!useV14Bootstrap && !Arrays.stream(hopefullyAppShellClass.getInterfaces()).map(Class::getName).collect(Collectors.toList()).contains(AppShellConfigurator.class.getName())) {
throw new IllegalStateException(ERROR_INVALID_PWA_ANNOTATION);
}
Annotation pwa = annotationFinder.apply(hopefullyAppShellClass, loadedPWAAnnotation).get(0);
String name = getAnnotationValueAsString(pwa, "name");
String shortName = getAnnotationValueAsString(pwa, "shortName");
String description = getAnnotationValueAsString(pwa, "description");
String backgroundColor = getAnnotationValueAsString(pwa, "backgroundColor");
String themeColor = getAnnotationValueAsString(pwa, "themeColor");
String iconPath = getAnnotationValueAsString(pwa, "iconPath");
String manifestPath = getAnnotationValueAsString(pwa, "manifestPath");
String offlinePath = getAnnotationValueAsString(pwa, "offlinePath");
String display = getAnnotationValueAsString(pwa, "display");
String startPath = getAnnotationValueAsString(pwa, "startPath");
String[] offlineResources = (String[]) getAnnotationValue(pwa, "offlineResources");
boolean offline = (Boolean) getAnnotationValue(pwa, "offline");
// required in @PWA annotation
assert shortName != null;
return new PwaConfiguration(true, name, shortName, description, backgroundColor, themeColor, iconPath, manifestPath, offlinePath, display, startPath, offlineResources, offline, useV14Bootstrap);
} catch (ClassNotFoundException exception) {
throw new IllegalStateException("Could not load PWA annotation class", exception);
}
}
Aggregations