001    /*
002     * Copyright 2006 Stephen McConnell.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package net.dpml.http;
017    
018    import net.dpml.logging.Logger;
019    
020    import net.dpml.metro.PartsManager;
021    import net.dpml.metro.ComponentHandler;
022    import net.dpml.component.Provider;
023    
024    /**
025     * A collection of handlers.
026     * For each request, all handler are called, regardless of 
027     * the response status or exceptions.
028     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
029     * @version 0.0.4
030     */
031    public class HandlerCollection extends org.mortbay.jetty.handler.HandlerCollection
032    {
033       /**
034        * Internal parts management interface.
035        */
036        public interface Parts extends PartsManager
037        {
038        }
039        
040        private final Logger m_logger;
041        private final Parts m_parts;
042    
043       /**
044        * Creation of a new handler collection.
045        * @param logger the assigned logging channel
046        * @param parts the parts manager
047        * @exception Exception if an instantiation error occurs
048        */
049        public HandlerCollection( Logger logger, Parts parts ) throws Exception
050        {
051            super();
052             
053            m_logger = logger;
054            m_parts = parts;
055    
056            getLogger().debug( "commencing handler addition" );
057            String[] keys = parts.getKeys();
058            getLogger().debug( "handler count: " + keys.length );
059            for( int i=0; i<keys.length; i++ )
060            {
061                String key = keys[i];
062                ComponentHandler handler = parts.getComponentHandler( key );
063                getLogger().info( "adding handler: " + handler );
064                try
065                {
066                    Provider provider = handler.getProvider();
067                    org.mortbay.jetty.Handler ch = 
068                      (org.mortbay.jetty.Handler) provider.getValue( false );
069                    super.addHandler( ch );
070                }
071                catch( Throwable e )
072                {
073                    final String error = 
074                      "Failed to deploy handler: " + handler;
075                    throw new Exception( error, e );
076                }
077            }
078        }
079        
080        private Logger getLogger()
081        {
082            return m_logger;
083        }
084    }