001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.myfaces;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    import javax.faces.context.ExternalContext;
027    
028    import org.apache.myfaces.config.annotation.LifecycleProvider;
029    import org.apache.myfaces.config.annotation.LifecycleProviderFactory;
030    
031    /**
032     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
033     */
034    public class ApplicationIndexedLifecycleProviderFactory extends LifecycleProviderFactory {
035    
036        private final Map<ClassLoader, LifecycleProvider> providers = new ConcurrentHashMap<ClassLoader, LifecycleProvider>();
037    
038        public LifecycleProvider getLifecycleProvider(ExternalContext externalContext) {
039            ClassLoader cl = Thread.currentThread().getContextClassLoader();
040            LifecycleProvider provider = providers.get(cl);
041            if (provider == null) {
042                throw new IllegalStateException("No LifecycleProvider registered for application classloader: " + cl);
043            }
044            return provider;
045        }
046    
047        /**
048         * Register a lifecycle provider for an application classloader.  This method is intended to be called
049         * by the container in which MyFaces is running, once for each application, during application startup before
050         * any other myfaces initialization has taken place.
051         *
052         * @param cl       application classloader, used to index LifecycleProviders
053         * @param provider LifecycleProvider for the application.
054         */
055        public void registerLifecycleProvider(ClassLoader cl, LifecycleProvider provider) {
056            providers.put(cl, provider);
057        }
058    
059        public void unregisterLifecycleProvider(ClassLoader cl) {
060            providers.remove(cl);
061        }
062    
063        public void release() {
064            providers.clear();
065        }
066    
067    }