1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cachewritescheduler.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
36 #include "cachewritescheduler.hxx"
37 #include "cachecontroller.hxx"
43 // =========================================================================
44 OCacheWriteScheduler::~OCacheWriteScheduler()
46 stopAndWriteCache();// last chance - violates precond
49 void OCacheWriteScheduler::stopAndWriteCache()
51 OSL_ASSERT(UnoApiLock::isHeld());
52 CFG_TRACE_INFO("Cancelling all cache writings, Stopping timer");
54 if (m_xTimer
.isValid())
55 m_xTimer
->dispose(); // just to be sure
62 // -------------------------------------------------------------------------
63 void OCacheWriteScheduler::Timer::onShot()
68 pParent
->onTimerShot();
70 CFG_TRACE_WARNING("Timer shot for disposed cache writer");
73 // -----------------------------------------------------------------------------
74 void OCacheWriteScheduler::onTimerShot()
78 CFG_TRACE_INFO("Write Timer invoked - executing write task");
83 CFG_TRACE_INFO_NI("Write timer: writing ended");
87 CFG_TRACE_ERROR_NI("Write timer: writing failed with an unknown exception");
88 OSL_ENSURE(false, "ERROR: Unknown Exception left a writer");
91 TimeStamp aNewTime
= implGetScheduleTime(TimeStamp::getCurrentTime(), m_aWriteInterval
);
93 implStartBefore(aNewTime
);
95 // -------------------------------------------------------------------------
96 void OCacheWriteScheduler::runWriter()
99 OSL_ASSERT(UnoApiLock::isHeld());
100 CFG_TRACE_INFO("Running write operations");
102 CacheWriteList aPendingWrites
;
103 m_aWriteList
.swap(aPendingWrites
);
105 CFG_TRACE_INFO_NI("Found %d sections to write", int(aPendingWrites
.size()));
106 for (CacheWriteList::iterator it
= aPendingWrites
.begin();
107 it
!= aPendingWrites
.end();
110 RequestOptions aTaskOption
= *it
;
113 writeOneTreeFoundByOption(aTaskOption
);
115 catch (uno::Exception
& e
)
118 CFG_TRACE_ERROR_NI("TreeCacheWriteScheduler: Attempt to write data failed - error is '%s' (currently ignored)",OUSTRING2ASCII(e
.Message
));
121 // m_aWriteList.clear();
122 CFG_TRACE_INFO_NI("DONE: Running write operations");
125 // -----------------------------------------------------------------------------
126 void OCacheWriteScheduler::writeOneTreeFoundByOption(RequestOptions
const& _aOptions
) SAL_THROW((com::sun::star::uno::Exception
))
128 CFG_TRACE_INFO("Writeing one cache tree for user '%s' with locale '%s'",
129 OUSTRING2ASCII(_aOptions
.getEntity()),
130 OUSTRING2ASCII(_aOptions
.getLocale()));
132 rtl::Reference
<CacheLoadingAccess
> aCache
;
133 aCache
= m_rTreeManager
.m_aCacheMap
.get(_aOptions
);
137 CFG_TRACE_INFO_NI("- Found matching data container - starting write task");
138 if (!m_rTreeManager
.saveAllPendingChanges(aCache
,_aOptions
))
140 m_aWriteList
.insert(_aOptions
);
142 CFG_TRACE_INFO_NI("- Write task incomplete -reregistering");
144 // we got a pending list with pointers from TreeInfo.
148 CFG_TRACE_WARNING_NI("- Data container (TreeInfo) to write not found: Ignoring task");
151 CFG_TRACE_INFO_NI("Removing written cache tree (for user '%s' with locale '%s')",
152 OUSTRING2ASCII(_aOptions
.getEntity()),
153 OUSTRING2ASCII(_aOptions
.getLocale()));
156 // -----------------------------------------------------------------------------
157 bool OCacheWriteScheduler::clearTasks(RequestOptions
const& _aOptions
)
159 // sadly list::remove doesn't return an indication of what it did
160 bool bFound
= m_aWriteList
.erase(_aOptions
) !=0;
163 CFG_TRACE_INFO("Write Scheduler: Dropped cache tree (for user '%s' with locale '%s') from task list",
164 OUSTRING2ASCII(_aOptions
.getEntity()),
165 OUSTRING2ASCII(_aOptions
.getLocale()));
171 // -----------------------------------------------------------------------------
172 // should be called guarded only
173 void OCacheWriteScheduler::implStartBefore(TimeStamp
const& _aTime
)
175 CFG_TRACE_INFO("Triggering write timer");
176 // check if we were cleared
177 if (!m_aWriteList
.empty())
179 if (!m_xTimer
->isTicking())
181 m_xTimer
->setAbsoluteTime(_aTime
.getTimeValue());
183 if (!m_xTimer
->isTicking())
186 OSL_ASSERT( m_xTimer
->isTicking() );
188 CFG_TRACE_INFO_NI("- Write timer running - next execution in %d seconds", int (m_xTimer
->getRemainingTime().Seconds
) );
189 CFG_TRACE_INFO_NI("- %d write tasks are pending", int(m_aWriteList
.size()) );
194 CFG_TRACE_INFO_NI("- Stopped timer - no more open write tasks");
198 // -----------------------------------------------------------------------------
199 void OCacheWriteScheduler::scheduleWrite(backend::ComponentRequest _aComponent
) SAL_THROW((com::sun::star::uno::Exception
))
201 OSL_ENSURE(_aComponent
.getOptions().hasLocale(), "ERROR: OTreeDisposeScheduler: cannot handle complete user scheduling");
203 CFG_TRACE_INFO("Scheduling cache write for user '%s' with locale '%s'",
204 OUSTRING2ASCII(_aComponent
.getOptions().getEntity()),
205 OUSTRING2ASCII(_aComponent
.getOptions().getLocale()));
208 m_aWriteList
.insert(_aComponent
.getOptions());
210 TimeStamp aNewTime
= implGetScheduleTime(TimeStamp::getCurrentTime(), m_aWriteInterval
);
211 implStartBefore(aNewTime
);
214 // -----------------------------------------------------------------------------