KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > bundle > ResourceBundleManager


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.bundle;
7
8 import java.util.Locale JavaDoc;
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
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 /**
24  * @author Benjamin Mestrallet benjamin.mestrallet@exoplatform.com
25  */

26 public class ResourceBundleManager {
27
28   //elements that must be found in the resource bundle
29
public static final String JavaDoc PORTLET_TITLE = "javax.portlet.title";
30
31   public static final String JavaDoc PORTLET_SHORT_TITLE = "javax.portlet.short-title";
32
33   public static final String JavaDoc 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 JavaDoc {
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 JavaDoc lookupBundle(Portlet portlet, Locale JavaDoc locale) {
51     String JavaDoc bundleName = portlet.getResourceBundle();
52     String JavaDoc key = portlet.getPortletClass() + bundleName + locale;
53     MapResourceBundle bundle = null;
54     try {
55       if (cache.get(key) != null)
56         return (ResourceBundle JavaDoc) 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 JavaDoc("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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
107         rB.add(KEYWORDS, pI.getKeywords());
108       }
109     }
110   }
111
112 }
Popular Tags