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     * Context handler collection.
026     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
027     * @version 0.0.4
028     */
029    public class ContextHandlerCollection extends org.mortbay.jetty.handler.ContextHandlerCollection
030    {
031       /**
032        * Internal parts management interface.
033        */
034        public interface Parts extends PartsManager
035        {
036        }
037        
038        private final Logger m_logger;
039        private final Parts m_parts;
040    
041       /**
042        * Creation of a new HTTP server implementation.
043        * @param logger the assigned logging channel
044        * @param parts the parts manager
045        * @exception Exception if an instantiation error occurs
046        */
047        public ContextHandlerCollection( Logger logger, Parts parts ) throws Exception
048        {
049            super();
050             
051            m_logger = logger;
052            m_parts = parts;
053    
054            getLogger().debug( "commencing handler addition" );
055            String[] keys = parts.getKeys();
056            getLogger().debug( "handler count: " + keys.length );
057            for( int i=0; i<keys.length; i++ )
058            {
059                String key = keys[i];
060                ComponentHandler handler = parts.getComponentHandler( key );
061                getLogger().info( "adding handler: " + handler );
062                try
063                {
064                    Provider provider = handler.getProvider();
065                    org.mortbay.jetty.Handler ch = 
066                      (org.mortbay.jetty.Handler) provider.getValue( false );
067                    super.addHandler( ch );
068                }
069                catch( Throwable e )
070                {
071                    final String error = 
072                      "Failed to deploy handler: " + handler;
073                    throw new Exception( error, e );
074                }
075            }
076        }
077        
078        private Logger getLogger()
079        {
080            return m_logger;
081        }
082    }