001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.core.internal;
020
021import java.lang.reflect.InvocationTargetException;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.concurrent.atomic.AtomicReference;
026import java.util.logging.Logger;
027import org.jgrapes.core.ComponentType;
028import org.jgrapes.core.annotation.HandlerDefinition;
029import org.jgrapes.core.annotation.HandlerDefinition.Evaluator;
030
031/**
032 * Common utility methods.
033 */
034@SuppressWarnings("PMD.MoreThanOneLogger")
035public final class CoreUtils {
036
037    @SuppressWarnings("PMD.FieldNamingConventions")
038    public static final Logger classNames
039        = Logger.getLogger(ComponentType.class.getPackage().getName()
040            + ".classNames");
041
042    @SuppressWarnings("PMD.FieldNamingConventions")
043    /* default */ static final Logger fireRestrictionLogger
044        = Logger.getLogger(CoreUtils.class.getPackage().getName()
045            + ".fireRestriction");
046
047    /** Handler factory cache. */
048    @SuppressWarnings("PMD.UseConcurrentHashMap")
049    private static Map<Class<? extends HandlerDefinition.Evaluator>,
050            HandlerDefinition.Evaluator> definitionEvaluators
051                = Collections.synchronizedMap(new HashMap<>());
052    private static AtomicReference<AssertionError> assertionError
053        = new AtomicReference<>();
054
055    private CoreUtils() {
056    }
057
058    /**
059     * Create a new definition evaluator.
060     *
061     * @param hda the handler definition annotation
062     * @return the evaluator
063     */
064    public static Evaluator definitionEvaluator(
065            HandlerDefinition hda) {
066        return definitionEvaluators.computeIfAbsent(hda.evaluator(), key -> {
067            try {
068                return hda.evaluator().getConstructor().newInstance();
069            } catch (InstantiationException | IllegalAccessException
070                    | IllegalArgumentException | InvocationTargetException
071                    | NoSuchMethodException | SecurityException e) {
072                throw new IllegalStateException(e);
073            }
074        });
075    }
076
077    /* default */ static void setAssertionError(AssertionError error) {
078        assertionError.compareAndSet(null, error);
079    }
080
081    /**
082     * Check if an assertion has been set and if, throw it.
083     */
084    public static void checkAssertions() {
085        AssertionError error = assertionError.getAndSet(null);
086        if (error != null) {
087            throw error;
088        }
089    }
090
091}