summaryrefslogtreecommitdiff
path: root/jurt/com/sun/star/comp/servicemanager/ServiceManager.java
blob: a76120ac0ffae08c17004198fedabd35441173bb (plain)
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

package com.sun.star.comp.servicemanager;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;

import com.sun.star.container.XContentEnumerationAccess;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XSet;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XEventListener;
import com.sun.star.lang.XInitialization;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.loader.XImplementationLoader;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.registry.XSimpleRegistry;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

/**
 * The <code>ServiceManager</code> class is an implmentation of the <code>ServiceManager</code>the central class needed for
 * implementing or using UNO components in Java.
 * <p>
 * The Methods <code>queryInterface</code> and <code>isSame</code> delegate
 * calls to the implementing objects and are used instead of casts
 * and identity comparisons.
 * <p>
 * @see         com.sun.star.lang.XMultiServiceFactory
 * @see         com.sun.star.container.XSet
 * @see         com.sun.star.container.XContentEnumerationAccess
 * @see         com.sun.star.lang.XComponent
 * @see         com.sun.star.lang.XServiceInfo
 * @see         com.sun.star.lang.XInitialization
 * @since       UDK1.0
 */
public class ServiceManager implements XMultiServiceFactory,
                                       XMultiComponentFactory,
                                       XSet,
                                       XContentEnumerationAccess,
                                       XComponent,
                                       XServiceInfo,
                                       XInitialization
{
    private static final boolean DEBUG = false;

    private static final void DEBUG (String dbg) {
        if (DEBUG) System.err.println( dbg );
    }

    private static com.sun.star.uno.Type UNO_TYPE = null;

    XImplementationLoader loader = null;

    static String[] supportedServiceNames = {
            "com.sun.star.lang.MultiServiceFactory",
            "com.sun.star.lang.ServiceManager"
    };

    ArrayList<XEventListener>    eventListener;
    java.util.HashMap<String, Object> factoriesByImplNames;
    java.util.HashMap<String, ArrayList<Object>> factoriesByServiceNames;  // keys:

    private com.sun.star.uno.XComponentContext m_xDefaultContext;

    /**
     * Creates a new instance of the <code>ServiceManager</code>.
     */
    public ServiceManager() {
        eventListener           = new ArrayList<XEventListener>();
        factoriesByImplNames    = new java.util.HashMap<String, Object>();
        factoriesByServiceNames = new java.util.HashMap<String, ArrayList<Object>>();
        m_xDefaultContext = null;
    }
    /**
     * Creates a new instance of the <code>ServiceManager</code>.
     */
    public ServiceManager( XComponentContext xContext ) {
        eventListener           = new ArrayList<XEventListener>();
        factoriesByImplNames    = new java.util.HashMap<String, Object>();
        factoriesByServiceNames = new java.util.HashMap<String, ArrayList<Object>>();
        m_xDefaultContext = xContext;
    }

    /**
     * Returns the service factory for the <code>ServiceManager</code>. If the given implementation name
     * does not equal to the <code>ServiceManagers</code> class name null will be returned.
     * <p>
     * @return     the factory for the <code>ServiceManager</code>.
     * @param      implName     the implementation name of the of the service.
     *                          Must be equal to <code>com.sun.star.comp.servicemanager.ServicManager</code>
     * @param      multiFactory refernce of the <code>MultiServiceFactory</code>. This parameter will be ignored.
     * @param      regKey       the root key of the registry. This parameter will be ignored.
     */
      public static XSingleServiceFactory getServiceFactory( String implName,
                                                           XMultiServiceFactory multiFactory,
                                                           XRegistryKey regKey)
    {
        if ( implName.equals(ServiceManager.class.getName()) )
            return new ServiceManagerFactory();

        return null;
    }


    /**
     * Supplies a Java component loader. The loader component must be enlisted at the <code>ServiceManager</code> before.
     * <p>
     * @return      a new instance of the Java component loader
     * @see         com.sun.star.loader.Java
     */
    private XImplementationLoader getLoader()
                throws  com.sun.star.uno.Exception,
                          com.sun.star.uno.RuntimeException
    {
        Object[] param = { this };
        DEBUG("make loader");
        Object loaderObj = createInstanceWithArgumentsAndContext(
            "com.sun.star.loader.Java", param, m_xDefaultContext );

        if (loaderObj == null)
            throw new com.sun.star.uno.Exception("Can get an instance of com.sun.star.loader.Java");

        return UnoRuntime.queryInterface( XImplementationLoader.class, loaderObj );
    }

    /**
     * Registers a list of components given by their class names.
     * <p>
     * @param   newImpls    list of the components that should be registered, given by their class names.
     *                      If any exception occurred during the registration, the process will be canceled.
     * @see     com.sun.star.container.XSet
     */
    private void xaddFactories( String[] newImpls )
                    throws com.sun.star.uno.Exception
    {
        for (int i=0; i<newImpls.length; i++) {
            DEBUG ("try to add " + newImpls[i] );
            Object newFactory = null;

            try {
                if (loader == null)
                    loader = getLoader();

                newFactory = loader.activate( newImpls[i], null, null, null );
            }
            catch (com.sun.star.uno.Exception e) {

//****************************** BEGIN DEPRECATED ******************************************

                try {
                    // try to get the class of the implementation
                    Class<?> clazz = Class.forName( newImpls[i] );

                    Class<?>[] methodClassParam = { String.class, XMultiServiceFactory.class, XRegistryKey.class };
                    java.lang.reflect.Method getFactoryMeth  ;
                    try {
                        getFactoryMeth = clazz.getMethod("__getServiceFactory", methodClassParam);
                    }
                    catch (NoSuchMethodException noSuchMethodEx) {
                        getFactoryMeth = null;
                    }
                    catch (SecurityException securityExc) {
                        getFactoryMeth = null;
                    }

                    if (getFactoryMeth == null)
                        getFactoryMeth = clazz.getMethod("getServiceFactory", methodClassParam);

                    Object[] methodParams = { newImpls[i], this, null };
                    newFactory = getFactoryMeth.invoke( clazz, methodParams );
                }
                catch (NoSuchMethodException ex) {}
                catch (SecurityException ex) {}
                catch (ClassNotFoundException ex) {}
                catch (IllegalAccessException ex) {}
                catch (IllegalArgumentException ex) {}
                catch (InvocationTargetException ex) {}

//****************************** END DEPRECATED ******************************************
            }

            if ( newFactory == null )
                throw new com.sun.star.loader.CannotActivateFactoryException("Can not get factory for " +  newImpls[i]);

            insert( newFactory );
        } // end of for ...
    }

    /**
     * The method is used to add components to the <code>ServiceManager</code>. The first argument indicates a <code>SimpleRegistry</code>.
     * The components which should be added will be searched under the <i>Implementations</i> key in the registry.
     * <p>
     * @param   args    the first argument ( args[0] ) specifices the SimpleRegistry object
     * @see     com.sun.star.lang.XInitialization
     * @see     com.sun.star.lang.RegistryServiceManager
     * @see     com.sun.star.registry.XSimpleRegistry
     */
    public void initialize( Object args[] )
                throws  com.sun.star.uno.Exception,
                        com.sun.star.uno.RuntimeException {
        XSimpleRegistry xSimpleRegistry  ;
        try {
            xSimpleRegistry = (XSimpleRegistry) args[0];
            if (xSimpleRegistry != null)
            {
                XRegistryKey rootkey = xSimpleRegistry.getRootKey();

                XRegistryKey implkey_xRegistryKey = rootkey.openKey("Implementations");
                if(implkey_xRegistryKey != null) {
                    XRegistryKey xRegistryKeys[] = implkey_xRegistryKey.openKeys();

                    for(int i = 0; i < xRegistryKeys.length; ++ i) {
                        xaddFactories(new String[]{xRegistryKeys[i].getStringValue()});
                    }
                }
            }

            if (args.length > 1)
            {
                m_xDefaultContext = (XComponentContext)args[ 1 ];
            }
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            throw new com.sun.star.lang.IllegalArgumentException("Argument must not be null.");
        }
    }

    /**
     * Creates a new instance of a specified service. Therefor the associated factory of the service is
     * looked up and used to instanciate a new component.
     * <p>
     * @return  newly created component
     * @param   serviceSpecifier    indicates the service or component name
     * @see     com.sun.star.lang.XMultiServiceFactory
     */
    public java.lang.Object createInstance( String serviceSpecifier )
            throws com.sun.star.uno.Exception,
                   com.sun.star.uno.RuntimeException
    {
        return createInstanceWithContext( serviceSpecifier, m_xDefaultContext );
    }

    /**
     * Creates a new instance of a specified service with the given parameters.
     * Therefor the associated factory of the service is  looked up and used to instanciate a new component.
     * <p>
     * @return  newly created component
     * @param   serviceSpecifier    indicates the service or component name
     * @see     com.sun.star.lang.XMultiServiceFactory
     */
    public java.lang.Object createInstanceWithArguments(
        String serviceSpecifier, Object[] args )
        throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException
    {
        if (DEBUG) {
            System.err.println("createInstanceWithArguments:" );

            for (int i=0; i<args.length; i++)
                System.err.print(" "+ args[i]);

            System.err.println();
        }

        return createInstanceWithArgumentsAndContext( serviceSpecifier, args, m_xDefaultContext );
    }

    /**
     * Look up the factory for a given service or implementation name.
     * First the requested service name is search in the list of avaible services. If it can not be found
     * the name is looked up in the implementation list.
     * <p>
     * @return  the factory of the service / implementation
     * @param   serviceName    indicates the service or implementation name
     * @see     com.sun.star.lang.XMultiServiceFactory
     */
    private Object queryServiceFactory(String serviceName)
            throws com.sun.star.uno.Exception,
                   com.sun.star.uno.RuntimeException
    {
        DEBUG("queryServiceFactory for name " + serviceName );
        Object factory = null;

        if ( factoriesByServiceNames.containsKey( serviceName ) ) {
            ArrayList<Object> availableFact = factoriesByServiceNames.get( serviceName );

            DEBUG("");
            DEBUG("aviable factories for " + serviceName +" "+ availableFact);
            DEBUG("");

            if ( !availableFact.isEmpty() )
                factory = availableFact.get(availableFact.size()-1);

        } else // not found in list of services - now try the implementations
            factory = factoriesByImplNames.get( serviceName ); // return null if none is aviable

        if (DEBUG) {
            if (factory == null) System.err.println("service not registered");
            else
                System.err.println("service found:" + factory + " " + UnoRuntime.queryInterface(XSingleServiceFactory.class, factory));
        }

        if (factory == null)
            throw new com.sun.star.uno.Exception("Query for service factory for " + serviceName + " failed.");

        return factory;
    }

    /**
     * Supplies a list of all avialable services names.
     * <p>
     * @return  list of Strings of all service names
     * @see     com.sun.star.container.XContentEnumerationAccess
     */
    public String[] getAvailableServiceNames()
            throws com.sun.star.uno.RuntimeException
    {
        int i = 0;
        String[] availableServiceNames = new String[factoriesByServiceNames.size()];

        java.util.Iterator<String> keys = factoriesByServiceNames.keySet().iterator();

        while (keys.hasNext())
            availableServiceNames[i++] = keys.next();

        return availableServiceNames;
    }

    // XMultiComponentFactory implementation

    /** Create a service instance with given context.

        @param rServiceSpecifier service name
        @param xContext context
        @return service instance
    */
    public java.lang.Object createInstanceWithContext(
        String rServiceSpecifier,
        com.sun.star.uno.XComponentContext xContext )
        throws com.sun.star.uno.Exception
    {
        Object fac = queryServiceFactory( rServiceSpecifier );
        if (fac != null)
        {
            XSingleComponentFactory xCompFac = UnoRuntime.queryInterface(
                XSingleComponentFactory.class, fac );
            if (xCompFac != null)
            {
                return xCompFac.createInstanceWithContext( xContext );
            }
            else
            {
                XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface(
                    XSingleServiceFactory.class, fac );
                if (xServiceFac != null)
                {
                    if (DEBUG)
                        System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" );
                    return xServiceFac.createInstance();
                }
                else
                {
                    throw new com.sun.star.uno.Exception(
                        "retrieved service factory object for \"" + rServiceSpecifier +
                        "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" );
                }
            }
        }
        return null;
    }
    /** Create a service instance with given context and arguments.

        @param rServiceSpecifier service name
        @param rArguments arguments
        @param xContext context
        @return service instance
    */
    public java.lang.Object createInstanceWithArgumentsAndContext(
        String rServiceSpecifier,
        java.lang.Object[] rArguments,
        com.sun.star.uno.XComponentContext xContext )
        throws com.sun.star.uno.Exception
    {
        Object fac = queryServiceFactory( rServiceSpecifier );
        if (fac != null)
        {
            XSingleComponentFactory xCompFac = UnoRuntime.queryInterface(
                XSingleComponentFactory.class, fac );
            if (xCompFac != null)
            {
                return xCompFac.createInstanceWithArgumentsAndContext( rArguments, xContext );
            }
            else
            {
                XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface(
                    XSingleServiceFactory.class, fac );
                if (xServiceFac != null)
                {
                    if (DEBUG)
                        System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" );
                    return xServiceFac.createInstanceWithArguments( rArguments );
                }
                else
                {
                    throw new com.sun.star.uno.Exception(
                        "retrieved service factory object for \"" + rServiceSpecifier +
                        "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" );
                }
            }
        }
        return null;
    }
//      public String[] getAvailableServiceNames();

    /**
     * Removes all listeners from the <code>ServiceManager</code> and clears the list of the services.
     * <p>
     * @see com.sun.star.lang.XComponent
     */
    public void dispose()
        throws com.sun.star.uno.RuntimeException
    {
        if (eventListener != null) {
            java.util.Iterator<XEventListener> enumer = eventListener.iterator();

            while (enumer.hasNext()) {
                XEventListener listener = enumer.next();
                listener.disposing(new com.sun.star.lang.EventObject(this));
            }
            eventListener.clear();
        }

        factoriesByServiceNames.clear();
        factoriesByImplNames.clear();
    }

    /**
     * Adds a new <code>EventListener</code>. The listener is notified when a
     * service is added (removed) to (from) the <code>ServiceManager</code>.
     * If the listener is already registred a
     * <code>com.sun.star.uno.RuntimeException</code> will be thrown.
     * <p>
     * @param   xListener   the new listener which should been added.
     * @see     com.sun.star.lang.XComponent
     */
    public void addEventListener( XEventListener xListener )
            throws com.sun.star.uno.RuntimeException
    {
        if (xListener == null)
            throw new com.sun.star.uno.RuntimeException("Listener must not be null");

          if ( eventListener.contains(xListener) )
              throw new com.sun.star.uno.RuntimeException("Listener already registred.");

           eventListener.add(xListener);
    }

    /**
     * Removes a <code>EventListener</code> from the <code>ServiceManager</code>.
     * If the listener is not registered a <code>com.sun.star.uno.RuntimeException</code>
     * will be thrown.
     * <p>
     * @param   xListener   the new listener which should been removed.
     * @see com.sun.star.lang.XComponent
     */
    public void removeEventListener( XEventListener xListener )
            throws com.sun.star.uno.RuntimeException
    {
        if (xListener == null)
            throw new com.sun.star.uno.RuntimeException("Listener must not be null");

          if ( !eventListener.contains(xListener) )
              throw new com.sun.star.uno.RuntimeException("Listener is not registered.");

        eventListener.remove(xListener);
    }

    /**
     * Checks if a component is registered at the <code>ServiceManager</code>. The given object argument must
     * provide a <code>XServiceInfo</code> interface.
     * <p>
     * @return  true if the component is registred otherwise false.
     * @param   object object which provides a <code>XServiceInfo</code> interface.
     * @see     com.sun.star.container.XSet
     * @see     com.sun.star.lang.XServiceInfo
     */
    public boolean has( Object object )
        throws com.sun.star.uno.RuntimeException
    {
            if (object == null)
                throw new com.sun.star.uno.RuntimeException("The parameter must not been null");

            XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, object);

        return xServiceInfo != null && UnoRuntime.areSame(factoriesByImplNames.get(xServiceInfo.getImplementationName()), object);

        }

    /**
     * Adds a <code>SingleServiceFactory</code> to the <code>ServiceManager</code>.
     * <p>
     * @param   object  factory which should be added.
     * @see com.sun.star.container.XSet
     * @see com.sun.star.lang.XSingleServiceFactory
     */
    public void insert( Object object )
        throws com.sun.star.lang.IllegalArgumentException,
               com.sun.star.container.ElementExistException,
               com.sun.star.uno.RuntimeException
    {
        if (object == null) throw new com.sun.star.lang.IllegalArgumentException();

        XServiceInfo xServiceInfo =
                UnoRuntime.queryInterface(XServiceInfo.class, object);

        if (xServiceInfo == null)
            throw new com.sun.star.lang.IllegalArgumentException(
                "The given object does not implement the XServiceInfo interface."
            );

        if ( factoriesByImplNames.containsKey( xServiceInfo.getImplementationName() ) ) {
            throw new com.sun.star.container.ElementExistException(
                xServiceInfo.getImplementationName() + " already registred"
            );
        }

        DEBUG("add factory " + object.toString() + " for " + xServiceInfo.getImplementationName());
        factoriesByImplNames.put( xServiceInfo.getImplementationName(), object );


        String[] serviceNames = xServiceInfo.getSupportedServiceNames();
        ArrayList<Object> vec  ;

        for (int i=0; i<serviceNames.length; i++) {
            if ( !factoriesByServiceNames.containsKey( serviceNames[i] ) ) {
                DEBUG("> no registered services found under " + serviceNames[i] + ": adding..." );
                factoriesByServiceNames.put(serviceNames[i], new ArrayList<Object>());
            }

            vec = factoriesByServiceNames.get( serviceNames[i] );

            if ( vec.contains( object ) )
                System.err.println("The implementation " + xServiceInfo.getImplementationName() +
                    " already registered for the service " + serviceNames[i] + " - ignoring!");
            else
                vec.add(object);
        }
    }

    /**
     * Removes a <code>SingleServiceFactory</code> from the <code>ServiceManager</code>.
     * <p>
     * @param   object  factory which should be removed.
     * @see com.sun.star.container.XSet
     * @see com.sun.star.lang.XSingleServiceFactory
     */
    public void remove( Object object )
        throws com.sun.star.lang.IllegalArgumentException,
               com.sun.star.container.NoSuchElementException,
               com.sun.star.uno.RuntimeException
    {
        if (object == null)
            throw new com.sun.star.lang.IllegalArgumentException(
                    "The given object must not be null."
            );

        XServiceInfo xServiceInfo =
            UnoRuntime.queryInterface(XServiceInfo.class, object);

        if (xServiceInfo == null)
            throw new com.sun.star.lang.IllegalArgumentException(
                    "The given object does not implement the XServiceInfo interface."
            );

        XSingleServiceFactory xSingleServiceFactory =
            UnoRuntime.queryInterface(XSingleServiceFactory.class, object);

        if (xSingleServiceFactory == null)
            throw new com.sun.star.lang.IllegalArgumentException(
                    "The given object does not implement the XSingleServiceFactory interface."
            );

        if ( factoriesByImplNames.remove( xServiceInfo.getImplementationName() ) == null )
            throw new com.sun.star.container.NoSuchElementException(
                    xServiceInfo.getImplementationName() +
                    " is not registered as an implementation."
            );

        String[] serviceNames = xServiceInfo.getSupportedServiceNames();

        for ( int i=0; i<serviceNames.length; i++ ) {
            if ( factoriesByServiceNames.containsKey( serviceNames[i] ) ) {
                ArrayList<Object> vec = factoriesByServiceNames.get(serviceNames[i]);

                if ( !vec.remove(object) )
                    System.err.println("The implementation " + xServiceInfo.getImplementationName() +
                        " is not registered for the service " + serviceNames[i] + " - ignoring!");

                if ( vec.isEmpty() ) // remove the vector if no implementations aviable for the service
                    factoriesByServiceNames.remove( serviceNames[i] );
            }
        }
    }

    /**
     * Provides an enumeration of all registered services.
     * <p>
     * @return  an enumeration of all available services.
     * @see     com.sun.star.container.XEnumerationAccess
     */
    public XEnumeration createEnumeration()
            throws com.sun.star.uno.RuntimeException
    {
        return new ServiceEnumerationImpl( factoriesByImplNames.values().iterator() );
    }

    /**
     * Provides the UNO type of the <code>ServiceManager</code>
     * <p>
     * @return  the UNO type of the <code>ServiceManager</code>.
     * @see     com.sun.star.container.XElementAccess
     * @see     com.sun.star.uno.TypeClass
     */
    public com.sun.star.uno.Type getElementType()
            throws com.sun.star.uno.RuntimeException
    {
        if ( UNO_TYPE == null )
            UNO_TYPE = new com.sun.star.uno.Type(ServiceManager.class);

        return UNO_TYPE;
    }

    /**
     * Checks if the any componets are registered.
     * <p>
     * @return  true - if the list of the registred components is not empty - otherwise false.
     * @see     com.sun.star.container.XElementAccess
     */
    public boolean hasElements() {
        return ! factoriesByImplNames.isEmpty();
    }

    /**
     * Provides an enumeration of of all factorys for a specified service.
     * <p>
     * @return  an enumeration for service name.
     * @param   serviceName     name of the requested service
     * @see     com.sun.star.container.XContentEnumerationAccess
     */
    public XEnumeration createContentEnumeration( String serviceName )
                throws com.sun.star.uno.RuntimeException
    {
        XEnumeration enumer  ;

        ArrayList<Object> serviceList = factoriesByServiceNames.get(serviceName);

        if (serviceList != null)
            enumer = new ServiceEnumerationImpl( serviceList.iterator() );
        else
            enumer = new ServiceEnumerationImpl();

        return enumer;
    }

    /**
     * Returns the implementation name of the <code>ServiceManager</code> component.
     * <p>
     * @return  the class name of the <code>ServiceManager</code>.
     * @see     com.sun.star.lang.XServiceInfo
     */
    public String getImplementationName()
            throws com.sun.star.uno.RuntimeException
    {
        return getClass().getName();
    }

    /**
     * Checks if the <code>ServiceManager</code> supports a service.
     * <p>
     * @return  true if the service is supported - otherwise false.
     * @param   serviceName service name which should be checked.
     * @see     com.sun.star.lang.XServiceInfo
     */
    public boolean supportsService( String serviceName )
            throws com.sun.star.uno.RuntimeException
    {
        for (int i=0; i<supportedServiceNames.length; i++)
            if (supportedServiceNames[i].equals( serviceName )) return true;

        return getImplementationName().equals(serviceName);

        }

    /**
     * Supplies list of all supported services.
     * <p>
     * @return  a list of all supported service names.
     * @see     com.sun.star.lang.XServiceInfo
     */
    public String[] getSupportedServiceNames()
            throws com.sun.star.uno.RuntimeException
    {
        return supportedServiceNames;
    }

    /**
     * The <code>ServiceEnumerationImpl</code> class provides an
     * implementation of the @see com.sun.star.container.XEnumeration interface.
     * It is a inner wrapper for a java.util.Enumeration object.
     * <p>
     * @see         com.sun.star.lang.XSingleServiceFactory
     * @see         com.sun.star.lang.XServiceInfo
     * @since       UDK1.0
     */
    class ServiceEnumerationImpl implements XEnumeration {
        java.util.Iterator<Object> enumeration = null;

        /**
         * Constructs a new empty instance.
         */
        public ServiceEnumerationImpl() {
        }

        /**
         * Constructs a new instance with a given enumeration.
         * <p>
         * @param   enumer  is the enumeration which should been wrapped.
         * @see     com.sun.star.container.XEnumeration
         */
        public ServiceEnumerationImpl(java.util.Enumeration<Object> enumer) {
            enumeration = Collections.list(enumer).iterator();
        }

        /**
         * Constructs a new instance with a given enumeration.
         * <p>
         * @param   enumer  is the enumeration which should been wrapped.
         * @see     com.sun.star.container.XEnumeration
         */
        public ServiceEnumerationImpl(java.util.Iterator<Object> enumer) {
            enumeration = enumer;
        }

        /**
         * Checks if the enumeration contains more elements.
         * <p>
         * @return  true if more elements are available - otherwise false.
         * @see     com.sun.star.container.XEnumeration
         */
        public boolean hasMoreElements()
                throws com.sun.star.uno.RuntimeException
        {
            return enumeration != null && enumeration.hasNext();

            }

        /**
         * Returns the next element of the enumeration. If no further elements
         * available a com.sun.star.container.NoSuchElementException exception will be thrown.
         * <p>
         * @return  the next element.
         * @see     com.sun.star.container.XEnumeration
         */
        public Object nextElement()
                throws com.sun.star.container.NoSuchElementException,
                       com.sun.star.lang.WrappedTargetException,
                       com.sun.star.uno.RuntimeException
        {
            if (enumeration == null)
                throw new com.sun.star.container.NoSuchElementException();

            try {
                return enumeration.next();
            } catch (java.util.NoSuchElementException e) {
                com.sun.star.container.NoSuchElementException ex =
                    new com.sun.star.container.NoSuchElementException(
                        e.toString());
                ex.initCause(e);
                throw ex;
            }
        }
    }
}
/**
 * The <code>ServiceManagerFactory</code> is the factory class for the
 * <code>ServiceManager</code>. As all factories it implments the
 * com.sun.star.lang.XSingleServiceFactory and the com.sun.star.lang.XServiceInfo
 * interfaces.
 * <p>
 * @see         com.sun.star.lang.XSingleServiceFactory
 * @see         com.sun.star.lang.XServiceInfo
 * @since       UDK1.0
*/
class ServiceManagerFactory implements  XServiceInfo, XSingleComponentFactory, XSingleServiceFactory
{
    /**
     * Creates a new instance of the <code>ServiceManagerFactory</code>.
     */
    public ServiceManagerFactory() {
    }

