KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > UserCountValidator


1 package org.jahia.security.license;
2
3 import java.util.Enumeration JavaDoc;
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 /**
10  * <p>Title: </p>
11  * <p>Description: </p>
12  * <p>Copyright: Copyright (c) 2002</p>
13  * <p>Company: Jahia Ltd</p>
14  * @author Serge Huber
15  * @version 1.0
16  */

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 JavaDoc name, String JavaDoc value, License license) {
24         super(name, value, license);
25     }
26
27     public boolean assertEquals (String JavaDoc value) {
28
29         int maxUsers = Integer.parseInt(value);
30         // Check if the number of users is not exceeding the fixed limit
31
try {
32
33             // get all the list of site
34
Enumeration JavaDoc 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 JavaDoc(nbItems), new Integer JavaDoc(maxUsers), new Integer JavaDoc(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 JavaDoc fromValue, String JavaDoc toValue) {
59         int minUsers = Integer.parseInt(fromValue);
60         int maxUsers = Integer.parseInt(toValue);
61         // Check if the number of users is not exceeding the fixed limit
62
try {
63
64             // get all the list of site
65
Enumeration JavaDoc 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 JavaDoc(nbItems), new Integer JavaDoc(minUsers), new Integer JavaDoc(maxUsers), new Integer JavaDoc(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