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

File ComponentsObjectFactory.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
13
2
1
96
44
5
0.38
6.5
2
2.5

Classes

Class Line # Actions
ComponentsObjectFactory 44 13 0% 5 1
0.9473684494.7%
 

Contributing tests

No tests hitting this source file were found.

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.rest.internal;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.restlet.ext.jaxrs.InstantiateException;
26    import org.restlet.ext.jaxrs.ObjectFactory;
27    import org.xwiki.component.descriptor.ComponentDescriptor;
28    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.context.Execution;
32    import org.xwiki.context.ExecutionContext;
33    import org.xwiki.rest.internal.Constants;
34    import org.xwiki.rest.XWikiRestComponent;
35   
36    /**
37    * This class is used to provide Restlet/JAX-RS a way to instantiate components. Instances requested through this
38    * factory are created using the XWiki component manager. A special list stored in the execution context is used in
39    * order to keep track of all the allocated components that have a "per-lookup" policy. This is needed in order to
40    * ensure proper release of those instances at the end of the request and to avoid memory leaks.
41    *
42    * @version $Id: e9ffaceb087317dde4eaf796d77da336e6d23b8f $
43    */
 
44    public class ComponentsObjectFactory implements ObjectFactory
45    {
46    /**
47    * The component manager used to lookup and instantiate components.
48    */
49    private ComponentManager componentManager;
50   
51    /**
52    * Constructor
53    *
54    * @param componentManager The component manager to be used.
55    */
 
56  18 toggle public ComponentsObjectFactory(ComponentManager componentManager)
57    {
58  18 this.componentManager = componentManager;
59    }
60   
 
61  2055 toggle @Override
62    @SuppressWarnings("unchecked")
63    public <T> T getInstance(Class<T> clazz) throws InstantiateException
64    {
65  2055 try {
66    // Use the component manager to lookup the class. This ensure that injections are properly executed.
67  2055 XWikiRestComponent component = componentManager.getInstance(XWikiRestComponent.class, clazz.getName());
68   
69    // JAX-RS resources and providers must be declared as components whose hint is the FQN of the class
70    // implementing it. This is needed because of they are looked up using the FQN as the hint.
71  2055 ComponentDescriptor<XWikiRestComponent> componentDescriptor =
72    componentManager.getComponentDescriptor(XWikiRestComponent.class, clazz.getName());
73   
74    // Retrieve the list of releasable components from the execution context. This is used to store component
75    // instances that need to be released at the end of the request.
76  2055 ExecutionContext executionContext = componentManager.<Execution> getInstance(Execution.class).getContext();
77  2055 List<XWikiRestComponent> releasableComponentReferences =
78    (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
79  2055 if (releasableComponentReferences == null) {
80  1709 releasableComponentReferences = new ArrayList<XWikiRestComponent>();
81  1709 executionContext.setProperty(Constants.RELEASABLE_COMPONENT_REFERENCES, releasableComponentReferences);
82    }
83   
84    // Only add the components that have a per-lookup instantiation strategy.
85  2055 if (componentDescriptor.getInstantiationStrategy() == ComponentInstantiationStrategy.PER_LOOKUP) {
86  1562 releasableComponentReferences.add(component);
87    }
88   
89    // Return the instantiated component. This cast should never fail if the programmer has correctly set the
90    // component hint to the actual fully qualified name of the Java class.
91  2055 return (T) component;
92    } catch (ComponentLookupException e) {
93  0 throw new InstantiateException(e);
94    }
95    }
96    }