    /**
     * Supplies the implementation name of the <code>ServiceManager</code>.
     * <p>
     * @return      <code>ServiceManager</code> class name.
      * @see         com.sun.star.lang.XServiceInfo
     */
    public String getImplementationName()
            throws com.sun.star.uno.RuntimeException
    {
        return ServiceManager.class.getName();
    }

    /**
     * Checks whether or not a service is supported.
     * <p>
     * @return      true - if the service is supported, otherwise false.
     * @param       serviceName     the name of the service that should be checked.
      * @see         com.sun.star.lang.XServiceInfo
     */
    public boolean supportsService( String serviceName )
            throws com.sun.star.uno.RuntimeException
    {
        for ( int i=0; i<ServiceManager.supportedServiceNames.length; i++ )
            if ( ServiceManager.supportedServiceNames[i].equals(serviceName) ) return true;

        return getImplementationName().equals(serviceName);

        }

    /**
     * Returns all service names which are supported by <code>ServiceManager</code>.
     * <p>
     * @return      a list aof all supported service names.
      * @see         com.sun.star.lang.XServiceInfo
     */
    public String[] getSupportedServiceNames()
            throws com.sun.star.uno.RuntimeException
    {
        return ServiceManager.supportedServiceNames;
    }

    /**
     * Creates a new instance of the <code>ServiceManager</code>.
     * <p>
     * @return      newly created <code>ServiceManager</code> object.
      * @see         com.sun.star.lang.XSingleServiceFactory
     */
    public java.lang.Object createInstance()
            throws com.sun.star.uno.Exception,
                   com.sun.star.uno.RuntimeException
    {
        return new ServiceManager();
    }

    /**
     * Creates a new instance of the <code>ServiceManager</code> with arguments.
     * At this time it always throws a com.sun.star.lang.NoSuchMethodException
     * because there is no the <code>ServiceManager</code> has no constructor with
     * arguments.
     * <p>
     * @return      null - always throws an exception
     * @param       aArguments arguments for new instance.
      * @see         com.sun.star.lang.XSingleServiceFactory
     */
    public java.lang.Object createInstanceWithArguments( java.lang.Object[] aArguments )
            throws com.sun.star.uno.Exception,
                   com.sun.star.uno.RuntimeException
    {
        throw new com.sun.star.lang.NoSuchMethodException("Constructor with arguments is not supported.");
    }

    // XSingleComponentFactory impl
    //______________________________________________________________________________________________
    public Object createInstanceWithContext( XComponentContext xContext )
        throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException
    {
        return new ServiceManager( xContext );
    }
    //______________________________________________________________________________________________
    public Object createInstanceWithArgumentsAndContext(
        Object aArguments [], XComponentContext xContext )
        throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException
    {
        throw new com.sun.star.lang.NoSuchMethodException(
            "ServiceManagerFactory.createInstanceWithArgumentsAndContext() not impl!" );
    }
}