@Override public void onApplicationEvent(WebServerInitializedEvent event) { File portFile = getPortFile(event.getApplicationContext()); try { String port = String.valueOf(event.getWebServer().getPort()); createParentFolder(portFile); FileCopyUtils.copy(port.getBytes(), portFile); portFile.deleteOnExit(); } catch (Exception ex) { logger.warn(String.format("Cannot create port file %s", this.file)); } }
/** * Return the actual port file that should be written for the given application * context. The default implementation builds a file from the source file and the * application context namespace if available. * @param applicationContext the source application context * @return the file that should be written */ protected File getPortFile(ApplicationContext applicationContext) { String namespace = getServerNamespace(applicationContext); if (StringUtils.isEmpty(namespace)) { return this.file; } String name = this.file.getName(); String extension = StringUtils.getFilenameExtension(this.file.getName()); name = name.substring(0, name.length() - extension.length() - 1); if (isUpperCase(name)) { name = name + "-" + namespace.toUpperCase(Locale.ENGLISH); } else { name = name + "-" + namespace.toLowerCase(Locale.ENGLISH); } if (StringUtils.hasLength(extension)) { name = name + "." + extension; } return new File(this.file.getParentFile(), name); }