1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.annotation

File ComponentAnnotationLoader.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

60
149
14
1
549
321
57
0.38
10.64
14
4.07

Classes

Class Line # Actions
ComponentAnnotationLoader 58 149 0% 57 51
0.7713004477.1%
 

Contributing tests

This file is covered by 37 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.annotation;
21   
22    import java.io.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.InputStreamReader;
26    import java.lang.annotation.Annotation;
27    import java.lang.reflect.ParameterizedType;
28    import java.lang.reflect.Type;
29    import java.net.URL;
30    import java.util.ArrayList;
31    import java.util.Arrays;
32    import java.util.Enumeration;
33    import java.util.HashMap;
34    import java.util.LinkedHashSet;
35    import java.util.List;
36    import java.util.Map;
37    import java.util.Set;
38    import java.util.zip.ZipEntry;
39    import java.util.zip.ZipInputStream;
40   
41    import javax.inject.Provider;
42   
43    import org.slf4j.Logger;
44    import org.slf4j.LoggerFactory;
45    import org.xwiki.component.descriptor.ComponentDescriptor;
46    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
47    import org.xwiki.component.internal.RoleHint;
48    import org.xwiki.component.manager.ComponentManager;
49    import org.xwiki.component.util.DefaultParameterizedType;
50    import org.xwiki.component.util.ReflectionUtils;
51   
52    /**
53    * Dynamically loads all components defined using Annotations and declared in META-INF/components.txt files.
54    *
55    * @version $Id: 085feb66781cccbfbc9d96c07bb9faf58acdf263 $
56    * @since 1.8.1
57    */
 
