1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26
27 #include "basecontainer.hxx"
28 #include "constant.hxx"
29
30 //_______________________________________________
31 // includes
32 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 #include <com/sun/star/uno/Type.h>
34 #include <comphelper/enumhelper.hxx>
35 #include <osl/diagnose.h>
36
37 //_______________________________________________
38 // namespace
39
40 #define LOAD_IMPLICIT
41
42 namespace css = ::com::sun::star;
43
44 namespace filter{
45 namespace config{
46
47 //_______________________________________________
48 // definitions
49
50 ::salhelper::SingletonRef< FilterCache >* BaseContainer::m_pPerformanceOptimizer = 0;
51
52 /*-----------------------------------------------
53 03.03.2004 11:37
54 -----------------------------------------------*/
BaseContainer()55 BaseContainer::BaseContainer()
56 : BaseLock ( )
57 , m_rCache ( )
58 , m_pFlushCache(NULL )
59 , m_lListener (m_aLock)
60 {
61 m_rCache->load(FilterCache::E_CONTAINS_STANDARD);
62
63 // GLOBAL SAFE (!) -> -----------------------
64 // TODO use rtl pattern to create it really threadsafe!
65 ::osl::ResettableMutexGuard aGlobalLock(::osl::Mutex::getGlobalMutex());
66 if (!m_pPerformanceOptimizer)
67 m_pPerformanceOptimizer = new ::salhelper::SingletonRef< FilterCache >();
68 aGlobalLock.clear();
69 // <- GLOBAL SAFE (!) -----------------------
70 }
71
72 /*-----------------------------------------------
73 02.07.2003 10:16
74 -----------------------------------------------*/
~BaseContainer()75 BaseContainer::~BaseContainer()
76 {
77 }
78
79 /*-----------------------------------------------
80 03.07.2003 11:16
81 -----------------------------------------------*/
init(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR,const::rtl::OUString & sImplementationName,const css::uno::Sequence<::rtl::OUString> & lServiceNames,FilterCache::EItemType eType)82 void BaseContainer::init(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
83 const ::rtl::OUString& sImplementationName,
84 const css::uno::Sequence< ::rtl::OUString >& lServiceNames ,
85 FilterCache::EItemType eType )
86 {
87 // SAFE ->
88 ::osl::ResettableMutexGuard aLock(m_aLock);
89
90 m_sImplementationName = sImplementationName;
91 m_lServiceNames = lServiceNames ;
92 m_xSMGR = xSMGR ;
93 m_eType = eType ;
94 m_xRefreshBroadcaster = css::uno::Reference< css::util::XRefreshable >(
95 xSMGR->createInstance(SERVICE_FILTERCONFIGREFRESH),
96 css::uno::UNO_QUERY);
97 // <- SAFE
98 }
99
100 /*-----------------------------------------------
101 28.10.2003 09:04
102 -----------------------------------------------*/
impl_loadOnDemand()103 void BaseContainer::impl_loadOnDemand()
104 {
105 #ifdef LOAD_IMPLICIT
106 // SAFE ->
107 ::osl::ResettableMutexGuard aLock(m_aLock);
108
109 // A generic container needs all items of a set of our cache!
110 // Of course it can block for a while, till the cache is really filled.
111 // Note: don't load all sets supported by the cache here!
112
113 FilterCache::EFillState eRequiredState = FilterCache::E_CONTAINS_NOTHING;
114 switch(m_eType)
115 {
116 case FilterCache::E_TYPE :
117 eRequiredState = FilterCache::E_CONTAINS_TYPES;
118 break;
119
120 case FilterCache::E_FILTER :
121 eRequiredState = FilterCache::E_CONTAINS_FILTERS;
122 break;
123
124 case FilterCache::E_DETECTSERVICE :
125 eRequiredState = FilterCache::E_CONTAINS_DETECTSERVICES;
126 break;
127
128 case FilterCache::E_FRAMELOADER :
129 eRequiredState = FilterCache::E_CONTAINS_FRAMELOADERS;
130 break;
131
132 case FilterCache::E_CONTENTHANDLER :
133 eRequiredState = FilterCache::E_CONTAINS_CONTENTHANDLERS;
134 break;
135 }
136
137 m_rCache->load(eRequiredState);
138 // <- SAFE
139 #endif
140 }
141
142 /*-----------------------------------------------
143 03.03.2004 12:18
144 -----------------------------------------------*/
impl_initFlushMode()145 void BaseContainer::impl_initFlushMode()
146 {
147 // SAFE ->
148 ::osl::ResettableMutexGuard aLock(m_aLock);
149 if (!m_pFlushCache)
150 m_pFlushCache = m_rCache->clone();
151 if (!m_pFlushCache)
152 throw css::uno::RuntimeException(
153 ::rtl::OUString::createFromAscii("Can't create write copy of internal used cache on demand."),
154 dynamic_cast< css::container::XNameAccess* >(this));
155 // <- SAFE
156 }
157
158 /*-----------------------------------------------
159 03.03.2004 12:11
160 -----------------------------------------------*/
impl_getWorkingCache() const161 FilterCache* BaseContainer::impl_getWorkingCache() const
162 {
163 // SAFE ->
164 ::osl::ResettableMutexGuard aLock(m_aLock);
165 if (m_pFlushCache)
166 return m_pFlushCache;
167 else
168 return &(*m_rCache);
169 // <- SAFE
170 }
171
172 /*-----------------------------------------------
173 03.07.2003 11:12
174 -----------------------------------------------*/
getImplementationName()175 ::rtl::OUString SAL_CALL BaseContainer::getImplementationName()
176 {
177 // SAFE ->
178 ::osl::ResettableMutexGuard aLock(m_aLock);
179 return m_sImplementationName;
180 // <- SAFE
181 }
182
183 /*-----------------------------------------------
184 03.07.2003 11:14
185 -----------------------------------------------*/
supportsService(const::rtl::OUString & sServiceName)186 sal_Bool SAL_CALL BaseContainer::supportsService(const ::rtl::OUString& sServiceName)
187 {
188 // SAFE ->
189 ::osl::ResettableMutexGuard aLock(m_aLock);
190
191 sal_Int32 c = m_lServiceNames.getLength();
192 const ::rtl::OUString* pNames = m_lServiceNames.getConstArray();
193 for (sal_Int32 i=0; i<c; ++i)
194 {
195 if (pNames[i].equals(sServiceName))
196 return sal_True;
197 }
198 return sal_False;
199 // <- SAFE
200 }
201
202 /*-----------------------------------------------
203 03.07.2003 11:12
204 -----------------------------------------------*/
getSupportedServiceNames()205 css::uno::Sequence< ::rtl::OUString > SAL_CALL BaseContainer::getSupportedServiceNames()
206 {
207 // SAFE ->
208 ::osl::ResettableMutexGuard aLock(m_aLock);
209 return m_lServiceNames;
210 // <- SAFE
211 }
212
213 /*-----------------------------------------------
214 20.10.2003 11:39
215 -----------------------------------------------*/
insertByName(const::rtl::OUString & sItem,const css::uno::Any & aValue)216 void SAL_CALL BaseContainer::insertByName(const ::rtl::OUString& sItem ,
217 const css::uno::Any& aValue)
218 {
219 if (!sItem.getLength())
220 throw css::lang::IllegalArgumentException(
221 ::rtl::OUString::createFromAscii("empty value not allowed as item name."),
222 static_cast< css::container::XNameContainer* >(this),
223 1);
224
225 CacheItem aItem;
226 try
227 {
228 aItem << aValue;
229 }
230 catch(const css::uno::Exception& ex)
231 {
232 throw css::lang::IllegalArgumentException(ex.Message, static_cast< css::container::XNameContainer* >(this), 2);
233 }
234
235 impl_loadOnDemand();
236
237 // SAFE -> ----------------------------------
238 ::osl::ResettableMutexGuard aLock(m_aLock);
239
240 // create write copy of used cache on demand ...
241 impl_initFlushMode();
242
243 FilterCache* pCache = impl_getWorkingCache();
244 if (pCache->hasItem(m_eType, sItem))
245 throw css::container::ElementExistException(::rtl::OUString(), static_cast< css::container::XNameContainer* >(this));
246 pCache->setItem(m_eType, sItem, aItem);
247
248 aLock.clear();
249 // <- SAFE ----------------------------------
250 }
251
252 /*-----------------------------------------------
253 03.03.2004 11:40
254 -----------------------------------------------*/
removeByName(const::rtl::OUString & sItem)255 void SAL_CALL BaseContainer::removeByName(const ::rtl::OUString& sItem)
256 {
257 impl_loadOnDemand();
258
259 // SAFE -> ----------------------------------
260 ::osl::ResettableMutexGuard aLock(m_aLock);
261
262 // create write copy of used cache on demand ...
263 impl_initFlushMode();
264
265 FilterCache* pCache = impl_getWorkingCache();
266 pCache->removeItem(m_eType, sItem); // throw exceptions automatically
267
268 aLock.clear();
269 // <- SAFE ----------------------------------
270 }
271
272 /*-----------------------------------------------
273 03.03.2004 11:41
274 -----------------------------------------------*/
replaceByName(const::rtl::OUString & sItem,const css::uno::Any & aValue)275 void SAL_CALL BaseContainer::replaceByName(const ::rtl::OUString& sItem ,
276 const css::uno::Any& aValue)
277 {
278 if (!sItem.getLength())
279 throw css::lang::IllegalArgumentException(
280 ::rtl::OUString::createFromAscii("empty value not allowed as item name."),
281 static_cast< css::container::XNameContainer* >(this),
282 1);
283
284 CacheItem aItem;
285 try
286 {
287 aItem << aValue;
288 }
289 catch(const css::uno::Exception& ex)
290 {
291 throw css::lang::IllegalArgumentException(ex.Message, static_cast< css::container::XNameContainer* >(this), 2);
292 }
293
294 impl_loadOnDemand();
295
296 // SAFE -> ----------------------------------
297 ::osl::ResettableMutexGuard aLock(m_aLock);
298
299 // create write copy of used cache on demand ...
300 impl_initFlushMode();
301
302 FilterCache* pCache = impl_getWorkingCache();
303 if (!pCache->hasItem(m_eType, sItem))
304 throw css::container::NoSuchElementException(::rtl::OUString(), static_cast< css::container::XNameContainer* >(this));
305 pCache->setItem(m_eType, sItem, aItem);
306
307 aLock.clear();
308 // <- SAFE ----------------------------------
309 }
310
311 /*-----------------------------------------------
312 03.03.2004 11:44
313 -----------------------------------------------*/
getByName(const::rtl::OUString & sItem)314 css::uno::Any SAL_CALL BaseContainer::getByName(const ::rtl::OUString& sItem)
315 {
316 if (!sItem.getLength())
317 throw css::container::NoSuchElementException(
318 ::rtl::OUString::createFromAscii("An empty item can't be part of this cache!"),
319 css::uno::Reference< css::uno::XInterface >(static_cast< css::container::XNameAccess* >(this), css::uno::UNO_QUERY));
320
321 css::uno::Any aValue;
322
323 impl_loadOnDemand();
324
325 // SAFE ->
326 ::osl::ResettableMutexGuard aLock(m_aLock);
327
328 CacheItem aItem;
329 try
330 {
331 FilterCache* pCache = impl_getWorkingCache();
332 aItem = pCache->getItem(m_eType, sItem);
333 pCache->addStatePropsToItem(m_eType, sItem, aItem); // add implicit props "Finalized"/"Mandatory"
334 }
335 catch(const css::container::NoSuchElementException& exNotExist)
336 {
337 throw exNotExist;
338 }
339 catch(const css::uno::Exception&)
340 {
341 // TODO invalid cache!? How should it be handled right?
342 aItem.clear();
343 }
344
345 aValue <<= aItem.getAsPackedPropertyValueList();
346 // <- SAFE
347
348 return aValue;
349 }
350
351 /*-----------------------------------------------
352 03.03.2004 11:46
353 -----------------------------------------------*/
getElementNames()354 css::uno::Sequence< ::rtl::OUString > SAL_CALL BaseContainer::getElementNames()
355 {
356 css::uno::Sequence< ::rtl::OUString > lNames;
357
358 impl_loadOnDemand();
359
360 // SAFE ->
361 ::osl::ResettableMutexGuard aLock(m_aLock);
362
363 try
364 {
365 FilterCache* pCache = impl_getWorkingCache();
366 OUStringList lKeys = pCache->getItemNames(m_eType);
367 lKeys >> lNames;
368 }
369 catch(const css::uno::Exception&)
370 {
371 // invalid cache!?
372 lNames.realloc(0);
373 }
374
375 // <- SAFE
376
377 return lNames;
378 }
379
380 /*-----------------------------------------------
381 03.03.2004 11:47
382 -----------------------------------------------*/
hasByName(const::rtl::OUString & sItem)383 sal_Bool SAL_CALL BaseContainer::hasByName(const ::rtl::OUString& sItem)
384 {
385 sal_Bool bHasOne = sal_False;
386
387 impl_loadOnDemand();
388
389 // SAFE ->
390 ::osl::ResettableMutexGuard aLock(m_aLock);
391
392 try
393 {
394 FilterCache* pCache = impl_getWorkingCache();
395 bHasOne = pCache->hasItem(m_eType, sItem);
396 }
397 catch(const css::uno::Exception&)
398 {
399 // invalid cache!?
400 bHasOne = sal_False;
401 }
402
403 // <- SAFE
404
405 return bHasOne;
406 }
407
408 /*-----------------------------------------------
409 02.07.2003 10:18
410 -----------------------------------------------*/
getElementType()411 css::uno::Type SAL_CALL BaseContainer::getElementType()
412 {
413 // no lock necessary - because the type of our items
414 // is fix! no internal call or member needed ...
415 return ::getCppuType(static_cast< css::uno::Sequence< css::beans::PropertyValue >* >(NULL));
416 }
417
418 /*-----------------------------------------------
419 03.03.2004 11:48
420 -----------------------------------------------*/
hasElements()421 sal_Bool SAL_CALL BaseContainer::hasElements()
422 {
423 sal_Bool bHasSome = sal_False;
424
425 impl_loadOnDemand();
426
427 // SAFE ->
428 ::osl::ResettableMutexGuard aLock(m_aLock);
429
430 try
431 {
432 FilterCache* pCache = impl_getWorkingCache();
433 bHasSome = pCache->hasItems(m_eType);
434 }
435 catch(const css::uno::Exception&)
436 {
437 // invalid cache?!
438 bHasSome = sal_False;
439 }
440
441 // <- SAFE
442
443 return bHasSome;
444 }
445
446 /*-----------------------------------------------
447 15.07.2003 09:21
448 -----------------------------------------------*/
createSubSetEnumerationByQuery(const::rtl::OUString &)449 css::uno::Reference< css::container::XEnumeration > SAL_CALL BaseContainer::createSubSetEnumerationByQuery(const ::rtl::OUString& /* sQuery */ )
450 {
451 OSL_ENSURE(sal_False, "not pure virtual ... but not really implemented .-)");
452
453 ::comphelper::OEnumerationByName* pEnum = new ::comphelper::OEnumerationByName(this, css::uno::Sequence< ::rtl::OUString >());
454 return css::uno::Reference< css::container::XEnumeration >(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY);
455 }
456
457 /*-----------------------------------------------
458 15.07.2003 10:15
459 -----------------------------------------------*/
createSubSetEnumerationByProperties(const css::uno::Sequence<css::beans::NamedValue> & lProperties)460 css::uno::Reference< css::container::XEnumeration > SAL_CALL BaseContainer::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
461 {
462 css::uno::Reference< css::container::XEnumeration > xEnum;
463 OUStringList lKeys;
464
465 impl_loadOnDemand();
466
467 // SAFE ->
468 ::osl::ResettableMutexGuard aLock(m_aLock);
469
470 try
471 {
472 // convert the given properties first to our internal representation
473 CacheItem lProps;
474 lProps << lProperties;
475
476 // search the key names of all items, where its properties match
477 // the given ones in its minimum
478 FilterCache* pCache = impl_getWorkingCache();
479 lKeys = pCache->getMatchingItemsByProps(m_eType, lProps);
480 }
481 catch(const css::uno::Exception&)
482 {
483 // invalid cache, internal failure, wrong conversion ...!?
484 // doesn't matter
485 lKeys.clear();
486 }
487
488 // <- SAFE
489
490 // create a specialized enumeration helper, which
491 // provides the collected informations outside.
492 // It hold a reference to us ... and call our container interface directly.
493 // be aware of some direct callbacks if it will be created :-)
494
495 /* Note: Its not allowed to return NULL. Because an empty enumeration
496 transport the same information but make no trouble outside.
497 Further its easier to work directly with the return value
498 instead of checking of NULL returns! */
499
500 css::uno::Sequence< ::rtl::OUString > lSubSet;
501 lKeys >> lSubSet;
502 ::comphelper::OEnumerationByName* pEnum = new ::comphelper::OEnumerationByName(this, lSubSet);
503 return css::uno::Reference< css::container::XEnumeration >(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY);
504 }
505
506 /*-----------------------------------------------
507 07.03.2004 10:48
508 -----------------------------------------------*/
flush()509 void SAL_CALL BaseContainer::flush()
510 {
511 // SAFE ->
512 ::osl::ResettableMutexGuard aLock(m_aLock);
513
514 if (!m_pFlushCache)
515 throw css::lang::WrappedTargetRuntimeException(
516 ::rtl::OUString::createFromAscii("Can't guarantee cache consistency. Special flush container does not exists!"),
517 dynamic_cast< css::container::XNameAccess* >(this),
518 css::uno::Any());
519
520 try
521 {
522 m_pFlushCache->flush();
523 // Take over all changes into the global cache and
524 // forget the clone.
525 /* TODO
526 -think about me
527 If the global cache gets this information via listener,
528 we should remove this method!
529 */
530 m_rCache->takeOver(*m_pFlushCache);
531 }
532 catch(const css::uno::Exception& ex)
533 {
534 // Dont remove the clone. May be the outside
535 // user whish to repair it now and calls flush()
536 // later again ...
537
538 throw css::lang::WrappedTargetRuntimeException(
539 ::rtl::OUString::createFromAscii("Flush rejected by internal container."),
540 dynamic_cast< css::container::XNameAccess* >(this),
541 css::uno::makeAny(ex));
542 }
543
544 delete m_pFlushCache;
545 m_pFlushCache = NULL;
546
547 css::uno::Reference< css::util::XRefreshable > xRefreshBroadcaster = m_xRefreshBroadcaster;
548
549 aLock.clear();
550 // <- SAFE
551
552 if (xRefreshBroadcaster.is())
553 xRefreshBroadcaster->refresh();
554
555 // notify listener outside the lock!
556 // The used listener helper lives if we live
557 // and is threadsafe by itself.
558 // Further its not a good idea to hold the own lock
559 // if an outside object is called :-)
560 css::lang::EventObject aSource (static_cast< css::util::XFlushable* >(this));
561 ::cppu::OInterfaceContainerHelper* pContainer = m_lListener.getContainer(::getCppuType(static_cast< css::uno::Reference< css::util::XFlushListener >* >(NULL)));
562 if (pContainer)
563 {
564 ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
565 while (pIterator.hasMoreElements())
566 {
567 try
568 {
569 // ... this pointer can be interesting to find out, where will be called as listener
570 // Dont optimize it to a direct iterator cast :-)
571 css::util::XFlushListener* pListener = (css::util::XFlushListener*)pIterator.next();
572 pListener->flushed(aSource);
573 }
574 catch(const css::uno::Exception&)
575 {
576 // ignore any "damaged" flush listener!
577 // May its remote reference is broken ...
578 pIterator.remove();
579 }
580 }
581 }
582 }
583
584 /*-----------------------------------------------
585 02.07.2003 12:16
586 -----------------------------------------------*/
addFlushListener(const css::uno::Reference<css::util::XFlushListener> & xListener)587 void SAL_CALL BaseContainer::addFlushListener(const css::uno::Reference< css::util::XFlushListener >& xListener)
588 {
589 // no locks necessary
590 // used helper lives if we live and is threadsafe by itself ...
591 m_lListener.addInterface(::getCppuType(static_cast< css::uno::Reference< css::util::XFlushListener >* >(NULL)),
592 xListener );
593 }
594
595 /*-----------------------------------------------
596 02.07.2003 12:18
597 -----------------------------------------------*/
removeFlushListener(const css::uno::Reference<css::util::XFlushListener> & xListener)598 void SAL_CALL BaseContainer::removeFlushListener(const css::uno::Reference< css::util::XFlushListener >& xListener)
599 {
600 // no locks necessary
601 // used helper lives if we live and is threadsafe by itself ...
602 m_lListener.removeInterface(::getCppuType(static_cast< css::uno::Reference< css::util::XFlushListener >* >(NULL)),
603 xListener );
604 }
605
606 } // namespace config
607 } // namespace filter
608