001: package org.gridsphere.portlet.impl;
002:
003: import org.gridsphere.portletcontainer.impl.descriptor.InitParam;
004: import org.gridsphere.portletcontainer.impl.descriptor.PortletDefinition;
005: import org.gridsphere.portletcontainer.impl.descriptor.PortletInfo;
006: import org.gridsphere.portletcontainer.impl.descriptor.SupportedLocale;
007:
008: import javax.portlet.Portlet;
009: import javax.portlet.PortletConfig;
010: import javax.portlet.PortletContext;
011: import javax.servlet.ServletConfig;
012: import java.util.*;
013:
014: /**
015: * The <CODE>PortletConfig</CODE> interface provides the portlet with
016: * its configuration. The configuration holds information about the
017: * portlet that is valid for all users. The configuration is retrieved
018: * from the portlet definition in the deployment descriptor.
019: * The portlet can only read the configuration data.
020: * <p/>
021: * The configuration information contains the portlet name, the portlet
022: * initialization parameters, the portlet resource bundle and the portlet
023: * application context.
024: *
025: * @see Portlet
026: */
027: public class PortletConfigImpl implements PortletConfig {
028:
029: private PortletContext context = null;
030: private ClassLoader classLoader = null;
031: private String portletName = null;
032: private ResourceBundle infoBundle = null;
033: private String resources = null;
034: private Hashtable initParams = new Hashtable();
035:
036: private static class DefaultResourceBundle extends
037: ListResourceBundle {
038: private Object[][] resources;
039:
040: public DefaultResourceBundle(PortletInfo portletInfo) {
041: String title = ((portletInfo.getTitle() != null) ? portletInfo
042: .getTitle().getContent()
043: : "");
044: String shortTitle = ((portletInfo.getShortTitle() != null) ? portletInfo
045: .getShortTitle().getContent()
046: : "");
047: String keywords = ((portletInfo.getKeywords() != null) ? portletInfo
048: .getKeywords().getContent()
049: : "");
050: resources = new Object[][] {
051: { "javax.portlet.title", title },
052: { "javax.portlet.short-title", shortTitle },
053: { "javax.portlet.keywords", keywords } };
054: }
055:
056: public Object[][] getContents() {
057: return resources;
058: }
059: }
060:
061: private static class ResourceBundleImpl extends ResourceBundle {
062: private HashMap data;
063:
064: public ResourceBundleImpl(ResourceBundle bundle,
065: ResourceBundle defaults) {
066: data = new HashMap();
067: importData(defaults);
068: importData(bundle);
069: }
070:
071: private void importData(ResourceBundle bundle) {
072: if (bundle != null) {
073: for (Enumeration e = bundle.getKeys(); e
074: .hasMoreElements();) {
075: String key = (String) e.nextElement();
076: Object value = bundle.getObject(key);
077: data.put(key, value);
078: }
079: }
080: }
081:
082: protected Object handleGetObject(String key) {
083: return data.get(key);
084: }
085:
086: public Enumeration getKeys() {
087: return new Enumerator(data.keySet());
088: }
089: }
090:
091: /**
092: * Constructs an instance of PortletConfig from a servlet configuration
093: * object and an application portlet descriptor
094: *
095: * @param servletConfig a <code>ServletConfig</code>
096: * @param definition a <code>PortletDefinition</code>
097: */
098: public PortletConfigImpl(ServletConfig servletConfig,
099: PortletDefinition definition, ClassLoader classLoader) {
100:
101: this .classLoader = classLoader;
102:
103: // create init params
104: InitParam[] params = definition.getInitParam();
105: if (params != null) {
106: for (int i = 0; i < params.length; i++) {
107: InitParam iparam = params[i];
108: String name = iparam.getName().getContent();
109: String value = iparam.getValue().getContent();
110: if ((name != null) && (value != null)) {
111: initParams.put(name, value);
112: }
113: }
114: }
115:
116: // create portlet context
117: context = new PortletContextImpl(servletConfig
118: .getServletContext());
119:
120: // get portlet name
121: portletName = definition.getPortletName().getContent();
122:
123: SupportedLocale[] locales = definition.getSupportedLocale();
124: Locale[] supportedLocales = new Locale[locales.length];
125: for (int i = 0; i < locales.length; i++) {
126: supportedLocales[i] = new Locale(locales[i].getContent());
127: }
128:
129: PortletInfo portletInfo = definition.getPortletInfo();
130: if (portletInfo != null) {
131: infoBundle = new DefaultResourceBundle(portletInfo);
132:
133: }
134:
135: if (definition.getResourceBundle() != null) {
136: resources = definition.getResourceBundle().getContent();
137:
138: }
139:
140: //this.logConfig();
141: }
142:
143: /**
144: * Returns the name of the portlet.
145: * <p/>
146: * The name may be provided via server administration, assigned in the
147: * portlet application deployment descriptor with the <code>portlet-name</code>
148: * tag.
149: *
150: * @return the portlet name
151: */
152: public String getPortletName() {
153: return portletName;
154: }
155:
156: /**
157: * Returns the <code>PortletContext</code> of the portlet application
158: * the portlet is in.
159: *
160: * @return a <code>PortletContext</code> object, used by the
161: * caller to interact with its portlet container
162: * @see PortletContext
163: */
164: public PortletContext getPortletContext() {
165: return context;
166: }
167:
168: /**
169: * Returns the path name of this portlet context
170: *
171: * @return Returns the context path of the web application.
172: */
173: public String getContextPath() {
174: // todo fix me to confirm to servlet spec 2.5
175: return "";
176: }
177:
178: /**
179: * Gets the resource bundle for the given locale based on the
180: * resource bundle defined in the deployment descriptor
181: * with <code>resource-bundle</code> tag or the inlined resources
182: * defined in the deployment descriptor.
183: *
184: * @param locale the locale for which to retrieve the resource bundle
185: * @return the resource bundle for the given locale
186: */
187: public ResourceBundle getResourceBundle(java.util.Locale locale) {
188: if (resources == null) {
189: return infoBundle;
190: }
191: ResourceBundle resourceBundle = null;
192: try {
193: resourceBundle = ResourceBundle.getBundle(resources,
194: locale, classLoader);
195: if (infoBundle != null) {
196: return new ResourceBundleImpl(resourceBundle,
197: infoBundle);
198: }
199: } catch (MissingResourceException e) {
200: System.err.println("Unable to find resource bundle: "
201: + resources + " for locale: " + locale);
202: if (infoBundle != null) {
203: return infoBundle;
204: }
205: // if everything goes wrong get the english locale (which needs to be there)
206: resourceBundle = ResourceBundle.getBundle(resources,
207: Locale.ENGLISH, classLoader);
208: }
209: return resourceBundle;
210: }
211:
212: /**
213: * Returns a String containing the value of the named initialization parameter,
214: * or null if the parameter does not exist.
215: *
216: * @param name a <code>String</code> specifying the name
217: * of the initialization parameter
218: * @return a <code>String</code> containing the value
219: * of the initialization parameter
220: * @exception IllegalArgumentException if name is <code>null</code>.
221: */
222: public String getInitParameter(String name) {
223: if (name == null)
224: throw new IllegalArgumentException("name is NULL");
225: return (String) initParams.get(name);
226: }
227:
228: /**
229: * Returns the names of the portlet initialization parameters as an
230: * <code>Enumeration</code> of String objects, or an empty <code>Enumeration</code> if the
231: * portlet has no initialization parameters.
232: *
233: * @return an <code>Enumeration</code> of <code>String</code>
234: * objects containing the names of the portlet
235: * initialization parameters, or an empty <code>Enumeration</code> if the
236: * portlet has no initialization parameters.
237: */
238: public java.util.Enumeration getInitParameterNames() {
239: return initParams.keys();
240: }
241:
242: }
|