1 5 6 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.bundle; 7 8 import java.util.Locale ; 9 import java.util.MissingResourceException ; 10 import java.util.ResourceBundle ; 11 12 import org.apache.commons.logging.Log; 13 import org.exoplatform.commons.utils.MapResourceBundle; 14 import org.exoplatform.container.PortalContainer; 15 import org.exoplatform.services.cache.CacheService; 16 import org.exoplatform.services.cache.ExoCache; 17 import org.exoplatform.services.log.LogService; 18 import org.exoplatform.services.portletcontainer.bundle.ResourceBundleDelegate; 19 import org.exoplatform.services.portletcontainer.impl.PortletContainerConf; 20 import org.exoplatform.services.portletcontainer.pci.model.Portlet; 21 import org.exoplatform.services.portletcontainer.pci.model.PortletInfo; 22 23 26 public class ResourceBundleManager { 27 28 public static final String PORTLET_TITLE = "javax.portlet.title"; 30 31 public static final String PORTLET_SHORT_TITLE = "javax.portlet.short-title"; 32 33 public static final String KEYWORDS = "javax.portlet.keywords"; 34 35 private PortletContainerConf conf; 36 37 private ExoCache cache; 38 39 private Log log_; 40 41 public ResourceBundleManager(PortletContainerConf conf, 42 LogService logService, 43 CacheService cacheService) throws Exception { 44 this.conf = conf; 45 this.cache = cacheService 46 .getCacheInstance(getClass().getName()); 47 log_ = logService.getLog("org.exoplatform.services.portletcontainer"); 48 } 49 50 public ResourceBundle lookupBundle(Portlet portlet, Locale locale) { 51 String bundleName = portlet.getResourceBundle(); 52 String key = portlet.getPortletClass() + bundleName + locale; 53 MapResourceBundle bundle = null; 54 try { 55 if (cache.get(key) != null) 56 return (ResourceBundle ) cache.get(key); 57 PortletInfo pI = portlet.getPortletInfo(); 58 if (bundleName == null || bundleName.equals("")) { 59 MapResourceBundle bundle2 = new MapResourceBundle(locale); 60 initBundle(pI, bundle2); 61 cache.put(key, bundle2); 62 return bundle2; 63 } 64 if (locale == null) 65 locale = new Locale ("en"); 66 67 if (conf.isBundleLookupDelegated()) { 68 ResourceBundleDelegate delegate = (ResourceBundleDelegate) PortalContainer 69 .getInstance().getComponentInstanceOfType( 70 ResourceBundleDelegate.class); 71 bundle = (MapResourceBundle) delegate.lookupBundle(bundleName, locale); 72 initBundle(pI, bundle); 73 } else { 74 ResourceBundle rB = ResourceBundle.getBundle(bundleName, locale, 75 Thread.currentThread().getContextClassLoader()); 76 bundle = new MapResourceBundle(rB, locale); 77 initBundle(pI, bundle); 78 cache.put(key, bundle); 79 } 80 } catch (Exception e) { 81 log_.error("Can not load resource bundle", e); 82 } 83 return bundle; 84 } 85 86 private void initBundle(PortletInfo pI, MapResourceBundle rB) { 87 if (pI != null && pI.getTitle() != null){ 88 try { 89 rB.getString(PORTLET_TITLE); 90 } catch(MissingResourceException ex) { 91 rB.add(PORTLET_TITLE, pI.getTitle()); 92 } 93 } 94 95 if (pI != null && pI.getShortTitle() != null){ 96 try { 97 rB.getString(PORTLET_SHORT_TITLE); 98 } catch(MissingResourceException ex) { 99 rB.add(PORTLET_SHORT_TITLE, pI.getShortTitle()); 100 } 101 } 102 103 if (pI != null && pI.getKeywords() != null){ 104 try { 105 rB.getString(KEYWORDS); 106 } catch(MissingResourceException ex) { 107 rB.add(KEYWORDS, pI.getKeywords()); 108 } 109 } 110 } 111 112 } | Popular Tags |