Skip to content

Commit

Permalink
Make info service a singlenton (#404)
Browse files Browse the repository at this point in the history
* make info endpoint a singlenton

* make info service a singleton
  • Loading branch information
elopezcastro authored Oct 10, 2023
1 parent c71802c commit 62c3677
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
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

0 comments on commit 62c3677

Please sign in to comment.