diff --git a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java index ccb1675..ad20cc3 100644 --- a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java @@ -159,7 +159,7 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS return toReturn; } - + /** * new method * @param pContext @@ -169,66 +169,72 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS * @param token * @return */ - private String getRStudioGCoreEndpoint(PortalContext pContext, long userId, GCubeUser curUser, String scope, String token) { + private String getRStudioGCoreEndpoint(PortalContext pContext, long userId, GCubeUser curUser, String context, String token) { String toReturn = ""; UserManager um = new LiferayUserManager(); _log.debug("Checking if user " + curUser.getFullname() + " has already an RStudio Instance set ... "); String hostedOnSet = null; try { - //check if an RStudio instance was previously set (RETRO-COMPATIBILY) - if (um.readCustomAttr(userId, RSTUDIO_URL) != null && um.readCustomAttr(userId, RSTUDIO_URL).toString().compareTo("") != 0) { - _log.debug("User had already an RStudio Instance set, upgrading to new version "); - hostedOnSet = (String) um.readCustomAttr(userId, RSTUDIO_URL); - writeRStudioInstanceInScope(um, curUser, scope, hostedOnSet); - um.saveCustomAttr(userId, RSTUDIO_URL, ""); //reset the old value to blank so that from now on it will read from the new field - } - hostedOnSet = getUserRStudioInstances(um,curUser).get(scope); - _log.info("**** Checking if the user instance " + hostedOnSet+" exists in the VO for this VRE: " + scope); + Map userRStudioInstances = getUserRStudioInstances(um, curUser); + hostedOnSet = userRStudioInstances.get(context); + _log.info("**** Checking if the user instance " + hostedOnSet+" exists in the VO for this VRE: " + context); //if the instance exists and is still available in the given context - List gCoreResources = getRStudioGCoreEndpoints(scope); + List gCoreResources = getRStudioGCoreEndpoints(context); if (hostedOnSet != null && checkRStudioGCoreInstanceExistence(curUser, hostedOnSet, gCoreResources) ) { toReturn = getRConnectorURL(hostedOnSet, token); _log.info("User " + curUser.getFullname() + " has RStudio Instance set and is valid, returning rConnector URL " + toReturn); } else {//need to find the RStudio - _log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set or the instance previous set no longer exists, calculating allocation ... "); - HashMap rStudioDistributionMap = new HashMap<>(); - for (GCoreEndpoint res : gCoreResources) { - String hostedOn = extractGCoreEndpointHostAndPort(res); - rStudioDistributionMap.put(hostedOn, 0); + //check first if there is an instance allocated on another scope + _log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set for this context "+context + " check if she as an instance assigned on another context"); + if (! userRStudioInstances.isEmpty()) { + String theFirstInstance = userRStudioInstances.values().iterator().next(); + toReturn = getRConnectorURL(theFirstInstance, token); + _log.info("Found instance: "+ theFirstInstance); + writeRStudioInstanceInScope(um, curUser, context, theFirstInstance); + _log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set reused = " + theFirstInstance + " for new context " + context); } - List vreUsers = um.listUsersByGroup(pContext.getCurrentGroupId(getThreadLocalRequest()), false); - _log.debug("VRE " + scope + " has totalUsers = " + vreUsers.size()); - for (GCubeUser gCubeUser : vreUsers) { - if (getUserRStudioInstances(um,gCubeUser).get(scope) != null) { - String hostedOn = getUserRStudioInstances(um,gCubeUser).get(scope); - if (rStudioDistributionMap.containsKey(hostedOn)) { - int noToSet = rStudioDistributionMap.get(hostedOn)+1; - rStudioDistributionMap.put(hostedOn, noToSet); + else { //no instance allocated on another scope + _log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set or the instance previous set no longer exists, calculating allocation ... "); + + HashMap rStudioDistributionMap = new HashMap<>(); + for (GCoreEndpoint res : gCoreResources) { + String hostedOn = extractGCoreEndpointHostAndPort(res); + rStudioDistributionMap.put(hostedOn, 0); + } + List vreUsers = um.listUsersByGroup(pContext.getCurrentGroupId(getThreadLocalRequest()), false); + _log.debug("VRE " + context + " has totalUsers = " + vreUsers.size()); + for (GCubeUser gCubeUser : vreUsers) { + if (getUserRStudioInstances(um,gCubeUser).get(context) != null) { + String hostedOn = getUserRStudioInstances(um,gCubeUser).get(context); + if (rStudioDistributionMap.containsKey(hostedOn)) { + int noToSet = rStudioDistributionMap.get(hostedOn)+1; + rStudioDistributionMap.put(hostedOn, noToSet); + } } } - } - _log.debug("VRE - RStudio allocaiton map as follows: "); - int min = 0; - int i = 0; - String host2Select = ""; - for (String host : rStudioDistributionMap.keySet()) { - _log.debug("Host " + host + " has # users=" + rStudioDistributionMap.get(host)); - if (i==0) { - host2Select = host; - min = rStudioDistributionMap.get(host); - } else { - int usersNo = rStudioDistributionMap.get(host); - if (usersNo < min) { - _log.debug("Host " + host + " has LESS users than " + host2Select + " updating"); - host2Select = host; + _log.debug("VRE - RStudio allocaiton map as follows: "); + int min = 0; + int i = 0; + String host2Select = ""; + for (String host : rStudioDistributionMap.keySet()) { + _log.debug("Host " + host + " has # users=" + rStudioDistributionMap.get(host)); + if (i==0) { + host2Select = host; + min = rStudioDistributionMap.get(host); + } else { + int usersNo = rStudioDistributionMap.get(host); + if (usersNo < min) { + _log.debug("Host " + host + " has LESS users than " + host2Select + " updating"); + host2Select = host; + } } + i++; } - i++; + writeRStudioInstanceInScope(um, curUser, context, host2Select); + _log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set calculated = " + host2Select + " for context " + context); + toReturn = getRConnectorURL(host2Select, token); } - writeRStudioInstanceInScope(um, curUser, scope, host2Select); - _log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set calculated = " + host2Select + " for context " + scope); - toReturn = getRConnectorURL(host2Select, token); _log.debug("User " + curUser.getFullname() + " has RStudio gCore Instance set, returning rConnector URL " + toReturn); }