xref: /AOO41X/main/ucb/source/ucp/hierarchy/hierarchyprovider.cxx (revision 2f86921c33504fdff5a030df6c0b258927045abb)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir 								TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  - XInitialization::initialize does not work any longer!
32cdf0e10cSrcweir 
33cdf0e10cSrcweir  *************************************************************************/
34cdf0e10cSrcweir #include <osl/diagnose.h>
35cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
38cdf0e10cSrcweir #include <com/sun/star/util/XOfficeInstallationDirectories.hpp>
39cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
40cdf0e10cSrcweir #include "hierarchyprovider.hxx"
41cdf0e10cSrcweir #include "hierarchycontent.hxx"
42cdf0e10cSrcweir #include "hierarchyuri.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "../inc/urihelper.hxx"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace com::sun::star;
47cdf0e10cSrcweir using namespace hierarchy_ucp;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //=========================================================================
50cdf0e10cSrcweir //=========================================================================
51cdf0e10cSrcweir //
52cdf0e10cSrcweir // HierarchyContentProvider Implementation.
53cdf0e10cSrcweir //
54cdf0e10cSrcweir //=========================================================================
55cdf0e10cSrcweir //=========================================================================
56cdf0e10cSrcweir 
HierarchyContentProvider(const uno::Reference<lang::XMultiServiceFactory> & rXSMgr)57cdf0e10cSrcweir HierarchyContentProvider::HierarchyContentProvider(
58cdf0e10cSrcweir             const uno::Reference< lang::XMultiServiceFactory >& rXSMgr )
59cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rXSMgr )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir //=========================================================================
64cdf0e10cSrcweir // virtual
~HierarchyContentProvider()65cdf0e10cSrcweir HierarchyContentProvider::~HierarchyContentProvider()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //=========================================================================
70cdf0e10cSrcweir //
71cdf0e10cSrcweir // XInterface methods.
72cdf0e10cSrcweir //
73cdf0e10cSrcweir //=========================================================================
74cdf0e10cSrcweir 
75cdf0e10cSrcweir XINTERFACE_IMPL_4( HierarchyContentProvider,
76cdf0e10cSrcweir                    lang::XTypeProvider,
77cdf0e10cSrcweir                    lang::XServiceInfo,
78cdf0e10cSrcweir                    ucb::XContentProvider,
79cdf0e10cSrcweir                    lang::XInitialization );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //=========================================================================
82cdf0e10cSrcweir //
83cdf0e10cSrcweir // XTypeProvider methods.
84cdf0e10cSrcweir //
85cdf0e10cSrcweir //=========================================================================
86cdf0e10cSrcweir 
87cdf0e10cSrcweir XTYPEPROVIDER_IMPL_4( HierarchyContentProvider,
88cdf0e10cSrcweir                       lang::XTypeProvider,
89cdf0e10cSrcweir                       lang::XServiceInfo,
90cdf0e10cSrcweir                       ucb::XContentProvider,
91cdf0e10cSrcweir                       lang::XInitialization );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //=========================================================================
94cdf0e10cSrcweir //
95cdf0e10cSrcweir // XServiceInfo methods.
96cdf0e10cSrcweir //
97cdf0e10cSrcweir //=========================================================================
98cdf0e10cSrcweir 
99cdf0e10cSrcweir XSERVICEINFO_IMPL_1( HierarchyContentProvider,
100cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
101cdf0e10cSrcweir 			 			"com.sun.star.comp.ucb.HierarchyContentProvider" ),
102cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
103cdf0e10cSrcweir 				 		HIERARCHY_CONTENT_PROVIDER_SERVICE_NAME ) );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir //=========================================================================
106cdf0e10cSrcweir //
107cdf0e10cSrcweir // Service factory implementation.
108cdf0e10cSrcweir //
109cdf0e10cSrcweir //=========================================================================
110cdf0e10cSrcweir 
111cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( HierarchyContentProvider );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //=========================================================================
114cdf0e10cSrcweir //
115cdf0e10cSrcweir // XContentProvider methods.
116cdf0e10cSrcweir //
117cdf0e10cSrcweir //=========================================================================
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // virtual
120cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL
queryContent(const uno::Reference<ucb::XContentIdentifier> & Identifier)121cdf0e10cSrcweir HierarchyContentProvider::queryContent(
122cdf0e10cSrcweir         const uno::Reference< ucb::XContentIdentifier >& Identifier )
123cdf0e10cSrcweir     throw( ucb::IllegalIdentifierException, uno::RuntimeException )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     HierarchyUri aUri( Identifier->getContentIdentifier() );
126cdf0e10cSrcweir     if ( !aUri.isValid() )
127cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	// Encode URL and create new Id. This may "correct" user-typed-in URL's.
130cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xCanonicId
131cdf0e10cSrcweir         = new ::ucbhelper::ContentIdentifier( m_xSMgr,
132cdf0e10cSrcweir                                               ::ucb_impl::urihelper::encodeURI(
133cdf0e10cSrcweir                                                   aUri.getUri() ) );
134cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     // Check, if a content with given id already exists...
137cdf0e10cSrcweir     uno::Reference< ucb::XContent > xContent
138cdf0e10cSrcweir 		= queryExistingContent( xCanonicId ).get();
139cdf0e10cSrcweir 	if ( xContent.is() )
140cdf0e10cSrcweir 		return xContent;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	// Create a new content.
143cdf0e10cSrcweir 	xContent = HierarchyContent::create( m_xSMgr, this, xCanonicId );
144cdf0e10cSrcweir     registerNewContent( xContent );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	if ( xContent.is() && !xContent->getIdentifier().is() )
147cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	return xContent;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir //=========================================================================
153cdf0e10cSrcweir //
154cdf0e10cSrcweir // XInitialization methods.
155cdf0e10cSrcweir //
156cdf0e10cSrcweir //=========================================================================
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // virtual
initialize(const uno::Sequence<uno::Any> & aArguments)159cdf0e10cSrcweir void SAL_CALL HierarchyContentProvider::initialize(
160cdf0e10cSrcweir                                 const uno::Sequence< uno::Any >& aArguments )
161cdf0e10cSrcweir     throw( uno::Exception, uno::RuntimeException )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir #if 0
164cdf0e10cSrcweir 	if ( aArguments.getLength() > 0 )
165cdf0e10cSrcweir 	{
166cdf0e10cSrcweir 	 	// Extract config provider from service init args.
167cdf0e10cSrcweir 	 	aArguments[ 0 ] >>= m_xConfigProvider;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         OSL_ENSURE( m_xConfigProvider.is(),
170cdf0e10cSrcweir 					"HierarchyContentProvider::initialize - "
171cdf0e10cSrcweir 					"No config provider!" );
172cdf0e10cSrcweir 	}
173cdf0e10cSrcweir #else
174cdf0e10cSrcweir 	if ( aArguments.getLength() > 0 )
175cdf0e10cSrcweir         OSL_ENSURE( false,
176cdf0e10cSrcweir                     "HierarchyContentProvider::initialize : not supported!" );
177cdf0e10cSrcweir #endif
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir //=========================================================================
181cdf0e10cSrcweir //
182cdf0e10cSrcweir //  Non-interface methods.
183cdf0e10cSrcweir //
184cdf0e10cSrcweir //=========================================================================
185cdf0e10cSrcweir 
186cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory >
getConfigProvider(const rtl::OUString & rServiceSpecifier)187cdf0e10cSrcweir HierarchyContentProvider::getConfigProvider(
188cdf0e10cSrcweir                                 const rtl::OUString & rServiceSpecifier )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
191cdf0e10cSrcweir     ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
192cdf0e10cSrcweir                                                     rServiceSpecifier );
193cdf0e10cSrcweir     if ( it == m_aConfigProviderMap.end() )
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         try
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             ConfigProviderMapEntry aEntry;
198cdf0e10cSrcweir             aEntry.xConfigProvider
199cdf0e10cSrcweir                 = uno::Reference< lang::XMultiServiceFactory >(
200cdf0e10cSrcweir                                 m_xSMgr->createInstance( rServiceSpecifier ),
201cdf0e10cSrcweir                                 uno::UNO_QUERY );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             if ( aEntry.xConfigProvider.is() )
204cdf0e10cSrcweir             {
205cdf0e10cSrcweir                 m_aConfigProviderMap[ rServiceSpecifier ] = aEntry;
206cdf0e10cSrcweir                 return aEntry.xConfigProvider;
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         catch ( uno::Exception const & )
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir //            OSL_ENSURE( sal_False,
212cdf0e10cSrcweir //                        "HierarchyContentProvider::getConfigProvider - "
213cdf0e10cSrcweir //                        "caught exception!" );
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         OSL_ENSURE( sal_False,
217cdf0e10cSrcweir                     "HierarchyContentProvider::getConfigProvider - "
218cdf0e10cSrcweir                     "No config provider!" );
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         return uno::Reference< lang::XMultiServiceFactory >();
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     return (*it).second.xConfigProvider;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //=========================================================================
227cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess >
getRootConfigReadNameAccess(const rtl::OUString & rServiceSpecifier)228cdf0e10cSrcweir HierarchyContentProvider::getRootConfigReadNameAccess(
229cdf0e10cSrcweir                                 const rtl::OUString & rServiceSpecifier )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
232cdf0e10cSrcweir     ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
233cdf0e10cSrcweir                                                     rServiceSpecifier );
234cdf0e10cSrcweir     if ( it != m_aConfigProviderMap.end() )
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         if ( !( (*it).second.xRootReadAccess.is() ) )
237cdf0e10cSrcweir 		{
238cdf0e10cSrcweir             if ( (*it).second.bTriedToGetRootReadAccess ) // #82494#
239cdf0e10cSrcweir 			{
240cdf0e10cSrcweir 				OSL_ENSURE( sal_False,
241cdf0e10cSrcweir 					"HierarchyContentProvider::getRootConfigReadNameAccess - "
242cdf0e10cSrcweir 					"Unable to read any config data! -> #82494#" );
243cdf0e10cSrcweir                 return uno::Reference< container::XHierarchicalNameAccess >();
244cdf0e10cSrcweir 			}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 			try
247cdf0e10cSrcweir 			{
248cdf0e10cSrcweir                 uno::Reference< lang::XMultiServiceFactory > xConfigProv
249cdf0e10cSrcweir                     = getConfigProvider( rServiceSpecifier );
250cdf0e10cSrcweir 
251cdf0e10cSrcweir                 if ( xConfigProv.is() )
252cdf0e10cSrcweir 				{
253cdf0e10cSrcweir                     uno::Sequence< uno::Any > aArguments( 1 );
254cdf0e10cSrcweir                     beans::PropertyValue      aProperty;
255cdf0e10cSrcweir                     aProperty.Name
256cdf0e10cSrcweir                         = rtl::OUString(
257cdf0e10cSrcweir                             RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
258cdf0e10cSrcweir                     aProperty.Value <<= rtl::OUString(); // root path
259cdf0e10cSrcweir                     aArguments[ 0 ] <<= aProperty;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir                     (*it).second.bTriedToGetRootReadAccess = true;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir                     (*it).second.xRootReadAccess
264cdf0e10cSrcweir                         = uno::Reference< container::XHierarchicalNameAccess >(
265cdf0e10cSrcweir                             xConfigProv->createInstanceWithArguments(
266cdf0e10cSrcweir                                 rtl::OUString(
267cdf0e10cSrcweir                                     RTL_CONSTASCII_USTRINGPARAM(
268cdf0e10cSrcweir                                         "com.sun.star.ucb."
269cdf0e10cSrcweir                                         "HierarchyDataReadAccess" ) ),
270cdf0e10cSrcweir                                 aArguments ),
271cdf0e10cSrcweir                             uno::UNO_QUERY );
272cdf0e10cSrcweir 				}
273cdf0e10cSrcweir 			}
274cdf0e10cSrcweir             catch ( uno::RuntimeException const & )
275cdf0e10cSrcweir 			{
276cdf0e10cSrcweir 				throw;
277cdf0e10cSrcweir 			}
278cdf0e10cSrcweir             catch ( uno::Exception const & )
279cdf0e10cSrcweir 			{
280cdf0e10cSrcweir 				// createInstance, createInstanceWithArguments
281cdf0e10cSrcweir 
282cdf0e10cSrcweir                 OSL_ENSURE( sal_False,
283cdf0e10cSrcweir 					"HierarchyContentProvider::getRootConfigReadNameAccess - "
284cdf0e10cSrcweir 					"caught Exception!" );
285cdf0e10cSrcweir 			}
286cdf0e10cSrcweir         }
287cdf0e10cSrcweir     }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     return (*it).second.xRootReadAccess;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir //=========================================================================
293cdf0e10cSrcweir uno::Reference< util::XOfficeInstallationDirectories >
getOfficeInstallationDirectories()294cdf0e10cSrcweir HierarchyContentProvider::getOfficeInstallationDirectories()
295cdf0e10cSrcweir {
296cdf0e10cSrcweir     if ( !m_xOfficeInstDirs.is() )
297cdf0e10cSrcweir     {
298cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aMutex );
299cdf0e10cSrcweir         if ( !m_xOfficeInstDirs.is() )
300cdf0e10cSrcweir         {
301cdf0e10cSrcweir             OSL_ENSURE( m_xSMgr.is(), "No service manager!" );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir             uno::Reference< uno::XComponentContext > xCtx;
304cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xPropSet(
305cdf0e10cSrcweir                 m_xSMgr, uno::UNO_QUERY );
306cdf0e10cSrcweir             if ( xPropSet.is() )
307cdf0e10cSrcweir             {
308cdf0e10cSrcweir                 xPropSet->getPropertyValue(
309cdf0e10cSrcweir                     rtl::OUString(
310cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) )
311cdf0e10cSrcweir                 >>= xCtx;
312cdf0e10cSrcweir             }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir             OSL_ENSURE( xCtx.is(),
315cdf0e10cSrcweir                         "Unable to obtain component context from "
316cdf0e10cSrcweir                         "service manager!" );
317cdf0e10cSrcweir 
318cdf0e10cSrcweir             if ( xCtx.is() )
319cdf0e10cSrcweir             {
320cdf0e10cSrcweir                 xCtx->getValueByName(
321cdf0e10cSrcweir                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
322cdf0e10cSrcweir                         "/singletons/"
323cdf0e10cSrcweir                         "com.sun.star.util.theOfficeInstallationDirectories" ) ) )
324cdf0e10cSrcweir                 >>= m_xOfficeInstDirs;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir // Be silent. singleton only available in an Office environment.
327cdf0e10cSrcweir //                OSL_ENSURE( m_xOfficeInstDirs.is(),
328cdf0e10cSrcweir //                            "Unable to obtain office directories singleton!" );
329cdf0e10cSrcweir             }
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir     return m_xOfficeInstDirs;
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335