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