58    public class ComponentAnnotationLoader
59    {
60    /**
61    * Location in the classloader of the file defining the list of component implementation class to parser for
62    * annotations.
63    */
64    public static final String COMPONENT_LIST = "META-INF/components.txt";
65   
66    /**
67    * Location in the classloader of the file specifying which component implementation to use when several components
68    * with the same role/hint are found.
69    *
70    * @deprecated starting with 3.3M1 use the notion of priorities instead (see {@link ComponentDeclaration}).
71    */
72    @Deprecated
73    public static final String COMPONENT_OVERRIDE_LIST = "META-INF/component-overrides.txt";
74   
75    /**
76    * The encoding used to parse component list files.
77    */
78    private static final String COMPONENT_LIST_ENCODING = "UTF-8";
79   
80    /**
81    * Logger to use for logging...
82    */
83    private static final Logger LOGGER = LoggerFactory.getLogger(ComponentAnnotationLoader.class);
84   
85    /**
86    * Factory to create a Component Descriptor from an annotated class.
87    */
88    private ComponentDescriptorFactory factory = new ComponentDescriptorFactory();
89   
90    /**
91    * Loads all components defined using annotations.
92    *
93    * @param manager the component manager to use to dynamically register components
94    * @param classLoader the classloader to use to look for the Component list declaration file (
95    * {@code META-INF/components.txt})
96    */
 
97  2476 toggle public void initialize(ComponentManager manager, ClassLoader classLoader)
98    {
99  2476 try {
100    // Find all declared components by retrieving the list defined in COMPONENT_LIST.
101  2476 List<ComponentDeclaration> componentDeclarations = getDeclaredComponents(classLoader, COMPONENT_LIST);
102   
103    // Find all the Component overrides and adds them to the bottom of the list as component declarations with
104    // the highest priority of 0. This is purely for backward compatibility since the override files is now
105    // deprecated.
106  2476 List<ComponentDeclaration> componentOverrideDeclarations =
107    getDeclaredComponents(classLoader, COMPONENT_OVERRIDE_LIST);
108  2476 for (ComponentDeclaration componentOverrideDeclaration : componentOverrideDeclarations) {
109    // Since the old way to declare an override was to define it in both a component.txt and a
110    // component-overrides.txt file we first need to remove the override component declaration stored in
111    // componentDeclarations.
112  3 componentDeclarations.remove(componentOverrideDeclaration);
113    // Add it to the end of the list with the highest priority.
114  3 componentDeclarations.add(new ComponentDeclaration(componentOverrideDeclaration
115    .getImplementationClassName(), 0));
116    }
117   
118  2476 initialize(manager, classLoader, componentDeclarations);
119    } catch (Exception e) {
120    // Make sure we make the calling code fail in order to fail fast and prevent the application to start
121    // if something is amiss.
122  0 throw new RuntimeException("Failed to get the list of components to load", e);
123    }
124    }
125   
126    /**
127    * @param manager the component manager to use to dynamically register components
128    * @param classLoader the classloader to use to look for the Component list declaration file (
129    * {@code META-INF/components.txt})
130    * @param componentDeclarations the declarations of components to register
131    * @since 3.3M1
132    */
 
133  3605 toggle public void initialize(ComponentManager manager, ClassLoader classLoader,
134    List<ComponentDeclaration> componentDeclarations)
135    {
136  3605 register(manager, classLoader, componentDeclarations);
137    }
138   
139    /**
140    * @param manager the component manager to use to dynamically register components
141    * @param classLoader the classloader to use to look for the Component list declaration file (
142    * {@code META-INF/components.txt})
143    * @param componentDeclarations the declarations of components to register
144    * @since 4.0M1
145    */
 
146  3605 toggle public void register(ComponentManager manager, ClassLoader classLoader,
147    List<ComponentDeclaration> componentDeclarations)
148    {
149  3605 try {
150    // 2) For each component class name found, load its class and use introspection to find the necessary
151    // annotations required to create a Component Descriptor.
152  3605 Map<RoleHint<?>, ComponentDescriptor<?>> descriptorMap =
153    new HashMap<RoleHint<?>, ComponentDescriptor<?>>();
154  3605 Map<RoleHint<?>, Integer> priorityMap = new HashMap<RoleHint<?>, Integer>();
155   
156  3605 for (ComponentDeclaration componentDeclaration : componentDeclarations) {
157  683010 Class<?> componentClass;
158  683010 try {
159  683010 componentClass = classLoader.loadClass(componentDeclaration.getImplementationClassName());
160    } catch (Exception e) {
161  0 throw new RuntimeException(
162    String.format("Failed to load component class [%s] for annotation parsing",
163    componentDeclaration.getImplementationClassName()), e);
164    }
165   
166    // Look for ComponentRole annotations and register one component per ComponentRole found
167  683010 for (Type componentRoleType : findComponentRoleTypes(componentClass)) {
168  724940 for (ComponentDescriptor<?> componentDescriptor : this.factory.createComponentDescriptors(
169    componentClass, componentRoleType)) {
170    // If there's already a existing role/hint in the list of descriptors then decide which one
171    // to keep by looking at their priorities. Highest priority wins (i.e. lowest integer value).
172  739417 RoleHint<?> roleHint =
173    new RoleHint(componentDescriptor.getRoleType(), componentDescriptor.getRoleHint());
174   
175  739417 addComponent(descriptorMap, priorityMap, roleHint, componentDescriptor, componentDeclaration,
176    true);
177    }
178    }
179    }
180   
181    // 3) Activate all component descriptors
182  3605 for (ComponentDescriptor<?> descriptor : descriptorMap.values()) {
183  734984 manager.registerComponent(descriptor);
184    }
185    } catch (Exception e) {
186    // Make sure we make the calling code fail in order to fail fast and prevent the application to start
187    // if something is amiss.
188  0 throw new RuntimeException("Failed to dynamically load components with annotations", e);
189    }
190    }
191   
 
192  739417 toggle private void addComponent(Map<RoleHint<?>, ComponentDescriptor<?>> descriptorMap,
193    Map<RoleHint<?>, Integer> priorityMap, RoleHint<?> roleHint, ComponentDescriptor<?> componentDescriptor,
194    ComponentDeclaration componentDeclaration, boolean warn)
195    {
196  739417 if (descriptorMap.containsKey(roleHint)) {
197    // Compare priorities
198  4433 int currentPriority = priorityMap.get(roleHint);
199  4433 if (componentDeclaration.getPriority() < currentPriority) {
200    // Override!
201  2232 descriptorMap.put(roleHint, componentDescriptor);
202  2232 priorityMap.put(roleHint, componentDeclaration.getPriority());
203  2201 } else if (componentDeclaration.getPriority() == currentPriority) {
204  0 if (warn) {
205    // Warning that we're not overwriting since they have the same priorities
206  0 getLogger().warn(
207    "Component [{}] which implements [{}] tried to overwrite component "
208    + "[{}]. However, no action was taken since both components have the same priority "
209    + "level of [{}].",
210    new Object[] { componentDeclaration.getImplementationClassName(), roleHint,
211    descriptorMap.get(roleHint).getImplementation().getName(), currentPriority });
212    }
213    } else {
214  2201 getLogger().debug(
215    "Ignored component [{}] since its priority level of [{}] is lower "
216    + "than the currently registered component [{}] which has a priority of [{}]",
217    new Object[] { componentDeclaration.getImplementationClassName(),
218    componentDeclaration.getPriority(), currentPriority });
219    }
220    } else {
221  734984 descriptorMap.put(roleHint, componentDescriptor);
222  734984 priorityMap.put(roleHint, componentDeclaration.getPriority());
223    }
224    }
225   
226    /**
227    * @param manager the component manager to use to dynamically register components
228    * @param classLoader the classloader to use to look for the Component list declaration file (
229    * {@code META-INF/components.txt})
230    * @param componentDeclarations the declarations of components to register
231    * @since 4.0M1
232    */
 
233  68 toggle public void unregister(ComponentManager manager, ClassLoader classLoader,
234    List<ComponentDeclaration> componentDeclarations)
235    {
236  68 for (ComponentDeclaration componentDeclaration : componentDeclarations) {
237  86 Class<?> componentClass = null;
238  86 try {
239  86 componentClass = classLoader.loadClass(componentDeclaration.getImplementationClassName());
240    } catch (ClassNotFoundException e) {
241  0 getLogger().warn("Can't find any existing component with class [{}]. Ignoring it.",
242    componentDeclaration.getImplementationClassName());
243    } catch (Exception e) {
244  0 getLogger().warn("Fail to load component implementation class [{}]. Ignoring it.",
245    componentDeclaration.getImplementationClassName(), e);
246    }
247   
248  86 if (componentClass != null) {
249  86 for (ComponentDescriptor<?> componentDescriptor : getComponentsDescriptors(componentClass)) {
250  86 manager.unregisterComponent(componentDescriptor);
251   
252  86 if (componentDescriptor.getRoleType() instanceof ParameterizedType) {
253  44 Class roleClass = ReflectionUtils.getTypeClass(componentDescriptor.getRoleType());
254   
255  44 DefaultComponentDescriptor<?> classComponentDescriptor =
256    new DefaultComponentDescriptor(componentDescriptor);
257  44 classComponentDescriptor.setRoleType(roleClass);
258   
259  44 manager.unregisterComponent(classComponentDescriptor);
260    }
261    }
262    }
263    }
264    }
265   
 
266  412 toggle public List<ComponentDescriptor> getComponentsDescriptors(Class<?> componentClass)
267    {
268  412 List<ComponentDescriptor> descriptors = new ArrayList<ComponentDescriptor>();
269   
270    // Look for ComponentRole annotations and register one component per ComponentRole found
271  412 for (Type componentRoleType : findComponentRoleTypes(componentClass)) {
272  412 descriptors.addAll(this.factory.createComponentDescriptors(componentClass, componentRoleType));
273    }
274   
275  412 return descriptors;
276    }
277   
 
278  685250 toggle public Set<Type> findComponentRoleTypes(Class<?> componentClass)
279    {
280  685250 return findComponentRoleTypes(componentClass, null);
281    }
282   
 
283  2716562 toggle public Set<Type> findComponentRoleTypes(Class<?> componentClass, Type[] parameters)
284    {
285    // Note: We use a Set to ensure that we don't register duplicate roles.
286  2716562 Set<Type> types = new LinkedHashSet<Type>();
287   
288  2716562 Component component = componentClass.getAnnotation(Component.class);
289   
290    // If the roles are specified by the user then don't auto-discover roles!
291  2716562 if (component != null && component.roles().length > 0) {
292  14250 types.addAll(Arrays.asList(component.roles()));
293    } else {
294    // Auto-discover roles by looking for a @Role annotation or a @Provider one in both the superclass
295    // and implemented interfaces.
296  2702312 for (Type interfaceType : getGenericInterfaces(componentClass)) {
297  1447316 Class<?> interfaceClass;
298  1447316 Type[] interfaceParameters;
299   
300  1447316 if (interfaceType instanceof ParameterizedType) {
301  181304 ParameterizedType interfaceParameterizedType = (ParameterizedType) interfaceType;
302   
303  181304 interfaceClass = ReflectionUtils.getTypeClass(interfaceType);
304  181304 Type[] variableParameters = interfaceParameterizedType.getActualTypeArguments();
305   
306  181304 interfaceParameters =
307    ReflectionUtils.resolveSuperArguments(variableParameters, componentClass, parameters);
308   
309  181304 if (interfaceParameters == null) {
310  669 interfaceType = interfaceClass;
311  180635 } else if (interfaceParameters != variableParameters) {
312  85560 interfaceType =
313    new DefaultParameterizedType(interfaceParameterizedType.getOwnerType(), interfaceClass,
314    interfaceParameters);
315    }
316  1266012 } else if (interfaceType instanceof Class) {
317  1266012 interfaceClass = (Class<?>) interfaceType;
318  1266012 interfaceParameters = null;
319    } else {
320  0 continue;
321    }
322   
323    // Handle superclass of interfaces
324  1447316 types.addAll(findComponentRoleTypes(interfaceClass, interfaceParameters));
325   
326    // Handle interfaces directly declared in the passed component class
327  1447316 if (ReflectionUtils.getDirectAnnotation(Role.class, interfaceClass) != null) {
328  701589 types.add(interfaceType);
329    }
330   
331    // Handle javax.inject.Provider
332  1447316 if (Provider.class.isAssignableFrom(interfaceClass)) {
333  17151 types.add(interfaceType);
334    }
335   
336    // Handle ComponentRole (retro-compatibility since 4.0M1)
337  1447316 if (ReflectionUtils.getDirectAnnotation(ComponentRole.class, interfaceClass) != null) {
338  9445 types.add(interfaceClass);
339    }
340    }
341   
342    // Note that we need to look into the superclass since the super class can itself implements an interface
343    // that has the @Role annotation.
344  2702312 Type superType = componentClass.getGenericSuperclass();
345  2702312 if (superType != null && superType != Object.class) {
346  583996 if (superType instanceof ParameterizedType) {
347  159726 ParameterizedType superParameterizedType = (ParameterizedType) superType;
348  159726 types.addAll(findComponentRoleTypes((Class) superParameterizedType.getRawType(), ReflectionUtils
349    .resolveSuperArguments(superParameterizedType.getActualTypeArguments(), componentClass,
350    parameters)));
351  424270 } else if (superType instanceof Class) {
352  424270 types.addAll(findComponentRoleTypes((Class) superType, null));
353    }
354    }
355    }
356   
357  2716562 return types;
358    }
359   
360    /**
361    * Helper method that generate a {@link RuntimeException} in case of a reflection error.
362    *
363    * @param componentClass the component for which to return the interface types
364    * @return the Types representing the interfaces directly implemented by the class or interface represented by this
365    * object
366    * @throws RuntimeException in case of a reflection error such as
367    * {@link java.lang.reflect.MalformedParameterizedTypeException}
368    */
 
369  2702312 toggle private Type[] getGenericInterfaces(Class<?> componentClass)
370    {
371  2702312 Type[] interfaceTypes;
372  2702312 try {
373  2702312 interfaceTypes = componentClass.getGenericInterfaces();
374    } catch (Exception e) {
375  0 throw new RuntimeException(String.format("Failed to get interface for [%s]", componentClass.getName()), e);
376    }
377  2702312 return interfaceTypes;
378    }
379   
380    /**
381    * Finds the interfaces that implement component roles by looking recursively in all interfaces of the passed
382    * component implementation class. If the roles annotation value is specified then use the specified list instead of
383    * doing auto-discovery. Also note that we support component classes implementing JSR 330's
384    * {@link javax.inject.Provider} (and thus without a component role annotation).
385    *
386    * @param componentClass the component implementation class for which to find the component roles it implements
387    * @return the list of component role classes implemented
388    * @deprecated since 4.0M1 use {@link #findComponentRoleTypes(Class)} instead
389    */
 
390  0 toggle @Deprecated
391    public Set<Class<?>> findComponentRoleClasses(Class<?> componentClass)
392    {
393    // Note: We use a Set to ensure that we don't register duplicate roles.
394  0 Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
395   
396  0 Component component = componentClass.getAnnotation(Component.class);
397  0 if (component != null && component.roles().length > 0) {
398  0 classes.addAll(Arrays.asList(component.roles()));
399    } else {
400    // Look in both superclass and interfaces for @Role or javax.inject.Provider
401  0 for (Class<?> interfaceClass : componentClass.getInterfaces()) {
402    // Handle superclass of interfaces
403  0 classes.addAll(findComponentRoleClasses(interfaceClass));
404   
405    // Handle interfaces directly declared in the passed component class
406  0 for (Annotation annotation : interfaceClass.getDeclaredAnnotations()) {
407  0 if (annotation.annotationType() == ComponentRole.class) {
408  0 classes.add(interfaceClass);
409    }
410    }
411   
412    // Handle javax.inject.Provider
413  0 if (Provider.class.isAssignableFrom(interfaceClass)) {
414  0 classes.add(interfaceClass);
415    }
416    }
417   
418    // Note that we need to look into the superclass since the super class can itself implements an interface
419    // that has the @Role annotation.
420  0 Class<?> superClass = componentClass.getSuperclass();
421  0 if (superClass != null && superClass != Object.class) {
422  0 classes.addAll(findComponentRoleClasses(superClass));
423    }
424    }
425   
426  0 return classes;
427    }
428   
429    /**
430    * Get all components listed in the passed resource file.
431    *
432    * @param classLoader the classloader to use to find the resources
433    * @param location the name of the resources to look for
434    * @return the list of component implementation class names
435    * @throws IOException in case of an error loading the component list resource
436    * @since 3.3M1
437    */
 
438  4952 toggle private List<ComponentDeclaration> getDeclaredComponents(ClassLoader classLoader, String location)
439    throws IOException
440    {
441  4952 List<ComponentDeclaration> annotatedClassNames = new ArrayList<ComponentDeclaration>();
442  4952 Enumeration<URL> urls = classLoader.getResources(location);
443  89358 while (urls.hasMoreElements()) {
444  84406 URL url = urls.nextElement();
445   
446  84406 LOGGER.debug("Loading declared component definitions from [{}]", url);
447   
448  84406 InputStream componentListStream = url.openStream();
449   
450  84406 try {
451  84406 annotatedClassNames.addAll(getDeclaredComponents(componentListStream));
452    } finally {
453  84406 componentListStream.close();
454    }
455    }
456   
457  4952 return annotatedClassNames;
458    }
459   
460    /**
461    * Get all components listed in the passed resource stream. The format is:
462    * {@code (priority level):(fully qualified component implementation name)}.
463    *
464    * @param componentListStream the stream to parse
465    * @return the list of component declaration (implementation class names and priorities)
466    * @throws IOException in case of an error loading the component list resource
467    * @since 3.3M1
468    */
 
469  84604 toggle public List<ComponentDeclaration> getDeclaredComponents(InputStream componentListStream) throws IOException
470    {
471  84604 List<ComponentDeclaration> annotatedClassNames = new ArrayList<ComponentDeclaration>();
472   
473    // Read all components definition from the URL
474    // Always force UTF-8 as the encoding, since these files are read from the official jars, and those are
475    // generated on an 8-bit system.
476  84604 BufferedReader in = new BufferedReader(new InputStreamReader(componentListStream, COMPONENT_LIST_ENCODING));
477  84604 String inputLine;
478  ? while ((inputLine = in.readLine()) != null) {
479    // Make sure we don't add empty lines
480  669983 if (inputLine.trim().length() > 0) {
481  667801 try {
482  667801 String[] chunks = inputLine.split(":");
483  667801 ComponentDeclaration componentDeclaration;
484  667801 if (chunks.length > 1) {
485  4530 componentDeclaration = new ComponentDeclaration(chunks[1], Integer.parseInt(chunks[0]));
486    } else {
487  663271 componentDeclaration = new ComponentDeclaration(chunks[0]);
488    }
489  667801 LOGGER.debug(" - Adding component definition [{}] with priority [{}]",
490    componentDeclaration.getImplementationClassName(), componentDeclaration.getPriority());
491  667801 annotatedClassNames.add(componentDeclaration);
492    } catch (Exception e) {
493  0 getLogger().error("Failed to parse component declaration from [{}]", inputLine, e);
494    }
495    }
496    }
497   
498  84604 return annotatedClassNames;
499    }
500   
501    /**
502    * Get all components listed in a JAR file.
503    *
504    * @param jarFile the JAR file to parse
505    * @return the list of component declaration (implementation class names and priorities)
506    * @throws IOException in case of an error loading the component list resource
507    */
 
508  202 toggle public List<ComponentDeclaration> getDeclaredComponentsFromJAR(InputStream jarFile) throws IOException
509    {
510  202 ZipInputStream zis = new ZipInputStream(jarFile);
511   
512  202 List<ComponentDeclaration> componentDeclarations = null;
513  202 List<ComponentDeclaration> componentOverrideDeclarations = null;
514   
515  1302 for (ZipEntry entry = zis.getNextEntry(); entry != null
516    && (componentDeclarations == null || componentOverrideDeclarations == null); entry = zis.getNextEntry()) {
517  1100 if (entry.getName().equals(ComponentAnnotationLoader.COMPONENT_LIST)) {
518  198 componentDeclarations = getDeclaredComponents(zis);
519  902 } else if (entry.getName().equals(ComponentAnnotationLoader.COMPONENT_OVERRIDE_LIST)) {
520  0 componentOverrideDeclarations = getDeclaredComponents(zis);
521    }
522    }
523   
524    // Merge all overrides found with a priority of 0. This is purely for backward compatibility since the
525    // override files is now deprecated.
526  202 if (componentOverrideDeclarations != null) {
527  0 if (componentDeclarations == null) {
528  0 componentDeclarations = new ArrayList<ComponentDeclaration>();
529    }
530  0 for (ComponentDeclaration componentOverrideDeclaration : componentOverrideDeclarations) {
531  0 componentDeclarations.add(new ComponentDeclaration(componentOverrideDeclaration
532    .getImplementationClassName(), 0));
533    }
534    }
535   
536  202 return componentDeclarations;
537    }
538   
539    /**
540    * Useful for unit tests that need to capture logs; they can return a mock logger instead of the real logger and
541    * thus assert what's been logged.
542    *
543    * @return the Logger instance to use to log
544    */
 
545  2201 toggle protected Logger getLogger()
546    {
547  2201 return LOGGER;
548    }
549    }