Search in sources :

Example 26 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class DeploymentConfigurationFactoryTest method createInitParameters_valuesFromContextAreIgnored_valuesAreTakenFromservletConfig.

@Test
public void createInitParameters_valuesFromContextAreIgnored_valuesAreTakenFromservletConfig() throws Exception {
    DeploymentConfigurationFactory factory = new DeploymentConfigurationFactory();
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinConfig config = Mockito.mock(VaadinConfig.class);
    Mockito.when(config.getVaadinContext()).thenReturn(context);
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(config.getConfigParameterNames()).thenReturn(Collections.enumeration(Collections.singleton("foo")));
    Mockito.when(context.getContextParameterNames()).thenReturn(Collections.enumeration(Collections.singleton("bar")));
    Mockito.when(config.getConfigParameter("foo")).thenReturn("baz");
    Mockito.when(context.getContextParameter("bar")).thenReturn("foobar");
    Mockito.when(context.getAttribute(Mockito.eq(ApplicationConfiguration.class), Mockito.any())).thenReturn(appConfig);
    Properties parameters = factory.createInitParameters(Object.class, config);
    Assert.assertEquals("baz", parameters.get("foo"));
    Assert.assertFalse(parameters.contains("bar"));
}
Also used : Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Example 27 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class DevModeInitializer method initDevModeHandler.

/**
 * Initialize the devmode server if not in production mode or compatibility
 * mode.
 *
 * @param classes
 *            classes to check for npm- and js modules
 * @param context
 *            VaadinContext we are running in
 * @return the initialized dev mode handler or {@code null} if none was
 *         created
 *
 * @throws VaadinInitializerException
 *             if dev mode can't be initialized
 */
public static DevModeHandler initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
    ApplicationConfiguration config = ApplicationConfiguration.get(context);
    if (config.isProductionMode()) {
        log().debug("Skipping DEV MODE because PRODUCTION MODE is set.");
        return null;
    }
    if (!config.enableDevServer()) {
        log().debug("Skipping DEV MODE because dev server shouldn't be enabled.");
        return null;
    }
    // This needs to be set as there is no "current service" available in
    // this call
    FeatureFlags.get(context).setPropertiesLocation(config.getJavaResourceFolder());
    String baseDir = config.getStringProperty(FrontendUtils.PROJECT_BASEDIR, null);
    if (baseDir == null) {
        baseDir = getBaseDirectoryFallback();
    }
    // Initialize the usage statistics if enabled
    if (config.isUsageStatisticsEnabled()) {
        StatisticsStorage storage = new StatisticsStorage();
        DevModeUsageStatistics.init(baseDir, storage, new StatisticsSender(storage));
    }
    String generatedDir = System.getProperty(PARAM_GENERATED_DIR, Paths.get(config.getBuildFolder(), DEFAULT_GENERATED_DIR).toString());
    String frontendFolder = config.getStringProperty(PARAM_FRONTEND_DIR, System.getProperty(PARAM_FRONTEND_DIR, DEFAULT_FRONTEND_DIR));
    File flowResourcesFolder = new File(baseDir, config.getFlowResourcesFolder());
    Lookup lookupFromContext = context.getAttribute(Lookup.class);
    Lookup lookupForClassFinder = Lookup.of(new DevModeClassFinder(classes), ClassFinder.class);
    Lookup lookup = Lookup.compose(lookupForClassFinder, lookupFromContext);
    Builder builder = new NodeTasks.Builder(lookup, new File(baseDir), new File(generatedDir), new File(frontendFolder), config.getBuildFolder());
    log().info("Starting dev-mode updaters in {} folder.", builder.getNpmFolder());
    if (!builder.getGeneratedFolder().exists()) {
        try {
            FileUtils.forceMkdir(builder.getGeneratedFolder());
        } catch (IOException e) {
            throw new UncheckedIOException(String.format("Failed to create directory '%s'", builder.getGeneratedFolder()), e);
        }
    }
    File generatedPackages = new File(builder.getGeneratedFolder(), PACKAGE_JSON);
    // Regenerate webpack configuration, as it may be necessary to
    // update it
    // TODO: make sure target directories are aligned with build
    // config,
    // see https://github.com/vaadin/flow/issues/9082
    File target = new File(baseDir, config.getBuildFolder());
    builder.withWebpack(Paths.get(target.getPath(), "classes", VAADIN_WEBAPP_RESOURCES).toFile(), Paths.get(target.getPath(), "classes", VAADIN_SERVLET_RESOURCES).toFile());
    builder.useV14Bootstrap(config.useV14Bootstrap());
    if (!config.useV14Bootstrap() && isEndpointServiceAvailable(lookup)) {
        String connectJavaSourceFolder = config.getStringProperty(CONNECT_JAVA_SOURCE_FOLDER_TOKEN, Paths.get(baseDir, DEFAULT_CONNECT_JAVA_SOURCE_FOLDER).toString());
        String connectApplicationProperties = config.getStringProperty(CONNECT_APPLICATION_PROPERTIES_TOKEN, Paths.get(baseDir, DEFAULT_CONNECT_APPLICATION_PROPERTIES).toString());
        String connectOpenApiJsonFile = config.getStringProperty(CONNECT_OPEN_API_FILE_TOKEN, Paths.get(baseDir, config.getBuildFolder(), DEFAULT_CONNECT_OPENAPI_JSON_FILE).toString());
        builder.withEndpointSourceFolder(new File(connectJavaSourceFolder)).withApplicationProperties(new File(connectApplicationProperties)).withEndpointGeneratedOpenAPIFile(new File(connectOpenApiJsonFile));
    }
    // generate those
    if (!new File(builder.getNpmFolder(), PACKAGE_JSON).exists() || !generatedPackages.exists()) {
        builder.createMissingPackageJson(true);
    }
    Set<File> frontendLocations = getFrontendLocationsFromClassloader(DevModeStartupListener.class.getClassLoader());
    boolean useByteCodeScanner = config.getBooleanProperty(SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, Boolean.parseBoolean(System.getProperty(SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, Boolean.FALSE.toString())));
    boolean enablePnpm = config.isPnpmEnabled();
    boolean useGlobalPnpm = config.isGlobalPnpm();
    boolean useHomeNodeExec = config.getBooleanProperty(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE, false);
    String[] additionalPostinstallPackages = config.getStringProperty(InitParameters.ADDITIONAL_POSTINSTALL_PACKAGES, "").split(",");
    String frontendGeneratedFolder = config.getStringProperty(PROJECT_FRONTEND_GENERATED_DIR_TOKEN, Paths.get(baseDir, DEFAULT_PROJECT_FRONTEND_GENERATED_DIR).toString());
    JsonObject tokenFileData = Json.createObject();
    NodeTasks tasks = builder.enablePackagesUpdate(true).useByteCodeScanner(useByteCodeScanner).withFlowResourcesFolder(flowResourcesFolder).withFrontendGeneratedFolder(new File(frontendGeneratedFolder)).copyResources(frontendLocations).copyLocalResources(new File(baseDir, Constants.LOCAL_FRONTEND_RESOURCES_PATH)).enableImportsUpdate(true).runNpmInstall(true).populateTokenFileData(tokenFileData).withEmbeddableWebComponents(true).enablePnpm(enablePnpm).useGlobalPnpm(useGlobalPnpm).withHomeNodeExecRequired(useHomeNodeExec).withProductionMode(config.isProductionMode()).withPostinstallPackages(Arrays.asList(additionalPostinstallPackages)).build();
    Runnable runnable = () -> runNodeTasks(context, tokenFileData, tasks);
    CompletableFuture<Void> nodeTasksFuture = CompletableFuture.runAsync(runnable);
    Lookup devServerLookup = Lookup.compose(lookup, Lookup.of(config, ApplicationConfiguration.class));
    if (FeatureFlags.get(context).isEnabled(FeatureFlags.VITE)) {
        return new ViteHandler(devServerLookup, 0, builder.getNpmFolder(), nodeTasksFuture);
    } else {
        return new WebpackHandler(devServerLookup, 0, builder.getNpmFolder(), nodeTasksFuture);
    }
}
Also used : Builder(com.vaadin.flow.server.frontend.NodeTasks.Builder) JsonObject(elemental.json.JsonObject) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StatisticsSender(com.vaadin.base.devserver.stats.StatisticsSender) WebpackHandler(com.vaadin.base.devserver.WebpackHandler) ViteHandler(com.vaadin.base.devserver.ViteHandler) StatisticsStorage(com.vaadin.base.devserver.stats.StatisticsStorage) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) File(java.io.File) NodeTasks(com.vaadin.flow.server.frontend.NodeTasks)

