Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2013, 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.bootstrap.enablement;
 
 import static com.google.common.collect.Lists.transform;
 import static org.jboss.weld.util.reflection.Reflections.cast;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
This service gathers globally enabled interceptors, decorators and alternatives and builds a list of each.

Author(s):
Jozef Hartinger
 
 
     private static class Item implements Comparable<Item> {
 
         private final Class<?> javaClass;
 
         private final Integer priority;
 
         private Item(Class<?> javaClass) {
             this(javaClassnull);
         }
 
         private Item(Class<?> javaClassInteger priority) {
             Preconditions.checkArgumentNotNull(javaClass"javaClass");
             this. = javaClass;
             this. = priority;
         }
 
         @Override
         public int compareTo(Item o) {
             if (.equals(o.priority)) {
                 /*
                  * The spec does not specify what happens if two records have the same priority. Instead of giving random results, we compare the records based
                  * on their class name lexicographically.
                  */
                 return .getName().compareTo(o.javaClass.getName());
             }
             return  - o.priority;
         }
 
         @Override
         public int hashCode() {
             return .hashCode();
         }
 
         @Override
         public boolean equals(Object obj) {
             if (this == obj) {
                 return true;
             }
             if (obj instanceof Item) {
                Item that = (Itemobj;
                return Objects.equal(that.javaClass);
            }
            return false;
        }
        @Override
        public String toString() {
            return "[Class=" +  + ", priority=" +  + "]";
        }
    }
    private static class ItemViewProvider implements ViewProvider<ItemClass<?>> {
        private static ItemViewProvider ITEM_VIEW_PROVIDER = new ItemViewProvider();
        @Override
        public Class<?> toView(Item item) {
            return item.javaClass;
        }
        @Override
        public Item fromView(Class<?> javaClass) {
            return new Item(javaClass);
        }
    }
    private abstract static class AbstractEnablementListView extends ListView<ItemClass<?>> {
        @Override
        protected ViewProvider<ItemClass<?>> getViewProvider() {
            return .;
        }
    }
    private final List<Itemalternatives = Collections.synchronizedList(new ArrayList<Item>());
    private final List<Iteminterceptors = Collections.synchronizedList(new ArrayList<Item>());
    private final List<Itemdecorators = Collections.synchronizedList(new ArrayList<Item>());
    private volatile Map<Class<?>, IntegercachedAlternativeMap;
    private volatile boolean sorted;
    private void addItem(List<ItemlistClass<?> javaClassint priority) {
        list.add(new Item(javaClasspriority));
    }
    public void addAlternative(Class<?> javaClassint priority) {
        addItem(javaClasspriority);
    }
    public void addInterceptor(Class<?> javaClassint priority) {
        addItem(javaClasspriority);
    }
    public void addDecorator(Class<?> javaClassint priority) {
        addItem(javaClasspriority);
    }
    public List<Class<?>> getAlternativeList() {
        initialize();
        return new AbstractEnablementListView() {
            @Override
            protected List<ItemgetDelegate() {
                return ;
            }
        };
    }
    public List<Class<?>> getInterceptorList() {
        initialize();
        return new AbstractEnablementListView() {
            @Override
            protected List<ItemgetDelegate() {
                return ;
            }
        };
    }
    public List<Class<?>> getDecoratorList() {
        initialize();
        return new AbstractEnablementListView() {
            @Override
            protected List<ItemgetDelegate() {
                return ;
            }
        };
    }
    /*
     * cachedAlternativeMap is accessed from a single thread only and the result is safely propagated. Therefore, there is no need to synchronize access to
     * cachedAlternativeMap.
     */
    private Map<Class<?>, IntegergetGlobalAlternativeMap() {
        if ( == null) {
            Map<Class<?>, Integermap = new HashMap<Class<?>, Integer>();
            for (ListIterator<Itemiterator = .listIterator(); iterator.hasNext();) {
                Item item = iterator.next();
                map.put(item.javaClassiterator.previousIndex());
            }
             = ImmutableMap.copyOf(map);
        }
        return ;
    }
    private void initialize() {
        if (!) {
            Collections.sort();
            Collections.sort();
            Collections.sort();
             = true;
        }
    }
        ClassLoader loader = new ClassLoader(deployment.getBeanManager().getServices().get(ResourceLoader.class));
        BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
        Set<Class<?>> alternativeClasses = null;
        Set<Class<? extends Annotation>> alternativeStereotypes = null;
        List<Class<?>> globallyEnabledInterceptors = getInterceptorList();
        List<Class<?>> globallyEnabledDecorators = getDecoratorList();
        ImmutableList.Builder<Class<?>> moduleInterceptorsBuilder = ImmutableList.<Class<?>> builder();
        moduleInterceptorsBuilder.addAll(globallyEnabledInterceptors);
        ImmutableList.Builder<Class<?>> moduleDecoratorsBuilder = ImmutableList.<Class<?>> builder();
        moduleDecoratorsBuilder.addAll(globallyEnabledDecorators);
        if (beansXml != null) {
            List<Class<?>> interceptorClasses = transform(beansXml.getEnabledInterceptors(), loader);
            moduleInterceptorsBuilder.addAll(filter(interceptorClassesgloballyEnabledInterceptors.,
                    deployment));
            List<Class<?>> decoratorClasses = transform(beansXml.getEnabledDecorators(), loader);
            moduleDecoratorsBuilder.addAll(filter(decoratorClassesgloballyEnabledDecorators.,
                    deployment));
            alternativeClasses = ImmutableSet.copyOf(transform(beansXml.getEnabledAlternativeClasses(), loader));
            alternativeStereotypes = cast(ImmutableSet.copyOf(transform(beansXml.getEnabledAlternativeStereotypes(), loader)));
        } else {
            alternativeClasses = Collections.emptySet();
            alternativeStereotypes = Collections.emptySet();
        }
        Map<Class<?>, IntegerglobalAlternatives = getGlobalAlternativeMap();
        return new ModuleEnablement(moduleInterceptorsBuilder.build(), moduleDecoratorsBuilder.build(), globalAlternativesalternativeClasses,
                alternativeStereotypes);
    }
    @Override
    public void cleanupAfterBoot() {
        .clear();
        .clear();
        .clear();
    }
    @Override
    public String toString() {
        return "GlobalEnablementBuilder [alternatives=" +  + ", interceptors=" +  + ", decorators=" +  + "]";
    }
    private <T> void checkForDuplicates(List<Metadata<T>> listMessageCallback<DeploymentExceptionmessageCallback) {
        Map<T, Metadata<T>> map = new HashMap<T, Metadata<T>>();
        for (Metadata<T> item : list) {
            Metadata<T> previousOccurrence = map.put(item.getValue(), item);
            if (previousOccurrence != null) {
                throw messageCallback.construct(item.getValue(), itempreviousOccurrence);
            }
        }
    }

    
