Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make info service a singlenton #404

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class CoreDataServicesModule extends PrivateModule
@Override
protected void configure()
{

bind(ProjectsService.class).to(ProjectsServiceImpl.class);
bind(InfoService.class);

expose(ProjectsService.class);
expose(InfoService.class);
Expand All @@ -48,4 +46,11 @@ public DependencyOverride initialiseDependencyCache()
{
return new DependencyUtil();
}

@Provides
@Singleton
public InfoService initInfo()
{
return new InfoService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ public class InfoService
private static final Logger LOGGER = LoggerFactory.getLogger(InfoService.class);
private static final String PLATFORM_VERSION_FILE = "version.json";
private static final ObjectMapper JSON = new ObjectMapper();
private static final long MEGABYTE = 1024L * 1024L;

private ServerInfo serverInfo;
private final ServerInfo serverInfo;


@Inject
public InfoService()
{
String hostName = tryGetValue(InfoService::getLocalHostName);
InfoService.PlatformVersionInfo platformVersionInfo = tryGetValue(InfoService::getPlatformVersionInfo);
String hostName = tryGetValue(() -> getLocalHostName());
InfoService.PlatformVersionInfo platformVersionInfo = tryGetValue(() -> getPlatformVersionInfo());
this.serverInfo = new InfoService.ServerInfo(hostName, platformVersionInfo);
}

private static <T> T tryGetValue(InfoService.SupplierWithException<T> supplier)
private <T> T tryGetValue(InfoService.SupplierWithException<T> supplier)
{
try
{
Expand All @@ -58,12 +57,12 @@ private static <T> T tryGetValue(InfoService.SupplierWithException<T> supplier)
}
}

private static String getLocalHostName() throws UnknownHostException
private String getLocalHostName() throws UnknownHostException
{
return InetAddress.getLocalHost().getHostName();
}

private static InfoService.PlatformVersionInfo getPlatformVersionInfo() throws IOException
private InfoService.PlatformVersionInfo getPlatformVersionInfo() throws IOException
{
URL url = InfoService.class.getClassLoader().getResource(PLATFORM_VERSION_FILE);
return url == null ? null : JSON.readValue(url, PlatformVersionInfo.class);
Expand Down Expand Up @@ -124,22 +123,16 @@ public String getBuildRevision()
}
}

public class ServerInfo
public static class ServerInfo
{
private final String hostName;
private final long totalMemory;
private final long maxMemory;
private final long usedMemory;
private final String serverTimeZone;
private final InfoService.ServerPlatformInfo platform;

private ServerInfo(String hostName, InfoService.PlatformVersionInfo platformVersionInfo)
{
this.hostName = hostName;
this.platform = platformVersionInfo == null ? new InfoService.ServerPlatformInfo() : new InfoService.ServerPlatformInfo(platformVersionInfo.version, platformVersionInfo.buildTime, platformVersionInfo.gitRevision);
this.totalMemory = Runtime.getRuntime().totalMemory() / MEGABYTE;
this.maxMemory = Runtime.getRuntime().maxMemory() / MEGABYTE;
this.usedMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MEGABYTE;
this.serverTimeZone = DatesHandler.ZONE_ID.getId();
}

Expand All @@ -148,21 +141,6 @@ public String getHostName()
return this.hostName;
}

public long getMaxMemory()
{
return maxMemory;
}

public long getUsedMemory()
{
return usedMemory;
}

public long getTotalMemory()
{
return totalMemory;
}

public InfoService.ServerPlatformInfo getPlatform()
{
return this.platform;
Expand Down
Loading