Example 28 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class FeatureView method getFeatureFile.

private File getFeatureFile() {
    final VaadinContext context = VaadinSession.getCurrent().getService().getContext();
    final ApplicationConfiguration configuration = ApplicationConfiguration.get(context);
    final File file = new File(configuration.getJavaResourceFolder(), FeatureFlags.PROPERTIES_FILENAME);
    return file;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) File(java.io.File)

Example 29 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class ServletContextTestSetup method setServletContext.

@Override
public void setServletContext(ServletContext servletContext) {
    Lookup lookup = Mockito.mock(Lookup.class);
    servletContext.setAttribute(Lookup.class.getName(), lookup);
    ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
    servletContext.setAttribute(ApplicationConfiguration.class.getName(), applicationConfiguration);
}
Also used : Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 30 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class DefaultBindingExceptionHandlerTest method mockUI.

private UI mockUI(boolean productionMode) {
    UI ui = new UI();
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    ui.getInternals().setSession(session);
    Mockito.when(session.getService()).thenReturn(service);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(service.getContext()).thenReturn(context);
    ApplicationConfiguration configuration = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(context.getAttribute(Mockito.eq(ApplicationConfiguration.class), Mockito.any())).thenReturn(configuration);
    Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
    return ui;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Aggregations

ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)61 Properties (java.util.Properties)39 Test (org.junit.Test)39 VaadinContext (com.vaadin.flow.server.VaadinContext)14 Lookup (com.vaadin.flow.di.Lookup)9 VaadinService (com.vaadin.flow.server.VaadinService)7 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)6 ServletContext (javax.servlet.ServletContext)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)4 VaadinServletService (com.vaadin.flow.server.VaadinServletService)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 File (java.io.File)4 Before (org.junit.Before)4 UI (com.vaadin.flow.component.UI)3 ResourceProvider (com.vaadin.flow.di.ResourceProvider)3 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)3 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2