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_ucbhelper.hxx"
30 
31 /**************************************************************************
32 								TODO
33  **************************************************************************
34 
35  *************************************************************************/
36 #include <osl/diagnose.h>
37 #include <osl/mutex.hxx>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
41 #include <com/sun/star/ucb/XContentProvider.hpp>
42 #include <com/sun/star/ucb/XContentProviderManager.hpp>
43 #include <com/sun/star/ucb/XCommandProcessor.hpp>
44 #include <ucbhelper/contentbroker.hxx>
45 
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::ucb;
48 using namespace com::sun::star::uno;
49 using namespace rtl;
50 
51 namespace
52 {
53     osl::Mutex globalContentBrokerMutex;
54     osl::Mutex & getGlobalContentBrokerMutex() { return globalContentBrokerMutex; }
55 
56 } // namespace
57 
58 namespace ucbhelper
59 {
60 
61 //=========================================================================
62 //=========================================================================
63 //
64 // class ContentBroker_Impl.
65 //
66 //=========================================================================
67 //=========================================================================
68 
69 class ContentBroker_Impl
70 {
71     Reference< XMultiServiceFactory >       m_xSMgr;
72     Reference< XContentIdentifierFactory >  m_xIdFac;
73     Reference< XContentProvider >           m_xProvider;
74     Reference< XContentProviderManager >    m_xProviderMgr;
75     Reference< XCommandProcessor >          m_xCommandProc;
76     osl::Mutex                              m_aMutex;
77     Sequence< Any >                         m_aArguments;
78     ContentProviderDataList                 m_aProvData;
79     bool                                    m_bInitDone;
80 
81 public:
82 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
83 						const Sequence< Any >& rArguments )
84     : m_xSMgr( rSMgr ), m_aArguments( rArguments ), m_bInitDone( sal_False )
85 	{}
86 
87 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
88                         const ContentProviderDataList & rData )
89     : m_xSMgr( rSMgr ), m_aProvData( rData ), m_bInitDone( sal_False )
90 	{}
91 
92 	~ContentBroker_Impl();
93 
94     bool initialize();
95 
96 	const Reference< XMultiServiceFactory >& getServiceManager() const
97 	{ return m_xSMgr; }
98 
99 	const Reference< XContentIdentifierFactory >& getIdFactory() const
100     { return m_xIdFac; }
101 
102 	const Reference< XContentProvider >& getProvider() const
103     { return m_xProvider; }
104 
105 	const Reference< XContentProviderManager >& getProviderManager() const
106     { return m_xProviderMgr; }
107 
108     const Reference< XCommandProcessor >& getCommandProcessor() const
109     { return m_xCommandProc; }
110 };
111 
112 //=========================================================================
113 //=========================================================================
114 //
115 // ContentBroker Implementation.
116 //
117 //=========================================================================
118 //=========================================================================
119 
120 // static member!
121 ContentBroker* ContentBroker::m_pTheBroker = 0;
122 
123 //=========================================================================
124 ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
125 							  const Sequence< Any >& rArguments )
126 {
127 	m_pImpl = new ContentBroker_Impl( rSMgr, rArguments );
128 }
129 
130 //=========================================================================
131 ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
132                               const ContentProviderDataList & rData )
133 {
134     m_pImpl = new ContentBroker_Impl( rSMgr, rData );
135 }
136 
137 //=========================================================================
138 ContentBroker::~ContentBroker()
139 {
140 	delete m_pImpl;
141 }
142 
143 //=========================================================================
144 Reference< XMultiServiceFactory > ContentBroker::getServiceManager() const
145 {
146 	return m_pImpl->getServiceManager();
147 }
148 
149 //=========================================================================
150 Reference< XContentIdentifierFactory >
151 				ContentBroker::getContentIdentifierFactoryInterface() const
152 {
153 	return m_pImpl->getIdFactory();
154 }
155 
156 //=========================================================================
157 Reference< XContentProvider >
158 				ContentBroker::getContentProviderInterface() const
159 {
160 	return m_pImpl->getProvider();
161 }
162 
163 //=========================================================================
164 Reference< XContentProviderManager >
165                 ContentBroker::getContentProviderManagerInterface() const
166 {
167 	return m_pImpl->getProviderManager();
168 }
169 
170 //=========================================================================
171 Reference< XCommandProcessor >
172                 ContentBroker::getCommandProcessorInterface() const
173 {
174     return m_pImpl->getCommandProcessor();
175 }
176 
177 //=========================================================================
178 // static
179 sal_Bool ContentBroker::initialize(
180                         const Reference< XMultiServiceFactory >& rSMgr,
181                         const Sequence< Any >& rArguments )
182 {
183     OSL_ENSURE( !m_pTheBroker,
184                 "ContentBroker::initialize - already initialized!" );
185 
186     if ( !m_pTheBroker )
187     {
188         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
189 
190         if ( !m_pTheBroker )
191         {
192             ContentBroker * pBroker = new ContentBroker( rSMgr, rArguments );
193 
194             // Force init to be able to detect UCB init trouble immediately.
195             if ( pBroker->m_pImpl->initialize() )
196                 m_pTheBroker = pBroker;
197             else
198                 delete pBroker;
199         }
200     }
201 
202     return m_pTheBroker != 0;
203 }
204 
205 //=========================================================================
206 // static
207 sal_Bool ContentBroker::initialize(
208                         const Reference< XMultiServiceFactory >& rSMgr,
209                         const ContentProviderDataList & rData )
210 {
211     OSL_ENSURE( !m_pTheBroker,
212                 "ContentBroker::initialize - already initialized!" );
213 
214     if ( !m_pTheBroker )
215     {
216         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
217 
218         if ( !m_pTheBroker )
219         {
220             ContentBroker * pBroker = new ContentBroker( rSMgr, rData );
221 
222             // Force init to be able to detect UCB init trouble immediately.
223             if ( pBroker->m_pImpl->initialize() )
224                 m_pTheBroker = pBroker;
225             else
226                 delete pBroker;
227         }
228     }
229 
230     return m_pTheBroker != 0;
231 }
232 
233 //=========================================================================
234 // static
235 void ContentBroker::deinitialize()
236 {
237     osl::MutexGuard aGuard( getGlobalContentBrokerMutex() );
238 
239 	delete m_pTheBroker;
240 	m_pTheBroker = 0;
241 }
242 
243 //=========================================================================
244 // static
245 ContentBroker* ContentBroker::get()
246 {
247 	return m_pTheBroker;
248 }
249 
250 //=========================================================================
251 //=========================================================================
252 //
253 // ContentBroker_Impl Implementation.
254 //
255 //=========================================================================
256 //=========================================================================
257 
258 ContentBroker_Impl::~ContentBroker_Impl()
259 {
260 	Reference< XComponent > xComponent(	m_xProvider, UNO_QUERY );
261     if ( xComponent.is() )
262 	{
263 		m_xIdFac 	   = 0;
264 		m_xProvider    = 0;
265 		m_xProviderMgr = 0;
266 
267 		xComponent->dispose();
268 	}
269 }
270 
271 //=========================================================================
272 bool ContentBroker_Impl::initialize()
273 {
274 	if ( !m_bInitDone )
275 	{
276         osl::MutexGuard aGuard( m_aMutex );
277 
278         if ( !m_bInitDone )
279         {
280             Reference< XInterface > xIfc;
281 
282             if ( m_aProvData.size() > 0 )
283             {
284                 try
285                 {
286                     xIfc = m_xSMgr->createInstance(
287                             OUString::createFromAscii(
288                                 "com.sun.star.ucb.UniversalContentBroker" ) );
289                 }
290                 catch ( Exception const & )
291                 {
292                 }
293 
294                 if ( xIfc.is() )
295                 {
296                     m_xProviderMgr
297                         = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
298 
299                     if ( m_xProviderMgr.is() )
300                     {
301                         ContentProviderDataList::const_iterator aEnd(m_aProvData.end());
302                         for (ContentProviderDataList::const_iterator aIt(m_aProvData.begin());
303 		                    aIt != aEnd; ++aIt)
304                     	{
305                             registerAtUcb(m_xProviderMgr,
306                                       m_xSMgr,
307                                       aIt->ServiceName,
308                                       aIt->Arguments,
309                                       aIt->URLTemplate,
310                                       0);
311 	                    }
312 
313                     }
314                 }
315             }
316             else
317             {
318                 try
319                 {
320                     xIfc = m_xSMgr->createInstanceWithArguments(
321                             OUString::createFromAscii(
322                                 "com.sun.star.ucb.UniversalContentBroker" ),
323                             m_aArguments );
324                 }
325                 catch ( Exception const & )
326                 {
327                 }
328             }
329 
330             OSL_ENSURE( xIfc.is(), "Error creating UCB service!" );
331 
332             if ( !xIfc.is() )
333                 return false;
334 
335 
336             m_xIdFac
337                 = Reference< XContentIdentifierFactory >( xIfc, UNO_QUERY );
338 
339             OSL_ENSURE( m_xIdFac.is(),
340                 "UCB without required interface XContentIdentifierFactory!" );
341 
342             if ( !m_xIdFac.is() )
343                 return false;
344 
345             m_xProvider = Reference< XContentProvider >( xIfc, UNO_QUERY );
346 
347             OSL_ENSURE( m_xProvider.is(),
348                 "UCB without required interface XContentProvider!" );
349 
350             if ( !m_xProvider.is() )
351                 return false;
352 
353             if ( !m_xProviderMgr.is() )
354                 m_xProviderMgr
355                     = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
356 
357             OSL_ENSURE( m_xProviderMgr.is(),
358                 "UCB without required interface XContentProviderManager!" );
359 
360             if ( !m_xProviderMgr.is() )
361                 return false;
362 
363             m_xCommandProc = Reference< XCommandProcessor >( xIfc, UNO_QUERY );
364 
365             OSL_ENSURE( m_xCommandProc.is(),
366                 "UCB without required interface XCommandProcessor!" );
367 
368             if ( !m_xCommandProc.is() )
369                 return false;
370 
371             // Everything okay.
372             m_bInitDone = sal_True;
373         }
374 	}
375 
376     return true;
377 }
378 
379 } /* namespace ucbhelper */
380 
381