001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-service-impl/impl/src/java/org/sakaiproject/portal/service/PortletTool.java $
003: * $Id: PortletTool.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.portal.service;
021:
022: import java.io.File;
023: import java.util.Collections;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Properties;
027: import java.util.Set;
028:
029: import javax.servlet.ServletContext;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.pluto.descriptors.portlet.PortletDD;
034: import org.apache.pluto.descriptors.portlet.PortletInfoDD;
035: import org.apache.pluto.internal.InternalPortletContext;
036: import org.sakaiproject.component.cover.ServerConfigurationService;
037: import org.sakaiproject.portal.api.PortalService;
038: import org.sakaiproject.tool.api.Tool;
039: import org.sakaiproject.tool.cover.ActiveToolManager;
040:
041: /**
042: * <p>
043: * Tool is a utility class that implements the Tool interface.
044: * </p>
045: *
046: * @author ieb
047: * @since Sakai 2.4
048: * @version $Rev: 29143 $
049: */
050: public class PortletTool implements org.sakaiproject.tool.api.Tool,
051: Comparable {
052: /** Our log (commons). */
053: private static Log M_log = LogFactory.getLog(PortletTool.class);
054:
055: /** The access security. */
056: protected PortletTool.AccessSecurity m_accessSecurity = PortletTool.AccessSecurity.PORTAL;
057:
058: /** The set of categories. */
059: protected Set m_categories = new HashSet();
060:
061: /** The description string. */
062: protected String m_description = null;
063:
064: /**
065: * The configuration properties that are set by registration and may not be
066: * changed by confguration.
067: */
068: protected Properties m_finalConfig = new Properties();
069:
070: /** Home destination. */
071: protected String m_home = null;
072:
073: /** The well known identifier string. */
074: protected String m_id = null;
075:
076: /** The set of keywords. */
077: protected Set m_keywords = new HashSet();
078:
079: /** The configuration properties that may be changed by configuration. */
080: protected Properties m_mutableConfig = new Properties();
081:
082: /** The title string. */
083: protected String m_title = null;
084:
085: /** The parsed tool registration (if any) * */
086: protected Tool m_tool = null;
087:
088: public PortletTool(PortletDD pdd, InternalPortletContext portlet,
089: ServletContext portalContext) {
090: String portletSupport = ServerConfigurationService
091: .getString("portlet.support");
092:
093: String portletName = pdd.getPortletName();
094: String appName = portlet.getApplicationId();
095: String homePath = ServerConfigurationService.getSakaiHomePath()
096: + "/portlets/";
097: String portletReg = homePath + appName + "/" + portletName
098: + ".xml";
099:
100: File toolRegFile = new File(portletReg);
101: if (!toolRegFile.canRead()) {
102: portletReg = homePath + portletName + ".xml";
103: toolRegFile = new File(portletReg);
104: }
105:
106: // Attempt to read and parse the registraiton file
107: List<Tool> toolRegs = ActiveToolManager.parseTools(new File(
108: portletReg));
109:
110: // We ignore the tool registrations other than the first
111: if (toolRegs != null && toolRegs.size() > 0) {
112: Tool t = toolRegs.get(0);
113: m_id = t.getId();
114: m_title = t.getTitle();
115: m_description = t.getDescription();
116: m_categories = t.getCategories();
117: // get the FinalConfig from the tool and make a copy
118: // locally as we will add information
119: Properties rv = t.getFinalConfig();
120: m_finalConfig.putAll(rv);
121: m_keywords = t.getKeywords();
122: m_mutableConfig = t.getMutableConfig();
123: // RegisteredConfig is derived in the getter of this class
124: M_log
125: .info("Portlet registered from tool registration with Sakai toolId="
126: + m_id);
127: } else {
128: m_id = "portlet." + portlet.getApplicationId() + "."
129: + pdd.getPortletName();
130: PortletInfoDD pidd = pdd.getPortletInfo();
131: if (pidd != null) {
132: m_title = pidd.getShortTitle();
133: m_description = pidd.getTitle();
134: }
135: if (m_title == null)
136: m_title = pdd.getPortletName();
137: if (m_description == null)
138: m_description = pdd.getPortletName();
139:
140: if ("stealth".equals(portletSupport)) {
141: M_log
142: .info("Portlet stealth-registered with Sakai toolId="
143: + m_id);
144: } else {
145: m_categories.add("myworkspace");
146: m_categories.add("project");
147: m_categories.add("course");
148: M_log.info("Portlet auto-registered with Sakai toolId="
149: + m_id);
150: }
151: }
152:
153: // Indicate that these tools are indeed portlets and where to dispatch
154: // the portlet
155: m_finalConfig.setProperty(
156: PortalService.TOOL_PORTLET_CONTEXT_PATH, portlet
157: .getApplicationId());
158: m_finalConfig.setProperty(PortalService.TOOL_PORTLET_NAME, pdd
159: .getPortletName());
160: m_finalConfig.setProperty(PortalService.TOOL_PORTLET_APP_NAME,
161: appName);
162: }
163:
164: /**
165: * @inheritDoc
166: */
167: public int compareTo(Object obj) {
168: // let it throw a class case exception if the obj is not some sort of
169: // Tool
170: org.sakaiproject.tool.api.Tool tool = (org.sakaiproject.tool.api.Tool) obj;
171:
172: // do an id based
173: return getId().compareTo(tool.getId());
174: }
175:
176: /**
177: * {@inheritDoc}
178: */
179: @Override
180: public boolean equals(Object obj) {
181: if (!(obj instanceof PortletTool)) {
182: return false;
183: }
184:
185: return ((PortletTool) obj).getId().equals(getId());
186: }
187:
188: /**
189: * @inheritDoc
190: */
191: public PortletTool.AccessSecurity getAccessSecurity() {
192: return m_accessSecurity;
193: }
194:
195: /**
196: * @inheritDoc
197: */
198: public Set getCategories() {
199: return Collections.unmodifiableSet(m_categories);
200: }
201:
202: /**
203: * @inheritDoc
204: */
205: public String getDescription() {
206: return m_description;
207: }
208:
209: /**
210: * @inheritDoc
211: */
212: public Properties getFinalConfig() {
213: // return a copy so that it is read only
214: Properties rv = new Properties();
215: rv.putAll(m_finalConfig);
216: return rv;
217: }
218:
219: /**
220: * {@inheritDoc}
221: */
222: public String getHome() {
223: return m_home;
224: }
225:
226: /**
227: * @inheritDoc
228: */
229: public String getId() {
230: return m_id;
231: }
232:
233: /**
234: * @inheritDoc
235: */
236: public Set getKeywords() {
237: return Collections.unmodifiableSet(m_keywords);
238: }
239:
240: /**
241: * @inheritDoc
242: */
243: public Properties getMutableConfig() {
244: // return a copy so that it is read only
245: Properties rv = new Properties();
246: rv.putAll(m_mutableConfig);
247: return rv;
248: }
249:
250: /**
251: * @inheritDoc
252: */
253: public Properties getRegisteredConfig() {
254: // combine the final and mutable, and return a copy so that it is read
255: // only
256: Properties rv = new Properties();
257: rv.putAll(m_finalConfig);
258: rv.putAll(m_mutableConfig);
259: return rv;
260: }
261:
262: /**
263: * @inheritDoc
264: */
265: public String getTitle() {
266: return m_title;
267: }
268:
269: /**
270: * {@inheritDoc}
271: */
272: @Override
273: public int hashCode() {
274: return getId().hashCode();
275: }
276:
277: /**
278: * Set the access security.
279: *
280: * @param access
281: * The new access security setting.
282: */
283: public void setAccessSecurity(PortletTool.AccessSecurity access) {
284: m_accessSecurity = access;
285: }
286:
287: /**
288: * Set the categories.
289: *
290: * @param categories
291: * The new categories set (Strings).
292: */
293: public void setCategories(Set categories) {
294: m_categories = categories;
295: }
296:
297: /**
298: * Set the description.
299: *
300: * @param description
301: * The description to set.
302: */
303: public void setDescription(String description) {
304: m_description = description;
305: }
306:
307: public void setHome(String home) {
308: m_home = home;
309: }
310:
311: /**
312: * Set the id.
313: *
314: * @param m_id
315: * The m_id to set.
316: */
317: public void setId(String id) {
318: m_id = id;
319: }
320:
321: /**
322: * Set the keywords.
323: *
324: * @param keywords
325: * The new keywords set (Strings).
326: */
327: public void setKeywords(Set keywords) {
328: m_keywords = keywords;
329: }
330:
331: /**
332: * Set the registered configuration.
333: *
334: * @param config
335: * The new registered configuration Properties.
336: */
337: public void setRegisteredConfig(Properties finalConfig,
338: Properties mutableConfig) {
339: m_finalConfig = finalConfig;
340: if (m_finalConfig == null) {
341: m_finalConfig = new Properties();
342: }
343:
344: m_mutableConfig = mutableConfig;
345: if (m_mutableConfig == null) {
346: m_mutableConfig = new Properties();
347: }
348: }
349:
350: /**
351: * Set the title.
352: *
353: * @param title
354: * The title to set.
355: */
356: public void setTitle(String title) {
357: m_title = title;
358: }
359: }
|