All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.metadata.cache.MetaAnnotationStore Maven / Gradle / Ivy

There is a newer version: 6.0.0.Beta4
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.metadata.cache;

import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;

import java.lang.annotation.Annotation;

import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.resources.ClassTransformer;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * Metadata singleton for holding EJB metadata, scope models etc.
 *
 * @author Pete Muir
 */
public class MetaAnnotationStore implements Service {

    private abstract static class AbstractMetaAnnotationFunction> extends CacheLoader, M> {

        private final ClassTransformer classTransformer;

        private AbstractMetaAnnotationFunction(ClassTransformer classTransformer) {
            this.classTransformer = classTransformer;
        }

        public ClassTransformer getClassTransformer() {
            return classTransformer;
        }

    }

    private static class StereotypeFunction extends AbstractMetaAnnotationFunction> {

        public StereotypeFunction(ClassTransformer classTransformer) {
            super(classTransformer);
        }

        public StereotypeModel load(Class from) {
            return new StereotypeModel(from, getClassTransformer());
        }

    }

    private static class ScopeFunction extends AbstractMetaAnnotationFunction> {

        public ScopeFunction(ClassTransformer classTransformer) {
            super(classTransformer);
        }

        public ScopeModel load(Class from) {
            return new ScopeModel(from, getClassTransformer());
        }

    }

    private static class QualifierFunction extends AbstractMetaAnnotationFunction> {

        public QualifierFunction(ClassTransformer classTransformer) {
            super(classTransformer);
        }

        public QualifierModel load(Class from) {
            return new QualifierModel(from, getClassTransformer());
        }

    }

    private static class InterceptorBindingFunction extends AbstractMetaAnnotationFunction> {

        public InterceptorBindingFunction(ClassTransformer classTransformer) {
            super(classTransformer);
        }

        public InterceptorBindingModel load(Class from) {
            return new InterceptorBindingModel(from, getClassTransformer());
        }

    }

    // The stereotype models
    private LoadingCache, StereotypeModel> stereotypes;
    // The scope models
    private LoadingCache, ScopeModel> scopes;
    // The binding type models
    private LoadingCache, QualifierModel> qualifiers;
    // the interceptor bindings
    private LoadingCache, InterceptorBindingModel> interceptorBindings;

    public MetaAnnotationStore(ClassTransformer classTransformer) {
        CacheBuilder builder = CacheBuilder.newBuilder();
        this.stereotypes = builder.build(new StereotypeFunction(classTransformer));
        this.scopes = builder.build(new ScopeFunction(classTransformer));
        this.qualifiers = builder.build(new QualifierFunction(classTransformer));
        this.interceptorBindings = builder.build(new InterceptorBindingFunction(classTransformer));
    }

    /**
     * removes all data for an annotation class. This should be called after an
     * annotation has been modified through the SPI
     */
    public void clearAnnotationData(Class annotationClass) {
        stereotypes.invalidate(annotationClass);
        scopes.invalidate(annotationClass);
        qualifiers.invalidate(annotationClass);
        interceptorBindings.invalidate(annotationClass);
    }

    /**
     * Gets a stereotype model
     * 

* Adds the model if it is not present. * * @param The type * @param stereotype The stereotype * @return The stereotype model */ public StereotypeModel getStereotype(final Class stereotype) { return getCastCacheValue(stereotypes, stereotype); } /** * Gets a scope model *

* Adds the model if it is not present. * * @param The type * @param scope The scope type * @return The scope type model */ public ScopeModel getScopeModel(final Class scope) { return getCastCacheValue(scopes, scope); } /** * Gets a binding type model. *

* Adds the model if it is not present. * * @param The type * @param bindingType The binding type * @return The binding type model */ public QualifierModel getBindingTypeModel(final Class bindingType) { return getCastCacheValue(qualifiers, bindingType); } /** * Gets a string representation * * @return A string representation */ @Override public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append("Metadata cache\n"); buffer.append("Registered binding type models: ").append(qualifiers.size()).append("\n"); buffer.append("Registered scope type models: ").append(scopes.size()).append("\n"); buffer.append("Registered stereotype models: ").append(stereotypes.size()).append("\n"); buffer.append("Registered interceptor binding models: ").append(interceptorBindings.size()).append("\n"); return buffer.toString(); } public void cleanup() { this.qualifiers.invalidateAll(); this.scopes.invalidateAll(); this.stereotypes.invalidateAll(); this.interceptorBindings.invalidateAll(); } public InterceptorBindingModel getInterceptorBindingModel(final Class interceptorBinding) { return getCastCacheValue(interceptorBindings, interceptorBinding); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy