1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.internal.multi

File AbstractGenericComponentManager.java

 

Coverage histogram

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

Code metrics

8
17
7
1
133
62
11
0.65
2.43
7
1.57

Classes

Class Line # Actions
AbstractGenericComponentManager 38 17 0% 11 3
0.9062590.6%
 

Contributing tests

This file is covered by 200 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.internal.multi;
21   
22    import java.lang.reflect.Type;
23   
24    import javax.inject.Inject;
25   
26    import org.xwiki.component.descriptor.ComponentDescriptor;
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.component.manager.ComponentRepositoryException;
29   
30    /**
31    * Generic implementation that creates Component Manager instances based on the generic notion of a key. This is used
32    * for example by the Wiki Component Manager or the User Component Manager which respectively have a key returning the
33    * current wiki and the current user in the Execution Context.
34    *
35    * @version $Id: 0b2f569941cb3cc35292e7c9b7e5934f11a9f108 $
36    * @since 3.3M2
37    */
 
38    public abstract class AbstractGenericComponentManager extends DelegateComponentManager
39    {
40    /**
41    * Store and provide {@link ComponentManager} instances.
42    */
43    @Inject
44    private ComponentManagerManager componentManagerManager;
45   
46    /**
47    * @see #getInternalParent()
48    */
49    private ComponentManager internalParent;
50   
51    /**
52    * @return the key (any String) representing the current Component Manager. For example in the case of a User
53    * Component Manager the key can be the current user in the execution context so that we can register/lookup
54    * components only for that user
55    */
56    protected abstract String getKey();
57   
 
58  161499430 toggle @Override
59    public ComponentManager getComponentManager()
60    {
61  161508265 ComponentManager componentManager = getComponentManagerInternal();
62  161527562 if (componentManager == null) {
63    // There's no specific Component Manager for the Current Context Component Manager.
64    // Redirect to the Parent Component Manager if it exists and if not, then use the Null pattern
65    // to return a Null Component Manager.
66  117016424 componentManager = getInternalParent();
67  117017150 if (componentManager == null) {
68  0 throw new RuntimeException("Key-based Proxy Component Managers should always have a Parent Component "
69    + "Manager defined so that it can be used if there's no Component Manager for the given key");
70    }
71    }
72   
73  161513155 return componentManager;
74    }
75   
76    /**
77    * @since 7.1RC1
78    */
 
79  1337535 toggle protected ComponentManager getComponentManagerInternal()
80    {
81  1337604 String key = getKey();
82  1337554 return key != null ? this.componentManagerManager.getComponentManager(key, false) : null;
83    }
84   
 
85  5171 toggle @Override
86    public <T> void registerComponent(ComponentDescriptor<T> componentDescriptor, T componentInstance)
87    throws ComponentRepositoryException
88    {
89  5171 synchronized (this) {
90    // Make sure the ComponentManager associated to the current key exists
91  5171 ComponentManager componentManager = this.componentManagerManager.getComponentManager(getKey(), true);
92   
93  5171 if (componentInstance == null) {
94  6 componentManager.registerComponent(componentDescriptor);
95    } else {
96  5165 componentManager.registerComponent(componentDescriptor, componentInstance);
97    }
98    }
99    }
100   
 
101  6 toggle @Override
102    public <T> void registerComponent(ComponentDescriptor<T> componentDescriptor) throws ComponentRepositoryException
103    {
104  6 registerComponent(componentDescriptor, null);
105    }
106   
 
107  170 toggle @Override
108    public void unregisterComponent(Type role, String roleHint)
109    {
110  170 super.unregisterComponent(role, roleHint);
111   
112    // Note: Ideally if the Component Manager for the current key is empty we could remove it from the list
113    // of managed Component Managers. However there's currently no way to ask a Component Manager for its
114    // full list of managed components or whether it's empty or not.
115    }
116   
117    /**
118    * @return the Parent Component Manager to default to when there's no Component Manager matching the key returned by
119    * {@link #getKey()}.
120    */
 
121  117015748 toggle protected ComponentManager getInternalParent()
122    {
123  117016033 return this.internalParent;
124    }
125   
126    /**
127    * @param parentComponentManager see {@link #getInternalParent()}
128    */
 
129  1156 toggle protected void setInternalParent(ComponentManager parentComponentManager)
130    {
131  1156 this.internalParent = parentComponentManager;
132    }
133    }