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