001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 */
016package org.nuxeo.ecm.platform.ui.web.application.config;
017
018import java.lang.annotation.Annotation;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Set;
023
024import javax.faces.bean.ManagedBean;
025import javax.faces.component.FacesComponent;
026import javax.faces.component.behavior.FacesBehavior;
027import javax.faces.convert.FacesConverter;
028import javax.faces.event.NamedEvent;
029import javax.faces.render.FacesBehaviorRenderer;
030import javax.faces.render.FacesRenderer;
031import javax.faces.validator.FacesValidator;
032import javax.servlet.ServletContainerInitializer;
033import javax.servlet.ServletContext;
034import javax.servlet.ServletException;
035import javax.servlet.annotation.HandlesTypes;
036
037/**
038 * TODO.
039 *
040 * @since 6.0
041 */
042@HandlesTypes({ FacesBehavior.class, FacesBehaviorRenderer.class, FacesComponent.class, FacesConverter.class,
043        FacesValidator.class, FacesRenderer.class, ManagedBean.class, NamedEvent.class })
044public class JSFContainerInitializer implements ServletContainerInitializer {
045
046    protected static JSFContainerInitializer self;
047
048    protected final Map<Class<? extends Annotation>, Set<Class<?>>> index = new HashMap<>();
049
050    {
051        self = this;
052        for (Class<?> each : JSFContainerInitializer.class.getAnnotation(HandlesTypes.class).value()) {
053            @SuppressWarnings("unchecked")
054            final Class<? extends Annotation> anno = (Class<? extends Annotation>) each;
055            index.put(anno, new HashSet<Class<?>>());
056        }
057    }
058
059    @Override
060    public void onStartup(Set<Class<?>> some, ServletContext ctx) throws ServletException {
061        for (Class<?> each : some) {
062            index(each);
063        }
064    }
065
066    protected void index(Class<?> aType) {
067        for (Class<? extends Annotation> each : index.keySet()) {
068            if (aType.getAnnotation(each) != null) {
069                index.get(each).add(aType);
070            }
071        }
072    }
073
074}