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));
}
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;
}
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);
}
}
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;
}
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));
}
Aggregations