| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.component.wiki.internal; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.Type; |
| 23 |
|
import java.util.ArrayList; |
| 24 |
|
import java.util.Iterator; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Map; |
| 27 |
|
import java.util.concurrent.ConcurrentHashMap; |
| 28 |
|
|
| 29 |
|
import javax.inject.Inject; |
| 30 |
|
import javax.inject.Singleton; |
| 31 |
|
|
| 32 |
|
import org.slf4j.Logger; |
| 33 |
|
import org.xwiki.component.annotation.Component; |
| 34 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
| 35 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
| 36 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 37 |
|
import org.xwiki.component.manager.ComponentManager; |
| 38 |
|
import org.xwiki.component.manager.ComponentRepositoryException; |
| 39 |
|
import org.xwiki.component.phase.Initializable; |
| 40 |
|
import org.xwiki.component.phase.InitializationException; |
| 41 |
|
import org.xwiki.component.util.ReflectionUtils; |
| 42 |
|
import org.xwiki.component.wiki.WikiComponent; |
| 43 |
|
import org.xwiki.component.wiki.WikiComponentException; |
| 44 |
|
import org.xwiki.component.wiki.WikiComponentManager; |
| 45 |
|
import org.xwiki.component.wiki.WikiComponentScope; |
| 46 |
|
import org.xwiki.model.reference.DocumentReference; |
| 47 |
|
import org.xwiki.model.reference.EntityReference; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@link |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
@version |
| 54 |
|
@since |
| 55 |
|
|
| 56 |
|
@Component |
| 57 |
|
@Singleton |
| |
|
| 84.3% |
Uncovered Elements: 13 (83) |
Complexity: 20 |
Complexity Density: 0.32 |
|
| 58 |
|
public class DefaultWikiComponentManager implements WikiComponentManager |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@Inject |
| 64 |
|
private Logger logger; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
@Inject |
| 70 |
|
private ComponentManager rootComponentManager; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@Inject |
| 76 |
|
private WikiComponentManagerContext wikiComponentManagerContext; |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
private Map<DocumentReference, List<WikiComponent>> registeredComponents = new ConcurrentHashMap<>(); |
| 84 |
|
|
| |
|
| 70% |
Uncovered Elements: 6 (20) |
Complexity: 5 |
Complexity Density: 0.28 |
|
| 85 |
312 |
@Override... |
| 86 |
|
public void registerWikiComponent(WikiComponent component) throws WikiComponentException |
| 87 |
|
{ |
| 88 |
|
|
| 89 |
312 |
DocumentReference currentUserReference = this.wikiComponentManagerContext.getCurrentUserReference(); |
| 90 |
312 |
EntityReference currentEntityReference = this.wikiComponentManagerContext.getCurrentEntityReference(); |
| 91 |
|
|
| 92 |
312 |
try { |
| 93 |
|
|
| 94 |
312 |
Type roleType = component.getRoleType(); |
| 95 |
312 |
Class< ? > roleTypeClass = ReflectionUtils.getTypeClass(roleType); |
| 96 |
312 |
ComponentDescriptor componentDescriptor = createComponentDescriptor(roleType, component.getRoleHint()); |
| 97 |
|
|
| 98 |
|
|
| 99 |
312 |
this.wikiComponentManagerContext.setCurrentUserReference(component.getAuthorReference()); |
| 100 |
312 |
this.wikiComponentManagerContext.setCurrentEntityReference(component.getDocumentReference()); |
| 101 |
|
|
| 102 |
|
|
| 103 |
312 |
if (this.isInitializable(component.getClass().getInterfaces())) { |
| 104 |
0 |
try { |
| 105 |
0 |
((Initializable) component).initialize(); |
| 106 |
|
} catch (InitializationException e) { |
| 107 |
0 |
this.logger.error("Failed to initialize wiki component", e); |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
312 |
getComponentManager(component.getScope()).registerComponent(componentDescriptor, |
| 113 |
|
roleTypeClass.cast(component)); |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
312 |
cacheWikiComponent(component); |
| 118 |
|
} catch (ComponentLookupException e) { |
| 119 |
0 |
throw new WikiComponentException(String.format("Failed to find a component manager for scope [%s] wiki " |
| 120 |
|
+ "component registration failed", component.getScope()), e); |
| 121 |
|
} catch (ComponentRepositoryException e) { |
| 122 |
0 |
throw new WikiComponentException("Failed to register wiki component against component repository", e); |
| 123 |
|
} finally { |
| 124 |
312 |
this.wikiComponentManagerContext.setCurrentUserReference(currentUserReference); |
| 125 |
312 |
this.wikiComponentManagerContext.setCurrentEntityReference(currentEntityReference); |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 129 |
312 |
private void cacheWikiComponent(WikiComponent component)... |
| 130 |
|
{ |
| 131 |
312 |
List<WikiComponent> wikiComponents = this.registeredComponents.get(component.getDocumentReference()); |
| 132 |
312 |
if (wikiComponents == null) { |
| 133 |
310 |
wikiComponents = new ArrayList<>(); |
| 134 |
310 |
this.registeredComponents.put(component.getDocumentReference(), wikiComponents); |
| 135 |
|
} |
| 136 |
312 |
if (!wikiComponents.contains(component)) { |
| 137 |
311 |
wikiComponents.add(component); |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
| |
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 141 |
3968 |
@Override... |
| 142 |
|
public void unregisterWikiComponents(DocumentReference reference) throws WikiComponentException |
| 143 |
|
{ |
| 144 |
3968 |
List<WikiComponent> wikiComponents = this.registeredComponents.get(reference); |
| 145 |
3968 |
if (wikiComponents != null) { |
| 146 |
7 |
Iterator<WikiComponent> iterator = wikiComponents.iterator(); |
| 147 |
14 |
while (iterator.hasNext()) { |
| 148 |
7 |
unregisterWikiComponent(iterator); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
7 |
wikiComponents = this.registeredComponents.get(reference); |
| 152 |
7 |
if (wikiComponents.isEmpty()) { |
| 153 |
7 |
this.registeredComponents.remove(reference); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
| 158 |
7 |
private void unregisterWikiComponent(Iterator<WikiComponent> iterator)... |
| 159 |
|
throws WikiComponentException |
| 160 |
|
{ |
| 161 |
7 |
WikiComponent wikiComponent = iterator.next(); |
| 162 |
|
|
| 163 |
|
|
| 164 |
7 |
DocumentReference currentUserReference = this.wikiComponentManagerContext.getCurrentUserReference(); |
| 165 |
7 |
EntityReference currentEntityReference = this.wikiComponentManagerContext.getCurrentEntityReference(); |
| 166 |
7 |
try { |
| 167 |
|
|
| 168 |
|
|
| 169 |
7 |
this.wikiComponentManagerContext.setCurrentUserReference(wikiComponent.getAuthorReference()); |
| 170 |
7 |
this.wikiComponentManagerContext.setCurrentEntityReference(wikiComponent.getDocumentReference()); |
| 171 |
|
|
| 172 |
7 |
getComponentManager(wikiComponent.getScope()).unregisterComponent(wikiComponent.getRoleType(), |
| 173 |
|
wikiComponent.getRoleHint()); |
| 174 |
|
|
| 175 |
7 |
iterator.remove(); |
| 176 |
|
} catch (ComponentLookupException e) { |
| 177 |
0 |
throw new WikiComponentException(String.format("Failed to find a component manager for scope [%s]", |
| 178 |
|
wikiComponent.getScope()), e); |
| 179 |
|
} finally { |
| 180 |
7 |
this.wikiComponentManagerContext.setCurrentUserReference(currentUserReference); |
| 181 |
7 |
this.wikiComponentManagerContext.setCurrentEntityReference(currentEntityReference); |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
|
| 186 |
|
@param |
| 187 |
|
@return |
| 188 |
|
|
| 189 |
|
|
| 190 |
|
@throws |
| 191 |
|
|
| |
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 192 |
319 |
private ComponentManager getComponentManager(WikiComponentScope scope) throws ComponentLookupException... |
| 193 |
|
{ |
| 194 |
319 |
ComponentManager cm; |
| 195 |
|
|
| 196 |
319 |
switch (scope) { |
| 197 |
0 |
case USER: |
| 198 |
0 |
cm = this.rootComponentManager.getInstance(ComponentManager.class, "user"); |
| 199 |
0 |
break; |
| 200 |
135 |
case WIKI: |
| 201 |
135 |
cm = this.rootComponentManager.getInstance(ComponentManager.class, "wiki"); |
| 202 |
135 |
break; |
| 203 |
184 |
default: |
| 204 |
184 |
cm = this.rootComponentManager; |
| 205 |
|
} |
| 206 |
|
|
| 207 |
319 |
return cm; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
@param |
| 214 |
|
@param |
| 215 |
|
@return@link |
| 216 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 217 |
312 |
private ComponentDescriptor createComponentDescriptor(Type roleType, String roleHint)... |
| 218 |
|
{ |
| 219 |
312 |
DefaultComponentDescriptor cd = new DefaultComponentDescriptor(); |
| 220 |
312 |
cd.setRoleType(roleType); |
| 221 |
312 |
cd.setRoleHint(roleHint); |
| 222 |
312 |
return cd; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
@link |
| 227 |
|
|
| 228 |
|
@param |
| 229 |
|
@return@link |
| 230 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 231 |
312 |
private boolean isInitializable(Class< ? >[] interfaces)... |
| 232 |
|
{ |
| 233 |
312 |
for (Class< ? > iface : interfaces) { |
| 234 |
624 |
if (Initializable.class.equals(iface)) { |
| 235 |
0 |
return true; |
| 236 |
|
} |
| 237 |
|
} |
| 238 |
312 |
return false; |
| 239 |
|
} |
| 240 |
|
} |