2 *******************************************************************************
3 * Copyright (C) 2001-2008, International Business Machines Corporation. *
4 * All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_SERVICE
15 #undef SERVICE_REFCOUNT
17 // in case we use the refcount stuff
22 ******************************************************************
25 const UChar
ICUServiceKey::PREFIX_DELIMITER
= 0x002F; /* '/' */
27 ICUServiceKey::ICUServiceKey(const UnicodeString
& id
)
31 ICUServiceKey::~ICUServiceKey()
36 ICUServiceKey::getID() const
42 ICUServiceKey::canonicalID(UnicodeString
& result
) const
44 return result
.append(_id
);
48 ICUServiceKey::currentID(UnicodeString
& result
) const
50 return canonicalID(result
);
54 ICUServiceKey::currentDescriptor(UnicodeString
& result
) const
57 result
.append(PREFIX_DELIMITER
);
58 return currentID(result
);
62 ICUServiceKey::fallback()
68 ICUServiceKey::isFallbackOf(const UnicodeString
& id
) const
74 ICUServiceKey::prefix(UnicodeString
& result
) const
80 ICUServiceKey::parsePrefix(UnicodeString
& result
)
82 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
91 ICUServiceKey::parseSuffix(UnicodeString
& result
)
93 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
95 result
.remove(0, n
+1);
102 ICUServiceKey::debug(UnicodeString
& result
) const
105 result
.append(" id: ");
111 ICUServiceKey::debugClass(UnicodeString
& result
) const
113 return result
.append("ICUServiceKey");
117 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey
)
120 ******************************************************************
123 SimpleFactory::SimpleFactory(UObject
* instanceToAdopt
, const UnicodeString
& id
, UBool visible
)
124 : _instance(instanceToAdopt
), _id(id
), _visible(visible
)
128 SimpleFactory::~SimpleFactory()
134 SimpleFactory::create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const
136 if (U_SUCCESS(status
)) {
138 if (_id
== key
.currentID(temp
)) {
139 return service
->cloneInstance(_instance
);
146 SimpleFactory::updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const
149 result
.put(_id
, (void*)this, status
); // cast away const
156 SimpleFactory::getDisplayName(const UnicodeString
& id
, const Locale
& /* locale */, UnicodeString
& result
) const
158 if (_visible
&& _id
== id
) {
168 SimpleFactory::debug(UnicodeString
& toAppendTo
) const
170 debugClass(toAppendTo
);
171 toAppendTo
.append(" id: ");
172 toAppendTo
.append(_id
);
173 toAppendTo
.append(", visible: ");
174 toAppendTo
.append(_visible
? "T" : "F");
179 SimpleFactory::debugClass(UnicodeString
& toAppendTo
) const
181 return toAppendTo
.append("SimpleFactory");
185 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory
)
188 ******************************************************************
191 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener
)
194 ******************************************************************
197 // Record the actual id for this service in the cache, so we can return it
198 // even if we succeed later with a different id.
199 class CacheEntry
: public UMemory
{
204 UnicodeString actualDescriptor
;
208 * Releases a reference to the shared resource.
214 CacheEntry(const UnicodeString
& _actualDescriptor
, UObject
* _service
)
215 : refcount(1), actualDescriptor(_actualDescriptor
), service(_service
) {
219 * Instantiation creates an initial reference, so don't call this
220 * unless you're creating a new pointer to this. Management of
221 * that pointer will have to know how to deal with refcounts.
222 * Return true if the resource has not already been released.
230 * Destructions removes a reference, so don't call this unless
231 * you're removing pointer to this somewhere. Management of that
232 * pointer will have to know how to deal with refcounts. Once
233 * the refcount drops to zero, the resource is released. Return
234 * false if the resouce has been released.
236 CacheEntry
* unref() {
237 if ((--refcount
) == 0) {
245 * Return TRUE if there is at least one reference to this and the
246 * resource has not been released.
248 UBool
isShared() const {
253 // UObjectDeleter for serviceCache
255 static void U_CALLCONV
256 cacheDeleter(void* obj
) {
257 U_NAMESPACE_USE ((CacheEntry
*)obj
)->unref();
261 * Deleter for UObjects
263 static void U_CALLCONV
264 deleteUObject(void *obj
) {
265 U_NAMESPACE_USE
delete (UObject
*) obj
;
270 ******************************************************************
273 class DNCache
: public UMemory
{
278 DNCache(const Locale
& _locale
)
279 : cache(), locale(_locale
)
281 // cache.setKeyDeleter(uhash_deleteUnicodeString);
287 ******************************************************************
291 StringPair::create(const UnicodeString
& displayName
,
292 const UnicodeString
& id
,
295 if (U_SUCCESS(status
)) {
296 StringPair
* sp
= new StringPair(displayName
, id
);
297 if (sp
== NULL
|| sp
->isBogus()) {
298 status
= U_MEMORY_ALLOCATION_ERROR
;
308 StringPair::isBogus() const {
309 return displayName
.isBogus() || id
.isBogus();
312 StringPair::StringPair(const UnicodeString
& _displayName
,
313 const UnicodeString
& _id
)
314 : displayName(_displayName
)
320 static void U_CALLCONV
321 userv_deleteStringPair(void *obj
) {
322 U_NAMESPACE_USE
delete (StringPair
*) obj
;
327 ******************************************************************
330 ICUService::ICUService()
342 ICUService::ICUService(const UnicodeString
& newName
)
354 ICUService::~ICUService()
366 ICUService::get(const UnicodeString
& descriptor
, UErrorCode
& status
) const
368 return get(descriptor
, NULL
, status
);
372 ICUService::get(const UnicodeString
& descriptor
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
374 UObject
* result
= NULL
;
375 ICUServiceKey
* key
= createKey(&descriptor
, status
);
377 result
= getKey(*key
, actualReturn
, status
);
384 ICUService::getKey(ICUServiceKey
& key
, UErrorCode
& status
) const
386 return getKey(key
, NULL
, status
);
389 // this is a vector that subclasses of ICUService can override to further customize the result object
390 // before returning it. All other public get functions should call this one.
393 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
395 return getKey(key
, actualReturn
, NULL
, status
);
398 // make it possible to call reentrantly on systems that don't have reentrant mutexes.
399 // we can use this simple approach since we know the situation where we're calling
400 // reentrantly even without knowing the thread.
401 class XMutex
: public UMemory
{
403 inline XMutex(UMTX
*mutex
, UBool reentering
)
405 , fActive(!reentering
)
407 if (fActive
) umtx_lock(fMutex
);
410 if (fActive
) umtx_unlock(fMutex
);
418 struct UVectorDeleter
{
420 UVectorDeleter() : _obj(NULL
) {}
421 ~UVectorDeleter() { delete _obj
; }
424 // called only by factories, treat as private
426 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, const ICUServiceFactory
* factory
, UErrorCode
& status
) const
428 if (U_FAILURE(status
)) {
433 return handleDefault(key
, actualReturn
, status
);
436 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
438 CacheEntry
* result
= NULL
;
440 // The factory list can't be modified until we're done,
441 // otherwise we might update the cache with an invalid result.
442 // The cache has to stay in synch with the factory list.
443 // ICU doesn't have monitors so we can't use rw locks, so
444 // we single-thread everything using this service, for now.
446 // if factory is not null, we're calling from within the mutex,
447 // and since some unix machines don't have reentrant mutexes we
448 // need to make sure not to try to lock it again.
449 XMutex
mutex(&ncthis
->lock
, factory
!= NULL
);
451 if (serviceCache
== NULL
) {
452 ncthis
->serviceCache
= new Hashtable(status
);
453 if (ncthis
->serviceCache
== NULL
) {
456 if (U_FAILURE(status
)) {
460 serviceCache
->setValueDeleter(cacheDeleter
);
463 UnicodeString currentDescriptor
;
464 UVectorDeleter cacheDescriptorList
;
465 UBool putInCache
= FALSE
;
467 int32_t startIndex
= 0;
468 int32_t limit
= factories
->size();
469 UBool cacheResult
= TRUE
;
471 if (factory
!= NULL
) {
472 for (int32_t i
= 0; i
< limit
; ++i
) {
473 if (factory
== (const ICUServiceFactory
*)factories
->elementAt(i
)) {
478 if (startIndex
== 0) {
479 // throw new InternalError("Factory " + factory + "not registered with service: " + this);
480 status
= U_ILLEGAL_ARGUMENT_ERROR
;
487 currentDescriptor
.remove();
488 key
.currentDescriptor(currentDescriptor
);
489 result
= (CacheEntry
*)serviceCache
->get(currentDescriptor
);
490 if (result
!= NULL
) {
494 // first test of cache failed, so we'll have to update
495 // the cache if we eventually succeed-- that is, if we're
496 // going to update the cache at all.
499 int32_t index
= startIndex
;
500 while (index
< limit
) {
501 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(index
++);
502 UObject
* service
= f
->create(key
, this, status
);
503 if (U_FAILURE(status
)) {
507 if (service
!= NULL
) {
508 result
= new CacheEntry(currentDescriptor
, service
);
509 if (result
== NULL
) {
511 status
= U_MEMORY_ALLOCATION_ERROR
;
519 // prepare to load the cache with all additional ids that
520 // will resolve to result, assuming we'll succeed. We
521 // don't want to keep querying on an id that's going to
522 // fallback to the one that succeeded, we want to hit the
523 // cache the first time next goaround.
524 if (cacheDescriptorList
._obj
== NULL
) {
525 cacheDescriptorList
._obj
= new UVector(uhash_deleteUnicodeString
, NULL
, 5, status
);
526 if (U_FAILURE(status
)) {
530 UnicodeString
* idToCache
= new UnicodeString(currentDescriptor
);
531 if (idToCache
== NULL
|| idToCache
->isBogus()) {
532 status
= U_MEMORY_ALLOCATION_ERROR
;
536 cacheDescriptorList
._obj
->addElement(idToCache
, status
);
537 if (U_FAILURE(status
)) {
540 } while (key
.fallback());
543 if (result
!= NULL
) {
544 if (putInCache
&& cacheResult
) {
545 serviceCache
->put(result
->actualDescriptor
, result
, status
);
546 if (U_FAILURE(status
)) {
551 if (cacheDescriptorList
._obj
!= NULL
) {
552 for (int32_t i
= cacheDescriptorList
._obj
->size(); --i
>= 0;) {
553 UnicodeString
* desc
= (UnicodeString
*)cacheDescriptorList
._obj
->elementAt(i
);
554 serviceCache
->put(*desc
, result
, status
);
555 if (U_FAILURE(status
)) {
561 cacheDescriptorList
._obj
->removeElementAt(i
);
566 if (actualReturn
!= NULL
) {
568 if (result
->actualDescriptor
.indexOf((UChar
)0x2f) == 0) { // U+002f=slash (/)
569 actualReturn
->remove();
570 actualReturn
->append(result
->actualDescriptor
,
572 result
->actualDescriptor
.length() - 1);
574 *actualReturn
= result
->actualDescriptor
;
577 if (actualReturn
->isBogus()) {
578 status
= U_MEMORY_ALLOCATION_ERROR
;
584 UObject
* service
= cloneInstance(result
->service
);
585 if (putInCache
&& !cacheResult
) {
592 return handleDefault(key
, actualReturn
, status
);
596 ICUService::handleDefault(const ICUServiceKey
& /* key */, UnicodeString
* /* actualIDReturn */, UErrorCode
& /* status */) const
602 ICUService::getVisibleIDs(UVector
& result
, UErrorCode
& status
) const {
603 return getVisibleIDs(result
, NULL
, status
);
607 ICUService::getVisibleIDs(UVector
& result
, const UnicodeString
* matchID
, UErrorCode
& status
) const
609 result
.removeAllElements();
611 if (U_FAILURE(status
)) {
615 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
617 Mutex
mutex(&ncthis
->lock
);
618 const Hashtable
* map
= getVisibleIDMap(status
);
620 ICUServiceKey
* fallbackKey
= createKey(matchID
, status
);
622 for (int32_t pos
= -1;;) {
623 const UHashElement
* e
= map
->nextElement(pos
);
628 const UnicodeString
* id
= (const UnicodeString
*)e
->key
.pointer
;
629 if (fallbackKey
!= NULL
) {
630 if (!fallbackKey
->isFallbackOf(*id
)) {
635 UnicodeString
* idClone
= new UnicodeString(*id
);
636 if (idClone
== NULL
|| idClone
->isBogus()) {
638 status
= U_MEMORY_ALLOCATION_ERROR
;
641 result
.addElement(idClone
, status
);
642 if (U_FAILURE(status
)) {
650 if (U_FAILURE(status
)) {
651 result
.removeAllElements();
657 ICUService::getVisibleIDMap(UErrorCode
& status
) const {
658 if (U_FAILURE(status
)) return NULL
;
660 // must only be called when lock is already held
662 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
663 if (idCache
== NULL
) {
664 ncthis
->idCache
= new Hashtable(status
);
665 if (idCache
== NULL
) {
666 status
= U_MEMORY_ALLOCATION_ERROR
;
667 } else if (factories
!= NULL
) {
668 for (int32_t pos
= factories
->size(); --pos
>= 0;) {
669 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(pos
);
670 f
->updateVisibleIDs(*idCache
, status
);
672 if (U_FAILURE(status
)) {
674 ncthis
->idCache
= NULL
;
684 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
) const
686 return getDisplayName(id
, result
, Locale::getDefault());
690 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
, const Locale
& locale
) const
693 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
694 UErrorCode status
= U_ZERO_ERROR
;
695 Mutex
mutex(&ncthis
->lock
);
696 const Hashtable
* map
= getVisibleIDMap(status
);
698 ICUServiceFactory
* f
= (ICUServiceFactory
*)map
->get(id
);
700 f
->getDisplayName(id
, locale
, result
);
705 UErrorCode status
= U_ZERO_ERROR
;
706 ICUServiceKey
* fallbackKey
= createKey(&id
, status
);
707 while (fallbackKey
->fallback()) {
709 fallbackKey
->currentID(us
);
710 f
= (ICUServiceFactory
*)map
->get(us
);
712 f
->getDisplayName(id
, locale
, result
);
725 ICUService::getDisplayNames(UVector
& result
, UErrorCode
& status
) const
727 return getDisplayNames(result
, Locale::getDefault(), NULL
, status
);
732 ICUService::getDisplayNames(UVector
& result
, const Locale
& locale
, UErrorCode
& status
) const
734 return getDisplayNames(result
, locale
, NULL
, status
);
738 ICUService::getDisplayNames(UVector
& result
,
739 const Locale
& locale
,
740 const UnicodeString
* matchID
,
741 UErrorCode
& status
) const
743 result
.removeAllElements();
744 result
.setDeleter(userv_deleteStringPair
);
745 if (U_SUCCESS(status
)) {
746 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
747 Mutex
mutex(&ncthis
->lock
);
749 if (dnCache
!= NULL
&& dnCache
->locale
!= locale
) {
751 ncthis
->dnCache
= NULL
;
754 if (dnCache
== NULL
) {
755 const Hashtable
* m
= getVisibleIDMap(status
);
757 ncthis
->dnCache
= new DNCache(locale
);
758 if (dnCache
== NULL
) {
759 status
= U_MEMORY_ALLOCATION_ERROR
;
764 const UHashElement
* entry
= NULL
;
765 while ((entry
= m
->nextElement(pos
)) != NULL
) {
766 const UnicodeString
* id
= (const UnicodeString
*)entry
->key
.pointer
;
767 ICUServiceFactory
* f
= (ICUServiceFactory
*)entry
->value
.pointer
;
769 f
->getDisplayName(*id
, locale
, dname
);
770 if (dname
.isBogus()) {
771 status
= U_MEMORY_ALLOCATION_ERROR
;
773 dnCache
->cache
.put(dname
, (void*)id
, status
); // share pointer with visibleIDMap
774 if (U_SUCCESS(status
)) {
779 ncthis
->dnCache
= NULL
;
786 ICUServiceKey
* matchKey
= createKey(matchID
, status
);
787 /* To ensure that all elements in the hashtable are iterated, set pos to -1.
788 * nextElement(pos) will skip the position at pos and begin the iteration
789 * at the next position, which in this case will be 0.
792 const UHashElement
*entry
= NULL
;
793 while ((entry
= dnCache
->cache
.nextElement(pos
)) != NULL
) {
794 const UnicodeString
* id
= (const UnicodeString
*)entry
->value
.pointer
;
795 if (matchKey
!= NULL
&& !matchKey
->isFallbackOf(*id
)) {
798 const UnicodeString
* dn
= (const UnicodeString
*)entry
->key
.pointer
;
799 StringPair
* sp
= StringPair::create(*id
, *dn
, status
);
800 result
.addElement(sp
, status
);
801 if (U_FAILURE(status
)) {
802 result
.removeAllElements();
812 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UErrorCode
& status
)
814 return registerInstance(objToAdopt
, id
, TRUE
, status
);
818 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
820 ICUServiceKey
* key
= createKey(&id
, status
);
822 UnicodeString canonicalID
;
823 key
->canonicalID(canonicalID
);
826 ICUServiceFactory
* f
= createSimpleFactory(objToAdopt
, canonicalID
, visible
, status
);
828 return registerFactory(f
, status
);
836 ICUService::createSimpleFactory(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
838 if (U_SUCCESS(status
)) {
839 if ((objToAdopt
!= NULL
) && (!id
.isBogus())) {
840 return new SimpleFactory(objToAdopt
, id
, visible
);
842 status
= U_ILLEGAL_ARGUMENT_ERROR
;
848 ICUService::registerFactory(ICUServiceFactory
* factoryToAdopt
, UErrorCode
& status
)
850 if (U_SUCCESS(status
) && factoryToAdopt
!= NULL
) {
853 if (factories
== NULL
) {
854 factories
= new UVector(deleteUObject
, NULL
, status
);
855 if (U_FAILURE(status
)) {
860 factories
->insertElementAt(factoryToAdopt
, 0, status
);
861 if (U_SUCCESS(status
)) {
864 delete factoryToAdopt
;
865 factoryToAdopt
= NULL
;
869 if (factoryToAdopt
!= NULL
) {
873 return (URegistryKey
)factoryToAdopt
;
877 ICUService::unregister(URegistryKey rkey
, UErrorCode
& status
)
879 ICUServiceFactory
*factory
= (ICUServiceFactory
*)rkey
;
880 UBool result
= FALSE
;
881 if (factory
!= NULL
&& factories
!= NULL
) {
884 if (factories
->removeElement(factory
)) {
888 status
= U_ILLEGAL_ARGUMENT_ERROR
;
903 reInitializeFactories();
910 ICUService::reInitializeFactories()
912 if (factories
!= NULL
) {
913 factories
->removeAllElements();
918 ICUService::isDefault() const
920 return countFactories() == 0;
924 ICUService::createKey(const UnicodeString
* id
, UErrorCode
& status
) const
926 return (U_FAILURE(status
) || id
== NULL
) ? NULL
: new ICUServiceKey(*id
);
930 ICUService::clearCaches()
932 // callers synchronize before use
938 delete serviceCache
; serviceCache
= NULL
;
942 ICUService::clearServiceCache()
944 // callers synchronize before use
945 delete serviceCache
; serviceCache
= NULL
;
949 ICUService::acceptsListener(const EventListener
& l
) const
951 return l
.getDynamicClassID() == ServiceListener::getStaticClassID();
955 ICUService::notifyListener(EventListener
& l
) const
957 ((ServiceListener
&)l
).serviceChanged(*this);
961 ICUService::getName(UnicodeString
& result
) const
963 return result
.append(name
);
967 ICUService::countFactories() const
969 return factories
== NULL
? 0 : factories
->size();
973 ICUService::getTimestamp() const
980 /* UCONFIG_NO_SERVICE */