xref: /trunk/main/cpputools/source/registercomponent/registercomponent.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <vector>
28 
29 #include "sal/main.h"
30 #include <rtl/strbuf.hxx>
31 #include <rtl/ustrbuf.hxx>
32 
33 #include <cppuhelper/servicefactory.hxx>
34 #include <cppuhelper/shlib.hxx>
35 
36 #include <com/sun/star/container/XSet.hpp>
37 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
38 #include <com/sun/star/registry/XImplementationRegistration2.hpp>
39 #include <com/sun/star/registry/XSimpleRegistry.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 
42 #include <algorithm>
43 #include <osl/process.h>
44 #include <osl/diagnose.h>
45 #include <osl/thread.h>
46 #include <osl/file.hxx>
47 
48 #ifdef SAL_UNX
49 #define SEPARATOR '/'
50 #else
51 #define SEPARATOR '\\'
52 #endif
53 
54 using namespace ::rtl;
55 using namespace ::osl;
56 using namespace ::cppu;
57 using namespace ::std;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::registry;
61 using com::sun::star::container::XSet;
62 using com::sun::star::container::XContentEnumerationAccess;
63 using com::sun::star::container::XEnumeration;
64 
65 namespace {
66 
replacePrefix(OUString const & url,OUString const & prefix)67 OUString replacePrefix(OUString const & url, OUString const & prefix) {
68     sal_Int32 i = url.lastIndexOf('/');
69     // Backward compatibility with stoc/source/implementationregistration/
70     // implreg.cxx:1.27 l. 1892:
71     if (i == -1) {
72         i = url.lastIndexOf('\\');
73     }
74     return prefix + url.copy(i + 1);
75 }
76 
77 }
78 
isFileUrl(const OUString & fileName)79 sal_Bool isFileUrl(const OUString& fileName)
80 {
81     if (fileName.indexOf(OUString::createFromAscii("file://")) == 0 )
82         return sal_True;
83     return sal_False;
84 }
85 
convertToFileUrl(const OUString & fileName)86 OUString convertToFileUrl(const OUString& fileName)
87 {
88     if ( isFileUrl(fileName) )
89     {
90         return fileName;
91     }
92 
93     OUString uUrlFileName;
94     if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
95     {
96         OUString uWorkingDir;
97         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
98             OSL_ASSERT(false);
99         }
100         if (FileBase::getAbsoluteFileURL(uWorkingDir, fileName, uUrlFileName)
101             != FileBase::E_None)
102         {
103             OSL_ASSERT(false);
104         }
105     } else
106     {
107         if (FileBase::getFileURLFromSystemPath(fileName, uUrlFileName)
108             != FileBase::E_None)
109         {
110             OSL_ASSERT(false);
111         }
112     }
113 
114     return uUrlFileName;
115 }
usingRegisterImpl()116 static void usingRegisterImpl()
117 {
118     fprintf(stderr, "usage: regcomp -register|revoke -r registryfile -c locationUrl [-br registryfile] [-l componentLoaderUrl] [-s] [-classpath path]\n");
119     fprintf(stderr, " Parameters:\n");
120     fprintf(stderr, "  -register\n"
121                     "        register a new component.\n");
122     fprintf(stderr, "  -revoke\n"
123                     "        revoke a component.\n");
124     fprintf(stderr, "  -br registryfile\n"
125                     "        the name of the registry used for bootstrapping\n"
126                     "        regcomp. The option can be given twice, each\n"
127                     "        one followed by exactly one registry file.\n"
128                     "        The registries are used to access both types and\n"
129                     "        registered components.\n");
130     fprintf(stderr, "  -r registryfile\n"
131                     "        the name of the target registry (will be created\n"
132                     "        if it does not exists). The file name may match\n"
133                     "        with one of the filenames given with the -br option.\n");
134     fprintf(stderr, "  -c locationUrls\n"
135                     "        the location of a component (a url to a shared\n"
136                     "        library or a absolute url to a .jar file) or a\n"
137                     "        list of urls separated by ';' or ' '. Note if a\n"
138                     "        list of urls is specified, the components must\n"
139                     "        all need the same loader (quoting is possible with\n"
140                     "        \\ or \"\").\n");
141     fprintf(stderr, "  -l componentLoaderUrl\n"
142                     "        the name of the needed loader. If no loader is\n"
143                     "        specified and the components have a .jar suffix,\n"
144                     "        the default is com.sun.star.loader.Java2.\n"
145                     "        Otherwise, the default is\n"
146                     "        com.sun.star.loader.SharedLibrary\n"
147                     "  -s\n"
148                     "        silent, regcomp prints messages only on error.\n"
149                     "  -wop\n"
150                     "        register the component name only without path\n"
151                     "  -wop=prefix\n"
152                     "        register the component name with path replaced\n"
153                     "        by given prefix\n"
154                     "  -classpath path\n"
155                     "        sets the java classpath to path (overwriting the\n"
156                     "        current classpath environment variable). Note that\n"
157                     "        in case you start regcomp e.g. within an office\n"
158                     "        environment, the classpath entries in the\n"
159                     "        configuration still have precedence over this\n"
160                     "        option.\n");
161 }
162 
163 class IllegalArgument
164 {
165 public:
IllegalArgument(const OString & rMessage)166     IllegalArgument(const OString& rMessage)
167         : m_message(rMessage)
168         {}
169 
170     OString m_message;
171 };
172 
173 struct Options
174 {
OptionsOptions175     Options()
176         : bRegister(sal_False)
177         , bRevoke(sal_False)
178         , bSilent( sal_False )
179         , bPrefix( sal_False )
180         {}
181 
182     sal_Bool bRegister;
183     sal_Bool bRevoke;
184     sal_Bool bSilent;
185     sal_Bool bPrefix;
186     OUString sPrefix;
187     OUString sProgramName;
188     OUString sBootRegName;
189     OUString sBootRegName2;
190     OUString sRegName;
191     OUString sComponentUrls;
192     OUString sLoaderName;
193 };
194 
parseOptions(int ac,char * av[],Options & rOptions,sal_Bool bCmdFile)195 sal_Bool parseOptions(int ac, char* av[], Options& rOptions, sal_Bool bCmdFile)
196 {
197     sal_Bool    ret = sal_True;
198     sal_uInt16  i=0;
199     sal_Bool bLoaderExplicitlyGiven = sal_False;
200 
201     rOptions.sProgramName = OUString::createFromAscii(av[i++]);
202 
203     if (!bCmdFile)
204     {
205         bCmdFile = sal_True;
206 
207         if (ac < 2)
208         {
209             usingRegisterImpl();
210             ret = sal_False;
211         }
212     }
213 
214     for (; i < ac; i++)
215     {
216         if (av[i][0] == '-')
217         {
218             switch (av[i][1])
219             {
220                 case 'r':
221                     if (strcmp(av[i], "-register") == 0)
222                     {
223                         rOptions.bRegister = sal_True;
224                     } else
225                     if (strcmp(av[i], "-revoke") == 0)
226                     {
227                         rOptions.bRevoke = sal_True;
228                     } else
229                     if (av[i][2] == '\0')
230                     {
231                         if (i < ac - 1 && av[i+1][0] != '-')
232                         {
233                             i++;
234                             rOptions.sRegName = OStringToOUString(av[i], osl_getThreadTextEncoding());
235                         } else
236                         {
237                             OString tmp("'-r', please check");
238                             if (i <= ac - 1)
239                             {
240                                 tmp += " your input '" + OString(av[i+1]) + "'";
241                             }
242                             throw IllegalArgument(tmp);
243                         }
244                     } else
245                     {
246                         rOptions.sRegName = OStringToOUString(av[i]+2, osl_getThreadTextEncoding());
247                     }
248                     break;
249                 case 'b':
250                     if (av[i][2] != 'r')
251                     {
252                         OString tmp("'-b', invalid option!");
253                         throw IllegalArgument(tmp);
254                     }
255                     if (av[i][3] == '\0')
256                     {
257                         if (i < ac - 1 && av[i+1][0] != '-')
258                         {
259                             i++;
260                             OUString regName = OStringToOUString(av[i], osl_getThreadTextEncoding());
261                             if( ! rOptions.sBootRegName.getLength() )
262                             {
263                                 rOptions.sBootRegName = regName;
264                             }
265                             else
266                             {
267                                 rOptions.sBootRegName2 = regName;
268                             }
269                         } else
270                         {
271                             OString tmp("'-br', please check");
272                             if (i <= ac - 1)
273                             {
274                                 tmp += " your input '" + OString(av[i+1]) + "'";
275                             }
276                             throw IllegalArgument(tmp);
277                         }
278                     } else
279                     {
280                         rOptions.sBootRegName = OStringToOUString(av[i]+3, osl_getThreadTextEncoding());
281                     }
282                     break;
283                 case 'c':
284                 {
285                     OUString sUrls;
286                     if (av[i][2] == '\0')
287                     {
288                         if (i < ac - 1 && av[i+1][0] != '-')
289                         {
290                             i++;
291                             sUrls = OStringToOUString(av[i], osl_getThreadTextEncoding());
292                         } else
293                         {
294                             OString tmp("'-c', please check");
295                             if (i <= ac - 1)
296                             {
297                                 tmp += " your input '" + OString(av[i+1]) + "'";
298                             }
299                             throw IllegalArgument(tmp);
300                         }
301                     }
302                     else if( 0 == strncmp( av[i] , "-classpath" ,10 ) )
303                     {
304                         i++;
305                         if( i < ac )
306                         {
307                             rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("CLASSPATH"));
308                             rtl::OUString envValue(av[i], strlen(av[i]), osl_getThreadTextEncoding());
309                             osl_setEnvironment(envVar.pData, envValue.pData);
310                         }
311                         break;
312                     }
313                     else
314                     {
315                         sUrls = OStringToOUString(av[i]+2, osl_getThreadTextEncoding());
316                     }
317 
318                     if (rOptions.sComponentUrls.getLength())
319                     {
320                         OUString tmp(rOptions.sComponentUrls + OUString(";", 1, osl_getThreadTextEncoding()) + sUrls);
321                         rOptions.sComponentUrls = tmp;
322                     } else
323                     {
324                         rOptions.sComponentUrls = sUrls;
325                     }
326                     break;
327                 }
328                 case 'l':
329                 {
330                     if (av[i][2] == '\0')
331                     {
332                         if (i < ac - 1 && av[i+1][0] != '-')
333                         {
334                             i++;
335                             rOptions.sLoaderName = OUString::createFromAscii(av[i]);
336                             bLoaderExplicitlyGiven = sal_True;
337                         } else
338                         {
339                             OString tmp("'-l', please check");
340                             if (i <= ac - 1)
341                             {
342                                 tmp += " your input '" + OString(av[i+1]) + "'";
343                             }
344                             throw IllegalArgument(tmp);
345                         }
346                     } else
347                     {
348                         bLoaderExplicitlyGiven = sal_True;
349                         rOptions.sLoaderName = OUString::createFromAscii(av[i]+2);
350                     }
351                     break;
352                 }
353                 case 's':
354                 {
355                     if( av[i][2] == 0 )
356                     {
357                         rOptions.bSilent = sal_True;
358                     }
359                     else
360                     {
361                         rtl::OStringBuffer buf;
362                         buf.append( "Unknown error " );
363                         buf.append( av[i] );
364                         throw IllegalArgument( av[i] );
365                     }
366                     break;
367                 }
368                 case 'e':
369                 {
370                     if( av[i][2] == 'n' && av[i][3] == 'v' && av[i][4] == ':' )
371                     {
372                         // bootstrap variable, ignore it
373                         break;
374                     }
375                 }
376                 case 'w':
377                 {
378                     if (strcmp(av[i], "-wop") == 0)
379                     {
380                         rOptions.bPrefix = sal_True;
381                         rOptions.sPrefix = OUString();
382                             // in case there are multiple -wops
383                         break;
384                     }
385                     else if (
386                         strncmp(av[i], "-wop=", RTL_CONSTASCII_LENGTH("-wop="))
387                         == 0)
388                     {
389                         rOptions.bPrefix = sal_True;
390                         rOptions.sPrefix = OStringToOUString(
391                             av[i] + RTL_CONSTASCII_LENGTH("-wop="),
392                             osl_getThreadTextEncoding());
393                         break;
394                     }
395                 }
396                 default:
397                 {
398                     OString tmp( "unknown option " );
399                     tmp += av[i];
400                     throw IllegalArgument( tmp );
401                 }
402             }
403         } else
404         {
405             if (av[i][0] == '@')
406             {
407                 FILE* cmdFile = fopen(av[i]+1, "r");
408                 if( cmdFile == NULL )
409                 {
410                     usingRegisterImpl();
411                     ret = sal_False;
412                 } else
413                 {
414                     fseek( cmdFile , 0 , SEEK_END );
415                     sal_Int32 nLen = ftell( cmdFile);
416                     fseek( cmdFile, 0, SEEK_SET );
417 
418                     // 2 chars per string is a upper limit for the number of
419                     // substrings ( at least one separator char needed for fscanf).
420                     char ** rargv = (char **)rtl_allocateMemory( nLen * sizeof( char* ) /2);
421                     if( ! rargv )
422                     {
423                         OStringBuffer buf;
424                         buf.append( "Not enough memory for reading command file " );
425                         buf.append( av[i] +1 );
426                         buf.append( " with length " );
427                         buf.append( nLen );
428                         buf.append( "." );
429                         throw IllegalArgument( buf.makeStringAndClear() );
430                     }
431                     char *buffer = ( char * )rtl_allocateMemory( nLen +1 );
432                     if( ! buffer )
433                     {
434                         OStringBuffer buf;
435                         buf.append( "Not enough memory for reading command file " );
436                         buf.append( av[i] +1 );
437                         buf.append( " with length " );
438                         buf.append( nLen );
439                         buf.append( "." );
440                         throw IllegalArgument( buf.makeStringAndClear() );
441                     }
442 
443                     // we start at one to omit argv[0]
444                     sal_Int32 rargc = 1;
445                     rargv[0] = av[0];
446                     while ( fscanf(cmdFile, "%s", buffer) != EOF )
447                     {
448                         rargv[rargc]= (char * )rtl_allocateMemory( strlen( buffer ) +1 );
449                         if( ! rargv[rargc] )
450                         {
451                             OStringBuffer buf;
452                             buf.append( "Not enough memory for reading command file " );
453                             buf.append( av[i] +1 );
454                             buf.append( " with length " );
455                             buf.append( nLen );
456                             buf.append( "." );
457                             throw IllegalArgument( buf.makeStringAndClear() );
458                         }
459                         strcpy( rargv[rargc] , buffer ); // #100211# - checked
460                         rargc++;
461                     }
462                     fclose(cmdFile);
463 
464                     parseOptions(rargc, rargv, rOptions, bCmdFile);
465 
466                     for (long j=1; j < rargc; j++)
467                     {
468                         rtl_freeMemory(rargv[j]);
469                     }
470                     rtl_freeMemory( buffer );
471                     rtl_freeMemory( rargv );
472                 }
473             } else
474             {
475                 usingRegisterImpl();
476                 ret = sal_False;
477             }
478         }
479     }
480 
481     if( ! bLoaderExplicitlyGiven )
482     {
483         if ( rOptions.sComponentUrls.getLength() > 4 &&
484              rOptions.sComponentUrls.matchAsciiL(
485                  ".jar" , 4 , rOptions.sComponentUrls.getLength() - 4 ) )
486         {
487             if( ! rOptions.bSilent )
488             {
489                 printf( "using loader com.sun.star.loader.Java2\n" );
490             }
491             rOptions.sLoaderName = OUString(
492                 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java2"));
493         }
494         else
495         {
496             rOptions.sLoaderName = OUString(
497                 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") );
498         }
499     }
500 
501     return ret;
502 }
503 
504 
505 struct DoIt
506 {
507     sal_Bool                                _bRegister;
508     sal_Bool                                _bRevoke;
509     sal_Bool                                _bSilent;
510     sal_Bool                                _bPrefix;
511     OUString                                _sPrefix;
512     OString                                 _sRegName;
513     OUString                                _sLoaderName;
514     Reference<XImplementationRegistration2> _xImplRegistration;
515     Reference<XSimpleRegistry>              _xReg;
516     sal_uInt32                            * _exitCode;
517 
518     DoIt(sal_Bool bRegister,
519          sal_Bool bRevoke,
520          sal_Bool bSilent,
521          sal_Bool bPrefix,
522          const OUString & sPrefix,
523          const Reference<XSimpleRegistry> & xReg,
524          const OString & sRegName,
525          const Reference<XImplementationRegistration2> & xImplRegistration,
526          const OUString & sLoaderName,
527          sal_uInt32 * exitCode)
528         throw();
529 
530     void operator()(const OUString & url) throw();
531 };
532 
DoIt(sal_Bool bRegister,sal_Bool bRevoke,sal_Bool bSilent,sal_Bool bPrefix,const OUString & sPrefix,const Reference<XSimpleRegistry> & xReg,const OString & sRegName,const Reference<XImplementationRegistration2> & xImplRegistration,const OUString & sLoaderName,sal_uInt32 * exitCode)533 DoIt::DoIt(sal_Bool bRegister,
534            sal_Bool bRevoke,
535            sal_Bool bSilent,
536            sal_Bool bPrefix,
537            const OUString & sPrefix,
538            const Reference<XSimpleRegistry> & xReg,
539            const OString & sRegName,
540            const Reference<XImplementationRegistration2> & xImplRegistration,
541            const OUString & sLoaderName,
542            sal_uInt32 * exitCode) throw()
543     : _bRegister(bRegister),
544       _bRevoke(bRevoke),
545       _bSilent( bSilent ),
546       _bPrefix( bPrefix ),
547       _sPrefix( sPrefix ),
548       _sRegName(sRegName),
549       _sLoaderName(sLoaderName),
550       _xImplRegistration(xImplRegistration),
551       _xReg(xReg),
552       _exitCode(exitCode)
553 {}
554 
operator ()(const OUString & url)555 void DoIt::operator() (const OUString & url) throw()
556 {
557     OString sUrl = OUStringToOString(url, osl_getThreadTextEncoding());
558 
559     if (_bRegister)
560     {
561         try
562         {
563             Reference<XImplementationRegistration2> _xImplRegistration2(_xImplRegistration, UNO_QUERY);
564             if ( _bPrefix ) {
565                 _xImplRegistration->registerImplementationWithLocation(
566                     _sLoaderName, url, replacePrefix(url, _sPrefix), _xReg);
567             } else {
568                 _xImplRegistration->registerImplementation(_sLoaderName, url, _xReg);
569             }
570 
571             if ( ! _bSilent )
572             {
573                 fprintf(stderr, "register component '%s' in registry '%s' successful!\n", sUrl.getStr(), _sRegName.getStr());
574             }
575 
576         }
577         catch(CannotRegisterImplementationException & cannotRegisterImplementationException) {
578             OString aMessage(OUStringToOString(cannotRegisterImplementationException.Message, RTL_TEXTENCODING_ASCII_US));
579             fprintf(stderr, "register component '%s' in registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr());
580             fprintf(stderr, "error (CannotRegisterImplementationException): %s\n", aMessage.getStr());
581 
582             ++ (*_exitCode);
583         }
584         catch( RuntimeException & e )
585         {
586             OString aMessage(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
587             fprintf(stderr, "register component '%s' in registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr());
588             fprintf(stderr, "error (RuntimeException): %s\n", aMessage.getStr());
589 
590             ++ (*_exitCode);
591         }
592     }
593     else if(_bRevoke)
594     {
595         try
596         {
597             sal_Bool bRet = _xImplRegistration->revokeImplementation(url, _xReg);
598 
599             if (bRet)
600             {
601                 if ( ! _bSilent )
602                     fprintf(stderr, "revoke component '%s' from registry '%s' successful!\n", sUrl.getStr(), _sRegName.getStr());
603             }
604             else
605             {
606                 fprintf(stderr, "revoke component '%s' from registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr());
607                 ++ (*_exitCode);
608             }
609         }
610         catch( RuntimeException & e )
611         {
612             OString aMessage(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
613             fprintf( stderr,
614                      "revoke component '%s' from registry '%s' failed!\n",
615                      sUrl.getStr(),
616                      _sRegName.getStr() );
617             fprintf( stderr, "RuntimeException: %s\n" , aMessage.getStr());
618             ++ (*_exitCode);
619         }
620     }
621 }
622 
hasService(const Reference<XMultiServiceFactory> & xSMgr,const sal_Char * service)623 static bool hasService(
624     const Reference< XMultiServiceFactory > &xSMgr,
625     const sal_Char * service )
626 {
627     bool ret = false;
628 
629     Reference< XContentEnumerationAccess > access( xSMgr, UNO_QUERY );
630     if( access.is( ))
631     {
632         Reference< XEnumeration > enumeration = access->createContentEnumeration(
633             OUString::createFromAscii( service ) );
634 
635         if( enumeration.is() && enumeration->hasMoreElements() )
636         {
637             ret = true;
638         }
639     }
640     return ret;
641 }
642 
bootstrap(Options & opt,Reference<XMultiServiceFactory> & xSMgr,Reference<XSimpleRegistry> & reg)643 static void bootstrap(
644     Options & opt ,
645     Reference< XMultiServiceFactory > &xSMgr,
646     Reference< XSimpleRegistry > & reg )
647 {
648     if( opt.sRegName.equals( opt.sBootRegName2 ) )
649     {
650         OUString tmp2 = opt.sBootRegName;
651         opt.sBootRegName = opt.sBootRegName2;
652         opt.sBootRegName2 = tmp2;
653     }
654 
655     if ( opt.sRegName.equals(opt.sBootRegName) )
656     {
657         if( opt.sBootRegName2.getLength() )
658         {
659             xSMgr = createRegistryServiceFactory(
660                 convertToFileUrl(opt.sRegName),
661                 convertToFileUrl(opt.sBootRegName2),
662                 sal_False );
663         }
664         else
665         {
666             xSMgr = createRegistryServiceFactory(
667                 convertToFileUrl(opt.sRegName) , sal_False );
668         }
669     }
670     else
671     {
672         if( opt.sBootRegName2.getLength() )
673         {
674             xSMgr = createRegistryServiceFactory(
675                 convertToFileUrl( opt.sBootRegName2 ),
676                 convertToFileUrl( opt.sBootRegName ),
677                 sal_True );
678         }
679         else if ( opt.sBootRegName.getLength() )
680         {
681             xSMgr = createRegistryServiceFactory(
682                 convertToFileUrl( opt.sBootRegName ),
683                 sal_True );
684         }
685         else
686         {
687             xSMgr = createServiceFactory();
688         }
689         reg = Reference< XSimpleRegistry >(
690             xSMgr->createInstance(
691                 rtl::OUString::createFromAscii("com.sun.star.registry.SimpleRegistry")), UNO_QUERY);
692 
693         if (reg.is())
694         {
695             try
696             {
697                 reg->open( convertToFileUrl(opt.sRegName), sal_False, sal_True);
698                 if (!reg->isValid())
699                 {
700                     fprintf(stderr, "ERROR: open|create registry '%s' failed!\n",
701                             OUStringToOString(opt.sRegName, osl_getThreadTextEncoding() ).getStr());
702                     exit(1);
703                 }
704             }
705             catch( InvalidRegistryException & e)
706             {
707                 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
708                 fprintf(stderr,
709                         "ERROR: create registry '%s' failed!\n"
710                         "InvalidRegistryException: %s\n",
711                          OUStringToOString( opt.sRegName, osl_getThreadTextEncoding()).getStr(),
712                         o.getStr() );
713                 exit(1);
714             }
715         }
716     }
717 
718     if( ! opt.sLoaderName.compareToAscii( "com.sun.star.loader.Java2" ) &&
719         ! hasService( xSMgr, "com.sun.star.loader.Java2" ) )
720     {
721         // we know our java loader, so we check, whether a java-loader is
722         // registered
723         Reference< XInterface > r = loadSharedLibComponentFactory(
724             OUString::createFromAscii( "javavm.uno" SAL_DLLEXTENSION ),
725             OUString(),
726             OUString::createFromAscii( "com.sun.star.comp.stoc.JavaVirtualMachine" ),
727             xSMgr,
728             Reference< XRegistryKey > () );
729         Reference< XInterface > r2 = loadSharedLibComponentFactory(
730             OUString::createFromAscii( "javaloader.uno" SAL_DLLEXTENSION ),
731             OUString(),
732             OUString::createFromAscii(( "com.sun.star.comp.stoc.JavaComponentLoader" ) ),
733             xSMgr,
734             Reference< XRegistryKey > () );
735         Reference <XSet> xSet( xSMgr, UNO_QUERY );
736         if( r.is() && r2.is() && xSet.is() )
737         {
738             xSet->insert( makeAny( r ) );
739             xSet->insert( makeAny( r2 ) );
740         }
741     }
742 }
743 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)744 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
745 {
746     sal_Bool    bRet = sal_False;
747     sal_uInt32  exitCode = 0;
748     Options     aOptions;
749 
750     try
751     {
752         if ( !parseOptions(argc, argv, aOptions, sal_False) )
753         {
754             exit(1);
755         }
756     }
757     catch ( IllegalArgument& e)
758     {
759         fprintf(stderr, "ERROR: %s\n", e.m_message.getStr());
760         exit(1);
761     }
762 
763     if( ! aOptions.sRegName.getLength() )
764     {
765         fprintf( stderr, "ERROR: target registry missing (-r option)\n" );
766         exit( 1 );
767     }
768     if ( aOptions.sComponentUrls.getLength() == 0 )
769     {
770         fprintf(stderr, "ERROR: no component url is specified!\n");
771         exit(1);
772     }
773 
774     Reference< XMultiServiceFactory >   xSMgr;
775     Reference< XSimpleRegistry >        xReg;
776     try
777     {
778         bootstrap( aOptions, xSMgr ,xReg );
779     }
780     catch( Exception& e )
781     {
782         fprintf(stderr, "ERROR: create ServiceManager failed!\n");
783         if ( e.Message.getLength() )
784         {
785             fprintf(stderr, "ERROR description: %s\n",
786                     OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
787         }
788         exit(1);
789     }
790 
791     Reference<XImplementationRegistration2> xImplRegistration(
792         xSMgr->createInstance(
793             OUString(RTL_CONSTASCII_USTRINGPARAM(
794                          "com.sun.star.registry.ImplementationRegistration"))),
795         UNO_QUERY);
796 
797     if (xImplRegistration.is())
798     {
799         sal_Int32 index = 0;
800         vector<OUString> urls;
801 
802         OUString urlListWithSemikolon = aOptions.sComponentUrls;
803         do {
804             OUString aToken = urlListWithSemikolon.getToken( 0, ';', index);
805             fprintf(stderr, "%s\n", OUStringToOString(aToken, osl_getThreadTextEncoding()).getStr());
806             urls.push_back(aToken);
807         } while ( index >= 0 );
808 
809 
810         OString sRegName = OUStringToOString( aOptions.sRegName, osl_getThreadTextEncoding() );
811         if(aOptions.bRegister || aOptions.bRevoke)
812         {
813             for_each(urls.begin(), urls.end(),
814                      DoIt(aOptions.bRegister, aOptions.bRevoke, aOptions.bSilent,
815                           aOptions.bPrefix, aOptions.sPrefix,
816                           xReg, sRegName, xImplRegistration,
817                           aOptions.sLoaderName, &exitCode));
818         }
819         else
820         {
821             ++ exitCode;
822              usingRegisterImpl();
823         }
824     }
825     else
826     {
827         fprintf(stderr, "Component registration service could not be loaded!\n");
828         exitCode++;
829     }
830 
831     if (!bRet && xReg.is() && xReg->isValid())
832         xReg->close();
833 
834     Reference< XComponent > xComponent( xSMgr, UNO_QUERY );
835     if ( xComponent.is() )
836         xComponent->dispose();
837 
838     return exitCode;
839 }
840