Search in sources :

Example 51 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class BrowserLiveReloadAccessorImplTest method getLiveReload_productionMode_nullIsReturned.

@Test
public void getLiveReload_productionMode_nullIsReturned() {
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
    Mockito.when(config.isProductionMode()).thenReturn(true);
    Assert.assertNull(access.getLiveReload(service));
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 52 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class VaadinService method init.

/**
 * Initializes this service. The service should be initialized before it is
 * used.
 *
 * @throws ServiceException
 *             if a problem occurs when creating the service
 */
public void init() throws ServiceException {
    doSetClassLoader();
    instantiator = createInstantiator();
    // init the router now so that registry will be available for
    // modifications
    router = new Router(getRouteRegistry());
    List<RequestHandler> handlers = createRequestHandlers();
    ServiceInitEvent event = new ServiceInitEvent(this);
    // allow service init listeners and DI to use thread local access to
    // e.g. application scoped route registry
    runWithServiceContext(() -> {
        instantiator.getServiceInitListeners().forEach(listener -> listener.serviceInit(event));
        event.getAddedRequestHandlers().forEach(handlers::add);
        Collections.reverse(handlers);
        requestHandlers = Collections.unmodifiableCollection(handlers);
        dependencyFilters = Collections.unmodifiableCollection(instantiator.getDependencyFilters(event.getAddedDependencyFilters()).collect(Collectors.toList()));
        bootstrapListeners = instantiator.getBootstrapListeners(event.getAddedBootstrapListeners()).collect(Collectors.toList());
        indexHtmlRequestListeners = instantiator.getIndexHtmlRequestListeners(event.getAddedIndexHtmlRequestListeners()).collect(Collectors.toList());
    });
    DeploymentConfiguration configuration = getDeploymentConfiguration();
    if (!configuration.isProductionMode()) {
        Logger logger = getLogger();
        logger.debug("The application has the following routes: ");
        List<RouteData> routeDataList = getRouteRegistry().getRegisteredRoutes();
        if (!routeDataList.isEmpty()) {
            addRouterUsageStatistics();
        }
        routeDataList.stream().map(Object::toString).forEach(logger::debug);
    }
    if (getDeploymentConfiguration().isPnpmEnabled()) {
        UsageStatistics.markAsUsed("flow/pnpm", null);
    }
    initialized = true;
}
Also used : StreamRequestHandler(com.vaadin.flow.server.communication.StreamRequestHandler) UidlRequestHandler(com.vaadin.flow.server.communication.UidlRequestHandler) SessionRequestHandler(com.vaadin.flow.server.communication.SessionRequestHandler) Router(com.vaadin.flow.router.Router) RouteData(com.vaadin.flow.router.RouteData) Logger(org.slf4j.Logger) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 53 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class FrontendUtils method getFileContent.

private static String getFileContent(VaadinService service, String path) throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    InputStream content = null;
    try {
        Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
        if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
            content = getFileFromDevModeHandler(devModeHandler.get(), path);
        }
        if (content == null) {
            content = getFileFromClassPath(service, path);
        }
        return content != null ? streamToString(content) : null;
    } finally {
        IOUtils.closeQuietly(content);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 54 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class FrontendUtils method getStatsAssetsByChunkName.

/**
 * Load the asset chunks from <code>stats.json</code>. We will only read the
 * file until we have reached the assetsByChunkName json and return that as
 * a json object string.
 *
 * Note: The <code>stats.json</code> is cached when external stats is
 * enabled or <code>stats.json</code> is provided from the class path. To
 * clear the cache use {@link #clearCachedStatsContent(VaadinService)}.
 *
 * @param service
 *            the Vaadin service.
 * @return json for assetsByChunkName object in stats.json or {@code null}
 *         if stats.json not found or content not found.
 * @throws IOException
 *             if an I/O error occurs while creating the input stream.
 */
public static String getStatsAssetsByChunkName(VaadinService service) throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
    if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
        return streamToString(getResourceFromWebpack(devModeHandler.get(), "/assetsByChunkName", "getting assets by chunk name."));
    }
    InputStream resourceAsStream;
    if (config.isStatsExternal()) {
        resourceAsStream = getStatsFromExternalUrl(config.getExternalStatsUrl(), service.getContext());
    } else {
        resourceAsStream = getStatsFromClassPath(service);
    }
    if (resourceAsStream == null) {
        return null;
    }
    try (Scanner scan = new Scanner(resourceAsStream, StandardCharsets.UTF_8.name())) {
        StringBuilder assets = new StringBuilder();
        assets.append("{");
        // Scan until we reach the assetsByChunkName object line
        scanToAssetChunkStart(scan, assets);
        // Add lines until we reach the first } breaking the object
        while (scan.hasNextLine()) {
            String line = scan.nextLine().trim();
            if ("}".equals(line) || "},".equals(line)) {
                // Encountering } or }, means end of asset chunk
                return assets.append("}").toString();
            } else if (line.endsWith("}") || line.endsWith("},")) {
                return assets.append(line.substring(0, line.indexOf('}')).trim()).append("}").toString();
            } else if (line.contains("{")) {
                // should only contain key-value pairs.
                break;
            }
            assets.append(line);
        }
        getLogger().error("Could not parse assetsByChunkName from stats.json");
    }
    return null;
}
Also used : Scanner(java.util.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 55 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class DeploymentConfigurationFactoryTest method createInitParameters_readDevModeProperties.

@Test
public void createInitParameters_readDevModeProperties() throws Exception {
    FileUtils.writeLines(tokenFile, Arrays.asList("{", "\"pnpm.enable\": true,", "\"require.home.node\": true,", "}"));
    DeploymentConfiguration config = createConfig(Collections.singletonMap(PARAM_TOKEN_FILE, tokenFile.getPath()));
    Assert.assertEquals(Boolean.TRUE.toString(), config.getInitParameters().getProperty(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM));
    Assert.assertEquals(Boolean.TRUE.toString(), config.getInitParameters().getProperty(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE));
}
Also used : DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Aggregations

DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)63 Test (org.junit.Test)24 VaadinService (com.vaadin.flow.server.VaadinService)23 VaadinSession (com.vaadin.flow.server.VaadinSession)9 UI (com.vaadin.flow.component.UI)8 Before (org.junit.Before)8 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)6 URL (java.net.URL)6 Properties (java.util.Properties)6 VaadinContext (com.vaadin.flow.server.VaadinContext)5 PushConfiguration (com.vaadin.flow.component.PushConfiguration)4 HashMap (java.util.HashMap)4 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)3 Location (com.vaadin.flow.router.Location)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 Method (java.lang.reflect.Method)3 Set (java.util.Set)3 ServletConfig (javax.servlet.ServletConfig)3 ServletException (javax.servlet.ServletException)3 Div (com.vaadin.flow.component.html.Div)2