Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <cassert>
23 : #include <cstdlib>
24 :
25 : #include <com/sun/star/loader/CannotActivateFactoryException.hpp>
26 : #include <cppuhelper/factory.hxx>
27 : #include <cppuhelper/shlib.hxx>
28 : #include <osl/module.hxx>
29 : #include <uno/environment.hxx>
30 : #include <uno/mapping.hxx>
31 :
32 : #include <loadsharedlibcomponentfactory.hxx>
33 :
34 : #if defined DISABLE_DYNLOADING
35 : #include <osl/detail/component-mapping.h>
36 : #endif
37 :
38 4 : css::uno::Environment cppuhelper::detail::getEnvironment(
39 : rtl::OUString const & name, rtl::OUString const & implementation)
40 : {
41 : assert(!implementation.isEmpty());
42 4 : rtl::OUString n(name);
43 4 : static char const * log = std::getenv("UNO_ENV_LOG");
44 4 : if (log != 0 && *log != 0) {
45 0 : rtl::OString imps(log);
46 0 : for (sal_Int32 i = 0; i != -1;) {
47 0 : rtl::OString imp(imps.getToken(0, ';', i));
48 : //TODO: this assumes UNO_ENV_LOG only contains ASCII characters:
49 0 : if (implementation.equalsAsciiL(imp.getStr(), imp.getLength())) {
50 0 : n += ":log";
51 0 : break;
52 : }
53 0 : }
54 : }
55 4 : return css::uno::Environment(n);
56 : }
57 :
58 : namespace {
59 :
60 : #if !defined DISABLE_DYNLOADING
61 :
62 4 : css::uno::Environment getEnvironmentFromModule(
63 : osl::Module const & module, css::uno::Environment const & target,
64 : rtl::OUString const & implementation, rtl::OUString const & prefix)
65 : {
66 4 : char const * name = 0;
67 4 : css::uno::Environment env;
68 8 : rtl::OUString fullPrefix(prefix);
69 4 : if (!fullPrefix.isEmpty()) {
70 4 : fullPrefix += "_";
71 : }
72 : component_getImplementationEnvironmentExtFunc fp1
73 : = reinterpret_cast<component_getImplementationEnvironmentExtFunc>(
74 4 : module.getFunctionSymbol(fullPrefix + COMPONENT_GETENVEXT));
75 4 : if (fp1 != 0) {
76 : (*fp1)(
77 : &name, reinterpret_cast<uno_Environment **>(&env),
78 : (rtl::OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US)
79 : .getStr()),
80 0 : target.get());
81 : } else {
82 : component_getImplementationEnvironmentFunc fp2
83 : = reinterpret_cast<component_getImplementationEnvironmentFunc>(
84 4 : module.getFunctionSymbol(fullPrefix + COMPONENT_GETENV));
85 4 : if (fp2 != 0) {
86 0 : (*fp2)(&name, reinterpret_cast<uno_Environment **>(&env));
87 : } else {
88 4 : name = CPPU_CURRENT_LANGUAGE_BINDING_NAME; //TODO: fail
89 : }
90 : }
91 4 : if (!env.is() && name != 0) {
92 8 : env = cppuhelper::detail::getEnvironment(
93 4 : rtl::OUString::createFromAscii(name), implementation);
94 : }
95 8 : return env;
96 : }
97 :
98 : #endif
99 :
100 0 : extern "C" void getFactory(va_list * args) {
101 0 : component_getFactoryFunc fn = va_arg(*args, component_getFactoryFunc);
102 0 : rtl::OString const * implementation = va_arg(*args, rtl::OString const *);
103 0 : void * smgr = va_arg(*args, void *);
104 0 : void * key = va_arg(*args, void *);
105 0 : void ** factory = va_arg(*args, void **);
106 0 : *factory = (*fn)(implementation->getStr(), smgr, key);
107 0 : }
108 :
109 4 : css::uno::Reference<css::uno::XInterface> invokeComponentFactory(
110 : css::uno::Environment const & source, css::uno::Environment const & target,
111 : component_getFactoryFunc function, rtl::OUString const & uri,
112 : rtl::OUString const & implementation,
113 : css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager)
114 : {
115 4 : if (!(source.is() && target.is())) {
116 : throw css::loader::CannotActivateFactoryException(
117 : "cannot get environments",
118 0 : css::uno::Reference<css::uno::XInterface>());
119 : }
120 : rtl::OString impl(
121 4 : rtl::OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US));
122 4 : if (source.get() == target.get()) {
123 : return css::uno::Reference<css::uno::XInterface>(
124 : static_cast<css::uno::XInterface *>(
125 4 : (*function)(impl.getStr(), serviceManager.get(), 0)),
126 4 : SAL_NO_ACQUIRE);
127 : } else {
128 0 : css::uno::Mapping mapTo(source, target);
129 0 : css::uno::Mapping mapFrom(target, source);
130 0 : if (!(mapTo.is() && mapFrom.is())) {
131 : throw css::loader::CannotActivateFactoryException(
132 : "cannot get mappings",
133 0 : css::uno::Reference<css::uno::XInterface>());
134 : }
135 : void * smgr = mapTo.mapInterface(
136 0 : serviceManager.get(),
137 0 : cppu::UnoType<css::lang::XMultiServiceFactory>::get());
138 0 : void * factory = 0;
139 0 : target.invoke(getFactory, function, &impl, smgr, 0, &factory);
140 0 : if (smgr != 0) {
141 0 : (*target.get()->pExtEnv->releaseInterface)(
142 0 : target.get()->pExtEnv, smgr);
143 : }
144 0 : if (factory == 0) {
145 : throw css::loader::CannotActivateFactoryException(
146 0 : ("calling factory function for \"" + implementation + "\" in <"
147 0 : + uri + "> returned null"),
148 0 : css::uno::Reference<css::uno::XInterface>());
149 : }
150 0 : css::uno::Reference<css::uno::XInterface> res;
151 : mapFrom.mapInterface(
152 : reinterpret_cast<void **>(&res), factory,
153 0 : cppu::UnoType<css::uno::XInterface>::get());
154 0 : (*target.get()->pExtEnv->releaseInterface)(
155 0 : target.get()->pExtEnv, factory);
156 0 : return res;
157 4 : }
158 : }
159 :
160 : }
161 :
162 4 : void cppuhelper::detail::loadSharedLibComponentFactory(
163 : rtl::OUString const & uri, rtl::OUString const & environment,
164 : rtl::OUString const & prefix, rtl::OUString const & implementation,
165 : rtl::OUString const & constructor,
166 : css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager,
167 : ImplementationConstructorFn ** constructorFunction,
168 : css::uno::Reference<css::uno::XInterface> * factory)
169 : {
170 : assert(constructor.isEmpty() || !environment.isEmpty());
171 : assert(
172 : (constructorFunction == 0 && constructor.isEmpty())
173 : || *constructorFunction == 0);
174 : assert(factory != 0 && !factory->is());
175 : #if defined DISABLE_DYNLOADING
176 : assert(!environment.isEmpty());
177 : if (constructor.isEmpty()) {
178 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
179 : css::uno::Environment env(getEnvironment(environment, implementation));
180 : if (!(curEnv.is() && env.is())) {
181 : throw css::loader::CannotActivateFactoryException(
182 : "cannot get environments",
183 : css::uno::Reference<css::uno::XInterface>());
184 : }
185 : if (curEnv.get() != env.get()) {
186 : std::abort();//TODO
187 : }
188 : rtl::OUString name(prefix == "direct" ? implementation : uri);
189 : SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
190 : lib_to_factory_mapping const * map = lo_get_factory_map();
191 : component_getFactoryFunc fp = 0;
192 : for (int i = 0; map[i].name != 0; ++i) {
193 : if (name.equalsAscii(map[i].name)) {
194 : fp = map[i].component_getFactory_function;
195 : break;
196 : }
197 : }
198 : if (fp == 0) {
199 : SAL_WARN("cppuhelper", "unknown factory name \"" << name << "\"");
200 : throw css::loader::CannotActivateFactoryException(
201 : "unknown factory name \"" + name + "\"",
202 : css::uno::Reference<css::uno::XInterface>());
203 : }
204 : *factory = invokeComponentFactory(
205 : css::uno::Environment::getCurrent(),
206 : getEnvironment(environment, implementation), fp, uri,
207 : implementation, serviceManager);
208 : } else {
209 : SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
210 : lib_to_constructor_mapping const * map = lo_get_constructor_map();
211 : for (int i = 0; map[i].name != 0; ++i) {
212 : if (constructor.equalsAscii(map[i].name)) {
213 : *constructorFunction
214 : = reinterpret_cast<ImplementationConstructorFn *>(
215 : map[i].constructor_function);
216 : return;
217 : }
218 : }
219 : SAL_WARN("cppuhelper", "unknown constructor name \"" << constructor << "\"");
220 : throw css::loader::CannotActivateFactoryException(
221 : "unknown constructor name \"" + constructor + "\"",
222 : css::uno::Reference<css::uno::XInterface>());
223 : }
224 : #else
225 4 : osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
226 4 : if (!mod.is()) {
227 : throw css::loader::CannotActivateFactoryException(
228 0 : "loading component library <" + uri + "> failed",
229 0 : css::uno::Reference<css::uno::XInterface>());
230 : }
231 4 : if (constructor.isEmpty()) {
232 4 : rtl::OUString sym;
233 4 : if (prefix == "direct") {
234 0 : sym = implementation.replace('.', '_') + "_" + COMPONENT_GETFACTORY;
235 4 : } else if (!prefix.isEmpty()) {
236 4 : sym = prefix + "_" + COMPONENT_GETFACTORY;
237 : } else {
238 0 : sym = COMPONENT_GETFACTORY;
239 : }
240 4 : oslGenericFunction fp = mod.getFunctionSymbol(sym);
241 4 : if (fp == 0) {
242 : throw css::loader::CannotActivateFactoryException(
243 0 : ("no factory symbol \"" + sym + "\" in component library <"
244 0 : + uri + ">"),
245 0 : css::uno::Reference<css::uno::XInterface>());
246 : }
247 8 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
248 8 : *factory = invokeComponentFactory(
249 : curEnv,
250 4 : (environment.isEmpty()
251 : ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
252 : : getEnvironment(environment, implementation)),
253 : reinterpret_cast<component_getFactoryFunc>(fp), uri, implementation,
254 8 : serviceManager);
255 : } else {
256 0 : oslGenericFunction fp = mod.getFunctionSymbol(constructor);
257 0 : if (fp == 0) {
258 : throw css::loader::CannotActivateFactoryException(
259 0 : ("no constructor symbol \"" + constructor
260 0 : + "\" in component library <" + uri + ">"),
261 0 : css::uno::Reference<css::uno::XInterface>());
262 : }
263 : *constructorFunction = reinterpret_cast<ImplementationConstructorFn *>(
264 0 : fp);
265 : }
266 4 : mod.release();
267 : #endif
268 4 : }
269 :
270 0 : css::uno::Reference<css::uno::XInterface> cppu::loadSharedLibComponentFactory(
271 : rtl::OUString const & uri, rtl::OUString const & rPath,
272 : rtl::OUString const & rImplName,
273 : css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
274 : css::uno::Reference<css::registry::XRegistryKey> const & xKey)
275 : SAL_THROW((css::loader::CannotActivateFactoryException))
276 : {
277 : assert(rPath.isEmpty()); (void) rPath;
278 : assert(!xKey.is()); (void) xKey;
279 0 : css::uno::Reference<css::uno::XInterface> fac;
280 : cppuhelper::detail::loadSharedLibComponentFactory(
281 0 : uri, "", "", rImplName, "", xMgr, 0, &fac);
282 0 : return fac;
283 : }
284 :
285 : #if !defined DISABLE_DYNLOADING
286 :
287 : namespace {
288 :
289 0 : extern "C" void writeInfo(va_list * args) {
290 0 : component_writeInfoFunc fn = va_arg(*args, component_writeInfoFunc);
291 0 : void * smgr = va_arg(*args, void *);
292 0 : void * key = va_arg(*args, void *);
293 0 : sal_Bool * ok = va_arg(*args, sal_Bool *);
294 0 : *ok = (*fn)(smgr, key);
295 0 : }
296 :
297 : }
298 :
299 0 : void cppu::writeSharedLibComponentInfo(
300 : rtl::OUString const & uri, rtl::OUString const & rPath,
301 : css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
302 : css::uno::Reference<css::registry::XRegistryKey> const & xKey)
303 : SAL_THROW((css::registry::CannotRegisterImplementationException))
304 : {
305 : assert(rPath.isEmpty()); (void) rPath;
306 0 : osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
307 0 : if (!mod.is()) {
308 : throw css::registry::CannotRegisterImplementationException(
309 0 : "loading component library <" + uri + "> failed",
310 0 : css::uno::Reference<css::uno::XInterface>());
311 : }
312 0 : oslGenericFunction fp = mod.getFunctionSymbol(COMPONENT_WRITEINFO);
313 0 : if (fp == 0) {
314 : throw css::registry::CannotRegisterImplementationException(
315 : ("no symbol \"" COMPONENT_WRITEINFO "\" in component library <"
316 0 : + uri + ">"),
317 0 : css::uno::Reference<css::uno::XInterface>());
318 : }
319 0 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
320 0 : css::uno::Environment env(getEnvironmentFromModule(mod, curEnv, "", ""));
321 0 : if (!(curEnv.is() && env.is())) {
322 : throw css::registry::CannotRegisterImplementationException(
323 : "cannot get environments",
324 0 : css::uno::Reference<css::uno::XInterface>());
325 : }
326 0 : css::uno::Mapping map(curEnv, env);
327 0 : if (!map.is()) {
328 : throw css::registry::CannotRegisterImplementationException(
329 0 : "cannot get mapping", css::uno::Reference<css::uno::XInterface>());
330 : }
331 : void * smgr = map.mapInterface(
332 0 : xMgr.get(), cppu::UnoType<css::lang::XMultiServiceFactory>::get());
333 : void * key = map.mapInterface(
334 0 : xKey.get(), cppu::UnoType<css::registry::XRegistryKey>::get());
335 : sal_Bool ok;
336 0 : env.invoke(writeInfo, fp, smgr, key, &ok);
337 0 : (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, key);
338 0 : if (smgr != 0) {
339 0 : (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, smgr);
340 : }
341 0 : if (!ok) {
342 : throw css::registry::CannotRegisterImplementationException(
343 0 : ("calling \"" COMPONENT_WRITEINFO "\" in component library <" + uri
344 0 : + "> returned false"),
345 0 : css::uno::Reference<css::uno::XInterface>());
346 0 : }
347 0 : }
348 :
349 : #endif
350 :
351 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|