/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/** @short implements a job executor, which can be triggered from any code @descr It uses the given trigger event to locate any registered job service inside the configuration and execute it. Of course it controls the lifetime of such jobs too.
*/ class JobExecutor : public Base
{ private:
/** reference to the uno service manager */
css::uno::Reference< css::uno::XComponentContext > m_xContext;
/** cached list of all registered event names of cfg for call optimization. */
std::vector<OUString> m_lEvents;
/** we listen at the configuration for changes at the event list. */
ConfigAccess m_aConfig;
/** helper to allow us listen to the configuration without a cyclic dependency */
rtl::Reference<WeakContainerListener> m_xConfigListener;
virtualvoid disposing(std::unique_lock<std::mutex>& rGuard) final override;
/** @short standard ctor @descr It initialize this new instance.
@param xContext reference to the uno service manager
*/
JobExecutor::JobExecutor( /*IN*/ const css::uno::Reference< css::uno::XComponentContext >& xContext )
: m_xContext (xContext )
, m_aConfig (xContext, u"/org.openoffice.Office.Jobs/Events"_ustr)
{
}
void JobExecutor::initListeners()
{ if (comphelper::IsFuzzing()) return;
// read the list of all currently registered events inside configuration. // e.g. "/org.openoffice.Office.Jobs/Events/<event name>" // We need it later to check if an incoming event request can be executed successfully // or must be rejected. It's an optimization! Of course we must implement updating of this // list too ... Be listener at the configuration.
m_aConfig.open(ConfigAccess::E_READONLY); if (m_aConfig.getMode() != ConfigAccess::E_READONLY) return;
/** @short implementation of XJobExecutor interface @descr We use the given event to locate any registered job inside our configuration and execute it. Further we control the lifetime of it and suppress shutdown of the office till all jobs was finished.
@param sEvent is used to locate registered jobs
*/ void SAL_CALL JobExecutor::trigger( const OUString& sEvent )
{
SAL_INFO( "fwk", "JobExecutor::trigger()");
/* SAFE */
{
std::unique_lock g(m_aMutex);
// Optimization! // Check if the given event name exist inside configuration and reject wrong requests. // This optimization suppress using of the cfg api for getting event and job descriptions ... if (std::find(m_lEvents.begin(), m_lEvents.end(), sEvent) == m_lEvents.end()) return;
} /* SAFE */
// get list of all enabled jobs // The called static helper methods read it from the configuration and // filter disabled jobs using it's time stamp values.
std::vector< OUString > lJobs = JobData::getEnabledJobsForEvent(m_xContext, sEvent);
// step over all enabled jobs and execute it
size_t c = lJobs.size(); for (size_t j=0; j<c; ++j)
{
JobData aCfg(m_xContext);
aCfg.setEvent(sEvent, lJobs[j]);
aCfg.setEnvironment(JobData::E_EXECUTION);
/*Attention! Jobs implements interfaces and dies by ref count! And freeing of such uno object is done by uno itself. So we have to use dynamic memory everytimes.
*/
rtl::Reference<Job> pJob = new Job(m_xContext, css::uno::Reference< css::frame::XFrame >());
pJob->setJobData(aCfg);
// Optimization! // Check if the given event name exist inside configuration and reject wrong requests. // This optimization suppress using of the cfg api for getting event and job descriptions. // see using of m_lEvents.find() below ...
// Special feature: If the events "OnNew" or "OnLoad" occurs - we generate our own event "onDocumentOpened". if (
(aEvent.EventName == "OnNew") ||
(aEvent.EventName == "OnLoad")
)
{ if (std::find(m_lEvents.begin(), m_lEvents.end(), EVENT_ON_DOCUMENT_OPENED) != m_lEvents.end())
JobData::appendEnabledJobsForEvent(m_xContext, EVENT_ON_DOCUMENT_OPENED, lJobs);
}
// Special feature: If the events "OnCreate" or "OnLoadFinished" occurs - we generate our own event "onDocumentAdded". if (
(aEvent.EventName == "OnCreate") ||
(aEvent.EventName == "OnLoadFinished")
)
{ if (std::find(m_lEvents.begin(), m_lEvents.end(), EVENT_ON_DOCUMENT_ADDED) != m_lEvents.end())
JobData::appendEnabledJobsForEvent(m_xContext, EVENT_ON_DOCUMENT_ADDED, lJobs);
}
// Add all jobs for "real" notified event too .-) if (std::find(m_lEvents.begin(), m_lEvents.end(), aEvent.EventName) != m_lEvents.end())
JobData::appendEnabledJobsForEvent(m_xContext, aEvent.EventName, lJobs);
} /* SAFE */
// step over all enabled jobs and execute it for (autoconst& lJob : lJobs)
{
rtl::Reference<Job> pJob;
if (!aCfg.hasCorrectContext(aModuleIdentifier)) continue;
/*Attention! Jobs implements interfaces and dies by ref count! And freeing of such uno object is done by uno itself. So we have to use dynamic memory everytimes.
*/
css::uno::Reference< css::frame::XModel > xModel(aEvent.Source, css::uno::UNO_QUERY);
pJob = new Job(m_xContext, xModel);
pJob->setJobData(aCfg);
void SAL_CALL JobExecutor::elementReplaced( const css::container::ContainerEvent& )
{ // I'm not interested on changed items :-)
}
/** @short the used cfg changes notifier wish to be released in its reference.
@descr We close our internal used configuration instance to free this reference.
@attention For the special feature "bind global document event broadcaster to job execution" this job executor instance was registered from outside code as css.document.XEventListener. So it can be, that this disposing call comes from the global event broadcaster service. But we don't hold any reference to this service which can or must be released. Because this broadcaster itself is a one instance service too, we can ignore this request. On the other side we must release our internal CFG reference... SOLUTION => check the given event source and react only, if it's our internal hold configuration object!
*/ void SAL_CALL JobExecutor::disposing( const css::lang::EventObject& aEvent )
{ /* SAFE { */
std::unique_lock g(m_aMutex);
css::uno::Reference< css::uno::XInterface > xCFG(m_aConfig.cfg(), css::uno::UNO_QUERY); if (
(xCFG == aEvent.Source ) &&
(m_aConfig.getMode() != ConfigAccess::E_CLOSED)
)
{
m_aConfig.close();
} /* } SAFE */
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.