<commit_before>/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2005, 2006 The Sakai Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.portal.service;

import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;

import org.apache.pluto.descriptors.portlet.PortletDD;
import org.apache.pluto.internal.InternalPortletContext;
import org.sakaiproject.portal.api.PortalService;

/**
 * <p>
 * Tool is a utility class that implements the Tool interface.
 * </p>
 */
public class PortletTool implements org.sakaiproject.tool.api.Tool, Comparable
{
	/** The access security. */
	protected PortletTool.AccessSecurity m_accessSecurity = PortletTool.AccessSecurity.PORTAL;

	/** The set of categories. */
	protected Set m_categories = new HashSet();

	/** The description string. */
	protected String m_description = null;

	/** The configuration properties that are set by registration and may not be changed by confguration. */
	protected Properties m_finalConfig = new Properties();

	/** Home destination. */
	protected String m_home = null;

	/** The well known identifier string. */
	protected String m_id = null;

	/** The set of keywords. */
	protected Set m_keywords = new HashSet();

	/** The configuration properties that may be changed by configuration. */
	protected Properties m_mutableConfig = new Properties();

	/** The title string. */
	protected String m_title = null;


	public PortletTool(PortletDD pdd, InternalPortletContext portlet, ServletContext portalContext)
	{
		m_id = "portlet."+portlet.getApplicationId()+"."+pdd.getPortletName();
		m_categories.add("myworkspace");
		m_categories.add("project");
		m_categories.add("course");
		if ( pdd.getPortletInfo() != null ) {
			m_description = "Auto Registered Portlet "+pdd.getPortletInfo().getTitle();
			m_title = "Portlet: "+pdd.getPortletInfo().getShortTitle();
		} else {
			m_description = "Auto Registered Portlet "+pdd.getPortletName();
			m_title = "Portlet: "+pdd.getPortletName();
		}
		m_finalConfig.setProperty(PortalService.TOOL_PORTLET_CONTEXT_PATH, portlet.getApplicationId());
		m_finalConfig.setProperty(PortalService.TOOL_PORTLET_NAME, pdd.getPortletName());
		
	}

	/**
	 * @inheritDoc
	 */
	public int compareTo(Object obj)
	{
		// let it throw a class case exception if the obj is not some sort of Tool
		org.sakaiproject.tool.api.Tool tool = (org.sakaiproject.tool.api.Tool) obj;

		// do an id based
		return getId().compareTo(tool.getId());
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj)
	{<commit_msg>Add features to Sakai's JSR-168 support as documented in<commit_after>/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2005, 2006 The Sakai Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.portal.service;

import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.List;
import java.util.Iterator;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContext;

import org.apache.pluto.descriptors.portlet.PortletDD;
import org.apache.pluto.internal.InternalPortletContext;
import org.sakaiproject.portal.api.PortalService;
import org.sakaiproject.tool.api.Tool;
import org.sakaiproject.tool.cover.ActiveToolManager;
import org.sakaiproject.component.cover.ServerConfigurationService;

/**
 * <p>
 * Tool is a utility class that implements the Tool interface.
 * </p>
 */
public class PortletTool implements org.sakaiproject.tool.api.Tool, Comparable
{
    	/** Our log (commons).  */
    	private static Log M_log = LogFactory.getLog(PortletTool.class);

	/** The access security. */
	protected PortletTool.AccessSecurity m_accessSecurity = PortletTool.AccessSecurity.PORTAL;

	/** The set of categories. */
	protected Set m_categories = new HashSet();

	/** The description string. */
	protected String m_description = null;

	/** The configuration properties that are set by registration and may not be changed by confguration. */
	protected Properties m_finalConfig = new Properties();

	/** Home destination. */
	protected String m_home = null;

	/** The well known identifier string. */
	protected String m_id = null;

	/** The set of keywords. */
	protected Set m_keywords = new HashSet();

	/** The configuration properties that may be changed by configuration. */
	protected Properties m_mutableConfig = new Properties();

	/** The title string. */
	protected String m_title = null;

	/** The parsed tool registration (if any) **/
	protected Tool m_tool = null;

	public PortletTool(PortletDD pdd, InternalPortletContext portlet, ServletContext portalContext)
	{
		String portletSupport = ServerConfigurationService.getString("portlet.support");

		String portletName = pdd.getPortletName();
		String appName = portlet.getApplicationId();
		String homePath = ServerConfigurationService.getSakaiHomePath()+"/portlets/";
		String portletReg = homePath+appName+"/"+portletName+".xml";

		File toolRegFile = new File(portletReg);
		if ( ! toolRegFile.canRead() ) 
		{
			portletReg  = homePath+portletName+".xml";
			toolRegFile = new File(portletReg);
		}

		// Attempt to read and parse the registraiton file
		List<Tool> toolRegs = ActiveToolManager.parseTools(new File(portletReg));

		// We ignore the tool registrations other than the first
		if ( toolRegs != null && toolRegs.size() > 0 ) 
		{
			Tool t = toolRegs.get(0);
			m_id = t.getId();
			m_title = t.getTitle();
			m_description = t.getDescription();
			m_categories = t.getCategories();
       			// get the FinalConfig from the tool and make a copy 
			// locally as we will add information
      			Properties rv = t.getFinalConfig();
       			m_finalConfig.putAll(rv);
			m_keywords = t.getKeywords();
			m_mutableConfig = t.getMutableConfig();
			// RegisteredConfig is derived in the getter of this class
			M_log.info("Portlet registered from tool registration with Sakai toolId="+m_id);
		} else {
			m_id = "portlet."+portlet.getApplicationId()+"."+pdd.getPortletName();
			m_description = pdd.getPortletName();
			m_title = pdd.getPortletName();

			if ( "stealth".equals(portletSupport) ) {
				M_log.info("Portlet stealth-registered with Sakai toolId="+m_id);
			} else {
				m_categories.add("myworkspace");
				m_categories.add("project");
				m_categories.add("course");
				M_log.info("Portlet auto-registered with Sakai toolId="+m_id);
			}
		}

		// Indicate that these tools are indeed portlets and where to dispatch the portlet
		m_finalConfig.setProperty(PortalService.TOOL_PORTLET_CONTEXT_PATH, portlet.getApplicationId());
		m_finalConfig.setProperty(PortalService.TOOL_PORTLET_NAME, pdd.getPortletName());
	}

	/**
	 * @inheritDoc
	 */
	public int compareTo(Object obj)
	{
		// let it throw a class case exception if the obj is not some sort of Tool
		org.sakaiproject.tool.api.Tool tool = (org.sakaiproject.tool.api.Tool) obj;

		// do an id based
		return getId().compareTo(tool.getId());
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj)
	{
{'fraction_non_alphanumeric': 0.15909351035824654, 'fraction_numerical': 0.0030903055968868032, 'mean_word_length': 4.390499691548427, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '423883', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2470, 'n_words': 727}