1 17 package org.jahia.resourcebundle; 18 19 import java.security.Principal ; 20 import java.security.acl.Group ; 21 import java.util.Enumeration ; 22 import java.util.Hashtable ; 23 import java.util.ResourceBundle ; 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 38 public class GrpUsrEngineResourceBundle 39 { 40 41 private static final String CLASS_NAME = 42 GrpUsrEngineResourceBundle.class.getName(); 43 44 private static GrpUsrEngineResourceBundle mInstance ; 45 46 private Hashtable usrResources ; 47 private Hashtable grpResources ; 48 49 50 private GrpUsrEngineResourceBundle(){ 51 usrResources = new Hashtable (); 52 grpResources = new Hashtable (); 53 } 54 55 public static synchronized GrpUsrEngineResourceBundle getInstance(){ 56 57 if ( mInstance == null ){ 58 mInstance = new GrpUsrEngineResourceBundle(); 59 } 60 return mInstance; 61 } 62 63 70 public void addResourceBundle(ContentPage contentPage, Principal p, ResourceBundle res){ 71 72 if ( contentPage == null || res == null || p == null ) 73 return; 74 75 boolean isGroup = ( p instanceof Group ); 76 String 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 95 public ResourceBundle getResourceBundle(ContentPage contentPage, Principal p){ 96 97 if ( contentPage == null || p == null ) 98 return null; 99 100 boolean isGroup = ( p instanceof Group ); 101 String key; 102 if ( isGroup ){ 103 key = ((JahiaGroup)p).getGroupKey(); 104 } else { 106 key = ((JahiaUser)p).getUserKey(); 107 } 109 110 ResourceBundle res; 111 if ( !isGroup ){ 112 res = (ResourceBundle )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 129 private ResourceBundle getGrpResourceBundle(ContentPage contentPage, Principal p){ 130 131 133 if ( contentPage == null || p == null ) 134 return null; 135 136 JahiaGroup grp; 137 String grpKey; 138 String key ; 139 String resName = JahiaResourceBundle.ENGINE_DEFAULT_RESOURCE_BUNDLE + "_" + contentPage.getID() + "_"; 140 ResourceBundle res = null; 141 Enumeration keys = grpResources.keys(); 142 while (keys.hasMoreElements()){ 143 grpKey = ""; 144 key = (String )keys.nextElement(); 145 147 if ( key.startsWith(resName) ){ 148 grpKey = key.substring(resName.length(),key.length()); 149 151 grp = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(grpKey); 152 if ( grp != null && grp.isMember(p)){ 153 res = (ResourceBundle )grpResources.get(key); 155 if ( res != null ) 156 return res; 157 } 158 } 159 } 160 return res; 161 } 162 163 } 164 165 | Popular Tags |