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.portletcontainercommon.descriptor;
014:
015: import java.io.InputStream;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.logging.Logger;
021:
022: import org.jdom.Element;
023: import org.jdom.Namespace;
024: import com.sun.portal.log.common.PortalLogger;
025:
026: /**
027: * The Portlet Application Descriptor is the main entry point of accessing
028: * the portlet deployment descriptor.
029: * <P>
030: * The PortletAppDescriptor loads the portlet app descriptors into memory,
031: * it delegates the loading of sub descriptors to the individual descriptor
032: * classes. The load() method is used to load the portlet app descriptor.
033: * Note that the load() method also loads the immediate children of the
034: * portlet app descriptor' by calling their load() methods.
035: * <P>
036: * The PortletAppDescriptor class provides member methods to get to its
037: * sub descriptors, as well as portlet app name and description.
038: * The following get**() methods are available:
039: * <UL>
040: * <LI><code>getPortletsDescriptor()</code>
041: * <LI><code>getPortletAppName()</code>
042: * <LI><code>getUserAttributeDescriptors()</code>
043: * <LI><code>getPortletAppDescription()</code>
044: *</UL>
045: *
046: */
047: public class PortletAppDescriptor {
048:
049: // Portlet App Descriptor Element Names
050: public static final String PORTLET_APP = "portlet-app";
051: public static final String PORTLET_APP_NAME = "portlet-app-name";
052: public static final String DESCRIPTION = "description";
053: public static final String DISPLAY_NAME = "display-name";
054: public static final String USER_ATTRIBUTE = "user-attribute";
055: public static final String SECURITY_CONSTRAINT = "security-constraint";
056: public static final String XML_LANG_ATTR = "lang";
057:
058: private List _descriptions = new ArrayList();
059: private List _displayNames = new ArrayList();
060: private PortletsDescriptor _portletsDescriptor;
061: private SecurityConstraintDescriptor _secConstraintDescriptor;
062: private List _userAttributeDescriptors = new ArrayList();
063: private InputStream _extStream;
064: private DeploymentExtensionDescriptor _depExtDescriptor;
065:
066: // Create a logger for this class
067: private static Logger debugLogger = PortalLogger
068: .getLogger(PortletAppDescriptor.class);
069:
070: public PortletAppDescriptor(Logger logger, InputStream extStream) {
071: _extStream = extStream;
072: }
073:
074: /**
075: * Loads the portlet app descriptor into memory.
076: * <P>
077: * This class only loads the top level elements, it delegates the load
078: * action into sub descriptor classes.
079: * <P>
080: * @param The root Element of the deployment descriptor document
081: */
082: public void load(Element root, Namespace namespace,
083: DeploymentDescriptorReader reader)
084: throws DeploymentDescriptorException {
085:
086: List descriptionElements = root.getChildren(DESCRIPTION,
087: namespace);
088: for (Iterator i = descriptionElements.iterator(); i.hasNext();) {
089: Element descriptionElement = (Element) i.next();
090: _descriptions.add(descriptionElement.getTextTrim());
091: }
092:
093: List displayNameElements = root.getChildren(DISPLAY_NAME,
094: namespace);
095: for (Iterator i = displayNameElements.iterator(); i.hasNext();) {
096: Element displayNameElement = (Element) i.next();
097:
098: _displayNames.add(displayNameElement.getTextTrim());
099: }
100:
101: //load deployment extension
102: if (_extStream != null) {
103: _depExtDescriptor = reader
104: .loadDeploymentExtensionDescriptor(_extStream);
105: }
106:
107: _portletsDescriptor = new PortletsDescriptor(debugLogger,
108: _depExtDescriptor);
109: _portletsDescriptor.load(root, namespace);
110:
111: // loads user attribute descriptors
112: List userAttrElements = root.getChildren(USER_ATTRIBUTE,
113: namespace);
114: for (int i = 0; i < userAttrElements.size(); i++) {
115: Element userAttrElement = (Element) userAttrElements.get(i);
116: UserAttributeDescriptor userAttrDescriptor = new UserAttributeDescriptor(
117: debugLogger);
118: userAttrDescriptor.load(userAttrElement, namespace);
119: _userAttributeDescriptors.add(userAttrDescriptor);
120: }
121:
122: // loads security-constraint
123: Element secConstraintElement = root.getChild(
124: SECURITY_CONSTRAINT, namespace);
125: if (secConstraintElement != null) {
126: _secConstraintDescriptor = new SecurityConstraintDescriptor(
127: debugLogger);
128: _secConstraintDescriptor.load(secConstraintElement,
129: namespace);
130: }
131:
132: }
133:
134: /**
135: * Returns the PortletsDescriptor.
136: * <P>
137: * @return PortletsDescriptor
138: */
139: public PortletsDescriptor getPortletsDescriptor() {
140: return _portletsDescriptor;
141: }
142:
143: /**
144: * Returns the UserAttributeDescriptors.
145: * <P>
146: * @return <code>List</code> of UserAttributeDescriptors.
147: */
148: public List getUserAttributeDescriptors() {
149: return _userAttributeDescriptors;
150: }
151:
152: /**
153: * Returns the app description as a <code>String</code>. If there's more
154: * than one descriptions are defined, returns the first one.
155: * <P>
156: * @return <code>String</code> of the description. The return
157: * value could be null if no description is defined.
158: */
159: public String getPortletAppDescription() {
160: String description = null;
161: if (!_descriptions.isEmpty()) {
162: description = (String) _descriptions.get(0);
163: }
164: return description;
165: }
166:
167: /**
168: * Returns the portlet app descriptions.
169: * <P>
170: * @return <code>List</code> of the <code>String</code>s. The returned value could
171: * be empty list if description is not defined.
172: */
173: public List getPortletAppDescriptions() {
174: return _descriptions;
175: }
176:
177: /**
178: * Returns the portlet app display names.
179: * <P>
180: * @return <code>List</code> of <code>String</code>s. The returned value could
181: * be empty list if display name is not defined.
182: */
183: public List getPortletAppDisplayNames() {
184: return _displayNames;
185: }
186:
187: /**
188: * Returns the SecurityConstrintDescriptor.
189: * <P>
190: * @return SecurityConstraintDescriptor
191: */
192: public SecurityConstraintDescriptor getSecurityConstraintDescriptor() {
193: return _secConstraintDescriptor;
194: }
195:
196: /*
197: * Returns whether the portlet app allows preferences values to be
198: * saved in the render method.
199: *
200: * @return true if preferences can be saved in the render method,
201: * false if not.
202: */
203: public boolean isSavePreferencesAllowed() {
204: boolean isAllowed = false;
205:
206: if (_depExtDescriptor != null) {
207: isAllowed = _depExtDescriptor.isSavePreferencesAllowed();
208: }
209:
210: return isAllowed;
211:
212: }
213:
214: public DeploymentExtensionDescriptor getDeploymentExtensionDescriptor() {
215: return _depExtDescriptor;
216: }
217:
218: /**
219: * The toString method.
220: * <P>
221: * @return the <code>String</code> representation of the deployment
222: * descriptor.
223: */
224: public String toString() {
225: StringBuffer sb = new StringBuffer("PortletAppDescriptor [");
226:
227: sb.append(" portlet app descriptions [");
228: Iterator iterator0 = _descriptions.iterator();
229: while (iterator0.hasNext()) {
230: sb.append((String) iterator0.next());
231: }
232: sb.append(" ]");
233:
234: sb.append(" portlet app display names [");
235: Iterator iterator2 = _displayNames.iterator();
236: while (iterator2.hasNext()) {
237: sb.append((String) iterator2.next());
238: }
239: sb.append(" ]");
240:
241: sb.append(getPortletsDescriptor().toString());
242:
243: Iterator iterator1 = _userAttributeDescriptors.iterator();
244: while (iterator1.hasNext()) {
245: UserAttributeDescriptor userAttrDescriptor = (UserAttributeDescriptor) iterator1
246: .next();
247: sb.append(userAttrDescriptor.toString());
248: sb.append("\n");
249: }
250:
251: sb.append("]");
252: sb.append(" security constraint description [");
253: if (_secConstraintDescriptor != null) {
254: sb.append(_secConstraintDescriptor.toString());
255: }
256:
257: sb.append("]");
258:
259: return sb.toString();
260: }
261: }
|