1 package org.jahia.security.license; 2 3 import java.util.Enumeration ; 4 import org.jahia.registries.ServicesRegistry; 5 import org.jahia.services.sites.JahiaSite; 6 import org.jahia.exceptions.JahiaException; 7 import org.jahia.resourcebundle.ResourceMessage; 8 9 17 18 public class UserCountValidator extends AbstractValidator { 19 20 private static org.apache.log4j.Logger logger = 21 org.apache.log4j.Logger.getLogger(UserCountValidator.class); 22 23 public UserCountValidator (String name, String value, License license) { 24 super(name, value, license); 25 } 26 27 public boolean assertEquals (String value) { 28 29 int maxUsers = Integer.parseInt(value); 30 try { 32 33 Enumeration enumeration = ServicesRegistry.getInstance() 35 .getJahiaSitesService() 36 .getSites(); 37 JahiaSite aSite = null; 38 int nbItems = 0; 39 while (enumeration.hasMoreElements()) { 40 aSite = (JahiaSite) enumeration.nextElement(); 41 nbItems = ServicesRegistry.getInstance(). 42 getJahiaUserManagerService(). 43 getNbUsers(aSite.getID()); 44 45 if (nbItems > maxUsers) { 46 errorMessage = new ResourceMessage("org.jahia.security.license.UserCountValidator.invalidUserCount.label", new Integer (nbItems), new Integer (maxUsers), new Integer (aSite.getID())); 47 return false; 48 } 49 } 50 } catch (JahiaException ex) { 51 logger.error("Error in user limit check", ex); 52 errorMessage = new ResourceMessage("org.jahia.security.license.UserCountValidator.errorInUserCountCheck.label"); 53 return false; 54 } 55 return true; 56 } 57 58 public boolean assertInRange (String fromValue, String toValue) { 59 int minUsers = Integer.parseInt(fromValue); 60 int maxUsers = Integer.parseInt(toValue); 61 try { 63 64 Enumeration enumeration = ServicesRegistry.getInstance() 66 .getJahiaSitesService() 67 .getSites(); 68 JahiaSite aSite = null; 69 int nbItems = 0; 70 while (enumeration.hasMoreElements()) { 71 aSite = (JahiaSite) enumeration.nextElement(); 72 nbItems = ServicesRegistry.getInstance(). 73 getJahiaUserManagerService(). 74 getNbUsers(aSite.getID()); 75 76 if ((nbItems > maxUsers) || (nbItems < minUsers)) { 77 errorMessage = new ResourceMessage("org.jahia.security.license.UserCountValidator.userCountNotInRange.label", new Integer (nbItems), new Integer (minUsers), new Integer (maxUsers), new Integer (aSite.getID())); 78 return false; 79 } 80 } 81 } catch (JahiaException ex) { 82 logger.error("Error in user limit check", ex); 83 errorMessage = new ResourceMessage("org.jahia.security.license.UserCountValidator.errorInUserCountCheck.label"); 84 return false; 85 } 86 return true; 87 } 88 89 } | Popular Tags |