| 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.util.Arrays; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Named; |
| 27 |
|
import javax.inject.Provider; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
| 31 |
|
import org.slf4j.Logger; |
| 32 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 33 |
|
import org.xwiki.bridge.event.ApplicationReadyEvent; |
| 34 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
| 35 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
| 36 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
| 37 |
|
import org.xwiki.bridge.event.WikiReadyEvent; |
| 38 |
|
import org.xwiki.component.annotation.Component; |
| 39 |
|
import org.xwiki.component.wiki.WikiComponent; |
| 40 |
|
import org.xwiki.component.wiki.WikiComponentBuilder; |
| 41 |
|
import org.xwiki.component.wiki.WikiComponentException; |
| 42 |
|
import org.xwiki.component.wiki.WikiComponentManager; |
| 43 |
|
import org.xwiki.model.reference.DocumentReference; |
| 44 |
|
import org.xwiki.observation.EventListener; |
| 45 |
|
import org.xwiki.observation.ObservationContext; |
| 46 |
|
import org.xwiki.observation.event.Event; |
| 47 |
|
|
| 48 |
|
import com.xpn.xwiki.XWikiContext; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@link |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@version |
| 56 |
|
@since |
| 57 |
|
|
| 58 |
|
@Component |
| 59 |
|
@Named("defaultWikiComponentManagerEventListener") |
| 60 |
|
@Singleton |
| |
|
| 91.8% |
Uncovered Elements: 4 (49) |
Complexity: 17 |
Complexity Density: 0.52 |
|
| 61 |
|
public class DefaultWikiComponentManagerEventListener implements EventListener |
| 62 |
|
{ |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
@Inject |
| 67 |
|
private Logger logger; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
private WikiComponentManager wikiComponentManager; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
@Inject |
| 79 |
|
private List<WikiComponentBuilder> wikiComponentProviders; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
@link |
| 83 |
|
|
| 84 |
|
@Inject |
| 85 |
|
private Provider<XWikiContext> xcontextProvider; |
| 86 |
|
|
| 87 |
|
@Inject |
| 88 |
|
private ObservationContext observationContext; |
| 89 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 90 |
60 |
@Override... |
| 91 |
|
public List<Event> getEvents() |
| 92 |
|
{ |
| 93 |
60 |
return Arrays.asList( |
| 94 |
|
new DocumentCreatedEvent(), |
| 95 |
|
new DocumentUpdatedEvent(), |
| 96 |
|
new DocumentDeletedEvent(), |
| 97 |
|
new ApplicationReadyEvent(), |
| 98 |
|
new WikiReadyEvent()); |
| 99 |
|
} |
| 100 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 101 |
720 |
@Override... |
| 102 |
|
public String getName() |
| 103 |
|
{ |
| 104 |
720 |
return "defaultWikiComponentManagerEventListener"; |
| 105 |
|
} |
| 106 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 7 |
Complexity Density: 0.78 |
|
| 107 |
4037 |
@Override... |
| 108 |
|
public void onEvent(Event event, Object source, Object data) |
| 109 |
|
{ |
| 110 |
4037 |
if (source instanceof DocumentModelBridge) { |
| 111 |
3971 |
DocumentModelBridge document = (DocumentModelBridge) source; |
| 112 |
3971 |
DocumentReference documentReference = document.getDocumentReference(); |
| 113 |
|
|
| 114 |
3971 |
if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) { |
| 115 |
3815 |
registerComponents(document); |
| 116 |
156 |
} else if (event instanceof DocumentDeletedEvent) { |
| 117 |
|
|
| 118 |
155 |
unregisterComponents(documentReference); |
| 119 |
|
} |
| 120 |
66 |
} else if (event instanceof ApplicationReadyEvent || event instanceof WikiReadyEvent) { |
| 121 |
|
|
| 122 |
65 |
registerAllComponents(); |
| 123 |
|
} |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 129 |
65 |
private void registerAllComponents()... |
| 130 |
|
{ |
| 131 |
|
|
| 132 |
65 |
for (WikiComponentBuilder provider : this.wikiComponentProviders) { |
| 133 |
199 |
for (DocumentReference reference : provider.getDocumentReferences()) { |
| 134 |
240 |
try { |
| 135 |
240 |
List<WikiComponent> components = provider.buildComponents(reference); |
| 136 |
240 |
for (WikiComponent component : components) { |
| 137 |
240 |
this.wikiComponentManager.registerWikiComponent(component); |
| 138 |
|
} |
| 139 |
|
} catch (Exception e) { |
| 140 |
0 |
this.logger.warn("Failed to register the wiki component located in the document [{}]: {}", |
| 141 |
|
reference, ExceptionUtils.getRootCauseMessage(e)); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
@param |
| 151 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 152 |
3815 |
private void registerComponents(DocumentModelBridge document)... |
| 153 |
|
{ |
| 154 |
3815 |
DocumentReference documentReference = document.getDocumentReference(); |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
3815 |
unregisterComponents(documentReference); |
| 160 |
|
|
| 161 |
|
|
| 162 |
3815 |
for (WikiComponentBuilder provider : this.wikiComponentProviders) { |
| 163 |
|
|
| 164 |
11559 |
if (provider.getDocumentReferences().contains(documentReference)) { |
| 165 |
73 |
try { |
| 166 |
73 |
List<WikiComponent> components = provider.buildComponents(documentReference); |
| 167 |
73 |
for (WikiComponent component : components) { |
| 168 |
|
|
| 169 |
75 |
try { |
| 170 |
75 |
this.wikiComponentManager.registerWikiComponent(component); |
| 171 |
|
} catch (WikiComponentException e) { |
| 172 |
0 |
this.logger.warn("Unable to register component(s) from document [{}]: {}", |
| 173 |
|
component.getDocumentReference(), ExceptionUtils.getRootCauseMessage(e)); |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
} catch (WikiComponentException e) { |
| 177 |
0 |
this.logger.warn("Failed to create wiki component(s) for document [{}]: {}", documentReference, |
| 178 |
|
ExceptionUtils.getRootCauseMessage(e)); |
| 179 |
|
} |
| 180 |
73 |
break; |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
@param |
| 189 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 190 |
3970 |
private void unregisterComponents(DocumentReference documentReference)... |
| 191 |
|
{ |
| 192 |
3970 |
try { |
| 193 |
3970 |
this.wikiComponentManager.unregisterWikiComponents(documentReference); |
| 194 |
|
} catch (WikiComponentException e) { |
| 195 |
0 |
this.logger.warn("Unable to unregister component(s) from document [{}]: {}", documentReference, |
| 196 |
|
ExceptionUtils.getRootCauseMessage(e)); |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
} |