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

File AbstractResourceReferenceHandlerManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
19
4
1
122
72
8
0.42
4.75
4
2

Classes

Class Line # Actions
AbstractResourceReferenceHandlerManager 48 19 0% 8 3
0.888888988.9%
 

Contributing tests

This file is covered by 1 test. .

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.resource.internal;
21   
22    import java.util.List;
23    import java.util.Set;
24    import java.util.TreeSet;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28   
29    import org.apache.commons.lang3.exception.ExceptionUtils;
30    import org.slf4j.Logger;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.resource.NotFoundResourceHandlerException;
35    import org.xwiki.resource.ResourceReference;
36    import org.xwiki.resource.ResourceReferenceHandler;
37    import org.xwiki.resource.ResourceReferenceHandlerChain;
38    import org.xwiki.resource.ResourceReferenceHandlerException;
39    import org.xwiki.resource.ResourceReferenceHandlerManager;
40   
41    /**
42    * Helper to implement {@link ResourceReferenceHandlerManager}.
43    *
44    * @param <T> the qualifying element to distinguish a Resource Reference (e.g. Resource Type, Entity Resource Action)
45    * @version $Id: 3f2bada30fcc2c4f1bfa9846d8608a94c4235d96 $
46    * @since 6.1M2
47    */
 
48    public abstract class AbstractResourceReferenceHandlerManager<T> implements ResourceReferenceHandlerManager<T>
49    {
50    /**
51    * Used to lookup Resource Handler components. We use the Context Component Manager so that Extensions can
52    * contribute Resource Handler.
53    */
54    @Inject
55    @Named("context")
56    private ComponentManager contextComponentManager;
57   
58    @Inject
59    private Logger logger;
60   
61    protected abstract boolean matches(ResourceReferenceHandler handler, T resourceReferenceQualifier);
62   
63    protected abstract T extractResourceReferenceQualifier(ResourceReference reference);
64   
 
65  11225 toggle @Override
66    public void handle(ResourceReference reference) throws ResourceReferenceHandlerException
67    {
68    // Look for a Handler supporting the Resource Type located in the passed Resource Reference object.
69  11223 Set<ResourceReferenceHandler> orderedHandlers =
70    getMatchingHandlers(extractResourceReferenceQualifier(reference));
71   
72  11220 if (!orderedHandlers.isEmpty()) {
73    // Create the Handler chain
74  1715 ResourceReferenceHandlerChain chain = new DefaultResourceReferenceHandlerChain(orderedHandlers);
75   
76    // Call the first Handler
77  1709 chain.handleNext(reference);
78    } else {
79    // Resource has not been handled since no Handler was found for it!
80  9499 throw new NotFoundResourceHandlerException(reference);
81    }
82    }
83   
 
84  13010 toggle @Override
85    public boolean canHandle(T resourceReferenceQualifier)
86    {
87  13012 boolean result;
88  13011 try {
89  13002 result = !getMatchingHandlers(resourceReferenceQualifier).isEmpty();
90    } catch (ResourceReferenceHandlerException e) {
91  0 this.logger.warn("Failed to list Resource Reference Handers. Error [{}]",
92    ExceptionUtils.getRootCauseMessage(e));
93  0 result = false;
94    }
95  12980 return result;
96    }
97   
 
98  24116 toggle private Set<ResourceReferenceHandler> getMatchingHandlers(T resourceReferenceQualifier)
99    throws ResourceReferenceHandlerException
100    {
101    // Look for a Handler supporting the Resource Type located in the passed Resource Reference object.
102    // TODO: Use caching to avoid having to sort all Handlers at every call.
103  24136 Set<ResourceReferenceHandler> orderedHandlers = new TreeSet<>();
104  24197 for (ResourceReferenceHandler handler : getHandlers(resourceReferenceQualifier.getClass())) {
105  29479 if (matches(handler, resourceReferenceQualifier)) {
106  3428 orderedHandlers.add(handler);
107    }
108    }
109   
110  24234 return orderedHandlers;
111    }
112   
 
113  24125 toggle private List<ResourceReferenceHandler> getHandlers(Class typeClass) throws ResourceReferenceHandlerException
114    {
115  24177 try {
116  24200 return this.contextComponentManager.getInstanceList(
117    new DefaultParameterizedType(null, ResourceReferenceHandler.class, typeClass));
118    } catch (ComponentLookupException e) {
119  0 throw new ResourceReferenceHandlerException("Failed to locate Resource Reference Handler components", e);
120    }
121    }
122    }