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