1 /**
2 *******************************************************************************
3 * Copyright (C) 2001-2007, International Business Machines Corporation. *
4 * All Rights Reserved. *
5 *******************************************************************************
6 */
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_SERVICE
11
12 #include "serv.h"
13 #include "umutex.h"
14
15 #undef SERVICE_REFCOUNT
16
17 // in case we use the refcount stuff
18
19 U_NAMESPACE_BEGIN
20
21 /*
22 ******************************************************************
23 */
24
25 const UChar ICUServiceKey::PREFIX_DELIMITER = 0x002F; /* '/' */
26
ICUServiceKey(const UnicodeString & id)27 ICUServiceKey::ICUServiceKey(const UnicodeString& id)
28 : _id(id) {
29 }
30
~ICUServiceKey()31 ICUServiceKey::~ICUServiceKey()
32 {
33 }
34
35 const UnicodeString&
getID() const36 ICUServiceKey::getID() const
37 {
38 return _id;
39 }
40
41 UnicodeString&
canonicalID(UnicodeString & result) const42 ICUServiceKey::canonicalID(UnicodeString& result) const
43 {
44 return result.append(_id);
45 }
46
47 UnicodeString&
currentID(UnicodeString & result) const48 ICUServiceKey::currentID(UnicodeString& result) const
49 {
50 return canonicalID(result);
51 }
52
53 UnicodeString&
currentDescriptor(UnicodeString & result) const54 ICUServiceKey::currentDescriptor(UnicodeString& result) const
55 {
56 prefix(result);
57 result.append(PREFIX_DELIMITER);
58 return currentID(result);
59 }
60
61 UBool
fallback()62 ICUServiceKey::fallback()
63 {
64 return FALSE;
65 }
66
67 UBool
isFallbackOf(const UnicodeString & id) const68 ICUServiceKey::isFallbackOf(const UnicodeString& id) const
69 {
70 return id == _id;
71 }
72
73 UnicodeString&
prefix(UnicodeString & result) const74 ICUServiceKey::prefix(UnicodeString& result) const
75 {
76 return result;
77 }
78
79 UnicodeString&
parsePrefix(UnicodeString & result)80 ICUServiceKey::parsePrefix(UnicodeString& result)
81 {
82 int32_t n = result.indexOf(PREFIX_DELIMITER);
83 if (n < 0) {
84 n = 0;
85 }
86 result.remove(n);
87 return result;
88 }
89
90 UnicodeString&
parseSuffix(UnicodeString & result)91 ICUServiceKey::parseSuffix(UnicodeString& result)
92 {
93 int32_t n = result.indexOf(PREFIX_DELIMITER);
94 if (n >= 0) {
95 result.remove(0, n+1);
96 }
97 return result;
98 }
99
100 #ifdef SERVICE_DEBUG
101 UnicodeString&
debug(UnicodeString & result) const102 ICUServiceKey::debug(UnicodeString& result) const
103 {
104 debugClass(result);
105 result.append(" id: ");
106 result.append(_id);
107 return result;
108 }
109
110 UnicodeString&
debugClass(UnicodeString & result) const111 ICUServiceKey::debugClass(UnicodeString& result) const
112 {
113 return result.append("ICUServiceKey");
114 }
115 #endif
116
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey)117 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey)
118
119 /*
120 ******************************************************************
121 */
122
123 SimpleFactory::SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible)
124 : _instance(instanceToAdopt), _id(id), _visible(visible)
125 {
126 }
127
~SimpleFactory()128 SimpleFactory::~SimpleFactory()
129 {
130 delete _instance;
131 }
132
133 UObject*
create(const ICUServiceKey & key,const ICUService * service,UErrorCode & status) const134 SimpleFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const
135 {
136 if (U_SUCCESS(status)) {
137 UnicodeString temp;
138 if (_id == key.currentID(temp)) {
139 return service->cloneInstance(_instance);
140 }
141 }
142 return NULL;
143 }
144
145 void
updateVisibleIDs(Hashtable & result,UErrorCode & status) const146 SimpleFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const
147 {
148 if (_visible) {
149 result.put(_id, (void*)this, status); // cast away const
150 } else {
151 result.remove(_id);
152 }
153 }
154
155 UnicodeString&
getDisplayName(const UnicodeString & id,const Locale &,UnicodeString & result) const156 SimpleFactory::getDisplayName(const UnicodeString& id, const Locale& /* locale */, UnicodeString& result) const
157 {
158 if (_visible && _id == id) {
159 result = _id;
160 } else {
161 result.setToBogus();
162 }
163 return result;
164 }
165
166 #ifdef SERVICE_DEBUG
167 UnicodeString&
debug(UnicodeString & toAppendTo) const168 SimpleFactory::debug(UnicodeString& toAppendTo) const
169 {
170 debugClass(toAppendTo);
171 toAppendTo.append(" id: ");
172 toAppendTo.append(_id);
173 toAppendTo.append(", visible: ");
174 toAppendTo.append(_visible ? "T" : "F");
175 return toAppendTo;
176 }
177
178 UnicodeString&
debugClass(UnicodeString & toAppendTo) const179 SimpleFactory::debugClass(UnicodeString& toAppendTo) const
180 {
181 return toAppendTo.append("SimpleFactory");
182 }
183 #endif
184
185 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory)
186
187 /*
188 ******************************************************************
189 */
190
191 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener)
192
193 /*
194 ******************************************************************
195 */
196
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 {
200 private:
201 int32_t refcount;
202
203 public:
204 UnicodeString actualDescriptor;
205 UObject* service;
206
207 /**
208 * Releases a reference to the shared resource.
209 */
~CacheEntry()210 ~CacheEntry() {
211 delete service;
212 }
213
CacheEntry(const UnicodeString & _actualDescriptor,UObject * _service)214 CacheEntry(const UnicodeString& _actualDescriptor, UObject* _service)
215 : refcount(1), actualDescriptor(_actualDescriptor), service(_service) {
216 }
217
218 /**
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.
223 */
ref()224 CacheEntry* ref() {
225 ++refcount;
226 return this;
227 }
228
229 /**
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.
235 */
unref()236 CacheEntry* unref() {
237 if ((--refcount) == 0) {
238 delete this;
239 return NULL;
240 }
241 return this;
242 }
243
244 /**
245 * Return TRUE if there is at least one reference to this and the
246 * resource has not been released.
247 */
isShared() const248 UBool isShared() const {
249 return refcount > 1;
250 }
251 };
252
253 // UObjectDeleter for serviceCache
254 U_CDECL_BEGIN
255 static void U_CALLCONV
cacheDeleter(void * obj)256 cacheDeleter(void* obj) {
257 U_NAMESPACE_USE ((CacheEntry*)obj)->unref();
258 }
259
260 /**
261 * Deleter for UObjects
262 */
263 static void U_CALLCONV
deleteUObject(void * obj)264 deleteUObject(void *obj) {
265 U_NAMESPACE_USE delete (UObject*) obj;
266 }
267 U_CDECL_END
268
269 /*
270 ******************************************************************
271 */
272
273 class DNCache : public UMemory {
274 public:
275 Hashtable cache;
276 const Locale locale;
277
DNCache(const Locale & _locale)278 DNCache(const Locale& _locale)
279 : cache(), locale(_locale)
280 {
281 // cache.setKeyDeleter(uhash_deleteUnicodeString);
282 }
283 };
284
285
286 /*
287 ******************************************************************
288 */
289
290 StringPair*
create(const UnicodeString & displayName,const UnicodeString & id,UErrorCode & status)291 StringPair::create(const UnicodeString& displayName,
292 const UnicodeString& id,
293 UErrorCode& status)
294 {
295 if (U_SUCCESS(status)) {
296 StringPair* sp = new StringPair(displayName, id);
297 if (sp == NULL || sp->isBogus()) {
298 status = U_MEMORY_ALLOCATION_ERROR;
299 delete sp;
300 return NULL;
301 }
302 return sp;
303 }
304 return NULL;
305 }
306
307 UBool
isBogus() const308 StringPair::isBogus() const {
309 return displayName.isBogus() || id.isBogus();
310 }
311
StringPair(const UnicodeString & _displayName,const UnicodeString & _id)312 StringPair::StringPair(const UnicodeString& _displayName,
313 const UnicodeString& _id)
314 : displayName(_displayName)
315 , id(_id)
316 {
317 }
318
319 U_CDECL_BEGIN
320 static void U_CALLCONV
userv_deleteStringPair(void * obj)321 userv_deleteStringPair(void *obj) {
322 U_NAMESPACE_USE delete (StringPair*) obj;
323 }
324 U_CDECL_END
325
326 /*
327 ******************************************************************
328 */
329
ICUService()330 ICUService::ICUService()
331 : name()
332 , lock(0)
333 , timestamp(0)
334 , factories(NULL)
335 , serviceCache(NULL)
336 , idCache(NULL)
337 , dnCache(NULL)
338 {
339 umtx_init(&lock);
340 }
341
ICUService(const UnicodeString & newName)342 ICUService::ICUService(const UnicodeString& newName)
343 : name(newName)
344 , lock(0)
345 , timestamp(0)
346 , factories(NULL)
347 , serviceCache(NULL)
348 , idCache(NULL)
349 , dnCache(NULL)
350 {
351 umtx_init(&lock);
352 }
353
~ICUService()354 ICUService::~ICUService()
355 {
356 {
357 Mutex mutex(&lock);
358 clearCaches();
359 delete factories;
360 factories = NULL;
361 }
362 umtx_destroy(&lock);
363 }
364
365 UObject*
get(const UnicodeString & descriptor,UErrorCode & status) const366 ICUService::get(const UnicodeString& descriptor, UErrorCode& status) const
367 {
368 return get(descriptor, NULL, status);
369 }
370
371 UObject*
get(const UnicodeString & descriptor,UnicodeString * actualReturn,UErrorCode & status) const372 ICUService::get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const
373 {
374 UObject* result = NULL;
375 ICUServiceKey* key = createKey(&descriptor, status);
376 if (key) {
377 result = getKey(*key, actualReturn, status);
378 delete key;
379 }
380 return result;
381 }
382
383 UObject*
getKey(ICUServiceKey & key,UErrorCode & status) const384 ICUService::getKey(ICUServiceKey& key, UErrorCode& status) const
385 {
386 return getKey(key, NULL, status);
387 }
388
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.
391
392 UObject*
getKey(ICUServiceKey & key,UnicodeString * actualReturn,UErrorCode & status) const393 ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const
394 {
395 return getKey(key, actualReturn, NULL, status);
396 }
397
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 {
402 public:
XMutex(UMTX * mutex,UBool reentering)403 inline XMutex(UMTX *mutex, UBool reentering)
404 : fMutex(mutex)
405 , fActive(!reentering)
406 {
407 if (fActive) umtx_lock(fMutex);
408 }
~XMutex()409 inline ~XMutex() {
410 if (fActive) umtx_unlock(fMutex);
411 }
412
413 private:
414 UMTX *fMutex;
415 UBool fActive;
416 };
417
418 struct UVectorDeleter {
419 UVector* _obj;
UVectorDeleterUVectorDeleter420 UVectorDeleter() : _obj(NULL) {}
~UVectorDeleterUVectorDeleter421 ~UVectorDeleter() { delete _obj; }
422 };
423
424 // called only by factories, treat as private
425 UObject*
getKey(ICUServiceKey & key,UnicodeString * actualReturn,const ICUServiceFactory * factory,UErrorCode & status) const426 ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const
427 {
428 if (U_FAILURE(status)) {
429 return NULL;
430 }
431
432 if (isDefault()) {
433 return handleDefault(key, actualReturn, status);
434 }
435
436 ICUService* ncthis = (ICUService*)this; // cast away semantic const
437
438 CacheEntry* result = NULL;
439 {
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.
445
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);
450
451 if (serviceCache == NULL) {
452 ncthis->serviceCache = new Hashtable(status);
453 if (U_FAILURE(status)) {
454 delete serviceCache;
455 return NULL;
456 }
457 serviceCache->setValueDeleter(cacheDeleter);
458 }
459
460 UnicodeString currentDescriptor;
461 UVectorDeleter cacheDescriptorList;
462 UBool putInCache = FALSE;
463
464 int32_t startIndex = 0;
465 int32_t limit = factories->size();
466 UBool cacheResult = TRUE;
467
468 if (factory != NULL) {
469 for (int32_t i = 0; i < limit; ++i) {
470 if (factory == (const ICUServiceFactory*)factories->elementAt(i)) {
471 startIndex = i + 1;
472 break;
473 }
474 }
475 if (startIndex == 0) {
476 // throw new InternalError("Factory " + factory + "not registered with service: " + this);
477 status = U_ILLEGAL_ARGUMENT_ERROR;
478 return NULL;
479 }
480 cacheResult = FALSE;
481 }
482
483 do {
484 currentDescriptor.remove();
485 key.currentDescriptor(currentDescriptor);
486 result = (CacheEntry*)serviceCache->get(currentDescriptor);
487 if (result != NULL) {
488 break;
489 }
490
491 // first test of cache failed, so we'll have to update
492 // the cache if we eventually succeed-- that is, if we're
493 // going to update the cache at all.
494 putInCache = TRUE;
495
496 int32_t index = startIndex;
497 while (index < limit) {
498 ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(index++);
499 UObject* service = f->create(key, this, status);
500 if (U_FAILURE(status)) {
501 delete service;
502 return NULL;
503 }
504 if (service != NULL) {
505 result = new CacheEntry(currentDescriptor, service);
506 if (result == NULL) {
507 delete service;
508 status = U_MEMORY_ALLOCATION_ERROR;
509 return NULL;
510 }
511
512 goto outerEnd;
513 }
514 }
515
516 // prepare to load the cache with all additional ids that
517 // will resolve to result, assuming we'll succeed. We
518 // don't want to keep querying on an id that's going to
519 // fallback to the one that succeeded, we want to hit the
520 // cache the first time next goaround.
521 if (cacheDescriptorList._obj == NULL) {
522 cacheDescriptorList._obj = new UVector(uhash_deleteUnicodeString, NULL, 5, status);
523 if (U_FAILURE(status)) {
524 return NULL;
525 }
526 }
527 UnicodeString* idToCache = new UnicodeString(currentDescriptor);
528 if (idToCache == NULL || idToCache->isBogus()) {
529 status = U_MEMORY_ALLOCATION_ERROR;
530 return NULL;
531 }
532
533 cacheDescriptorList._obj->addElement(idToCache, status);
534 if (U_FAILURE(status)) {
535 return NULL;
536 }
537 } while (key.fallback());
538 outerEnd:
539
540 if (result != NULL) {
541 if (putInCache && cacheResult) {
542 serviceCache->put(result->actualDescriptor, result, status);
543 if (U_FAILURE(status)) {
544 delete result;
545 return NULL;
546 }
547
548 if (cacheDescriptorList._obj != NULL) {
549 for (int32_t i = cacheDescriptorList._obj->size(); --i >= 0;) {
550 UnicodeString* desc = (UnicodeString*)cacheDescriptorList._obj->elementAt(i);
551 serviceCache->put(*desc, result, status);
552 if (U_FAILURE(status)) {
553 delete result;
554 return NULL;
555 }
556
557 result->ref();
558 cacheDescriptorList._obj->removeElementAt(i);
559 }
560 }
561 }
562
563 if (actualReturn != NULL) {
564 // strip null prefix
565 if (result->actualDescriptor.indexOf((UChar)0x2f) == 0) { // U+002f=slash (/)
566 actualReturn->remove();
567 actualReturn->append(result->actualDescriptor,
568 1,
569 result->actualDescriptor.length() - 1);
570 } else {
571 *actualReturn = result->actualDescriptor;
572 }
573
574 if (actualReturn->isBogus()) {
575 status = U_MEMORY_ALLOCATION_ERROR;
576 delete result;
577 return NULL;
578 }
579 }
580
581 UObject* service = cloneInstance(result->service);
582 if (putInCache && !cacheResult) {
583 delete result;
584 }
585 return service;
586 }
587 }
588
589 return handleDefault(key, actualReturn, status);
590 }
591
592 UObject*
handleDefault(const ICUServiceKey &,UnicodeString *,UErrorCode &) const593 ICUService::handleDefault(const ICUServiceKey& /* key */, UnicodeString* /* actualIDReturn */, UErrorCode& /* status */) const
594 {
595 return NULL;
596 }
597
598 UVector&
getVisibleIDs(UVector & result,UErrorCode & status) const599 ICUService::getVisibleIDs(UVector& result, UErrorCode& status) const {
600 return getVisibleIDs(result, NULL, status);
601 }
602
603 UVector&
getVisibleIDs(UVector & result,const UnicodeString * matchID,UErrorCode & status) const604 ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorCode& status) const
605 {
606 result.removeAllElements();
607
608 if (U_FAILURE(status)) {
609 return result;
610 }
611
612 ICUService * ncthis = (ICUService*)this; // cast away semantic const
613 {
614 Mutex mutex(&ncthis->lock);
615 const Hashtable* map = getVisibleIDMap(status);
616 if (map != NULL) {
617 ICUServiceKey* fallbackKey = createKey(matchID, status);
618
619 for (int32_t pos = -1;;) {
620 const UHashElement* e = map->nextElement(pos);
621 if (e == NULL) {
622 break;
623 }
624
625 const UnicodeString* id = (const UnicodeString*)e->key.pointer;
626 if (fallbackKey != NULL) {
627 if (!fallbackKey->isFallbackOf(*id)) {
628 continue;
629 }
630 }
631
632 UnicodeString* idClone = new UnicodeString(*id);
633 if (idClone == NULL || idClone->isBogus()) {
634 delete idClone;
635 status = U_MEMORY_ALLOCATION_ERROR;
636 break;
637 }
638 result.addElement(idClone, status);
639 if (U_FAILURE(status)) {
640 delete idClone;
641 break;
642 }
643 }
644 delete fallbackKey;
645 }
646 }
647 if (U_FAILURE(status)) {
648 result.removeAllElements();
649 }
650 return result;
651 }
652
653 const Hashtable*
getVisibleIDMap(UErrorCode & status) const654 ICUService::getVisibleIDMap(UErrorCode& status) const {
655 if (U_FAILURE(status)) return NULL;
656
657 // must only be called when lock is already held
658
659 ICUService* ncthis = (ICUService*)this; // cast away semantic const
660 if (idCache == NULL) {
661 ncthis->idCache = new Hashtable(status);
662 if (idCache == NULL) {
663 status = U_MEMORY_ALLOCATION_ERROR;
664 } else if (factories != NULL) {
665 for (int32_t pos = factories->size(); --pos >= 0;) {
666 ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(pos);
667 f->updateVisibleIDs(*idCache, status);
668 }
669 if (U_FAILURE(status)) {
670 delete idCache;
671 ncthis->idCache = NULL;
672 }
673 }
674 }
675
676 return idCache;
677 }
678
679
680 UnicodeString&
getDisplayName(const UnicodeString & id,UnicodeString & result) const681 ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result) const
682 {
683 return getDisplayName(id, result, Locale::getDefault());
684 }
685
686 UnicodeString&
getDisplayName(const UnicodeString & id,UnicodeString & result,const Locale & locale) const687 ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result, const Locale& locale) const
688 {
689 {
690 ICUService* ncthis = (ICUService*)this; // cast away semantic const
691 UErrorCode status = U_ZERO_ERROR;
692 Mutex mutex(&ncthis->lock);
693 const Hashtable* map = getVisibleIDMap(status);
694 if (map != NULL) {
695 ICUServiceFactory* f = (ICUServiceFactory*)map->get(id);
696 if (f != NULL) {
697 f->getDisplayName(id, locale, result);
698 return result;
699 }
700
701 // fallback
702 UErrorCode status = U_ZERO_ERROR;
703 ICUServiceKey* fallbackKey = createKey(&id, status);
704 while (fallbackKey->fallback()) {
705 UnicodeString us;
706 fallbackKey->currentID(us);
707 f = (ICUServiceFactory*)map->get(us);
708 if (f != NULL) {
709 f->getDisplayName(id, locale, result);
710 delete fallbackKey;
711 return result;
712 }
713 }
714 delete fallbackKey;
715 }
716 }
717 result.setToBogus();
718 return result;
719 }
720
721 UVector&
getDisplayNames(UVector & result,UErrorCode & status) const722 ICUService::getDisplayNames(UVector& result, UErrorCode& status) const
723 {
724 return getDisplayNames(result, Locale::getDefault(), NULL, status);
725 }
726
727
728 UVector&
getDisplayNames(UVector & result,const Locale & locale,UErrorCode & status) const729 ICUService::getDisplayNames(UVector& result, const Locale& locale, UErrorCode& status) const
730 {
731 return getDisplayNames(result, locale, NULL, status);
732 }
733
734 UVector&
getDisplayNames(UVector & result,const Locale & locale,const UnicodeString * matchID,UErrorCode & status) const735 ICUService::getDisplayNames(UVector& result,
736 const Locale& locale,
737 const UnicodeString* matchID,
738 UErrorCode& status) const
739 {
740 result.removeAllElements();
741 result.setDeleter(userv_deleteStringPair);
742 if (U_SUCCESS(status)) {
743 ICUService* ncthis = (ICUService*)this; // cast away semantic const
744 Mutex mutex(&ncthis->lock);
745
746 if (dnCache != NULL && dnCache->locale != locale) {
747 delete dnCache;
748 ncthis->dnCache = NULL;
749 }
750
751 if (dnCache == NULL) {
752 const Hashtable* m = getVisibleIDMap(status);
753 if (m != NULL) {
754 ncthis->dnCache = new DNCache(locale);
755 if (dnCache == NULL) {
756 status = U_MEMORY_ALLOCATION_ERROR;
757 return result;
758 }
759
760 int32_t pos = 0;
761 const UHashElement* entry = NULL;
762 while ((entry = m->nextElement(pos)) != NULL) {
763 const UnicodeString* id = (const UnicodeString*)entry->key.pointer;
764 ICUServiceFactory* f = (ICUServiceFactory*)entry->value.pointer;
765 UnicodeString dname;
766 f->getDisplayName(*id, locale, dname);
767 if (dname.isBogus()) {
768 status = U_MEMORY_ALLOCATION_ERROR;
769 } else {
770 dnCache->cache.put(dname, (void*)id, status); // share pointer with visibleIDMap
771 if (U_SUCCESS(status)) {
772 continue;
773 }
774 }
775 delete dnCache;
776 ncthis->dnCache = NULL;
777 return result;
778 }
779 }
780 }
781 }
782
783 ICUServiceKey* matchKey = createKey(matchID, status);
784 int32_t pos = 0;
785 const UHashElement *entry = NULL;
786 while ((entry = dnCache->cache.nextElement(pos)) != NULL) {
787 const UnicodeString* id = (const UnicodeString*)entry->value.pointer;
788 if (matchKey != NULL && !matchKey->isFallbackOf(*id)) {
789 continue;
790 }
791 const UnicodeString* dn = (const UnicodeString*)entry->key.pointer;
792 StringPair* sp = StringPair::create(*id, *dn, status);
793 result.addElement(sp, status);
794 if (U_FAILURE(status)) {
795 result.removeAllElements();
796 break;
797 }
798 }
799 delete matchKey;
800
801 return result;
802 }
803
804 URegistryKey
registerInstance(UObject * objToAdopt,const UnicodeString & id,UErrorCode & status)805 ICUService::registerInstance(UObject* objToAdopt, const UnicodeString& id, UErrorCode& status)
806 {
807 return registerInstance(objToAdopt, id, TRUE, status);
808 }
809
810 URegistryKey
registerInstance(UObject * objToAdopt,const UnicodeString & id,UBool visible,UErrorCode & status)811 ICUService::registerInstance(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status)
812 {
813 ICUServiceKey* key = createKey(&id, status);
814 if (key != NULL) {
815 UnicodeString canonicalID;
816 key->canonicalID(canonicalID);
817 delete key;
818
819 ICUServiceFactory* f = createSimpleFactory(objToAdopt, canonicalID, visible, status);
820 if (f != NULL) {
821 return registerFactory(f, status);
822 }
823 }
824 delete objToAdopt;
825 return NULL;
826 }
827
828 ICUServiceFactory*
createSimpleFactory(UObject * objToAdopt,const UnicodeString & id,UBool visible,UErrorCode & status)829 ICUService::createSimpleFactory(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status)
830 {
831 if (U_SUCCESS(status)) {
832 if ((objToAdopt != NULL) && (!id.isBogus())) {
833 return new SimpleFactory(objToAdopt, id, visible);
834 }
835 status = U_ILLEGAL_ARGUMENT_ERROR;
836 }
837 return NULL;
838 }
839
840 URegistryKey
registerFactory(ICUServiceFactory * factoryToAdopt,UErrorCode & status)841 ICUService::registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status)
842 {
843 if (U_SUCCESS(status) && factoryToAdopt != NULL) {
844 Mutex mutex(&lock);
845
846 if (factories == NULL) {
847 factories = new UVector(deleteUObject, NULL, status);
848 if (U_FAILURE(status)) {
849 delete factories;
850 return NULL;
851 }
852 }
853 factories->insertElementAt(factoryToAdopt, 0, status);
854 if (U_SUCCESS(status)) {
855 clearCaches();
856 } else {
857 delete factoryToAdopt;
858 factoryToAdopt = NULL;
859 }
860 }
861
862 if (factoryToAdopt != NULL) {
863 notifyChanged();
864 }
865
866 return (URegistryKey)factoryToAdopt;
867 }
868
869 UBool
unregister(URegistryKey rkey,UErrorCode & status)870 ICUService::unregister(URegistryKey rkey, UErrorCode& status)
871 {
872 ICUServiceFactory *factory = (ICUServiceFactory*)rkey;
873 UBool result = FALSE;
874 if (factory != NULL && factories != NULL) {
875 Mutex mutex(&lock);
876
877 if (factories->removeElement(factory)) {
878 clearCaches();
879 result = TRUE;
880 } else {
881 status = U_ILLEGAL_ARGUMENT_ERROR;
882 delete factory;
883 }
884 }
885 if (result) {
886 notifyChanged();
887 }
888 return result;
889 }
890
891 void
reset()892 ICUService::reset()
893 {
894 {
895 Mutex mutex(&lock);
896 reInitializeFactories();
897 clearCaches();
898 }
899 notifyChanged();
900 }
901
902 void
reInitializeFactories()903 ICUService::reInitializeFactories()
904 {
905 if (factories != NULL) {
906 factories->removeAllElements();
907 }
908 }
909
910 UBool
isDefault() const911 ICUService::isDefault() const
912 {
913 return countFactories() == 0;
914 }
915
916 ICUServiceKey*
createKey(const UnicodeString * id,UErrorCode & status) const917 ICUService::createKey(const UnicodeString* id, UErrorCode& status) const
918 {
919 return (U_FAILURE(status) || id == NULL) ? NULL : new ICUServiceKey(*id);
920 }
921
922 void
clearCaches()923 ICUService::clearCaches()
924 {
925 // callers synchronize before use
926 ++timestamp;
927 delete dnCache;
928 dnCache = NULL;
929 delete idCache;
930 idCache = NULL;
931 delete serviceCache; serviceCache = NULL;
932 }
933
934 void
clearServiceCache()935 ICUService::clearServiceCache()
936 {
937 // callers synchronize before use
938 delete serviceCache; serviceCache = NULL;
939 }
940
941 UBool
acceptsListener(const EventListener & l) const942 ICUService::acceptsListener(const EventListener& l) const
943 {
944 return l.getDynamicClassID() == ServiceListener::getStaticClassID();
945 }
946
947 void
notifyListener(EventListener & l) const948 ICUService::notifyListener(EventListener& l) const
949 {
950 ((ServiceListener&)l).serviceChanged(*this);
951 }
952
953 UnicodeString&
getName(UnicodeString & result) const954 ICUService::getName(UnicodeString& result) const
955 {
956 return result.append(name);
957 }
958
959 int32_t
countFactories() const960 ICUService::countFactories() const
961 {
962 return factories == NULL ? 0 : factories->size();
963 }
964
965 int32_t
getTimestamp() const966 ICUService::getTimestamp() const
967 {
968 return timestamp;
969 }
970
971 U_NAMESPACE_END
972
973 /* UCONFIG_NO_SERVICE */
974 #endif
975