/cpputools/source/registercomponent/registercomponent.cxx

https://bitbucket.org/mst/ooo340 · C++ · 855 lines · 753 code · 65 blank · 37 comment · 118 complexity · e302e83a71d8cc38c6cbabdadf723c8c MD5 · raw file

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