xref: /trunk/main/ucb/source/core/provprox.cxx (revision cdf0e10c)
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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_ucb.hxx"
30 #include <osl/diagnose.h>
31 #include "provprox.hxx"
32 #include <com/sun/star/lang/XInitialization.hpp>
33 
34 using namespace rtl;
35 using namespace com::sun::star::lang;
36 using namespace com::sun::star::ucb;
37 using namespace com::sun::star::uno;
38 
39 //=========================================================================
40 //=========================================================================
41 //
42 // UcbContentProviderProxyFactory Implementation.
43 //
44 //=========================================================================
45 //=========================================================================
46 
47 UcbContentProviderProxyFactory::UcbContentProviderProxyFactory(
48 						const Reference< XMultiServiceFactory >& rxSMgr )
49 : m_xSMgr( rxSMgr )
50 {
51 }
52 
53 //=========================================================================
54 // virtual
55 UcbContentProviderProxyFactory::~UcbContentProviderProxyFactory()
56 {
57 }
58 
59 //=========================================================================
60 //
61 // XInterface methods.
62 //
63 //=========================================================================
64 
65 XINTERFACE_IMPL_3( UcbContentProviderProxyFactory,
66 				   XTypeProvider,
67 				   XServiceInfo,
68 				   XContentProviderFactory );
69 
70 //=========================================================================
71 //
72 // XTypeProvider methods.
73 //
74 //=========================================================================
75 
76 XTYPEPROVIDER_IMPL_3( UcbContentProviderProxyFactory,
77 	 		   		  XTypeProvider,
78 					  XServiceInfo,
79 	 		   		  XContentProviderFactory );
80 
81 //=========================================================================
82 //
83 // XServiceInfo methods.
84 //
85 //=========================================================================
86 
87 XSERVICEINFO_IMPL_1( UcbContentProviderProxyFactory,
88 					 OUString::createFromAscii(
89 		 				"com.sun.star.comp.ucb.UcbContentProviderProxyFactory" ),
90 					 OUString::createFromAscii(
91 		 				PROVIDER_FACTORY_SERVICE_NAME ) );
92 
93 //=========================================================================
94 //
95 // Service factory implementation.
96 //
97 //=========================================================================
98 
99 ONE_INSTANCE_SERVICE_FACTORY_IMPL( UcbContentProviderProxyFactory );
100 
101 //=========================================================================
102 //
103 // XContentProviderFactory methods.
104 //
105 //=========================================================================
106 
107 // virtual
108 Reference< XContentProvider > SAL_CALL
109 UcbContentProviderProxyFactory::createContentProvider(
110 												const OUString& Service )
111 	throw( RuntimeException )
112 {
113 	return Reference< XContentProvider >(
114 						new UcbContentProviderProxy( m_xSMgr, Service ) );
115 }
116 
117 //=========================================================================
118 //=========================================================================
119 //
120 // UcbContentProviderProxy Implementation.
121 //
122 //=========================================================================
123 //=========================================================================
124 
125 UcbContentProviderProxy::UcbContentProviderProxy(
126 						const Reference< XMultiServiceFactory >& rxSMgr,
127 						const OUString& Service )
128 : m_aService( Service ),
129   m_bReplace( sal_False ),
130   m_bRegister( sal_False ),
131   m_xSMgr( rxSMgr )
132 {
133 }
134 
135 //=========================================================================
136 // virtual
137 UcbContentProviderProxy::~UcbContentProviderProxy()
138 {
139 }
140 
141 //=========================================================================
142 //
143 // XInterface methods.
144 //
145 //=========================================================================
146 
147 XINTERFACE_COMMON_IMPL( UcbContentProviderProxy );
148 
149 //============================================================================
150 // virtual
151 Any SAL_CALL
152 UcbContentProviderProxy::queryInterface( const Type & rType )
153 	throw ( RuntimeException )
154 {
155 	Any aRet = cppu::queryInterface( rType,
156 				static_cast< XTypeProvider * >( this ),
157 				static_cast< XServiceInfo * >( this ),
158 				static_cast< XContentProvider * >( this ),
159 				static_cast< XParameterizedContentProvider * >( this ),
160 				static_cast< XContentProviderSupplier * >( this ) );
161 
162 	if ( !aRet.hasValue() )
163 		aRet = OWeakObject::queryInterface( rType );
164 
165 	if ( !aRet.hasValue() )
166 	{
167 		// Get original provider an forward the call...
168 		osl::Guard< osl::Mutex > aGuard( m_aMutex );
169         Reference< XContentProvider > xProvider = getContentProvider();
170 		if ( xProvider.is() )
171 			aRet = xProvider->queryInterface( rType );
172 	}
173 
174 	return aRet;
175 }
176 
177 //=========================================================================
178 //
179 // XTypeProvider methods.
180 //
181 //=========================================================================
182 
183 XTYPEPROVIDER_COMMON_IMPL( UcbContentProviderProxy );
184 
185 //=========================================================================
186 
187 Sequence< Type > SAL_CALL UcbContentProviderProxy::getTypes()                                                           \
188     throw( RuntimeException )
189 {
190     // Get original provider an forward the call...
191     osl::Guard< osl::Mutex > aGuard( m_aMutex );
192     Reference< XTypeProvider > xProvider( getContentProvider(), UNO_QUERY );
193     if ( xProvider.is() )
194     {
195         return xProvider->getTypes();
196     }
197     else
198     {
199         static cppu::OTypeCollection collection(
200             CPPU_TYPE_REF( XTypeProvider ),
201             CPPU_TYPE_REF( XServiceInfo ),
202             CPPU_TYPE_REF( XContentProvider ),
203             CPPU_TYPE_REF( XParameterizedContentProvider ),
204             CPPU_TYPE_REF( XContentProviderSupplier ) );
205         return collection.getTypes();
206     }
207 }
208 
209 //=========================================================================
210 //
211 // XServiceInfo methods.
212 //
213 //=========================================================================
214 
215 XSERVICEINFO_NOFACTORY_IMPL_1( UcbContentProviderProxy,
216 				 		   OUString::createFromAscii(
217 	 		   				"com.sun.star.comp.ucb.UcbContentProviderProxy" ),
218 				 		   OUString::createFromAscii(
219 	 						PROVIDER_PROXY_SERVICE_NAME ) );
220 
221 //=========================================================================
222 //
223 // XContentProvider methods.
224 //
225 //=========================================================================
226 
227 // virtual
228 Reference< XContent > SAL_CALL UcbContentProviderProxy::queryContent(
229 						const Reference< XContentIdentifier >& Identifier )
230 	throw( IllegalIdentifierException,
231 		   RuntimeException )
232 {
233 	// Get original provider an forward the call...
234 
235 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
236 
237 	Reference< XContentProvider > xProvider = getContentProvider();
238 	if ( xProvider.is() )
239 		return xProvider->queryContent( Identifier );
240 
241 	return Reference< XContent >();
242 }
243 
244 //=========================================================================
245 // virtual
246 sal_Int32 SAL_CALL UcbContentProviderProxy::compareContentIds(
247 					   const Reference< XContentIdentifier >& Id1,
248 					   const Reference< XContentIdentifier >& Id2 )
249 	throw( RuntimeException )
250 {
251 	// Get original provider an forward the call...
252 
253 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
254 	Reference< XContentProvider > xProvider = getContentProvider();
255 	if ( xProvider.is() )
256 		return xProvider->compareContentIds( Id1, Id2 );
257 
258     // OSL_ENSURE( sal_False,
259     // "UcbContentProviderProxy::compareContentIds - No provider!" );
260 
261 	// @@@ What else?
262 	return 0;
263 }
264 
265 //=========================================================================
266 //
267 // XParameterizedContentProvider methods.
268 //
269 //=========================================================================
270 
271 // virtual
272 Reference< XContentProvider > SAL_CALL
273 UcbContentProviderProxy::registerInstance( const OUString& Template,
274 					  					   const OUString& Arguments,
275 					  					   sal_Bool ReplaceExisting )
276 	throw( IllegalArgumentException,
277 		   RuntimeException )
278 {
279 	// Just remember that this method was called ( and the params ).
280 
281 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
282 
283 	if ( !m_bRegister )
284 	{
285 //		m_xTargetProvider = 0;
286 		m_aTemplate  = Template;
287 		m_aArguments = Arguments;
288 		m_bReplace   = ReplaceExisting;
289 
290 		m_bRegister  = sal_True;
291 	}
292 	return this;
293 }
294 
295 //=========================================================================
296 // virtual
297 Reference< XContentProvider > SAL_CALL
298 UcbContentProviderProxy::deregisterInstance( const OUString& Template,
299 											 const OUString& Arguments )
300 	throw( IllegalArgumentException,
301 		   RuntimeException )
302 {
303 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
304 
305 	// registerInstance called at proxy and at original?
306 	if ( m_bRegister && m_xTargetProvider.is() )
307 	{
308 		m_bRegister       = sal_False;
309 		m_xTargetProvider = 0;
310 
311 		Reference< XParameterizedContentProvider >
312 								xParamProvider( m_xProvider, UNO_QUERY );
313 		if ( xParamProvider.is() )
314 		{
315 			try
316 			{
317                 xParamProvider->deregisterInstance( Template, Arguments );
318 			}
319 			catch ( IllegalIdentifierException const & )
320 			{
321 				OSL_ENSURE( sal_False,
322 					"UcbContentProviderProxy::deregisterInstance - "
323 					"Caught IllegalIdentifierException!" );
324 			}
325 		}
326 	}
327 
328 	return this;
329 }
330 
331 //=========================================================================
332 //
333 // XContentProviderSupplier methods.
334 //
335 //=========================================================================
336 
337 // virtual
338 Reference< XContentProvider > SAL_CALL
339 UcbContentProviderProxy::getContentProvider()
340 	throw( RuntimeException )
341 {
342 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
343 	if ( !m_xProvider.is() )
344 	{
345 		try
346 		{
347 			m_xProvider
348 				= Reference< XContentProvider >(
349 				  	m_xSMgr->createInstance( m_aService ), UNO_QUERY );
350             if(m_aArguments.compareToAscii("NoConfig") == 0)
351             {
352                 Reference<XInitialization> xInit(m_xProvider,UNO_QUERY);
353                 if(xInit.is()) {
354                     Sequence<Any> aArgs(1);
355                     aArgs[0] <<= m_aArguments;
356                     xInit->initialize(aArgs);
357                 }
358             }
359 		}
360 		catch ( RuntimeException const & )
361 		{
362 			throw;
363 		}
364 		catch ( Exception const & )
365 		{
366 		}
367 
368 		// registerInstance called at proxy, but not yet at original?
369 		if ( m_xProvider.is() && m_bRegister )
370 		{
371 			Reference< XParameterizedContentProvider >
372 				xParamProvider( m_xProvider, UNO_QUERY );
373 			if ( xParamProvider.is() )
374 			{
375 				try
376 				{
377 					m_xTargetProvider
378 						= xParamProvider->registerInstance( m_aTemplate,
379 															m_aArguments,
380 															m_bReplace );
381 				}
382 				catch ( IllegalIdentifierException const & )
383 				{
384 					OSL_ENSURE( sal_False,
385 						"UcbContentProviderProxy::getContentProvider - "
386 						"Caught IllegalIdentifierException!" );
387 				}
388 
389 				OSL_ENSURE( m_xTargetProvider.is(),
390 					"UcbContentProviderProxy::getContentProvider - "
391 					"No provider!" );
392 			}
393 		}
394 		if ( !m_xTargetProvider.is() )
395 			m_xTargetProvider = m_xProvider;
396 	}
397 
398 	OSL_ENSURE( m_xProvider.is(),
399 				"UcbContentProviderProxy::getContentProvider - No provider!" );
400 	return m_xTargetProvider;
401 }
402