Java tutorial
package pl.psi.licensing.license.monitoring; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import pl.psi.licensing.UnlimitedInteger; import pl.psi.licensing.license.License; import pl.psi.licensing.license.ModuleLicense; import javax.ejb.Singleton; import java.util.*; import java.util.concurrent.TimeUnit; /** * Created by mblaszyk on 2016-07-25. */ @Singleton public class LicenseSessionRegistry { private Cache<String, LicenseSession> licenseSessionRegistry; public LicenseSessionRegistry() { licenseSessionRegistry = CacheBuilder.newBuilder().expireAfterAccess(24, TimeUnit.HOURS).build(); } /** * Used to request a license session */ public boolean requestLicenseSession(String sessionID, String module, String licenseType, UnlimitedInteger maxOfThisType) { boolean licensesAvailable = checkLicenseAvailability(module, licenseType, maxOfThisType); if (licensesAvailable) { LicenseSession requestedLicenseSession = new LicenseSession(sessionID, module, licenseType, new Date()); licenseSessionRegistry.put(sessionID, requestedLicenseSession); } return licensesAvailable; } private boolean checkLicenseAvailability(String module, String licenseType, UnlimitedInteger maxOfThisType) { int counter = getCountOfSpecificActiveLicenseSessions(module, licenseType); return maxOfThisType.intValue() > counter; } private int getCountOfSpecificActiveLicenseSessions(String module, String licenseType) { int counter = 0; for (LicenseSession ls : licenseSessionRegistry.asMap().values()) { if (ls.getModule().equals(module) && ls.getLicenseType().equals(licenseType)) { counter++; } } return counter; } /** * Used to free a license */ public void freeLicenseSession(String sessionId) { licenseSessionRegistry.invalidate(sessionId); } /** * Getter for a specific LicenseSession */ public LicenseSession getLicenseSession(String sessionId) { return licenseSessionRegistry.getIfPresent(sessionId); } /** * Verifies that license session exists */ public boolean checkSessionValid(String sessionId) { LicenseSession licenseSessionDuringVerification = getLicenseSession(sessionId); if (licenseSessionDuringVerification != null) { return true; } return false; } /**** * methods for use with admin endpoints ****/ public List<LicenseSession> getLicenseSessionForModule(String moduleName) { Collection<LicenseSession> vals = licenseSessionRegistry.asMap().values(); vals.removeIf(licenseSession -> !licenseSession.getModule().equals(moduleName)); return new ArrayList<LicenseSession>(vals); } public List<LicenseSession> getLicenseSessionAll() { return new ArrayList<LicenseSession>(licenseSessionRegistry.asMap().values()); } public int terminateSessionsForModule(String moduleName) { int initSize = (int) licenseSessionRegistry.size(); licenseSessionRegistry.asMap().entrySet().removeIf(e -> e.getValue().getModule().equals(moduleName)); licenseSessionRegistry.cleanUp(); return initSize - (int) licenseSessionRegistry.size(); } public int terminateSessionsAll() { int size = (int) licenseSessionRegistry.size(); licenseSessionRegistry.invalidateAll(); licenseSessionRegistry.cleanUp(); return size; } /** * Used when verifying licensing state after introducing a new license. */ public boolean verifyState(License newLicense) { List<ModuleLicense> moduleList = newLicense.getModules(); Collection<LicenseSession> vals = licenseSessionRegistry.asMap().values(); boolean flag = true; for (ModuleLicense module : moduleList) { flag &= verifyPerModule(module, vals); } return flag && vals.isEmpty(); } private boolean verifyPerModule(ModuleLicense module, Collection<LicenseSession> vals) { HashMap<String, UnlimitedInteger> licTypes = module.getFloatingIDontKnowTheRestOfTheName(); boolean flag = true; for (Map.Entry<String, UnlimitedInteger> entry : licTypes.entrySet()) { flag &= verifyPerLicenseType(module.getModuleName(), entry.getKey(), entry.getValue(), vals); } return flag; } private boolean verifyPerLicenseType(String moduleName, String licenseType, UnlimitedInteger licensedCount, Collection<LicenseSession> vals) { int counter = 0; Iterator<LicenseSession> i = vals.iterator(); LicenseSession ls; while (i.hasNext()) { ls = i.next(); if (ls.getModule().equals(moduleName) && ls.getLicenseType().equals(licenseType)) { counter++; i.remove(); } } return licensedCount.intValue() >= counter; } }