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