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