001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.driver.services.portal;
018:
019: /**
020: * Configuration of a portlet window on the portal page.
021: */
022: public class PortletWindowConfig {
023:
024: // Private Member Variables ------------------------------------------------
025:
026: /** The context path of the associated portlet. */
027: private String contextPath;
028:
029: /** The portlet name. */
030: private String portletName;
031:
032: /** The metaInfo that provides uniqueness across window ids. */
033: private String metaInfo;
034:
035: // Constructor -------------------------------------------------------------
036:
037: /**
038: * No-arg constructor.
039: */
040: public PortletWindowConfig() {
041:
042: }
043:
044: private PortletWindowConfig(String contextPath, String portletName,
045: String metaInfo) {
046: this .contextPath = contextPath;
047: this .portletName = portletName;
048: this .metaInfo = metaInfo;
049: }
050:
051: // Public Methods ----------------------------------------------------------
052:
053: public String getId() {
054: return createPortletId(contextPath, portletName, metaInfo);
055: }
056:
057: public String getContextPath() {
058: return contextPath;
059: }
060:
061: public void setContextPath(String contextPath) {
062: this .contextPath = contextPath;
063: }
064:
065: public String getPortletName() {
066: return portletName;
067: }
068:
069: public void setPortletName(String portletName) {
070: this .portletName = portletName;
071: }
072:
073: public String getMetaInfo() {
074: return metaInfo;
075: }
076:
077: public void setMetaInfo(String metaInfo) {
078: this .metaInfo = metaInfo;
079: }
080:
081: // Public Static Methods ---------------------------------------------------
082:
083: /**
084: * Creates the portlet ID from context path and portlet name. The portlet ID
085: * is constructed by concatinating the context path and the portlet name
086: * using a dot ('.').
087: *
088: * The method checks that the portlet name parameter does not have a dot. This check
089: * is not done for the portlet ID.
090: *
091: * @param contextPath the portlet context path.
092: * @param portletName the portlet name.
093: * @throws IllegalArgumentException if the portletName has a dot
094: * @throws NullPointerException if the portlet Name or context path is null.
095: */
096: public static String createPortletId(String contextPath,
097: String portletName, String metaInfo)
098: throws NullPointerException, IllegalArgumentException {
099:
100: if (contextPath == null) {
101: throw new NullPointerException(
102: "Context path must not be null.");
103: }
104: if (portletName == null) {
105: throw new NullPointerException(
106: "Portlet name must not be null.");
107: }
108: if (portletName.indexOf('.') != -1) {
109: throw new IllegalArgumentException(
110: "Portlet name must not have a dot(period). Please remove the dot from the value of the portlet-name element ("
111: + portletName + ") in portlet.xml");
112: }
113:
114: if (metaInfo == null) {
115: metaInfo = "";
116: }
117: return contextPath + "." + portletName + "!" + metaInfo;
118: }
119:
120: /**
121: * Parses out the portlet context path from the portlet ID.
122: * @param portletId the portlet ID to parse.
123: * @return the portlet context path.
124: */
125: public static String parseContextPath(String portletId) {
126: int index = getSeparatorIndex(portletId);
127: return portletId.substring(0, index);
128: }
129:
130: /**
131: * Parses out the portlet context path from the portlet ID.
132: * @param portletId the portlet ID to parse.
133: * @return the portlet context path.
134: */
135: public static String parsePortletName(String portletId) {
136: int index = getSeparatorIndex(portletId);
137: String postfix = portletId.substring(index + 1);
138: index = postfix.indexOf("!");
139: if (index > -1) {
140: return postfix.substring(0, index);
141: }
142: return postfix;
143: }
144:
145: public static String parseMetaInfo(String portletId) {
146: int index = portletId.indexOf("!");
147: if (index > -1) {
148: return portletId.substring(index + 1);
149: }
150: return "";
151: }
152:
153: // Private Static Method ---------------------------------------------------
154:
155: /**
156: * Parses the portlet ID and returns the separator (".") index. The portlet
157: * ID passed in should be a valid ID: not null, not starts with ".",
158: * not ends with ".", and contains ".". The portlet ID can have more than
159: * one dot, but the last one is taken to be the separator.
160: *
161: * @param portletId the portlet ID to parse.
162: * @return the separator index.
163: * @throws IllegalArgumentException if portlet ID does not contain a dot or the dot is the first or last character.
164: * @throws NullPointerException if the portlet ID is null
165: */
166: private static int getSeparatorIndex(String portletId)
167: throws NullPointerException, IllegalArgumentException {
168: if (portletId == null) {
169: throw new NullPointerException("Portlet ID is null");
170: }
171:
172: int index = portletId.lastIndexOf(".");
173: if (index <= 0 || index == portletId.length() - 1) {
174: throw new IllegalArgumentException(
175: "Portlet ID '"
176: + portletId
177: + "' does not contain a dot or the dot is the first or last character");
178: }
179: return index;
180: }
181:
182: public static PortletWindowConfig fromId(String portletWindowId) {
183: String contextPath = parseContextPath(portletWindowId);
184: String portletName = parsePortletName(portletWindowId);
185: String metaInfo = parseMetaInfo(portletWindowId);
186: return new PortletWindowConfig(contextPath, portletName,
187: metaInfo);
188: }
189: }
|