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