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

File AbstractEntityComponentManager.java

 

Coverage histogram

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

Code metrics

12
21
4
2
124
66
12
0.57
5.25
2
3

Classes

Class Line # Actions
AbstractEntityComponentManager 40 19 0% 11 3
0.911764791.2%
AbstractEntityComponentManager.EntityComponentManagerInstance 42 2 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 194 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;
21   
22    import javax.inject.Inject;
23   
24    import org.xwiki.component.internal.multi.AbstractGenericComponentManager;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.component.phase.Initializable;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.model.reference.EntityReference;
30    import org.xwiki.model.reference.EntityReferenceSerializer;
31   
32    /**
33    * Proxy Component Manager that creates and queries individual Component Managers specific to the current entity in the
34    * Execution Context. These Component Managers are created on the fly the first time a component is registered for the
35    * current entity.
36    *
37    * @version $Id: fff0e0df86ca977deef1e4779656661204ae97ca $
38    * @since 5.0M2
39    */
 
40    public abstract class AbstractEntityComponentManager extends AbstractGenericComponentManager implements Initializable
41    {
 
42    private static class EntityComponentManagerInstance
43    {
44    protected final EntityReference entityReference;
45   
46    protected final ComponentManager componentManager;
47   
 
48  178491 toggle EntityComponentManagerInstance(EntityReference entityReference, ComponentManager componentManager)
49    {
50  178496 this.entityReference = entityReference;
51  178494 this.componentManager = componentManager;
52    }
53    }
54   
55    @Inject
56    private EntityReferenceSerializer<String> serializer;
57   
58    @Inject
59    private Execution execution;
60   
61    /**
62    * The key used to access the cache {@link ComponentManager} in the {@link ExecutionContext}.
63    */
64    private final String contextKey = getClass().getName();
65   
66    protected abstract EntityReference getCurrentReference();
67   
 
68  11289632 toggle @Override
69    protected ComponentManager getComponentManagerInternal()
70    {
71    // Get current user reference
72  11289636 EntityReference entityReference = getCurrentReference();
73  11289786 if (entityReference == null) {
74  699338 return null;
75    }
76   
77  10590437 ExecutionContext econtext = this.execution.getContext();
78   
79    // If there is no context don't try to find or register the component manager
80  10590459 if (econtext == null) {
81  0 return super.getComponentManagerInternal();
82    }
83   
84    // Try to find the user component manager in the context
85  10590468 EntityComponentManagerInstance contextComponentManager =
86    (EntityComponentManagerInstance) econtext.getProperty(this.contextKey);
87  10590696 if (contextComponentManager != null && contextComponentManager.entityReference.equals(entityReference)) {
88  10412137 return contextComponentManager.componentManager;
89    }
90   
91    // Fallback on regular user component manager search
92  178509 ComponentManager componentManager = super.getComponentManagerInternal();
93   
94    // Cache the component manager
95  178466 econtext.setProperty(this.contextKey, new EntityComponentManagerInstance(entityReference, componentManager));
96   
97  178518 return componentManager;
98    }
99   
 
100  117172 toggle @Override
101    protected String getKey()
102    {
103  117175 EntityReference reference = getCurrentReference();
104   
105  117214 return reference != null ? reference.getType().getLowerCase() + ':' + this.serializer.serialize(reference)
106    : null;
107    }
108   
 
109  2732 toggle protected void onComponentAdded()
110    {
111  2732 ExecutionContext econtext = this.execution.getContext();
112   
113    // Remove any null cached ComponentManager in the context
114    // Note: namespace based component managers aren't deleted yet so the opposite is not needed but it could
115    // change)
116  2732 if (econtext != null) {
117  2476 EntityComponentManagerInstance contextComponentManager =
118    (EntityComponentManagerInstance) econtext.getProperty(this.contextKey);
119  2476 if (contextComponentManager != null && contextComponentManager.componentManager == null) {
120  1633 econtext.removeProperty(this.contextKey);
121    }
122    }
123    }
124    }