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 #include <ucbhelper/registerucb.hxx>
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderManager.hpp>
29cdf0e10cSrcweir #include <com/sun/star/ucb/XParameterizedContentProvider.hpp>
30cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderFactory.hpp>
31cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "osl/diagnose.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace ucbhelper {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //============================================================================
40cdf0e10cSrcweir //
41cdf0e10cSrcweir //  registerAtUcb
42cdf0e10cSrcweir //
43cdf0e10cSrcweir //============================================================================
44cdf0e10cSrcweir 
45cdf0e10cSrcweir bool
registerAtUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,uno::Reference<lang::XMultiServiceFactory> const & rServiceFactory,rtl::OUString const & rName,rtl::OUString const & rArguments,rtl::OUString const & rTemplate,ContentProviderRegistrationInfo * pInfo)46cdf0e10cSrcweir registerAtUcb(
47cdf0e10cSrcweir     uno::Reference< ucb::XContentProviderManager > const & rManager,
48cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
49cdf0e10cSrcweir 	rtl::OUString const & rName,
50cdf0e10cSrcweir 	rtl::OUString const & rArguments,
51cdf0e10cSrcweir 	rtl::OUString const & rTemplate,
52cdf0e10cSrcweir 	ContentProviderRegistrationInfo * pInfo)
53cdf0e10cSrcweir 	throw (uno::RuntimeException)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 	OSL_ENSURE(rServiceFactory.is(),
56cdf0e10cSrcweir 			   "ucb::registerAtUcb(): No service factory");
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	bool bNoProxy
59cdf0e10cSrcweir 		= rArguments.compareToAscii(RTL_CONSTASCII_STRINGPARAM("{noproxy}"))
60cdf0e10cSrcweir 		      == 0;
61cdf0e10cSrcweir 	rtl::OUString
62cdf0e10cSrcweir 		aProviderArguments(bNoProxy ?
63cdf0e10cSrcweir 						       rArguments.
64cdf0e10cSrcweir 						           copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
65cdf0e10cSrcweir 						       rArguments);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	// First, try to instantiate proxy for provider:
68cdf0e10cSrcweir 	uno::Reference< ucb::XContentProvider > xProvider;
69cdf0e10cSrcweir 	if (!bNoProxy)
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
72cdf0e10cSrcweir 		try
73cdf0e10cSrcweir 		{
74cdf0e10cSrcweir 			xProxyFactory
75cdf0e10cSrcweir 				= uno::Reference< ucb::XContentProviderFactory >(
76cdf0e10cSrcweir 					  rServiceFactory->
77cdf0e10cSrcweir 				          createInstance(
78cdf0e10cSrcweir 							  rtl::OUString(
79cdf0e10cSrcweir 								  RTL_CONSTASCII_USTRINGPARAM(
80cdf0e10cSrcweir 							"com.sun.star.ucb.ContentProviderProxyFactory"))),
81cdf0e10cSrcweir 					  uno::UNO_QUERY);
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 		catch (uno::Exception const &) {}
84cdf0e10cSrcweir 		OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
85cdf0e10cSrcweir 		if (xProxyFactory.is())
86cdf0e10cSrcweir 			xProvider = xProxyFactory->createContentProvider(rName);
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	// Then, try to instantiate provider directly:
90cdf0e10cSrcweir 	if (!xProvider.is())
91cdf0e10cSrcweir 		try
92cdf0e10cSrcweir 		{
93cdf0e10cSrcweir 			xProvider = uno::Reference< ucb::XContentProvider >(
94cdf0e10cSrcweir 				            rServiceFactory->createInstance(rName),
95cdf0e10cSrcweir 							uno::UNO_QUERY);
96cdf0e10cSrcweir 		}
97cdf0e10cSrcweir 		catch (uno::RuntimeException const &) { throw; }
98cdf0e10cSrcweir 		catch (uno::Exception const &) {}
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	uno::Reference< ucb::XContentProvider >
101cdf0e10cSrcweir 		xOriginalProvider(xProvider);
102cdf0e10cSrcweir 	uno::Reference< ucb::XParameterizedContentProvider >
103cdf0e10cSrcweir 		xParameterized(xProvider, uno::UNO_QUERY);
104cdf0e10cSrcweir 	if (xParameterized.is())
105cdf0e10cSrcweir 	{
106cdf0e10cSrcweir 		uno::Reference< ucb::XContentProvider > xInstance;
107cdf0e10cSrcweir 		try
108cdf0e10cSrcweir 		{
109cdf0e10cSrcweir 			xInstance = xParameterized->registerInstance(rTemplate,
110cdf0e10cSrcweir 														 aProviderArguments,
111cdf0e10cSrcweir 														 true);
112cdf0e10cSrcweir 				//@@@ if this call replaces an old instance, the commit-or-
113cdf0e10cSrcweir 				// rollback code below will not work
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 		catch (lang::IllegalArgumentException const &) {}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		if (xInstance.is())
118cdf0e10cSrcweir 			xProvider = xInstance;
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	bool bSuccess = false;
122cdf0e10cSrcweir 	if (rManager.is() && xProvider.is())
123cdf0e10cSrcweir 		try
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			rManager->registerContentProvider(xProvider, rTemplate, true);
126cdf0e10cSrcweir 			bSuccess = true;
127cdf0e10cSrcweir 		}
128cdf0e10cSrcweir 		catch (ucb::DuplicateProviderException const &)
129cdf0e10cSrcweir 		{
130cdf0e10cSrcweir 			if (xParameterized.is())
131cdf0e10cSrcweir 				try
132cdf0e10cSrcweir 				{
133cdf0e10cSrcweir 					xParameterized->deregisterInstance(rTemplate,
134cdf0e10cSrcweir 													   aProviderArguments);
135cdf0e10cSrcweir 				}
136cdf0e10cSrcweir 				catch (lang::IllegalArgumentException const &) {}
137cdf0e10cSrcweir 		}
138cdf0e10cSrcweir 		catch (...)
139cdf0e10cSrcweir 		{
140cdf0e10cSrcweir 			if (xParameterized.is())
141cdf0e10cSrcweir 				try
142cdf0e10cSrcweir 				{
143cdf0e10cSrcweir 					xParameterized->deregisterInstance(rTemplate,
144cdf0e10cSrcweir 													   aProviderArguments);
145cdf0e10cSrcweir 				}
146cdf0e10cSrcweir 				catch (lang::IllegalArgumentException const &) {}
147cdf0e10cSrcweir 				catch (uno::RuntimeException const &) {}
148cdf0e10cSrcweir 			throw;
149cdf0e10cSrcweir 		}
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	if (bSuccess && pInfo)
152cdf0e10cSrcweir 	{
153cdf0e10cSrcweir 		pInfo->m_xProvider = xOriginalProvider;
154cdf0e10cSrcweir 		pInfo->m_aArguments = aProviderArguments;
155cdf0e10cSrcweir 		pInfo->m_aTemplate = rTemplate;
156cdf0e10cSrcweir 	}
157cdf0e10cSrcweir 	return bSuccess;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir }
161