KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > resourcebundle > GrpUsrEngineResourceBundle


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// NK 18.02.2002 - added in Jahia
15
//
16

17 package org.jahia.resourcebundle;
18
19 import java.security.Principal JavaDoc;
20 import java.security.acl.Group JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.services.pages.ContentPage;
27 import org.jahia.services.usermanager.JahiaGroup;
28 import org.jahia.services.usermanager.JahiaUser;
29 import org.jahia.utils.JahiaConsole;
30
31
32 /**
33  * Tools to handles Pages engine resource bundle for a given principal.
34  *
35  * @author Khue Nguyen
36  * @version 1.0
37  */

38 public class GrpUsrEngineResourceBundle
39 {
40
41     private static final String JavaDoc CLASS_NAME =
42                 GrpUsrEngineResourceBundle.class.getName();
43
44     private static GrpUsrEngineResourceBundle mInstance ;
45
46     private Hashtable JavaDoc usrResources ;
47     private Hashtable JavaDoc grpResources ;
48
49
50     private GrpUsrEngineResourceBundle(){
51         usrResources = new Hashtable JavaDoc();
52         grpResources = new Hashtable JavaDoc();
53     }
54
55     public static synchronized GrpUsrEngineResourceBundle getInstance(){
56
57         if ( mInstance == null ){
58             mInstance = new GrpUsrEngineResourceBundle();
59         }
60         return mInstance;
61     }
62
63     /**
64      * Add a resource bundle
65      *
66      * @param JahiaPage page
67      * @param Principal p, a JahiaUser or JahiaGroup
68      * @param ResourceBundle res
69      */

70     public void addResourceBundle(ContentPage contentPage, Principal JavaDoc p, ResourceBundle JavaDoc res){
71
72         if ( contentPage == null || res == null || p == null )
73             return;
74
75         boolean isGroup = ( p instanceof Group JavaDoc );
76         String JavaDoc key;
77         if ( isGroup ){
78             key = ((JahiaGroup)p).getGroupKey();
79             JahiaConsole.println(CLASS_NAME+".addResourceBundle","Added grp res. bun. : " + key ) ;
80             grpResources.put( JahiaResourceBundle.ENGINE_DEFAULT_RESOURCE_BUNDLE + "_" + contentPage.getID() + "_" + key , res );
81         } else {
82             key = ((JahiaUser)p).getUserKey();
83             JahiaConsole.println(CLASS_NAME+".addResourceBundle","Added user res. bun. : " + key ) ;
84             usrResources.put( JahiaResourceBundle.ENGINE_DEFAULT_RESOURCE_BUNDLE + "_" + contentPage.getID() + "_" + key , res );
85         }
86     }
87
88     /**
89      * Returns a resource bundle for a given page and a given principal
90      *
91      * @param JahiaPage page
92      * @param Principal p, a JahiaUser or JahiaGroup
93      * @return ResourceBundle res
94      */

95     public ResourceBundle JavaDoc getResourceBundle(ContentPage contentPage, Principal JavaDoc p){
96
97         if ( contentPage == null || p == null )
98             return null;
99
100         boolean isGroup = ( p instanceof Group JavaDoc );
101         String JavaDoc key;
102         if ( isGroup ){
103             key = ((JahiaGroup)p).getGroupKey();
104             //JahiaConsole.println(CLASS_NAME+".getResourceBundle","For group : " + key ) ;
105
} else {
106             key = ((JahiaUser)p).getUserKey();
107             //JahiaConsole.println(CLASS_NAME+".getResourceBundle","For user : " + key ) ;
108
}
109
110         ResourceBundle JavaDoc res;
111         if ( !isGroup ){
112             res = (ResourceBundle JavaDoc)usrResources.get( JahiaResourceBundle.ENGINE_DEFAULT_RESOURCE_BUNDLE
113                               + "_" + contentPage.getID() + "_" + key );
114             if ( res == null )
115                 res = getGrpResourceBundle(contentPage,p);
116         } else {
117             res = getGrpResourceBundle(contentPage,p);
118         }
119         return res;
120     }
121
122     /**
123      * Returns a resource bundle for a given JahiaUser looking at its group membership
124      *
125      * @param JahiaPage page
126      * @param Principal p, a JahiaUser or JahiaGroup
127      * @return ResourceBundle res
128      */

129     private ResourceBundle JavaDoc getGrpResourceBundle(ContentPage contentPage, Principal JavaDoc p){
130
131         //JahiaConsole.println(CLASS_NAME+".getGrpResourceBundle()","Started looking for page :" + page.getID());
132

133         if ( contentPage == null || p == null )
134             return null;
135
136         JahiaGroup grp;
137         String JavaDoc grpKey;
138         String JavaDoc key ;
139         String JavaDoc resName = JahiaResourceBundle.ENGINE_DEFAULT_RESOURCE_BUNDLE + "_" + contentPage.getID() + "_";
140         ResourceBundle JavaDoc res = null;
141         Enumeration JavaDoc keys = grpResources.keys();
142         while (keys.hasMoreElements()){
143             grpKey = "";
144             key = (String JavaDoc)keys.nextElement();
145             //JahiaConsole.println(CLASS_NAME+".getGrpResourceBundle()","res key found :" + key);
146

147             if ( key.startsWith(resName) ){
148                 grpKey = key.substring(resName.length(),key.length());
149                 //JahiaConsole.println(CLASS_NAME+".getGrpResourceBundle()","groupkey :" + grpKey);
150

151                 grp = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(grpKey);
152                 if ( grp != null && grp.isMember(p)){
153                     //JahiaConsole.println(CLASS_NAME+".getGrpResourceBundle()","user " + ((JahiaUser)p).getUsername() + " is member of group :" + grpKey);
154
res = (ResourceBundle JavaDoc)grpResources.get(key);
155                     if ( res != null )
156                         return res;
157                 }
158             }
159         }
160         return res;
161     }
162
163 }
164
165
Popular Tags