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 TemplateCountValidator extends AbstractValidator { 19 20 private static org.apache.log4j.Logger logger = 21 org.apache.log4j.Logger.getLogger(TemplateCountValidator.class); 22 23 public TemplateCountValidator(String name, String value, License license) { 24 super(name, value, license); 25 } 26 public boolean assertEquals(String value) { 27 28 int maxTemplates = Integer.parseInt(value); 29 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 42 nbItems = ServicesRegistry.getInstance(). 43 getJahiaPageTemplateService(). 44 getNbPageTemplates (aSite.getID()); 45 46 47 if (nbItems > maxTemplates) { 48 errorMessage = new ResourceMessage("org.jahia.security.license.TemplateCountValidator.invalidTemplateCount.label", new Integer (nbItems), new Integer (maxTemplates), new Integer (aSite.getID())); 49 return false; 50 } 51 } 52 53 } catch (JahiaException ex) { 54 logger.error("Error while checking template limit", ex); 55 errorMessage = new ResourceMessage("org.jahia.security.license.TemplateCountValidator.errorInTemplateCountCheck.label"); 56 return false; 57 } 58 return true; 59 } 60 61 public boolean assertInRange(String fromValue, String toValue) { 62 63 int minTemplates = Integer.parseInt(fromValue); 64 int maxTemplates = Integer.parseInt(toValue); 65 66 try { 68 69 Enumeration enumeration = ServicesRegistry.getInstance() 71 .getJahiaSitesService() 72 .getSites(); 73 JahiaSite aSite = null; 74 int nbItems = 0; 75 while( enumeration.hasMoreElements() ){ 76 aSite = (JahiaSite)enumeration.nextElement(); 77 78 nbItems = ServicesRegistry.getInstance(). 79 getJahiaPageTemplateService(). 80 getNbPageTemplates (aSite.getID()); 81 if ((nbItems > maxTemplates) || (nbItems < minTemplates)) { 82 errorMessage = new ResourceMessage("org.jahia.security.license.TemplateCountValidator.templateCountNotInRange.label", new Integer (nbItems), new Integer (minTemplates), new Integer (maxTemplates), new Integer (aSite.getID())); 83 return false; 84 } 85 } 86 87 } catch (JahiaException ex) { 88 logger.error("Error while checking template limit", ex); 89 errorMessage = new ResourceMessage("org.jahia.security.license.TemplateCountValidator.errorInTemplateCountCheck.label"); 90 return false; 91 } 92 return true; 93 } 94 95 } | Popular Tags |