001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 */
018package org.nuxeo.ecm.platform.ui.web.application.config;
019
020import java.lang.annotation.Annotation;
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.Map;
024import java.util.Set;
025
026import javax.faces.bean.ManagedBean;
027import javax.faces.component.FacesComponent;
028import javax.faces.component.behavior.FacesBehavior;
029import javax.faces.convert.FacesConverter;
030import javax.faces.event.NamedEvent;
031import javax.faces.render.FacesBehaviorRenderer;
032import javax.faces.render.FacesRenderer;
033import javax.faces.validator.FacesValidator;
034import javax.servlet.ServletContainerInitializer;
035import javax.servlet.ServletContext;
036import javax.servlet.ServletException;
037import javax.servlet.annotation.HandlesTypes;
038
039/**
040 * TODO.
041 *
042 * @since 6.0
043 */
044@HandlesTypes({ FacesBehavior.class, FacesBehaviorRenderer.class, FacesComponent.class, FacesConverter.class,
045        FacesValidator.class, FacesRenderer.class, ManagedBean.class, NamedEvent.class })
046public class JSFContainerInitializer implements ServletContainerInitializer {
047
048    protected static JSFContainerInitializer self;
049
050    protected final Map<Class<? extends Annotation>, Set<Class<?>>> index = new HashMap<>();
051
052    {
053        self = this;
054        for (Class<?> each : JSFContainerInitializer.class.getAnnotation(HandlesTypes.class).value()) {
055            @SuppressWarnings("unchecked")
056            final Class<? extends Annotation> anno = (Class<? extends Annotation>) each;
057            index.put(anno, new HashSet<Class<?>>());
058        }
059    }
060
061    @Override
062    public void onStartup(Set<Class<?>> some, ServletContext ctx) throws ServletException {
063        for (Class<?> each : some) {
064            index(each);
065        }
066    }
067
068    protected void index(Class<?> aType) {
069        for (Class<? extends Annotation> each : index.keySet()) {
070            if (aType.getAnnotation(each) != null) {
071                index.get(each).add(aType);
072            }
073        }
074    }
075
076}