Filter out interceptors and decorators which are also enabled globally.

Parameters:
enabledClasses
globallyEnabledClasses
logMessageCallback
deployment
Returns:
the filtered list
    private <T> List<Class<?>> filter(List<Class<?>> enabledClassesList<Class<?>> globallyEnabledClassesLogMessageCallback logMessageCallback,
            BeanDeployment deployment) {
        for (Iterator<Class<?>> iterator = enabledClasses.iterator(); iterator.hasNext();) {
            Class<?> enabledClass = iterator.next();
            if (globallyEnabledClasses.contains(enabledClass)) {
                logMessageCallback.log(enabledClassdeployment.getBeanDeploymentArchive().getId());
                iterator.remove();
            }
        }
        return enabledClasses;
    }
    private static class ClassLoader implements Function<Metadata<String>, Class<?>> {
        private final ResourceLoader resourceLoader;
        public ClassLoader(ResourceLoader resourceLoader) {
            this. = resourceLoader;
        }
        @Override
        public Class<?> apply(Metadata<Stringfrom) {
            try {
                return .classForName(from.getValue());
            } catch (ResourceLoadingException e) {
                throw ..errorLoadingBeansXmlEntry(from.getValue(), from.getLocation(), e.getCause());
            } catch (Exception e) {
                throw ..errorLoadingBeansXmlEntry(from.getValue(), from.getLocation(), e);
            }
        }
    }
New to GrepCode? Check out our FAQ X