001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.portlet.impl;
014:
015: import java.util.Collections;
016: import java.util.Enumeration;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Locale;
021: import java.util.Map;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: import javax.portlet.PortletConfig;
026: import javax.portlet.PortletContext;
027:
028: import com.sun.portal.portletcontainercommon.descriptor.InitParamDescriptor;
029: import com.sun.portal.portletcontainercommon.descriptor.PortletDescriptor;
030: import com.sun.portal.portletcontainercommon.descriptor.PortletInfoDescriptor;
031:
032: /**
033: * The <code>PortletConfigImpl</code> class provides a default
034: * implementation for the <code>PortletConfig</code> interface.
035: *
036: */
037:
038: public class PortletConfigImpl implements PortletConfig {
039:
040: // private data members
041: private String _portletName;
042: private PortletContext _portletContext;
043: private PortletDescriptor _pd;
044: private Map _paramsMap = new HashMap();
045:
046: // constructor
047: public PortletConfigImpl(PortletDescriptor portletDescriptor,
048: String portletName, PortletContext portletContext) {
049:
050: _portletName = portletName;
051: _portletContext = portletContext;
052: _pd = portletDescriptor;
053: initParamsMap();
054: }
055:
056: /**
057: * construct an internal map to store the init parameters
058: */
059: private void initParamsMap() {
060:
061: if (_pd != null) {
062: for (Iterator i = _pd.getInitParamDescriptors().iterator(); i
063: .hasNext();) {
064: InitParamDescriptor ipd = (InitParamDescriptor) i
065: .next();
066: _paramsMap.put(ipd.getParamName(), ipd.getParamValue());
067: }
068: }
069: }
070:
071: /**
072: * Returns a String containing the value of the named initialization parameter,
073: * or null if the parameter does not exist.
074: *
075: * @param name a <code>String</code> specifying the name
076: * of the initialization parameter
077: *
078: * @return a <code>String</code> containing the value
079: * of the initialization parameter
080: *
081: * @exception java.lang.IllegalArgumentException
082: * if name is <code>null</code>.
083: */
084: public String getInitParameter(java.lang.String name)
085: throws IllegalArgumentException {
086:
087: if (name == null) {
088: throw new IllegalArgumentException();
089: }
090: return (String) _paramsMap.get(name);
091: }
092:
093: /**
094: * Returns the names of the portlet initialization parameters as an
095: * <code>Enumeration</code> of String objects, or an empty <code>Enumeration</code> if the
096: * portlet has no initialization parameters.
097: *
098: * @return an <code>Enumeration</code> of <code>String</code>
099: * objects containing the names of the portlet
100: * initialization parameters, or an empty <code>Enumeration</code> if the
101: * portlet has no initialization parameters.
102: */
103: public Enumeration getInitParameterNames() {
104:
105: return Collections.enumeration(_paramsMap.keySet());
106: }
107:
108: /**
109: * Returns the <code>PortletContext</code> of the portlet application
110: * the portlet is in.
111: *
112: * @return a <code>PortletContext</code> object, used by the
113: * caller to interact with its portlet container
114: *
115: * @see PortletContext
116: */
117: public PortletContext getPortletContext() {
118: return _portletContext;
119: }
120:
121: /**
122: * Returns the name of the portlet.
123: * <P>
124: * The name may be provided via server administration, assigned in the
125: * portlet application deployment descriptor with the <code>portlet-name</code>
126: * tag.
127: *
128: * @return the portlet name
129: */
130: public String getPortletName() {
131:
132: return _portletName;
133: }
134:
135: /**
136: * Gets the resource bundle for the given locale based on the
137: * resource bundle defined in the deployment descriptor
138: * with <code>resource-bundle</code> tag or the inlined resources
139: * defined in the deployment descriptor.
140: *
141: * @param locale the locale for which to retrieve the resource bundle
142: *
143: * @return the resource bundle for the given locale
144: *
145: */
146: public ResourceBundle getResourceBundle(Locale locale)
147: throws MissingResourceException {
148:
149: String rbName;
150: ResourceBundle rb = null;
151:
152: // check if the portlet descriptor is null
153: if (_pd == null) {
154: // cannot obtain name of the resource class and the
155: // name of the key for the resource
156: throw new MissingResourceException(
157: "PortletConfigImpl.getResourceBundle: missing portlet descriptor",
158: null, null);
159: } else {
160: rbName = _pd.getResourceBundle();
161: }
162:
163: PortletInfoDescriptor pid = _pd.getPortletInfoDescriptor();
164: Object values[] = new Object[] { "", "", "" };
165: boolean valuesDefinedInline[] = new boolean[] { false, false,
166: false };
167:
168: if (pid != null) {
169: if (pid.getTitle() != null) {
170: values[0] = pid.getTitle();
171: valuesDefinedInline[0] = true;
172: }
173: if (pid.getShortTitle() != null) {
174: values[1] = pid.getShortTitle();
175: valuesDefinedInline[1] = true;
176: }
177: if (pid.getKeywords() != null) {
178: List keywords = pid.getKeywords();
179:
180: String[] strArray = new String[keywords.size()];
181: values[2] = (String[]) keywords.toArray(strArray);
182: valuesDefinedInline[2] = true;
183: }
184: }
185:
186: try {
187: if (rbName != null) {
188: rb = ResourceBundle.getBundle(rbName, locale, Thread
189: .currentThread().getContextClassLoader());
190: }
191: } catch (MissingResourceException me) {
192: //falls through
193: }
194: rb = (ResourceBundle) new PortletResourceBundle(rb, rbName,
195: values, valuesDefinedInline);
196:
197: return rb;
198: }
199: }
|