1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**************************************************************************
32*cdf0e10cSrcweir 								TODO
33*cdf0e10cSrcweir  **************************************************************************
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir  *************************************************************************/
36*cdf0e10cSrcweir #include <osl/diagnose.h>
37*cdf0e10cSrcweir #include <osl/mutex.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderManager.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandProcessor.hpp>
44*cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace com::sun::star::lang;
47*cdf0e10cSrcweir using namespace com::sun::star::ucb;
48*cdf0e10cSrcweir using namespace com::sun::star::uno;
49*cdf0e10cSrcweir using namespace rtl;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir namespace
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     osl::Mutex globalContentBrokerMutex;
54*cdf0e10cSrcweir     osl::Mutex & getGlobalContentBrokerMutex() { return globalContentBrokerMutex; }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir } // namespace
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace ucbhelper
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir //=========================================================================
62*cdf0e10cSrcweir //=========================================================================
63*cdf0e10cSrcweir //
64*cdf0e10cSrcweir // class ContentBroker_Impl.
65*cdf0e10cSrcweir //
66*cdf0e10cSrcweir //=========================================================================
67*cdf0e10cSrcweir //=========================================================================
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir class ContentBroker_Impl
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir     Reference< XMultiServiceFactory >       m_xSMgr;
72*cdf0e10cSrcweir     Reference< XContentIdentifierFactory >  m_xIdFac;
73*cdf0e10cSrcweir     Reference< XContentProvider >           m_xProvider;
74*cdf0e10cSrcweir     Reference< XContentProviderManager >    m_xProviderMgr;
75*cdf0e10cSrcweir     Reference< XCommandProcessor >          m_xCommandProc;
76*cdf0e10cSrcweir     osl::Mutex                              m_aMutex;
77*cdf0e10cSrcweir     Sequence< Any >                         m_aArguments;
78*cdf0e10cSrcweir     ContentProviderDataList                 m_aProvData;
79*cdf0e10cSrcweir     bool                                    m_bInitDone;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir public:
82*cdf0e10cSrcweir 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
83*cdf0e10cSrcweir 						const Sequence< Any >& rArguments )
84*cdf0e10cSrcweir     : m_xSMgr( rSMgr ), m_aArguments( rArguments ), m_bInitDone( sal_False )
85*cdf0e10cSrcweir 	{}
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
88*cdf0e10cSrcweir                         const ContentProviderDataList & rData )
89*cdf0e10cSrcweir     : m_xSMgr( rSMgr ), m_aProvData( rData ), m_bInitDone( sal_False )
90*cdf0e10cSrcweir 	{}
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	~ContentBroker_Impl();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     bool initialize();
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	const Reference< XMultiServiceFactory >& getServiceManager() const
97*cdf0e10cSrcweir 	{ return m_xSMgr; }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	const Reference< XContentIdentifierFactory >& getIdFactory() const
100*cdf0e10cSrcweir     { return m_xIdFac; }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	const Reference< XContentProvider >& getProvider() const
103*cdf0e10cSrcweir     { return m_xProvider; }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	const Reference< XContentProviderManager >& getProviderManager() const
106*cdf0e10cSrcweir     { return m_xProviderMgr; }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     const Reference< XCommandProcessor >& getCommandProcessor() const
109*cdf0e10cSrcweir     { return m_xCommandProc; }
110*cdf0e10cSrcweir };
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir //=========================================================================
113*cdf0e10cSrcweir //=========================================================================
114*cdf0e10cSrcweir //
115*cdf0e10cSrcweir // ContentBroker Implementation.
116*cdf0e10cSrcweir //
117*cdf0e10cSrcweir //=========================================================================
118*cdf0e10cSrcweir //=========================================================================
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir // static member!
121*cdf0e10cSrcweir ContentBroker* ContentBroker::m_pTheBroker = 0;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir //=========================================================================
124*cdf0e10cSrcweir ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
125*cdf0e10cSrcweir 							  const Sequence< Any >& rArguments )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	m_pImpl = new ContentBroker_Impl( rSMgr, rArguments );
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir //=========================================================================
131*cdf0e10cSrcweir ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
132*cdf0e10cSrcweir                               const ContentProviderDataList & rData )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     m_pImpl = new ContentBroker_Impl( rSMgr, rData );
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir //=========================================================================
138*cdf0e10cSrcweir ContentBroker::~ContentBroker()
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	delete m_pImpl;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir //=========================================================================
144*cdf0e10cSrcweir Reference< XMultiServiceFactory > ContentBroker::getServiceManager() const
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	return m_pImpl->getServiceManager();
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir //=========================================================================
150*cdf0e10cSrcweir Reference< XContentIdentifierFactory >
151*cdf0e10cSrcweir 				ContentBroker::getContentIdentifierFactoryInterface() const
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	return m_pImpl->getIdFactory();
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //=========================================================================
157*cdf0e10cSrcweir Reference< XContentProvider >
158*cdf0e10cSrcweir 				ContentBroker::getContentProviderInterface() const
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir 	return m_pImpl->getProvider();
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir //=========================================================================
164*cdf0e10cSrcweir Reference< XContentProviderManager >
165*cdf0e10cSrcweir                 ContentBroker::getContentProviderManagerInterface() const
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	return m_pImpl->getProviderManager();
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir //=========================================================================
171*cdf0e10cSrcweir Reference< XCommandProcessor >
172*cdf0e10cSrcweir                 ContentBroker::getCommandProcessorInterface() const
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir     return m_pImpl->getCommandProcessor();
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir //=========================================================================
178*cdf0e10cSrcweir // static
179*cdf0e10cSrcweir sal_Bool ContentBroker::initialize(
180*cdf0e10cSrcweir                         const Reference< XMultiServiceFactory >& rSMgr,
181*cdf0e10cSrcweir                         const Sequence< Any >& rArguments )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     OSL_ENSURE( !m_pTheBroker,
184*cdf0e10cSrcweir                 "ContentBroker::initialize - already initialized!" );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     if ( !m_pTheBroker )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         if ( !m_pTheBroker )
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             ContentBroker * pBroker = new ContentBroker( rSMgr, rArguments );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir             // Force init to be able to detect UCB init trouble immediately.
195*cdf0e10cSrcweir             if ( pBroker->m_pImpl->initialize() )
196*cdf0e10cSrcweir                 m_pTheBroker = pBroker;
197*cdf0e10cSrcweir             else
198*cdf0e10cSrcweir                 delete pBroker;
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     return m_pTheBroker != 0;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir //=========================================================================
206*cdf0e10cSrcweir // static
207*cdf0e10cSrcweir sal_Bool ContentBroker::initialize(
208*cdf0e10cSrcweir                         const Reference< XMultiServiceFactory >& rSMgr,
209*cdf0e10cSrcweir                         const ContentProviderDataList & rData )
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     OSL_ENSURE( !m_pTheBroker,
212*cdf0e10cSrcweir                 "ContentBroker::initialize - already initialized!" );
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     if ( !m_pTheBroker )
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         if ( !m_pTheBroker )
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             ContentBroker * pBroker = new ContentBroker( rSMgr, rData );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir             // Force init to be able to detect UCB init trouble immediately.
223*cdf0e10cSrcweir             if ( pBroker->m_pImpl->initialize() )
224*cdf0e10cSrcweir                 m_pTheBroker = pBroker;
225*cdf0e10cSrcweir             else
226*cdf0e10cSrcweir                 delete pBroker;
227*cdf0e10cSrcweir         }
228*cdf0e10cSrcweir     }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     return m_pTheBroker != 0;
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir //=========================================================================
234*cdf0e10cSrcweir // static
235*cdf0e10cSrcweir void ContentBroker::deinitialize()
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     osl::MutexGuard aGuard( getGlobalContentBrokerMutex() );
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 	delete m_pTheBroker;
240*cdf0e10cSrcweir 	m_pTheBroker = 0;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir //=========================================================================
244*cdf0e10cSrcweir // static
245*cdf0e10cSrcweir ContentBroker* ContentBroker::get()
246*cdf0e10cSrcweir {
247*cdf0e10cSrcweir 	return m_pTheBroker;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir //=========================================================================
251*cdf0e10cSrcweir //=========================================================================
252*cdf0e10cSrcweir //
253*cdf0e10cSrcweir // ContentBroker_Impl Implementation.
254*cdf0e10cSrcweir //
255*cdf0e10cSrcweir //=========================================================================
256*cdf0e10cSrcweir //=========================================================================
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir ContentBroker_Impl::~ContentBroker_Impl()
259*cdf0e10cSrcweir {
260*cdf0e10cSrcweir 	Reference< XComponent > xComponent(	m_xProvider, UNO_QUERY );
261*cdf0e10cSrcweir     if ( xComponent.is() )
262*cdf0e10cSrcweir 	{
263*cdf0e10cSrcweir 		m_xIdFac 	   = 0;
264*cdf0e10cSrcweir 		m_xProvider    = 0;
265*cdf0e10cSrcweir 		m_xProviderMgr = 0;
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 		xComponent->dispose();
268*cdf0e10cSrcweir 	}
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir //=========================================================================
272*cdf0e10cSrcweir bool ContentBroker_Impl::initialize()
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	if ( !m_bInitDone )
275*cdf0e10cSrcweir 	{
276*cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aMutex );
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         if ( !m_bInitDone )
279*cdf0e10cSrcweir         {
280*cdf0e10cSrcweir             Reference< XInterface > xIfc;
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir             if ( m_aProvData.size() > 0 )
283*cdf0e10cSrcweir             {
284*cdf0e10cSrcweir                 try
285*cdf0e10cSrcweir                 {
286*cdf0e10cSrcweir                     xIfc = m_xSMgr->createInstance(
287*cdf0e10cSrcweir                             OUString::createFromAscii(
288*cdf0e10cSrcweir                                 "com.sun.star.ucb.UniversalContentBroker" ) );
289*cdf0e10cSrcweir                 }
290*cdf0e10cSrcweir                 catch ( Exception const & )
291*cdf0e10cSrcweir                 {
292*cdf0e10cSrcweir                 }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir                 if ( xIfc.is() )
295*cdf0e10cSrcweir                 {
296*cdf0e10cSrcweir                     m_xProviderMgr
297*cdf0e10cSrcweir                         = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir                     if ( m_xProviderMgr.is() )
300*cdf0e10cSrcweir                     {
301*cdf0e10cSrcweir                         ContentProviderDataList::const_iterator aEnd(m_aProvData.end());
302*cdf0e10cSrcweir                         for (ContentProviderDataList::const_iterator aIt(m_aProvData.begin());
303*cdf0e10cSrcweir 		                    aIt != aEnd; ++aIt)
304*cdf0e10cSrcweir                     	{
305*cdf0e10cSrcweir                             registerAtUcb(m_xProviderMgr,
306*cdf0e10cSrcweir                                       m_xSMgr,
307*cdf0e10cSrcweir                                       aIt->ServiceName,
308*cdf0e10cSrcweir                                       aIt->Arguments,
309*cdf0e10cSrcweir                                       aIt->URLTemplate,
310*cdf0e10cSrcweir                                       0);
311*cdf0e10cSrcweir 	                    }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir                     }
314*cdf0e10cSrcweir                 }
315*cdf0e10cSrcweir             }
316*cdf0e10cSrcweir             else
317*cdf0e10cSrcweir             {
318*cdf0e10cSrcweir                 try
319*cdf0e10cSrcweir                 {
320*cdf0e10cSrcweir                     xIfc = m_xSMgr->createInstanceWithArguments(
321*cdf0e10cSrcweir                             OUString::createFromAscii(
322*cdf0e10cSrcweir                                 "com.sun.star.ucb.UniversalContentBroker" ),
323*cdf0e10cSrcweir                             m_aArguments );
324*cdf0e10cSrcweir                 }
325*cdf0e10cSrcweir                 catch ( Exception const & )
326*cdf0e10cSrcweir                 {
327*cdf0e10cSrcweir                 }
328*cdf0e10cSrcweir             }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir             OSL_ENSURE( xIfc.is(), "Error creating UCB service!" );
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir             if ( !xIfc.is() )
333*cdf0e10cSrcweir                 return false;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir             m_xIdFac
337*cdf0e10cSrcweir                 = Reference< XContentIdentifierFactory >( xIfc, UNO_QUERY );
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir             OSL_ENSURE( m_xIdFac.is(),
340*cdf0e10cSrcweir                 "UCB without required interface XContentIdentifierFactory!" );
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir             if ( !m_xIdFac.is() )
343*cdf0e10cSrcweir                 return false;
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir             m_xProvider = Reference< XContentProvider >( xIfc, UNO_QUERY );
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir             OSL_ENSURE( m_xProvider.is(),
348*cdf0e10cSrcweir                 "UCB without required interface XContentProvider!" );
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir             if ( !m_xProvider.is() )
351*cdf0e10cSrcweir                 return false;
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir             if ( !m_xProviderMgr.is() )
354*cdf0e10cSrcweir                 m_xProviderMgr
355*cdf0e10cSrcweir                     = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir             OSL_ENSURE( m_xProviderMgr.is(),
358*cdf0e10cSrcweir                 "UCB without required interface XContentProviderManager!" );
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir             if ( !m_xProviderMgr.is() )
361*cdf0e10cSrcweir                 return false;
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir             m_xCommandProc = Reference< XCommandProcessor >( xIfc, UNO_QUERY );
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir             OSL_ENSURE( m_xCommandProc.is(),
366*cdf0e10cSrcweir                 "UCB without required interface XCommandProcessor!" );
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir             if ( !m_xCommandProc.is() )
369*cdf0e10cSrcweir                 return false;
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir             // Everything okay.
372*cdf0e10cSrcweir             m_bInitDone = sal_True;
373*cdf0e10cSrcweir         }
374*cdf0e10cSrcweir 	}
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir     return true;
377*cdf0e10cSrcweir }
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir } /* namespace ucbhelper */
380*cdf0e10cSrcweir 
381