<commit_before>/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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.apache.pluto.portalImpl.services.portletdefinitionregistry;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.pluto.om.common.ObjectID;
import org.apache.pluto.om.portlet.PortletApplicationDefinition;
import org.apache.pluto.om.portlet.PortletApplicationDefinitionList;
import org.apache.pluto.om.portlet.PortletDefinition;
import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionImpl;
import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionListImpl;
import org.apache.pluto.portalImpl.om.servlet.impl.WebApplicationDefinitionImpl;
import org.apache.pluto.portalImpl.services.log.Log;
import org.apache.pluto.portalImpl.util.Properties;
import org.apache.pluto.portalImpl.xml.XmlParser;
import org.apache.pluto.services.log.Logger;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

/**
 * A simple XML Castor file based implementation of the <code>PortletRegistryService</config>
 * <p>This store persit the PortletRegistry informations</p>
 *
 */

public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService
{

    private static String fileSeparator = System.getProperty("file.separator");

    // 
    public final  static String TMP_PREFIX="tmp12345";
    private final static String WAR_FILE_EXT=".war";
    
    // default configuration values
    public final static String DEFAULT_MAPPING_PORTLETXML = "WEB-INF/data/xml/portletdefinitionmapping.xml";
    public final static String DEFAULT_MAPPING_WEBXML     = "WEB-INF/data/xml/servletdefinitionmapping.xml";
    // configuration keys
    private final static String CONFIG_MAPPING_PORTLETXML = "mapping.portletxml.configfile";
    private final static String CONFIG_MAPPING_WEBXML     = "mapping.webxml.configfile";

    // Castor mapping file
    private Mapping mappingPortletXml = null;
    private Mapping mappingWebXml = null;
    // Servlet Context
    private ServletContext servletContext = null;
    // Base Dir where all web modules are located
    private String baseWMDir = null;
    private Logger log = null;

    // Helper lists and hashtables to access the data as fast as possible
    // List containing all portlet applications available in the system
    protected PortletApplicationDefinitionListImpl registry = 
        new PortletApplicationDefinitionListImpl();
    protected Map portletsKeyObjectId = new HashMap();

    public void init (ServletConfig config, Properties properties) throws Exception
    {
        log = Log.getService().getLogger(getClass());
        servletContext = config.getServletContext();

        if (properties.getBoolean("non-servlet")==Boolean.TRUE)
        {
            String root = config.getServletContext().getRealPath("/"); //root
            baseWMDir = root + fileSeparator + 
		"WEB-INF" + fileSeparator + 
		"portletapps" + fileSeparator; //org.apache.pluto.portalImpl.services.deploy.DeployServiceFileImpl.DEFAULT_PROTECTED;
            if(log.isDebugEnabled())
                log.debug("baseWMDir = " + baseWMDir + " fileSeparator = " + fileSeparator);
        }
        else
        {
            baseWMDir = servletContext.getRealPath("");
            baseWMDir = baseWMDir.substring(0,
                                            baseWMDir.lastIndexOf(fileSeparator))+fileSeparator;
        }

        // get portlet xml mapping file
        String _mapping = properties.getString(CONFIG_MAPPING_PORTLETXML, DEFAULT_MAPPING_PORTLETXML);
        File f = new File(_mapping);
        if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
        this.mappingPortletXml = new Mapping();
        try
        {
            this.mappingPortletXml.loadMapping(_mapping);
        }
        catch (Exception e)
        {
            log.error("Failed to load mapping file "+_mapping,e);
            throw e;
        }
        // get web xml mapping file
        _mapping = properties.getString(CONFIG_MAPPING_WEBXML, DEFAULT_MAPPING_WEBXML);
        f = new File(_mapping);
        if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
        this.mappingWebXml = new Mapping();
        try
        {
            this.mappingWebXml.loadMapping(_mapping);
        }
        catch (Exception e)
        {
            log.error("Failed to load mapping file "+_mapping,e);
            throw e;
        }

        load();

        fill();
    }

    public PortletApplicationDefinitionList getPortletApplicationDefinitionList()
    {
        return registry;
    }

    public PortletDefinition getPortletDefinition(ObjectID id)
    {
        return (PortletDefinition)portletsKeyObjectId.get(id);
    }

    private void load() throws Exception
    {
        File f = new File(baseWMDir);
        String[] entries = f.list();
        for (int i=0; i<entries.length; i++)
        {
            File entry = new File(baseWMDir+entries[i]);
            if (entry.isDirectory())
            {
                load(baseWMDir, entries[i]);
            }
        }
    }
    /**
    * Handles resolution of a web module's file system name to its
    * URI identifier.
    *
    * @param webModule The file system name.
    * @return The URI part.
    */
    private String resolveURI(String webModule)
    {
    // For JBoss compatibility, change webModule from the form
    // of "tmp12345foo.war" to "foo".
    int len = webModule.length();
    if (webModule.endsWith(WAR_FILE_EXT) && webModule.startsWith(TMP_PREFIX) && len > TMP_PREFIX.length() + WAR_FILE_EXT.length()) {
        webModule = webModule.substring(TMP_PREFIX.length(), len - WAR_FILE_EXT.length());
    }
    // else assumed literal.
    return webModule;
    }

    private void load(String baseDir, String webModule) throws Exception
    {
        String directory = baseDir+webModule+fileSeparator+"WEB-INF"+fileSeparator;

        File portletXml = new File(directory+"portlet.xml");
        File webXml = new File(directory+"web.xml");

        // check for the porlet.xml. If there is no portlet.xml this is not a
        // portlet application web module
        if (portletXml.exists()) // && (webXml.exists()))
        {
            if (log.isDebugEnabled())
            {
                log.debug("Loading the following Portlet Applications XML files..."+portletXml+", "+webXml);
            }

            InputSource source = new InputSource(new FileInputStream(portletXml));
            source.setSystemId(portletXml.toURL().toExternalForm());
            <commit_msg>This change has been made for support of JBoss.<commit_after>/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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.apache.pluto.portalImpl.services.portletdefinitionregistry;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.pluto.om.common.ObjectID;
import org.apache.pluto.om.portlet.PortletApplicationDefinition;
import org.apache.pluto.om.portlet.PortletApplicationDefinitionList;
import org.apache.pluto.om.portlet.PortletDefinition;
import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionImpl;
import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionListImpl;
import org.apache.pluto.portalImpl.om.servlet.impl.WebApplicationDefinitionImpl;
import org.apache.pluto.portalImpl.services.log.Log;
import org.apache.pluto.portalImpl.util.Properties;
import org.apache.pluto.portalImpl.xml.XmlParser;
import org.apache.pluto.services.log.Logger;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

/**
 * A simple XML Castor file based implementation of the <code>PortletRegistryService</config>
 * <p>This store persit the PortletRegistry informations</p>
 *
 */

public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService
{

    private static String fileSeparator = System.getProperty("file.separator");

    // 
    public final  static String TMP_PREFIX="tmp12345";
    private final static String WAR_FILE_EXT=".war";
    
    // default configuration values
    public final static String DEFAULT_MAPPING_PORTLETXML = "WEB-INF/data/xml/portletdefinitionmapping.xml";
    public final static String DEFAULT_MAPPING_WEBXML     = "WEB-INF/data/xml/servletdefinitionmapping.xml";
    // configuration keys
    private final static String CONFIG_MAPPING_PORTLETXML = "mapping.portletxml.configfile";
    private final static String CONFIG_MAPPING_WEBXML     = "mapping.webxml.configfile";

    // Castor mapping file
    private Mapping mappingPortletXml = null;
    private Mapping mappingWebXml = null;
    // Servlet Context
    private ServletContext servletContext = null;
    // Base Dir where all web modules are located
    private String baseWMDir = null;
    private Logger log = null;

    // Helper lists and hashtables to access the data as fast as possible
    // List containing all portlet applications available in the system
    protected PortletApplicationDefinitionListImpl registry = 
        new PortletApplicationDefinitionListImpl();
    protected Map portletsKeyObjectId = new HashMap();

    public void init (ServletConfig config, Properties properties) throws Exception
    {
        log = Log.getService().getLogger(getClass());
        servletContext = config.getServletContext();

        if (properties.getBoolean("non-servlet")==Boolean.TRUE)
        {
            String root = config.getServletContext().getRealPath("/"); //root
            baseWMDir = root + fileSeparator + 
		"WEB-INF" + fileSeparator + 
		"portletapps" + fileSeparator; //org.apache.pluto.portalImpl.services.deploy.DeployServiceFileImpl.DEFAULT_PROTECTED;
            if(log.isDebugEnabled())
                log.debug("baseWMDir = " + baseWMDir + " fileSeparator = " + fileSeparator);
        }
        else
        {
            baseWMDir = servletContext.getRealPath("");
            baseWMDir = baseWMDir.substring(0,
                                            baseWMDir.lastIndexOf(fileSeparator))+fileSeparator;
        }

        // get portlet xml mapping file
        String _mapping = properties.getString(CONFIG_MAPPING_PORTLETXML, DEFAULT_MAPPING_PORTLETXML);
        File f = new File(_mapping);
        if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
        this.mappingPortletXml = new Mapping();
        try
        {
            this.mappingPortletXml.loadMapping(_mapping);
        }
        catch (Exception e)
        {
            log.error("Failed to load mapping file "+_mapping,e);
            throw e;
        }
        // get web xml mapping file
        _mapping = properties.getString(CONFIG_MAPPING_WEBXML, DEFAULT_MAPPING_WEBXML);
        f = new File(_mapping);
        if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
        this.mappingWebXml = new Mapping();
        try
        {
            this.mappingWebXml.loadMapping(_mapping);
        }
        catch (Exception e)
        {
            log.error("Failed to load mapping file "+_mapping,e);
            throw e;
        }

        load();

        fill();
    }

    public PortletApplicationDefinitionList getPortletApplicationDefinitionList()
    {
        return registry;
    }

    public PortletDefinition getPortletDefinition(ObjectID id)
    {
        return (PortletDefinition)portletsKeyObjectId.get(id);
    }

    private void load() throws Exception
    {
        File f = new File(baseWMDir);
        String[] entries = f.list();
        for (int i=0; i<entries.length; i++)
        {
            File entry = new File(baseWMDir+entries[i]);
            if (entry.isDirectory())
            {
                load(baseWMDir, entries[i]);
            }
        }
    }
    /**
    * Handles resolution of a web module's file system name to its
    * URI identifier.
    *
    * @param webModule The file system name.
    * @return The URI part.
    */
    private String resolveURI(String webModule)
    {
    // For JBoss compatibility, change webModule from the form
    // of "tmp12345foo.war" to "foo".
    int len = webModule.length();
    if (webModule.endsWith(WAR_FILE_EXT) && webModule.startsWith(TMP_PREFIX.substring(0,3)) && len > TMP_PREFIX.length() + WAR_FILE_EXT.length()) { 
        webModule = webModule.substring(TMP_PREFIX.length(), len - WAR_FILE_EXT.length());
    }
    // else assumed literal.
    return webModule;
    }

    private void load(String baseDir, String webModule) throws Exception
    {
        String directory = baseDir+webModule+fileSeparator+"WEB-INF"+fileSeparator;

        File portletXml = new File(directory+"portlet.xml");
        File webXml = new File(directory+"web.xml");

        // check for the porlet.xml. If there is no portlet.xml this is not a
        // portlet application web module
        if (portletXml.exists()) // && (webXml.exists()))
        {
            if (log.isDebugEnabled())
            {
                log.debug("Loading the following Portlet Applications XML files..."+portletXml+", "+webXml);
            }

            InputSource source = new InputSource(new FileInputStream(portletXml));
            source.setSystemId(portletXml.toURL().toExternalForm());
            
{'fraction_non_alphanumeric': 0.09460005386479936, 'fraction_numerical': 0.0033665499596014004, 'mean_word_length': 2.902522333158171, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4746449', 'n_tokens_mistral': 4130, 'n_tokens_neox': 4060, 'n_words': 1032}