head	1.2;
access;
symbols;
locks; strict;
comment	@# @;


1.2
date	2001.08.30.17.57.12;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2001.08.30.16.02.35;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Servlet configuration is now part of component configuration. ComponentTools and ComponentTool provide an interface to component configuration and will be used in templates to process component-specific template fragments.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.component;

import java.util.*;

import org.apache.turbine.util.Log;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.util.StringUtils;

import org.apache.fulcrum.ServiceManager;
import org.apache.fulcrum.TurbineServices;
import org.apache.turbine.services.pull.ApplicationTool;
import org.apache.turbine.util.Log;

/**
 * A class for accessing the installed components from a template.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author <a href="mailto:dlr@@finemaltcoding.com">Daniel Rall</a>
 * @@version $Id: ComponentTools.java,v 1.1 2001/08/30 16:02:35 leonardr Exp $
 */
public class ComponentTools implements ApplicationTool
{
    private static String COMPONENT_CONF_PREFIX="component";

    public static ComponentTools singleton = new ComponentTools();

    private static Map components = new HashMap();
    
    /**
     * Creates a new component tool.
     *
     * @@param componentName The name of the component to create a tool
     * for.
     */
    public ComponentTools()
    {
    }

    public Map getTools()
    {
        return components;
    }

    public ComponentTool getTool(String name)
    {
        return (ComponentTool)components.get(name);
    }

    /**
     * Simply calls {@@link #refresh()}.
     */
    public void init(Object ignored)
    {
        refresh();
    }

    /**
     * Refreshes this component's configuration.
     */
    public void refresh()
    {
        ServiceManager mgr = TurbineServices.getInstance();
        if (mgr != null)
        {
            ExtendedProperties componentConf = mgr.getConfiguration().subset
                (COMPONENT_CONF_PREFIX);
            Set processedComponentNames = new HashSet();
            for (Iterator i = componentConf.getKeys(); i.hasNext();)
            {            
                String name = (String)i.next();                
                String componentName = name.substring(0,name.indexOf('.'));
                if (!processedComponentNames.contains(componentName))
                {
                    processedComponentNames.add(componentName);
                    ExtendedProperties props = 
                        componentConf.subset(componentName);
                    components.put
                        (componentName, 
                         new ComponentTool(componentName, props));
                }
            }            
        }
    }

    /**
     * Finds properties for the named servlet. If more than one
     * component defines a servlet with that name, this method will
     * return the properties defined for that servletby an essentially
     * random component. This could stand to be improved.
     */
    public Properties findServletProperties(String servletName)
    {
        Properties props = null;
        for (Iterator i = components.keySet().iterator(); i.hasNext();)
        {
            String componentName  = (String)i.next();
            props = getTool(componentName).getServletProperties(servletName);
            if (props != null)
            {
                break;
            }
        }
        return props;
    }
}
@


1.1
log
@Sorted out my and dlr's code.
@
text
@d65 1
a65 1
 * @@version $Id: ComponentTool.java,v 1.3 2001/08/30 02:01:38 dlr Exp $
d71 1
a71 1
    public static ComponentTools singleton;
d73 1
a73 6
    private Map components;

    static
    {
        singleton = new ComponentTools();
    }
a107 1
        components = new HashMap();
d111 1
a111 1
            ExtendedProperties components = mgr.getConfiguration().subset
d114 1
a114 1
            for (Iterator i = components.getKeys(); i.hasNext();)
d122 1
a122 1
                        components.subset(componentName);
d134 2
a135 2
     * return the properties defined by one of them, seemingly
     * at random.
a142 2
            Log.debug("Does " + componentName 
                      + " provide a servlet called " + servletName + "?");
a145 1
                Log.debug("Yes!");
a147 1
            Log.debug("No.");
@

