use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.
the class LocalEventListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
if (this.remoteObservationManager == null) {
try {
// Make sure to not receive events until RemoteObservationManager is ready
ObservationManager om = this.componentManager.getInstance(ObservationManager.class);
om.removeListener(getName());
this.remoteObservationManager = this.componentManager.getInstance(RemoteObservationManager.class);
om.addListener(this);
this.remoteObservationManager.notify(new LocalEventData(event, source, data));
} catch (ComponentLookupException e) {
this.logger.error("Failed to initialize the Remote Observation Manager", e);
}
} else {
this.remoteObservationManager.notify(new LocalEventData(event, source, data));
}
}
use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.
the class ImportAction method importPackageFilterStream.
private void importPackageFilterStream(XWikiAttachment packFile, XWikiRequest request, XWikiContext context) throws IOException, XWikiException, FilterException {
String[] pages = request.getParameterValues("pages");
XARInputProperties xarProperties = new XARInputProperties();
DocumentInstanceOutputProperties instanceProperties = new DocumentInstanceOutputProperties();
instanceProperties.setSaveComment("Imported from XAR");
if (pages != null) {
EntityReferenceSet entities = new EntityReferenceSet();
EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING, "relative");
for (String pageEntry : pages) {
if (StringUtils.isNotEmpty(pageEntry)) {
String locale = getLocale(pageEntry, request);
int iAction = getAction(pageEntry, locale, request);
String documentReference = getDocumentReference(pageEntry);
if (iAction == DocumentInfo.ACTION_OVERWRITE) {
entities.includes(new LocalDocumentReference(resolver.resolve(documentReference, EntityType.DOCUMENT), LocaleUtils.toLocale(locale)));
}
}
}
xarProperties.setEntities(entities);
}
// Set the appropriate strategy to handle versions
if (StringUtils.equals(request.getParameter("historyStrategy"), "reset")) {
instanceProperties.setPreviousDeleted(true);
instanceProperties.setVersionPreserved(false);
xarProperties.setWithHistory(false);
} else if (StringUtils.equals(request.getParameter("historyStrategy"), "replace")) {
instanceProperties.setPreviousDeleted(true);
instanceProperties.setVersionPreserved(true);
xarProperties.setWithHistory(true);
} else {
instanceProperties.setPreviousDeleted(false);
instanceProperties.setVersionPreserved(false);
xarProperties.setWithHistory(false);
}
// Set the backup pack option
if (StringUtils.equals(request.getParameter("importAsBackup"), "true")) {
instanceProperties.setAuthorPreserved(true);
} else {
instanceProperties.setAuthorPreserved(false);
}
BeanInputFilterStreamFactory<XARInputProperties> xarFilterStreamFactory = Utils.getComponent((Type) InputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
BeanInputFilterStream<XARInputProperties> xarFilterStream = xarFilterStreamFactory.createInputFilterStream(xarProperties);
BeanOutputFilterStreamFactory<InstanceOutputProperties> instanceFilterStreamFactory = Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
BeanOutputFilterStream<InstanceOutputProperties> instanceFilterStream = instanceFilterStreamFactory.createOutputFilterStream(instanceProperties);
// Notify all the listeners about import
ObservationManager observation = Utils.getComponent(ObservationManager.class);
InputStream source = packFile.getContentInputStream(context);
xarProperties.setSource(new DefaultInputStreamInputSource(source));
// Setup log
xarProperties.setVerbose(true);
instanceProperties.setVerbose(true);
instanceProperties.setStoppedWhenSaveFail(false);
LoggerManager loggerManager = Utils.getComponent(LoggerManager.class);
LogQueue logger = new LogQueue();
if (loggerManager != null) {
// Isolate log
loggerManager.pushLogListener(new LoggerListener(UUID.randomUUID().toString(), logger));
}
observation.notify(new XARImportingEvent(), null, context);
try {
xarFilterStream.read(instanceFilterStream.getFilter());
xarFilterStream.close();
instanceFilterStream.close();
} finally {
if (loggerManager != null) {
// Stop isolating log
loggerManager.popLogListener();
}
// Print the import log
if (LOGGER.isDebugEnabled()) {
logger.log(LOGGER);
} else {
// TODO: remove when the UI show the log properly
for (LogEvent logEvent : logger.getLogsFrom(LogLevel.ERROR)) {
logEvent.log(LOGGER);
}
}
// Close the input source
source.close();
observation.notify(new XARImportedEvent(), null, context);
}
// Generate import report
// Emulate old packager report (for retro compatibility)
Package oldImporter = new Package();
if (logger.containLogsFrom(LogLevel.ERROR)) {
context.put("install_status", DocumentInfo.INSTALL_ERROR);
} else {
context.put("install_status", DocumentInfo.INSTALL_OK);
}
EntityReferenceSerializer<String> serializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
for (LogEvent log : logger) {
Marker marker = log.getMarker();
if (marker != null) {
if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_CREATED.getName()) || marker.contains(WikiDocumentFilter.LOG_DOCUMENT_UPDATED.getName())) {
oldImporter.getInstalled(context).add(serializer.serialize((EntityReference) log.getArgumentArray()[0]));
} else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED.getName())) {
oldImporter.getSkipped(context).add(serializer.serialize((EntityReference) log.getArgumentArray()[0]));
} else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_ERROR.getName())) {
Object entity = log.getArgumentArray()[0];
if (entity != null) {
oldImporter.getErrors(context).add(entity instanceof EntityReference ? serializer.serialize((EntityReference) log.getArgumentArray()[0]) : entity.toString());
}
}
}
}
}
use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.
the class ContextComponentManagerTest method testDeleteWiki.
@Test
public void testDeleteWiki() throws ComponentLookupException, Exception {
getMockery().checking(new Expectations() {
{
allowing(mockWikiDescriptorManager).getCurrentWikiId();
will(returnValue("wiki"));
allowing(mockCurrentSpaceReferenceProvider).get();
will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
allowing(mockCurrentDocumentReferenceProvider).get();
will(returnValue(new DocumentReference("wiki", "space", "document")));
allowing(mockDocumentAccessBridge).getCurrentUserReference();
will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
}
});
// Register in the current wiki.
ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
cd.setRoleType(Role.class);
cd.setImplementation(RoleImpl.class);
wikiCM.registerComponent(cd);
ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
observationManager.notify(new WikiDeletedEvent("wiki"), null, null);
Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
}
use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method setUp.
@Before
public void setUp() throws Exception {
// mock
this.contextUser = new DocumentReference(getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
this.localXarExtensiontId1 = new ExtensionId("test", "1.0");
this.localXarExtensiontId2 = new ExtensionId("test", "2.0");
this.collisionextension1 = new ExtensionId("collisionextension1", "version");
this.collisionextension2 = new ExtensionId("collisionextension2", "version");
// classes
BaseClass styleSheetClass = new BaseClass();
this.classes.put("StyleSheetExtension", styleSheetClass);
// checking
doReturn(true).when(this.oldcore.getSpyXWiki()).hasAttachmentRecycleBin(any(XWikiContext.class));
getXWikiContext().setUserReference(this.contextUser);
((XWikiStubContextProvider) this.componentManager.getInstance(XWikiStubContextProvider.class)).initialize(getXWikiContext());
CoreConfiguration coreConfiguration = this.componentManager.getInstance(CoreConfiguration.class);
doReturn(Syntax.PLAIN_1_0).when(coreConfiguration).getDefaultDocumentSyntax();
// lookup
this.jobExecutor = this.componentManager.getInstance(JobExecutor.class);
this.xarExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, XarExtensionHandler.TYPE);
this.observation = this.repositoryUtil.getComponentManager().getInstance(ObservationManager.class);
// Get rid of wiki macro listener
this.componentManager.<ObservationManager>getInstance(ObservationManager.class).removeListener("RegisterMacrosOnImportListener");
this.installedExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, "xar");
// Programming right is not required for XAR extensions
doThrow(AccessDeniedException.class).when(this.oldcore.getMockAuthorizationManager()).checkAccess(eq(Right.PROGRAM), any(), any());
}
use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.
the class XWiki method copyWiki.
/**
* Copy an entire wiki to a target wiki.
*
* @param sourceWiki the source wiki identifier
* @param targetWiki the target wiki identifier
* @param locale the locale to copy
* @param clean clean the target wiki before copying
* @param context see {@link XWikiContext}
* @return the number of copied documents
* @throws XWikiException failed to copy wiki
* @deprecated since 5.3, use {@link WikiManager#copy(String, String, String, boolean, boolean, boolean)} instead
*/
@Deprecated
public int copyWiki(String sourceWiki, String targetWiki, String locale, boolean clean, XWikiContext context) throws XWikiException {
int documents = copySpaceBetweenWikis(null, sourceWiki, targetWiki, locale, clean, context);
ObservationManager om = getObservationManager();
if (om != null) {
om.notify(new WikiCopiedEvent(sourceWiki, targetWiki), sourceWiki, context);
}
return documents;
}
Aggregations