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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*************************************************************************
*
* $RCSfile: disposetimer.cxx,v $
*
* $Revision: 1.4 $
*
* last change: $Author: lla $ $Date: 2001-01-17 15:02:34 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (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.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#include <stdio.h>
#include "disposetimer.hxx"
#include "treecache.hxx"
#include "treedata.hxx"
#ifndef _CONFIGMGR_TRACER_HXX_
#include "tracer.hxx"
#endif
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif
namespace configmgr
{
//==========================================================================
//=
//==========================================================================
// -------------------------------------------------------------------------
void OTreeDisposeScheduler::scheduleCleanup(vos::ORef< OOptions > const& _xOptions)
{
OSL_ASSERT(_xOptions.isValid());
OSL_ENSURE(_xOptions->getLocale().getLength() >0, "ERROR: OTreeDisposeScheduler: cannot handle complete user scheduling");
osl::MutexGuard aGuard( m_aMutex );
CFG_TRACE_INFO("Scheduling data cleanup for user '%s' with locale '%s'",
OUSTRING2ASCII(_xOptions->getUser()), OUSTRING2ASCII(_xOptions->getLocale()));
CFG_TRACE_INFO_NI("- Cleanup will be started in about %d seconds", int(m_aCleanupDelay.getTimeValue().Seconds));
TimeStamp aNewTime = implGetCleanupTime(TimeStamp::getCurrentTime(), m_aCleanupDelay);
OSL_ASSERT(!aNewTime.isNever());
TimeStamp aScheduleTime = implAddTask(_xOptions, aNewTime);
OSL_ASSERT(aScheduleTime <= aNewTime);
OSL_ASSERT(!aScheduleTime.isNever());
implStartBefore(aNewTime);
}
// -------------------------------------------------------------------------
static
inline
bool equivalentOptions(vos::ORef< OOptions > const& lhs, vos::ORef< OOptions > const& rhs)
{
ltOptions lessThan;
return ! (lessThan(lhs,rhs) || lessThan(rhs,lhs));
}
// -------------------------------------------------------------------------
void OTreeDisposeScheduler::clearTasks(vos::ORef< OOptions > const& _xOptions)
{
osl::MutexGuard aOwnGuard( m_aMutex );
CFG_TRACE_INFO("Cancelling all data cleanup tasks for user '%s' with locale '%s'",
OUSTRING2ASCII(_xOptions->getUser()), OUSTRING2ASCII(_xOptions->getLocale()));
Agenda::iterator it = m_aAgenda.begin();
while(it != m_aAgenda.end())
{
Agenda::iterator cur = it++;
if (equivalentOptions(_xOptions,cur->second))
{
m_aAgenda.erase(cur);
CFG_TRACE_INFO_NI("- One pending task canceled");
}
}
}
// -------------------------------------------------------------------------
void OTreeDisposeScheduler::stopAndClearTasks()
{
osl::MutexGuard aOwnGuard( m_aMutex );
CFG_TRACE_INFO("Cancelling all data cleanup tasks, Stopping Cleanup timer");
CFG_TRACE_INFO_NI("- %d cleanup tasks were pending", int(m_aAgenda.size()) );
if (m_xTimer.isValid())
{
m_xTimer->stop(); // just to be sure
m_xTimer.unbind(); // just to be sure
}
m_aAgenda.clear();
}
// -------------------------------------------------------------------------
vos::ORef< OOptions > OTreeDisposeScheduler::getTask(TimeStamp const& _aActualTime, TimeStamp& _rNextTime)
{
OSL_ASSERT( _rNextTime.isNever() ); // internal contract, we set this only in the positive case
osl::MutexGuard aOwnGuard( m_aMutex );
vos::ORef< OOptions > xHandleNow;
if (!m_aAgenda.empty())
{
Agenda::iterator const it = m_aAgenda.begin();
if (it->first <= _aActualTime)
{
xHandleNow = it->second;
m_aAgenda.erase(it);
}
}
if (!m_aAgenda.empty())
{
Agenda::iterator const it = m_aAgenda.begin();
_rNextTime = it->first;
}
return xHandleNow;
}
// -------------------------------------------------------------------------
void OTreeDisposeScheduler::Timer::onShot()
{
rParent.onTimerShot();
}
// -------------------------------------------------------------------------
void OTreeDisposeScheduler::onTimerShot()
{
//m_aTimer.stop();
CFG_TRACE_INFO("Cleanup Timer invoked - executing dispose task");
TimeStamp aActualTime = TimeStamp::getCurrentTime();
TimeStamp aNextTime = implGetCleanupTime(aActualTime, getCleanupInterval());
try
{
TimeStamp aNextDisposeTime = runDisposer(aActualTime);
if (aNextTime < aNextDisposeTime)
aNextTime = aNextDisposeTime;
}
catch (uno::Exception& ue)
{
OSL_ENSURE(false, "ERROR: UNO Exception left a disposer");
ue;
}
catch (configuration::Exception& ce)
{
OSL_ENSURE(false, "ERROR: configuration::Exception left a disposer");
ce;
}
catch (...)
{
OSL_ENSURE(false, "ERROR: Unknown Exception left a disposer");
}
osl::MutexGuard aGuard(m_aMutex);
implStartBefore(aNextTime);
}
// -------------------------------------------------------------------------
// this really should be a member of the TreeManager (see TreeManager::disposeOne etc.)
TimeStamp OTreeDisposeScheduler::runDisposer(TimeStamp const& _aActualTime)
{
TimeStamp aNextTime = TimeStamp::never();
OSL_ASSERT(aNextTime.isNever());
osl::ClearableMutexGuard aGuard( m_rTreeManager.m_aTreeListMutex );
vos::ORef< OOptions > xTaskOption = this->getTask( _aActualTime, aNextTime );
if (xTaskOption.isValid())
{
CFG_TRACE_INFO("Found cleanup task for user %s and locale %s",
OUSTRING2ASCII(xTaskOption->getUser()), OUSTRING2ASCII(xTaskOption->getLocale()));
if (TreeInfo* pInfo = m_rTreeManager.requestTreeInfo(xTaskOption,false))
{
CFG_TRACE_INFO_NI("- Found matching data container (TreeInfo) - collecting data");
TreeInfo::DisposeList aDisposeList;
TimeStamp aNextTaskTime = pInfo->runDisposer(aDisposeList, _aActualTime);
// remove unnecessry notification entires
uno::Sequence< OUString > aNodeList = pInfo->removeNotificationEntries(aDisposeList);
CFG_TRACE_INFO_NI("- Found %d module trees to dispose", int(aDisposeList.size()) );
if (!aNextTaskTime.isNever())
{
OSL_ENSURE( !pInfo->isEmpty(), "ERROR: Empty TreeInfo returning finite dispose time");
// repost with new time
osl::MutexGuard aOwnGuard( m_aMutex );
CFG_TRACE_INFO_NI("- Rescheduling current option set" );
aNextTime = this->implAddTask(xTaskOption,aNextTaskTime);
}
else if (pInfo->isEmpty())// may have been the last one - check that
{
CFG_TRACE_INFO_NI("- Disposing last data for this options set => Removing TreeInfo" );
// get rid of it - see TreeManager::disposeOne
std::auto_ptr<TreeInfo> pDisposeInfo(pInfo);
m_rTreeManager.m_aTreeList.erase(xTaskOption);
// got it out of reachability - now dispose/notify without lock
aGuard.clear();
m_rTreeManager.ConfigChangeBroadcaster::disposeBroadcastHelper(pInfo->pBroadcastHelper);
OSL_ENSURE(pInfo->m_aNotificationList.empty(),
"WARNING: Empty TreeInfo still has notifications registered - will be leaked");
pInfo = NULL;
}
else
CFG_TRACE_INFO_NI("- Currently no more cleanup tasks for this options set" );
// clear the guard and throw away the nodes
aGuard.clear();
if (!aDisposeList.empty())
{
if (m_rTreeManager.m_pSession)
{
if (aNodeList.getLength() > 0)
{
CFG_TRACE_INFO_NI("- Stoping notifications for %d Nodes", int(aNodeList.getLength()) );
m_rTreeManager.cancelNotify(aNodeList, xTaskOption);
}
uno::Sequence< OUString > aCloseList = TreeInfo::collectNodeIds(aDisposeList);
if (aCloseList.getLength() > 0)
{
CFG_TRACE_INFO_NI("- Closing %d NodeIds", int(aCloseList.getLength()) );
m_rTreeManager.closeNodes(aCloseList,xTaskOption);
}
}
CFG_TRACE_INFO_NI("- Now disposing %d module trees", int(aDisposeList.size()) );
}
else
CFG_TRACE_INFO_NI("- No modules trees to dispose");
}
else
CFG_TRACE_INFO_NI("- No matching data container (TreeInfo) found - task is obsolete");
}
else
CFG_TRACE_INFO("No eligible task found - may reschedule");
return aNextTime;
}
// -------------------------------------------------------------------------
static inline
TimeStamp getExpirationTime( vos::OTimer const& aTimer )
{
return TimeStamp::getCurrentTime() + TimeInterval( aTimer.getRemainingTime() );
}
// -------------------------------------------------------------------------
// should be called guarded only
void OTreeDisposeScheduler::implStartBefore(TimeStamp const& _aTime)
{
// check if we were cleared
if (!m_aAgenda.empty() && !_aTime.isNever())
{
if (m_xTimer.isEmpty())
m_xTimer = new Timer(*this);
if (!m_xTimer->isTicking() || _aTime < getExpirationTime(*m_xTimer))
{
m_xTimer->setAbsoluteTime(_aTime.getTimeValue());
if (!m_xTimer->isTicking()) m_xTimer->start();
OSL_ASSERT( m_xTimer->isTicking() && getExpirationTime(*m_xTimer) <= _aTime );
}
CFG_TRACE_INFO_NI("- Cleanup timer running - next execution in %d seconds", int (m_xTimer->getRemainingTime().Seconds) );
CFG_TRACE_INFO_NI("- %d cleanup tasks are pending", int(m_aAgenda.size()) );
}
else
{
if (!m_xTimer.isEmpty())
{
m_xTimer->stop();
CFG_TRACE_INFO_NI("- Stopped timer - no more open cleanup tasks");
}
}
}
// -------------------------------------------------------------------------
// should be called guarded only (m_aMutex must be locked)
TimeStamp OTreeDisposeScheduler::implAddTask(vos::ORef< OOptions > const& _xOptions, TimeStamp const& _aTime)
{
typedef Agenda::value_type Task;
// try to insert after euivalent entries (but STL may ignore the hint)
Agenda::iterator where = m_aAgenda.upper_bound(_aTime);
m_aAgenda.insert(where, Task(_aTime,_xOptions));
OSL_ASSERT(!m_aAgenda.empty());
return m_aAgenda.begin()->first;
}
// -------------------------------------------------------------------------
// =========================================================================
OTreeCacheWriteScheduler::~OTreeCacheWriteScheduler()
{
stopAndWriteCache();
}
void OTreeCacheWriteScheduler::stopAndWriteCache()
{
osl::MutexGuard aOwnGuard( m_aMutex );
CFG_TRACE_INFO("Cancelling all cache writings, Stopping timer");
if (m_xTimer.isValid())
{
m_xTimer->stop(); // just to be sure
m_xTimer.unbind(); // just to be sure
}
runDisposer();
}
// -------------------------------------------------------------------------
void OTreeCacheWriteScheduler::Timer::onShot()
{
rParent.onTimerShot();
}
// -----------------------------------------------------------------------------
void OTreeCacheWriteScheduler::onTimerShot()
{
//m_aTimer.stop();
CFG_TRACE_INFO("Cleanup Timer invoked - executing dispose task");
try
{
runDisposer();
}
catch (uno::Exception& ue)
{
OSL_ENSURE(false, "ERROR: UNO Exception left a disposer");
ue;
}
catch (configuration::Exception& ce)
{
OSL_ENSURE(false, "ERROR: configuration::Exception left a disposer");
ce;
}
catch (...)
{
OSL_ENSURE(false, "ERROR: Unknown Exception left a disposer");
}
TimeStamp aNewTime = implGetCleanupTime(TimeStamp::getCurrentTime(), m_aCleanupInterval);
osl::MutexGuard aGuard(m_aMutex);
implStartBefore(aNewTime);
}
// -------------------------------------------------------------------------
void OTreeCacheWriteScheduler::runDisposer()
{
// Write Cache
CFG_TRACE_INFO("Starting lasy write");
osl::ClearableMutexGuard aGuard( m_rTreeManager.m_aUpdateMutex );
for (CacheWriteList::iterator it = m_aWriteList.begin();
it != m_aWriteList.end();
++it)
{
vos::ORef< OOptions > xTaskOption = *it;
if (xTaskOption.isValid())
{
if (TreeInfo* pInfo = m_rTreeManager.requestTreeInfo(xTaskOption,false))
{
CFG_TRACE_INFO_NI("- Found matching data container (TreeInfo) - collecting data");
PendingList aList;
sal_Int32 nCount = pInfo->syncPending(xTaskOption, aList);
if (nCount > 0)
{
CFG_TRACE_INFO_NI("write down %d pendings", nCount);
for(PendingList::iterator it = aList.begin();
it != aList.end();
++it)
{
rtl::OUString sName = it->first;
auto_ptr<SubtreeChange> aSubtreeChange = it->second;
ConfigurationName aName; // MUST be empty, we have the ptr to the root obj
m_rTreeManager.sessionUpdate(xTaskOption, aName, aSubtreeChange);
}
}
}
}
else
{
CFG_TRACE_WARNING_NI("runDisposer: TaskOption not valid");
}
}
m_aWriteList.clear();
}
// -----------------------------------------------------------------------------
// should be called guarded only
void OTreeCacheWriteScheduler::implStartBefore(TimeStamp const& _aTime)
{
// check if we were cleared
if (!m_aWriteList.empty())
{
if (m_xTimer.isEmpty())
m_xTimer = new Timer(*this);
if (!m_xTimer->isTicking())
{
m_xTimer->setAbsoluteTime(_aTime.getTimeValue());
if (!m_xTimer->isTicking())
m_xTimer->start();
OSL_ASSERT( m_xTimer->isTicking() );
}
CFG_TRACE_INFO_NI("- Cleanup timer running - next execution in %d seconds", int (m_xTimer->getRemainingTime().Seconds) );
CFG_TRACE_INFO_NI("- %d cleanup tasks are pending", int(m_aWriteList.size()) );
}
else
{
if (!m_xTimer.isEmpty())
{
m_xTimer->stop();
CFG_TRACE_INFO_NI("- Stopped timer - no more open cleanup tasks");
}
}
}
// -----------------------------------------------------------------------------
void OTreeCacheWriteScheduler::scheduleWrite(vos::ORef< OOptions > const& _xOptions, bool _bSync)
{
OSL_ASSERT(_xOptions.isValid());
OSL_ENSURE(_xOptions->getLocale().getLength() >0, "ERROR: OTreeDisposeScheduler: cannot handle complete user scheduling");
osl::MutexGuard aGuard( m_aMutex );
CFG_TRACE_INFO("Scheduling cache write for user '%s' with locale '%s'",
OUSTRING2ASCII(_xOptions->getUser()), OUSTRING2ASCII(_xOptions->getLocale()));
CFG_TRACE_INFO_NI("- cache write will be started in about %d seconds", int(m_aCleanupInterval.getTimeValue().Seconds));
m_aWriteList.push_back(_xOptions);
if (_bSync || m_bSyncron)
{
// write now!
runDisposer();
}
else
{
// lasy writing
TimeStamp aNewTime = implGetCleanupTime(TimeStamp::getCurrentTime(), m_aCleanupInterval);
implStartBefore(aNewTime);
}
}
} // namespace
|