1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* -*- 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 .
*/
#include <jobs/job.hxx>
#include <jobs/configaccess.hxx>
#include <classes/converter.hxx>
#include <helper/mischelper.hxx>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XContainer.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/task/XJobExecutor.hpp>
#include <com/sun/star/container/XContainerListener.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/document/XEventListener.hpp>
#include <cppuhelper/basemutex.hxx>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/configpaths.hxx>
#include <rtl/ref.hxx>
#include <sal/log.hxx>
#include <vcl/svapp.hxx>
using namespace framework;
namespace {
typedef cppu::WeakComponentImplHelper<
css::lang::XServiceInfo
, css::task::XJobExecutor
, css::container::XContainerListener // => lang.XEventListener
, css::document::XEventListener >
Base;
/**
@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 : private cppu::BaseMutex, 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 */
css::uno::Reference<css::container::XContainerListener> m_xConfigListener;
virtual void SAL_CALL disposing() final override;
public:
explicit JobExecutor(const css::uno::Reference< css::uno::XComponentContext >& xContext);
virtual ~JobExecutor() override;
virtual OUString SAL_CALL getImplementationName() override
{
return "com.sun.star.comp.framework.JobExecutor";
}
virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
{
return cppu::supportsService(this, ServiceName);
}
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
{
return {"com.sun.star.task.JobExecutor"};
}
// task.XJobExecutor
virtual void SAL_CALL trigger( const OUString& sEvent ) override;
/// Initialization function after having acquire()'d.
void initListeners();
// document.XEventListener
virtual void SAL_CALL notifyEvent( const css::document::EventObject& aEvent ) override;
// container.XContainerListener
virtual void SAL_CALL elementInserted( const css::container::ContainerEvent& aEvent ) override;
virtual void SAL_CALL elementRemoved ( const css::container::ContainerEvent& aEvent ) override;
virtual void SAL_CALL elementReplaced( const css::container::ContainerEvent& aEvent ) override;
// lang.XEventListener
virtual void SAL_CALL disposing( const css::lang::EventObject& aEvent ) 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 )
: Base (m_aMutex)
, m_xContext (xContext )
, m_aConfig (xContext, "/org.openoffice.Office.Jobs/Events")
{
}
void JobExecutor::initListeners()
{
if (utl::ConfigManager::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;
css::uno::Reference< css::container::XNameAccess > xRegistry(
m_aConfig.cfg(), css::uno::UNO_QUERY);
if (xRegistry.is())
m_lEvents = Converter::convert_seqOUString2OUStringList(
xRegistry->getElementNames());
css::uno::Reference< css::container::XContainer > xNotifier(
m_aConfig.cfg(), css::uno::UNO_QUERY);
if (xNotifier.is())
{
m_xConfigListener = new WeakContainerListener(this);
xNotifier->addContainerListener(m_xConfigListener);
}
// don't close cfg here!
// It will be done inside disposing ...
}
JobExecutor::~JobExecutor()
{
disposing();
}
void JobExecutor::disposing() {
css::uno::Reference<css::container::XContainer> notifier;
css::uno::Reference<css::container::XContainerListener> listener;
{
osl::MutexGuard g(rBHelper.rMutex);
if (m_aConfig.getMode() != ConfigAccess::E_CLOSED) {
notifier.set(m_aConfig.cfg(), css::uno::UNO_QUERY);
listener = m_xConfigListener;
m_aConfig.close();
}
m_xConfigListener.clear();
}
if (notifier.is()) {
notifier->removeContainerListener(listener);
}
}
/**
@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()");
std::vector< OUString > lJobs;
/* SAFE */ {
osl::MutexGuard g(rBHelper.rMutex);
// 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;
// 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.
lJobs = JobData::getEnabledJobsForEvent(m_xContext, sEvent);
} /* SAFE */
// step over all enabled jobs and execute it
size_t c = lJobs.size();
for (size_t j=0; j<c; ++j)
{
rtl::Reference<Job> pJob;
/* SAFE */
{
SolarMutexGuard g2;
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.
*/
pJob = new Job(m_xContext, css::uno::Reference< css::frame::XFrame >());
pJob->setJobData(aCfg);
} /* SAFE */
pJob->execute(css::uno::Sequence< css::beans::NamedValue >());
}
}
void SAL_CALL JobExecutor::notifyEvent( const css::document::EventObject& aEvent )
{
static const OUStringLiteral EVENT_ON_DOCUMENT_OPENED(u"onDocumentOpened"); // Job UI event : OnNew or OnLoad
static const OUStringLiteral EVENT_ON_DOCUMENT_ADDED(u"onDocumentAdded"); // Job API event : OnCreate or OnLoadFinished
OUString aModuleIdentifier;
::std::vector< JobData::TJob2DocEventBinding > lJobs;
/* SAFE */ {
osl::MutexGuard g(rBHelper.rMutex);
// 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 ...
// retrieve event context from event source
try
{
aModuleIdentifier = css::frame::ModuleManager::create( m_xContext )->identify( aEvent.Source );
}
catch( const css::uno::Exception& )
{}
// 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 (auto const& lJob : lJobs)
{
rtl::Reference<Job> pJob;
/* SAFE */ {
SolarMutexGuard g2;
const JobData::TJob2DocEventBinding& rBinding = lJob;
JobData aCfg(m_xContext);
aCfg.setEvent(rBinding.m_sDocEvent, rBinding.m_sJobName);
aCfg.setEnvironment(JobData::E_DOCUMENTEVENT);
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);
} /* SAFE */
pJob->execute(css::uno::Sequence< css::beans::NamedValue >());
}
}
void SAL_CALL JobExecutor::elementInserted( const css::container::ContainerEvent& aEvent )
{
OUString sValue;
if (aEvent.Accessor >>= sValue)
{
OUString sEvent = ::utl::extractFirstFromConfigurationPath(sValue);
if (!sEvent.isEmpty())
{
std::vector<OUString>::iterator pEvent = std::find(m_lEvents.begin(), m_lEvents.end(), sEvent);
if (pEvent == m_lEvents.end())
m_lEvents.push_back(sEvent);
}
}
}
void SAL_CALL JobExecutor::elementRemoved ( const css::container::ContainerEvent& aEvent )
{
OUString sValue;
if (aEvent.Accessor >>= sValue)
{
OUString sEvent = ::utl::extractFirstFromConfigurationPath(sValue);
if (!sEvent.isEmpty())
{
std::vector<OUString>::iterator pEvent = std::find(m_lEvents.begin(), m_lEvents.end(), sEvent);
if (pEvent != m_lEvents.end())
m_lEvents.erase(pEvent);
}
}
}
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 { */
osl::MutexGuard g(rBHelper.rMutex);
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 */
}
struct Instance {
explicit Instance(
css::uno::Reference<css::uno::XComponentContext> const & context):
instance(
static_cast<cppu::OWeakObject *>(new JobExecutor(context)))
{
// 2nd phase initialization needed
static_cast<JobExecutor *>(static_cast<cppu::OWeakObject *>
(instance.get()))->initListeners();
}
rtl::Reference<css::uno::XInterface> instance;
};
struct Singleton:
public rtl::StaticWithArg<
Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
{};
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_framework_JobExecutor_get_implementation(
css::uno::XComponentContext *context,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(static_cast<cppu::OWeakObject *>(
Singleton::get(context).instance.get()));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|