001: /*
002: * Copyright 2004-2005 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.portletcontainercommon.descriptor;
014:
015: import java.util.*;
016: import java.util.List;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023:
024: import javax.portlet.PreferencesValidator;
025:
026: import org.jdom.Attribute;
027: import org.jdom.Element;
028: import org.jdom.Namespace;
029:
030: import com.sun.portal.log.common.PortalLogger;
031:
032: /**
033: * The portlet descriptor is the descriptor for a portlet.
034: * <P>
035: * The PortletDescriptor loads all the descriptors for a portlet into
036: * memory, it delegates the loading of each descriptor to the sub
037: * descriptor classe.
038: * <P>
039: * The PortletDescriptor class provides member methods to get to all
040: * sub descriptors, the portlet name, portlet class name, as well as
041: * the description, if available. The following get**() methods are defined:
042: * <UL>
043: * <LI><code>getPortletName()</code>
044: * <LI><code>getPortletClassName()</code>
045: * <LI><code>getDescriptions()</code>
046: * <LI><code>getDisplayName()</code>
047: * <LI><code>getDisplayNames()</code>
048: * <LI><code>getDisplayNameMap()</code>
049: * <LI><code>getSupportedLocales()</code>
050: * <LI><code>getPortletPreferencesDescriptor()</code>
051: * <LI><code>getInitParamDescriptors()</code>
052: * <LI><code>getSupportsDescriptors()</code>
053: * <LI><code>getSupportsDescriptorMap()</code>
054: * <LI><code>getSupportedPortletModes</code>
055: * <LI><code>getCacheExpiration</code>
056: * <LI><code>getPortletInfoDescriptor</code>
057: * <LI><code>getSecurityRoleRefDescriptors</code>
058: * <LI><code>getDeploymentExtensionDescriptors</code>
059: *</UL>
060: */
061: public class PortletDescriptor {
062:
063: // Portlet Descriptor Element Names
064: public static final String PORTLET = "portlet";
065: public static final String PORTLET_NAME = "portlet-name";
066: public static final String PORTLET_CLASS = "portlet-class";
067: public static final String PORTLET_PREFERENCES = "portlet-preferences";
068: public static final String INIT_PARAM = "init-param";
069: public static final String SUPPORTED_LOCALE = "supported-locale";
070: public static final String EXPIRATION_CACHE = "expiration-cache";
071: public static final String PORTLET_INFO = "portlet-info";
072: public static final String SUPPORTS = "supports";
073: public static final String SECURITY_ROLE_REF = "security-role-ref";
074: public static final String DEPLOYMENT_EXTENSION = "deployment-extension";
075: public static final String RESOURCE_BUNDLE = "resource-bundle";
076:
077: public static final int EXPIRATION_CACHE_NOT_DEFINED = -999;
078:
079: // Global Variables
080: private String _portletName;
081: private String _portletClassName;
082: private String _resourceBundle;
083: private List _descriptions = new ArrayList();
084: private List _displayNames = new ArrayList();
085: private Map _descriptionMap = new HashMap();
086: private Map _displayNameMap = new HashMap();
087: private List _supportedLocales = new ArrayList();
088: private PortletPreferencesDescriptor _pPreferencesDescriptor;
089: private List _initParamDescriptors = new ArrayList();
090: private List _supportsDescriptors = new ArrayList();
091: private String _cacheExpiration;
092: private PortletInfoDescriptor _infoDescriptor;
093: private List _secRoleRefDescriptors = new ArrayList();
094:
095: private DeploymentExtensionDescriptor _depExtDescriptor;
096:
097: // Create a logger for this class
098: private static Logger debugLogger = PortalLogger
099: .getLogger(PortletDescriptor.class);
100:
101: public PortletDescriptor(Logger logger,
102: DeploymentExtensionDescriptor depExtDescriptor) {
103: _depExtDescriptor = depExtDescriptor;
104: }
105:
106: /**
107: * Loads the portlet descriptor and sub descriptors into memory.
108: * <P>
109: * The portlet sub descriptors includes:
110: * <UL>
111: * <LI><code>SupportsDescriptor()</code>
112: * <LI><code>PortletPreferencesDescriptor()</code>
113: * <LI><code>InitParamDescriptor()</code>
114: * <LI><code>PortletInfoDescriptor()</code>
115: * </UL>
116: * @param The portlet descriptor Element
117: * @see SupportsDescriptor
118: * @see PortletPreferencesDescriptor
119: * @see InitParamDescriptor
120: * @see PortletInfoDescriptor
121: */
122: public void load(Element element, Namespace namespace)
123: throws DeploymentDescriptorException {
124: _portletName = element
125: .getChildTextTrim(PORTLET_NAME, namespace);
126: _portletClassName = element.getChildTextTrim(PORTLET_CLASS,
127: namespace);
128:
129: if (_portletName == null) {
130: debugLogger.warning("PSPL_PCCCSPPCCD0005");
131: }
132: if (_portletClassName == null) {
133: debugLogger.warning("PSPL_PCCCSPPCCD0006");
134: }
135:
136: List descriptionElements = element.getChildren(
137: PortletAppDescriptor.DESCRIPTION, namespace);
138: for (Iterator i = descriptionElements.iterator(); i.hasNext();) {
139: Element descriptionElement = (Element) i.next();
140: _descriptions.add(descriptionElement.getTextTrim());
141: List descList = descriptionElement.getAttributes();
142: for (Iterator dit = descList.iterator(); dit.hasNext();) {
143: Attribute dAttr = (Attribute) dit.next();
144: if (dAttr.getName().equals(
145: PortletAppDescriptor.XML_LANG_ATTR)) {
146: _descriptionMap.put(dAttr.getValue(),
147: descriptionElement.getTextTrim());
148: }
149:
150: }
151: }
152:
153: List displayNameElements = element.getChildren(
154: PortletAppDescriptor.DISPLAY_NAME, namespace);
155: for (Iterator i = displayNameElements.iterator(); i.hasNext();) {
156: Element displayNameElement = (Element) i.next();
157: _displayNames.add(displayNameElement.getTextTrim());
158: List attrList = displayNameElement.getAttributes();
159: for (Iterator it = attrList.iterator(); it.hasNext();) {
160: Attribute attr = (Attribute) it.next();
161: if (attr.getName().equals(
162: PortletAppDescriptor.XML_LANG_ATTR)) {
163: debugLogger.log(Level.SEVERE,
164: "PSPL_PCCCSPPCCD0007", new Object[] {
165: attr.getName(), attr.getValue() });
166: _displayNameMap.put(attr.getValue(),
167: displayNameElement.getTextTrim());
168: }
169:
170: }
171:
172: }
173:
174: List supportedLocaleElements = element.getChildren(
175: SUPPORTED_LOCALE, namespace);
176: for (Iterator i = supportedLocaleElements.iterator(); i
177: .hasNext();) {
178: Element supportedLocaleElement = (Element) i.next();
179: _supportedLocales.add(supportedLocaleElement.getTextTrim());
180: }
181:
182: // loads supports descriptor
183: List supportsElements = element
184: .getChildren(SUPPORTS, namespace);
185: if (supportsElements.isEmpty()) {
186: debugLogger.warning("PSPL_PCCCSPPCCD0008");
187: } else {
188: for (Iterator i = supportsElements.iterator(); i.hasNext();) {
189: Element supportsElement = (Element) i.next();
190: SupportsDescriptor supportsDescriptor = new SupportsDescriptor();
191: supportsDescriptor.load(supportsElement, namespace);
192: _supportsDescriptors.add(supportsDescriptor);
193: }
194:
195: }
196:
197: // loads preferences descriptor
198: Element pPreferencesElement = element.getChild(
199: PORTLET_PREFERENCES, namespace);
200: if (pPreferencesElement != null) {
201: _pPreferencesDescriptor = new PortletPreferencesDescriptor(
202: debugLogger, _portletName);
203: _pPreferencesDescriptor
204: .load(pPreferencesElement, namespace);
205:
206: }
207: // loads init param descriptor
208: List initParamElements = element.getChildren(INIT_PARAM,
209: namespace);
210: for (Iterator i = initParamElements.iterator(); i.hasNext();) {
211: Element initParamElement = (Element) i.next();
212: InitParamDescriptor initParamDescriptor = new InitParamDescriptor();
213: initParamDescriptor.load(initParamElement, namespace);
214: _initParamDescriptors.add(initParamDescriptor);
215: }
216:
217: // loads cache descriptor
218: _cacheExpiration = element.getChildTextTrim(EXPIRATION_CACHE,
219: namespace);
220:
221: // loads info descriptor
222: Element infoElement = element.getChild(PORTLET_INFO, namespace);
223: if (infoElement != null) {
224: _infoDescriptor = new PortletInfoDescriptor(debugLogger);
225: _infoDescriptor.load(infoElement, namespace);
226: }
227:
228: // loads security role ref descriptors
229: List secRoleElements = element.getChildren(SECURITY_ROLE_REF,
230: namespace);
231: for (int i = 0; i < secRoleElements.size(); i++) {
232: Element secRoleElement = (Element) secRoleElements.get(i);
233: SecurityRoleRefDescriptor secRoleRefDescriptor = new SecurityRoleRefDescriptor(
234: debugLogger);
235: secRoleRefDescriptor.load(secRoleElement, namespace);
236: _secRoleRefDescriptors.add(secRoleRefDescriptor);
237: }
238:
239: // loads resource bundle
240: _resourceBundle = element.getChildTextTrim(RESOURCE_BUNDLE,
241: namespace);
242: }
243:
244: /**
245: * Returns portlet name.
246: * <P>
247: * @return <code>String</code> of the portlet name.
248: */
249: public String getPortletName() {
250: return _portletName;
251: }
252:
253: /**
254: * Returns portlet class name.
255: * <P>
256: * @return <code>String</code> of the portlet class name.
257: */
258: public String getClassName() {
259: return _portletClassName;
260: }
261:
262: /**
263: * Returns the description as a <code>String</code>. If there's more
264: * than one descriptions are defined, returns the first one.
265: * <P>
266: * @return <code>String</code> of the description. The return
267: * value could be null if no description is defined.
268: */
269: public String getDescription() {
270: String description = null;
271: if (!_descriptions.isEmpty()) {
272: description = (String) _descriptions.get(0);
273: }
274: return description;
275: }
276:
277: /**
278: * Returns portlet descriptions in a List.
279: * <P>
280: * @return <code>List</code> of <code>String</code>s of the
281: * portlet descriptions. Empty <code>List</code> will be returned
282: * if not defined.
283: */
284: public List getDescriptions() {
285: return _descriptions;
286: }
287:
288: /**
289: * Returns portlet descriptions in a Map.
290: * <P>
291: * @return <code>Map</code> of lang/description pairs of the
292: * portlet descriptions. Empty <code>Map</code> will be returned
293: * if not defined.
294: */
295: public Map getDescriptionMap() {
296: return _descriptionMap;
297: }
298:
299: /**
300: * Returns portlet display names in a List.
301: * <P>
302: * @return <code>List</code> of <code>String</code>s of the
303: * portlet display names. Empty <code>List</code> will be returned
304: * if not defined.
305: */
306: public List getDisplayNames() {
307: return _displayNames;
308: }
309:
310: /**
311: * Returns portlet display names in a Map
312: * <P>
313: * @return <code>Map</code> of lang/display name pairs of the
314: * portlet display names. Empty <code>Map</code> will be returned
315: * if not defined.
316: */
317: public Map getDisplayNameMap() {
318: return _displayNameMap;
319: }
320:
321: /**
322: * Returns the supported locales in a List.
323: * <P>
324: * @return <code>List</code> of <code>String</code>s of the
325: * supported locales. Empty <code>List</code> will be returned
326: * if not defined.
327: */
328: public List getSupportedLocales() {
329: return _supportedLocales;
330: }
331:
332: /**
333: * Returns the portlet preferences descriptor.
334: * <P>
335: * @return <code>PortletPreferencesDescriptor</code>
336: */
337: public PortletPreferencesDescriptor getPortletPreferencesDescriptor() {
338: return _pPreferencesDescriptor;
339: }
340:
341: /**
342: * Returns the init param descriptors.
343: * <P>
344: * @return <code>List</code> of <code>InitParamDescriptor</code>s.
345: */
346: public List getInitParamDescriptors() {
347: return _initParamDescriptors;
348: }
349:
350: /**
351: * Returns the supports descriptors.
352: * <P>
353: * @return <code>List</code> of <code>SupportsDescriptor</code>s.
354: */
355: public List getSupportsDescriptors() {
356: return _supportsDescriptors;
357: }
358:
359: /**
360: * Returns the supported mime types.
361: * <P>
362: * @return <code>List</code> of supported mime types.
363: */
364: public List getSupportedMimeTypes() {
365: List mimeTypeList = new ArrayList();
366: Iterator iterator = _supportsDescriptors.iterator();
367: while (iterator.hasNext()) {
368: SupportsDescriptor supportsDescriptor = (SupportsDescriptor) iterator
369: .next();
370: mimeTypeList.add(supportsDescriptor.getMimeType());
371: }
372: return mimeTypeList;
373: }
374:
375: /**
376: * Returns the supported portlet modes of a specified mime type.
377: * <P>
378: * @return <code>List</code> of supported portlet modes.
379: */
380: public List getSupportedPortletModes(String mime) {
381:
382: List supportList = null;
383: boolean stop = false;
384: Iterator iterator = _supportsDescriptors.iterator();
385: while (iterator.hasNext() && !stop) {
386: SupportsDescriptor supportsDescriptor = (SupportsDescriptor) iterator
387: .next();
388: String mimeType = supportsDescriptor.getMimeType();
389: if (mimeType.toLowerCase().equals("*/*")) {
390: supportList = supportsDescriptor.getPortletModes();
391: stop = true;
392: } else if (mimeType.toLowerCase().equals("text/*")
393: && mime.toLowerCase().startsWith("text/")) {
394: supportList = supportsDescriptor.getPortletModes();
395: stop = true;
396: } else if (supportsDescriptor.getMimeType().equals(mime)) {
397: supportList = supportsDescriptor.getPortletModes();
398: stop = true;
399: }
400: }
401:
402: return supportList;
403: }
404:
405: /**
406: * Returns the cache expiration.
407: * <P>
408: * @return <code>int</code> of expiration number. Returns -999 if
409: * expiration is not defined. A value of -1 means cache is never
410: * expired, a value of 0 means cache is disabled.
411: */
412: public int getCacheExpiration() {
413: int retValue = EXPIRATION_CACHE_NOT_DEFINED;
414: if (_cacheExpiration != null) {
415: try {
416: retValue = Integer.parseInt(_cacheExpiration);
417: } catch (NumberFormatException ne) {
418: debugLogger.log(Level.WARNING, "PSPL_PCCCSPPCCD0009",
419: _portletName);
420: }
421: }
422: return retValue;
423:
424: }
425:
426: /**
427: * Returns the portlet info descriptor.
428: * <P>
429: * @return <code>PortletInfoDescriptor</code>
430: */
431: public PortletInfoDescriptor getPortletInfoDescriptor() {
432: return _infoDescriptor;
433: }
434:
435: /**
436: * Returns the security role ref descriptors.
437: * <P>
438: * @return <code>List</code> of <code>SecurityRoleRefDescriptor</code>s.
439: */
440: public List getSecurityRoleRefDescriptors() {
441: return _secRoleRefDescriptors;
442: }
443:
444: public boolean sessionEnabled() {
445: boolean sessionEnabled = true;
446:
447: if (_depExtDescriptor != null) {
448: sessionEnabled = _depExtDescriptor
449: .sessionEnabled(getPortletName());
450: }
451:
452: return sessionEnabled;
453: }
454:
455: public LinkedHashMap getGeneratedEvents() {
456: return _depExtDescriptor.getGeneratedEvents();
457: }
458:
459: public LinkedHashMap getConsumeEvents() {
460: return _depExtDescriptor.getConsumeEvents();
461: }
462:
463: /**
464: * Returns the name of the resource bundle. Clients
465: * should call this method only if <code>getType</code>() returns
466: * RESOURCE_TYPE_RB.
467: * <P>
468: * @return <code>String</code> of the resource bundle.
469: */
470: public String getResourceBundle() {
471: return _resourceBundle;
472: }
473:
474: /**
475: * The toString method.
476: * <P>
477: * @return the <code>String</code> representation of the portlet
478: * descriptor.
479: */
480: public String toString() {
481: StringBuffer sb = new StringBuffer("PortletDescriptor ");
482:
483: sb.append(" portlet name [");
484: if (_portletName != null) {
485: sb.append(_portletName);
486: } else {
487: sb.append("NULL");
488: }
489: sb.append("]");
490:
491: sb.append(" portlet class [");
492: if (_portletClassName != null) {
493: sb.append(_portletClassName);
494: } else {
495: sb.append("NULL");
496: }
497: sb.append("]");
498:
499: sb.append(" portlet descriptions [");
500: Iterator iterator0 = _descriptions.iterator();
501: while (iterator0.hasNext()) {
502: sb.append((String) iterator0.next());
503: }
504: sb.append(" ]");
505:
506: sb.append(" portlet display names [");
507: Iterator iterator3 = _displayNames.iterator();
508: while (iterator3.hasNext()) {
509: sb.append((String) iterator3.next());
510: }
511: sb.append(" ]");
512:
513: sb.append(" portlet supported locales [");
514: Iterator iterator5 = _supportedLocales.iterator();
515: while (iterator5.hasNext()) {
516: sb.append((String) iterator5.next());
517: }
518: sb.append(" ]");
519:
520: Iterator iterator = _initParamDescriptors.iterator();
521: while (iterator.hasNext()) {
522: InitParamDescriptor initParamDescriptor = (InitParamDescriptor) iterator
523: .next();
524: sb.append(initParamDescriptor.toString());
525: sb.append("\n");
526: }
527:
528: Iterator iterator1 = _supportsDescriptors.iterator();
529: while (iterator1.hasNext()) {
530: SupportsDescriptor supportsDescriptor = (SupportsDescriptor) iterator1
531: .next();
532: sb.append(supportsDescriptor.toString());
533: sb.append("\n");
534: }
535:
536: if (_cacheExpiration != null) {
537: sb.append(_cacheExpiration);
538: }
539:
540: if (_infoDescriptor != null) {
541: sb.append(_infoDescriptor.toString());
542: }
543:
544: if (_pPreferencesDescriptor != null) {
545: sb.append(_pPreferencesDescriptor.toString());
546: }
547:
548: Iterator iterator2 = _secRoleRefDescriptors.iterator();
549: while (iterator2.hasNext()) {
550: SecurityRoleRefDescriptor secRoleRefDescriptor = (SecurityRoleRefDescriptor) iterator2
551: .next();
552: sb.append(secRoleRefDescriptor.toString());
553: sb.append("\n");
554: }
555:
556: sb.append("deployment extension descriptor: ["
557: + _depExtDescriptor);
558: if (_depExtDescriptor != null) {
559: _depExtDescriptor.toString();
560: }
561: sb.append(" ]");
562:
563: if (_resourceBundle != null) {
564: sb.append(" resource bundle [");
565: sb.append(_resourceBundle);
566: sb.append("]");
567: }
568:
569: return (sb.toString());
570: }
571:
572: }
|