Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.getCacheValue;
 import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;
 
 import java.util.Set;
 
 
 
Metadata singleton for holding EJB metadata, scope models etc.

Author(s):
Pete Muir
 
 public class MetaAnnotationStore implements Service {
 
     private abstract static class AbstractMetaAnnotationFunction<M extends AnnotationModel<Annotation>> extends
             CacheLoader<Class<Annotation>, M> {
 
         private final ClassTransformer classTransformer;
 
         private AbstractMetaAnnotationFunction(ClassTransformer classTransformer) {
             this. = classTransformer;
         }
 
         public ClassTransformer getClassTransformer() {
             return ;
         }
 
     }
 
     private static class StereotypeFunction extends AbstractMetaAnnotationFunction<StereotypeModel<Annotation>> {
 
         public StereotypeFunction(ClassTransformer classTransformer) {
             super(classTransformer);
         }
 
         @Override
         public StereotypeModel<Annotationload(Class<Annotationfrom) {
             return new StereotypeModel<Annotation>(getClassTransformer().getEnhancedAnnotation(from));
         }
 
     }
 
     private static class ScopeFunction extends AbstractMetaAnnotationFunction<ScopeModel<Annotation>> {
 
         public ScopeFunction(ClassTransformer classTransformer) {
             super(classTransformer);
         }
 
         @Override
         public ScopeModel<Annotationload(Class<Annotationfrom) {
             return new ScopeModel<Annotation>(getClassTransformer().getEnhancedAnnotation(from));
         }
 
     }
 
     private static class QualifierFunction extends AbstractMetaAnnotationFunction<QualifierModel<Annotation>> {
 
         public QualifierFunction(ClassTransformer classTransformer) {
             super(classTransformer);
         }
 
         @Override
         public QualifierModel<Annotationload(Class<Annotationfrom) {
             return new QualifierModel<Annotation>(getClassTransformer().getEnhancedAnnotation(from));
         }
 
     }
        public InterceptorBindingFunction(ClassTransformer classTransformer) {
            super(classTransformer);
        }
        @Override
        public InterceptorBindingModel<Annotationload(Class<Annotationfrom) {
            return new InterceptorBindingModel<Annotation>(getClassTransformer().getEnhancedAnnotation(from));
        }
    }
    private static class QualifierInstanceFunction extends CacheLoader<AnnotationQualifierInstance> {
        private final MetaAnnotationStore metaAnnotationStore;
        private QualifierInstanceFunction(MetaAnnotationStore metaAnnotationStore) {
            super();
            this. = metaAnnotationStore;
        }
        @Override
        public QualifierInstance load(Annotation keythrows Exception {
            return QualifierInstance.of(key);
        }
    }
    // The stereotype models
    // The scope models
    // The binding type models
    // the interceptor bindings
    private final SharedObjectCache sharedObjectCache;
    public MetaAnnotationStore(ClassTransformer classTransformer) {
        CacheBuilder<ObjectObjectcacheBuilder = CacheBuilder.newBuilder();
        this. = cacheBuilder.build(new StereotypeFunction(classTransformer));
        this. = cacheBuilder.build(new ScopeFunction(classTransformer));
        this. = cacheBuilder.build(new QualifierFunction(classTransformer));
        this. = cacheBuilder.build(new InterceptorBindingFunction(classTransformer));
        this. = cacheBuilder.build(new QualifierInstanceFunction(this));
        this. = classTransformer.getSharedObjectCache();
    }

    
removes all data for an annotation class. This should be called after an annotation has been modified through the SPI
    public void clearAnnotationData(Class<? extends AnnotationannotationClass) {
        .invalidate(annotationClass);
        .invalidate(annotationClass);
        .invalidate(annotationClass);
        .invalidate(annotationClass);
    }

    
Gets a stereotype model

Adds the model if it is not present.

Parameters:
<T> The type
stereotype The stereotype
Returns:
The stereotype model
    public <T extends AnnotationStereotypeModel<T> getStereotype(final Class<T> stereotype) {
        return getCastCacheValue(stereotype);
    }

    
Gets a scope model

Adds the model if it is not present.

Parameters:
<T> The type
scope The scope type
Returns:
The scope type model
    public <T extends AnnotationScopeModel<T> getScopeModel(final Class<T> scope) {
        return getCastCacheValue(scope);
    }

    
Gets a binding type model.

Adds the model if it is not present.

Parameters:
<T> The type
bindingType The binding type
Returns:
The binding type model
    public <T extends AnnotationQualifierModel<T> getBindingTypeModel(final Class<T> bindingType) {
        return getCastCacheValue(bindingType);
    }

    

Parameters:
interceptorBinding
Returns:
    public <T extends AnnotationInterceptorBindingModel<T> getInterceptorBindingModel(final Class<T> interceptorBinding) {
        return getCastCacheValue(interceptorBinding);
    }

    

Parameters:
annotation
Returns:
the qualifier instance for the given annotation, uses cache if possible
    public QualifierInstance getQualifierInstance(final Annotation annotation) {
        return isCacheAllowed(annotation) ? getCacheValue(annotation) : QualifierInstance.of(annotationthis);
    }

    

Parameters:
bean
Returns:
the set of qualifier instances for the given bean, uses caches if possible
    public Set<QualifierInstancegetQualifierInstances(final Bean<?> bean) {
        if (bean instanceof RIBean) {
            return ((RIBean<?>) bean).getQualifierInstances();
        }
        return getQualifierInstances(bean.getQualifiers());
    }

    

Parameters:
annotations
Returns:
the set of qualifier instances, uses caches if possible
    public Set<QualifierInstancegetQualifierInstances(final Set<Annotationannotations) {
        if (annotations == null || annotations.isEmpty()) {
            return Collections.emptySet();
        }
        ImmutableSet.Builder<QualifierInstancebuilder = ImmutableSet.builder();
        boolean useSharedCache = true;
        for (Annotation annotation : annotations) {
            if (isCacheAllowed(annotation)) {
                builder.add(getCacheValue(annotation));
            } else {
                builder.add(QualifierInstance.of(annotationthis));
                // Don't use shared object cache if there's some qualifier instance which should not be cached
                useSharedCache = false;
            }
        }
        return useSharedCache ? .getSharedSet(builder.build()) : builder.build();
    }

    
Gets a string representation

Returns:
A string representation
    @Override
    public String toString() {
        final String newLine = "\n";
        StringBuilder buffer = new StringBuilder();
        buffer.append("Metadata cache").append(newLine);
        buffer.append("Registered binding type models: ").append(.size()).append(newLine);
        buffer.append("Registered scope type models: ").append(.size()).append(newLine);
        buffer.append("Registered stereotype models: ").append(.size()).append(newLine);
        buffer.append("Registered interceptor binding models: ").append(.size()).append(newLine);
        buffer.append("Cached qualifier instances: ").append(.size()).append(newLine);
        return buffer.toString();
    }
    @Override
    public void cleanup() {
        this..invalidateAll();
        this..invalidateAll();
        this..invalidateAll();
        this..invalidateAll();
    }
    private static boolean isCacheAllowed(Annotation annotation) {
        if (annotation.annotationType().equals(Named.class)) {
            // Don't cache @Named with non-default value.
            Named named = (Namedannotation;
            return named.value().equals("");
        }
        return true;
    }
New to GrepCode? Check out our FAQ X