merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / standalone / mscsfit / helper.cxx
blob86cacafa4a8b200e118ff4d3d94444c204075479
1 #include "helper.hxx"
2 #include "osl/diagnose.h"
3 #include "rtl/ustring.h"
5 /*-
6 * Helper : create a input stream from a file
7 */
8 Reference< XInputStream > createStreamFromFile( const OUString sFile )
10 const sal_Char* pcFile ;
11 OString aString ;
13 aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ;
14 pcFile = aString.getStr() ;
15 if( pcFile != NULL ) {
16 FILE *f = fopen( pcFile , "rb" );
17 Reference< XInputStream > r;
19 if( f ) {
20 fseek( f , 0 , SEEK_END );
21 int nLength = ftell( f );
22 fseek( f , 0 , SEEK_SET );
24 Sequence<sal_Int8> seqIn(nLength);
25 fread( seqIn.getArray() , nLength , 1 , f );
27 r = Reference< XInputStream > ( new OInputStream( seqIn ) );
28 fclose( f );
30 return r;
31 } else {
32 return NULL ;
35 return NULL ;
38 /*-
39 * Helper : set a output stream to a file
41 Reference< XOutputStream > createStreamToFile( const OUString sFile )
43 const sal_Char* pcFile ;
44 OString aString ;
46 aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ;
47 pcFile = aString.getStr() ;
48 if( pcFile != NULL )
49 return Reference< XOutputStream >( new OOutputStream( pcFile ) ) ;
50 else
51 return NULL ;
54 /*-
55 * Helper : get service manager and context
57 Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception )
59 Reference< XMultiComponentFactory > xLocalServiceManager = NULL ;
60 Reference< XComponentContext > xLocalComponentContext = NULL ;
61 Reference< XMultiComponentFactory > xUsedServiceManager = NULL ;
62 Reference< XComponentContext > xUsedComponentContext = NULL ;
64 OSL_ENSURE( !sUnoUrl.equalsAscii( "" ) ,
65 "serviceManager - "
66 "No uno URI specified" ) ;
68 OSL_ENSURE( !sRdbUrl.equalsAscii( "" ) ,
69 "serviceManager - "
70 "No rdb URI specified" ) ;
72 if( sUnoUrl.equalsAscii( "local" ) ) {
73 Reference< XSimpleRegistry > xSimpleRegistry = createSimpleRegistry();
74 OSL_ENSURE( xSimpleRegistry.is() ,
75 "serviceManager - "
76 "Cannot create simple registry" ) ;
78 //xSimpleRegistry->open(OUString::createFromAscii("xmlsecurity.rdb"), sal_False, sal_False);
79 xSimpleRegistry->open(sRdbUrl, sal_True, sal_False);
80 OSL_ENSURE( xSimpleRegistry->isValid() ,
81 "serviceManager - "
82 "Cannot open xml security registry rdb" ) ;
84 xLocalComponentContext = bootstrap_InitialComponentContext( xSimpleRegistry ) ;
85 OSL_ENSURE( xLocalComponentContext.is() ,
86 "serviceManager - "
87 "Cannot create intial component context" ) ;
89 xLocalServiceManager = xLocalComponentContext->getServiceManager() ;
90 OSL_ENSURE( xLocalServiceManager.is() ,
91 "serviceManager - "
92 "Cannot create intial service manager" ) ;
94 /*-
95 * Because of the exception rasied from
96 * ucbhelper/source/provider/provconf.cxx, lin 323
97 * I do not use the content broker at present
98 ********************************************************************
99 //init ucb
100 if( ::ucb::ContentBroker::get() == NULL ) {
101 Reference< lang::XMultiServiceFactory > xSvmg( xLocalServiceManager , UNO_QUERY ) ;
102 OSL_ENSURE( xLocalServiceManager.is() ,
103 "serviceManager - "
104 "Cannot get multi-service factory" ) ;
106 Sequence< Any > args( 2 ) ;
107 args[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL ) ;
108 args[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE ) ;
109 if( ! ::ucb::ContentBroker::initialize( xSvmg , args ) ) {
110 throw RuntimeException( OUString::createFromAscii( "Cannot inlitialize ContentBroker" ) , Reference< XInterface >() , Any() ) ;
113 ********************************************************************/
115 // MARKER(update_precomp.py): autogen include statement, do not remove
116 #include "precompiled_xmlsecurity.hxx"
118 xUsedComponentContext = xLocalComponentContext ;
119 xUsedServiceManager = xLocalServiceManager ;
120 } else {
121 Reference< XComponentContext > xLocalComponentContext = defaultBootstrap_InitialComponentContext() ;
122 OSL_ENSURE( xLocalComponentContext.is() ,
123 "serviceManager - "
124 "Cannot create intial component context" ) ;
126 Reference< XMultiComponentFactory > xLocalServiceManager = xLocalComponentContext->getServiceManager();
127 OSL_ENSURE( xLocalServiceManager.is() ,
128 "serviceManager - "
129 "Cannot create intial service manager" ) ;
131 Reference< XInterface > urlResolver =
132 xLocalServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver") , xLocalComponentContext ) ;
133 OSL_ENSURE( urlResolver.is() ,
134 "serviceManager - "
135 "Cannot get service instance of \"bridge.UnoUrlResolver\"" ) ;
137 Reference< XUnoUrlResolver > xUnoUrlResolver( urlResolver , UNO_QUERY ) ;
138 OSL_ENSURE( xUnoUrlResolver.is() ,
139 "serviceManager - "
140 "Cannot get interface of \"XUnoUrlResolver\" from service \"bridge.UnoUrlResolver\"" ) ;
142 Reference< XInterface > initialObject = xUnoUrlResolver->resolve( sUnoUrl ) ;
143 OSL_ENSURE( initialObject.is() ,
144 "serviceManager - "
145 "Cannot resolve uno url" ) ;
148 * Method 1: with Naming Service
149 ********************************************************************
150 Reference< XNamingService > xNamingService( initialObject , UNO_QUERY ) ;
151 OSL_ENSURE( xNamingService.is() ,
152 "serviceManager - "
153 "Cannot get interface of \"XNamingService\" from URL resolver" ) ;
155 Reference< XInterface > serviceManager =
156 xNamingService->getRegisteredObject( OUString::createFromAscii( "StarOffice.ServiceManager" ) ) ;
157 OSL_ENSURE( serviceManager.is() ,
158 "serviceManager - "
159 "Cannot get service instance of \"StarOffice.ServiceManager\"" ) ;
161 xUsedServiceManager = Reference< XMultiComponentFactory >( serviceManager , UNO_QUERY );
162 OSL_ENSURE( xUsedServiceManager.is() ,
163 "serviceManager - "
164 "Cannot get interface of \"XMultiComponentFactory\" from service \"StarOffice.ServiceManager\"" ) ;
166 Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ;
167 OSL_ENSURE( xPropSet.is() ,
168 "serviceManager - "
169 "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ;
171 xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
172 OSL_ENSURE( xUsedComponentContext.is() ,
173 "serviceManager - "
174 "Cannot create remote component context" ) ;
176 ********************************************************************/
179 * Method 2: with Componnent context
180 ********************************************************************
181 Reference< XPropertySet > xPropSet( initialObject , UNO_QUERY ) ;
182 OSL_ENSURE( xPropSet.is() ,
183 "serviceManager - "
184 "Cannot get interface of \"XPropertySet\" from URL resolver" ) ;
186 xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
187 OSL_ENSURE( xUsedComponentContext.is() ,
188 "serviceManager - "
189 "Cannot create remote component context" ) ;
191 xUsedServiceManager = xUsedComponentContext->getServiceManager();
192 OSL_ENSURE( xUsedServiceManager.is() ,
193 "serviceManager - "
194 "Cannot create remote service manager" ) ;
195 ********************************************************************/
198 * Method 3: with Service Manager
199 ********************************************************************/
200 xUsedServiceManager = Reference< XMultiComponentFactory >( initialObject , UNO_QUERY );
201 OSL_ENSURE( xUsedServiceManager.is() ,
202 "serviceManager - "
203 "Cannot create remote service manager" ) ;
205 Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ;
206 OSL_ENSURE( xPropSet.is() ,
207 "serviceManager - "
208 "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ;
210 xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
211 OSL_ENSURE( xUsedComponentContext.is() ,
212 "serviceManager - "
213 "Cannot create remote component context" ) ;
214 /********************************************************************/
217 xContext = xUsedComponentContext ;
218 return xUsedServiceManager ;