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 "framework/ModuleController.hxx"
21 :
22 : #include "tools/ConfigurationAccess.hxx"
23 : #include <comphelper/processfactory.hxx>
24 : #include <boost/bind.hpp>
25 : #include <unordered_map>
26 :
27 : #include <tools/diagnose_ex.h>
28 :
29 : #include <facreg.hxx>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 : using namespace ::com::sun::star::drawing::framework;
34 : using ::sd::tools::ConfigurationAccess;
35 :
36 : namespace sd { namespace framework {
37 :
38 : static const sal_uInt32 snFactoryPropertyCount (2);
39 : static const sal_uInt32 snStartupPropertyCount (1);
40 :
41 127 : class ModuleController::ResourceToFactoryMap
42 : : public std::unordered_map<
43 : OUString,
44 : OUString,
45 : OUStringHash>
46 : {
47 : public:
48 127 : ResourceToFactoryMap() {}
49 : };
50 :
51 127 : class ModuleController::LoadedFactoryContainer
52 : : public std::unordered_map<
53 : OUString,
54 : WeakReference<XInterface>,
55 : OUStringHash>
56 : {
57 : public:
58 127 : LoadedFactoryContainer() {}
59 : };
60 :
61 : //===== ModuleController ======================================================
62 127 : Reference<XModuleController> ModuleController::CreateInstance (
63 : const Reference<XComponentContext>& rxContext)
64 : {
65 127 : return new ModuleController(rxContext);
66 : }
67 :
68 127 : ModuleController::ModuleController (const Reference<XComponentContext>& rxContext)
69 : throw (std::exception)
70 : : ModuleControllerInterfaceBase(MutexOwner::maMutex),
71 : mxController(),
72 0 : mpResourceToFactoryMap(new ResourceToFactoryMap()),
73 127 : mpLoadedFactories(new LoadedFactoryContainer())
74 : {
75 : (void)rxContext;
76 127 : LoadFactories(rxContext);
77 127 : }
78 :
79 168 : ModuleController::~ModuleController() throw()
80 : {
81 168 : }
82 :
83 127 : void SAL_CALL ModuleController::disposing()
84 : {
85 : // Break the cyclic reference back to DrawController object
86 127 : mpLoadedFactories.reset();
87 127 : mpResourceToFactoryMap.reset();
88 127 : mxController.clear();
89 127 : }
90 :
91 127 : void ModuleController::LoadFactories (const Reference<XComponentContext>& rxContext)
92 : {
93 : try
94 : {
95 : ConfigurationAccess aConfiguration (
96 : rxContext,
97 : "/org.openoffice.Office.Impress/",
98 127 : ConfigurationAccess::READ_ONLY);
99 : Reference<container::XNameAccess> xFactories (
100 : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
101 254 : UNO_QUERY);
102 254 : ::std::vector<OUString> aProperties (snFactoryPropertyCount);
103 127 : aProperties[0] = "ServiceName";
104 127 : aProperties[1] = "ResourceList";
105 : ConfigurationAccess::ForAll(
106 : xFactories,
107 : aProperties,
108 254 : ::boost::bind(&ModuleController::ProcessFactory, this, _2));
109 : }
110 0 : catch (Exception&)
111 : {
112 : DBG_UNHANDLED_EXCEPTION();
113 : }
114 127 : }
115 :
116 508 : void ModuleController::ProcessFactory (const ::std::vector<Any>& rValues)
117 : {
118 : OSL_ASSERT(rValues.size() == snFactoryPropertyCount);
119 :
120 : // Get the service name of the factory.
121 508 : OUString sServiceName;
122 508 : rValues[0] >>= sServiceName;
123 :
124 : // Get all resource URLs that are created by the factory.
125 1016 : Reference<container::XNameAccess> xResources (rValues[1], UNO_QUERY);
126 1016 : ::std::vector<OUString> aURLs;
127 : tools::ConfigurationAccess::FillList(
128 : xResources,
129 : "URL",
130 508 : aURLs);
131 :
132 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::adding factory " <<
133 : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
134 :
135 : // Add the resource URLs to the map.
136 508 : ::std::vector<OUString>::const_iterator iResource;
137 2794 : for (iResource=aURLs.begin(); iResource!=aURLs.end(); ++iResource)
138 : {
139 2286 : (*mpResourceToFactoryMap)[*iResource] = sServiceName;
140 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": " <<
141 : OUStringToOString(*iResource, RTL_TEXTENCODING_UTF8).getStr());
142 508 : }
143 508 : }
144 :
145 127 : void ModuleController::InstantiateStartupServices()
146 : {
147 : try
148 : {
149 : tools::ConfigurationAccess aConfiguration (
150 : "/org.openoffice.Office.Impress/",
151 127 : tools::ConfigurationAccess::READ_ONLY);
152 : Reference<container::XNameAccess> xFactories (
153 : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
154 254 : UNO_QUERY);
155 254 : ::std::vector<OUString> aProperties (snStartupPropertyCount);
156 127 : aProperties[0] = "ServiceName";
157 : tools::ConfigurationAccess::ForAll(
158 : xFactories,
159 : aProperties,
160 254 : ::boost::bind(&ModuleController::ProcessStartupService, this, _2));
161 : }
162 0 : catch (Exception&)
163 : {
164 : OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
165 : }
166 127 : }
167 :
168 127 : void ModuleController::ProcessStartupService (const ::std::vector<Any>& rValues)
169 : {
170 : OSL_ASSERT(rValues.size() == snStartupPropertyCount);
171 :
172 : try
173 : {
174 : // Get the service name of the startup service.
175 127 : OUString sServiceName;
176 127 : rValues[0] >>= sServiceName;
177 :
178 : // Instantiate service.
179 : Reference<uno::XComponentContext> xContext =
180 254 : ::comphelper::getProcessComponentContext();
181 :
182 : // Create the startup service.
183 254 : Sequence<Any> aArguments(1);
184 127 : aArguments[0] <<= mxController;
185 : // Note that when the new object will be destroyed at the end of
186 : // this scope when it does not register itself anywhere.
187 : // Typically it will add itself as ConfigurationChangeListener
188 : // at the configuration controller.
189 127 : xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aArguments, xContext);
190 :
191 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::created startup service " <<
192 127 : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
193 : }
194 0 : catch (Exception&)
195 : {
196 : OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
197 : }
198 127 : }
199 :
200 : //----- XModuleController -----------------------------------------------------
201 :
202 477 : void SAL_CALL ModuleController::requestResource (const OUString& rsResourceURL)
203 : throw (RuntimeException, std::exception)
204 : {
205 477 : ResourceToFactoryMap::const_iterator iFactory (mpResourceToFactoryMap->find(rsResourceURL));
206 477 : if (iFactory != mpResourceToFactoryMap->end())
207 : {
208 : // Check that the factory has already been loaded and not been
209 : // destroyed in the meantime.
210 295 : Reference<XInterface> xFactory;
211 : LoadedFactoryContainer::const_iterator iLoadedFactory (
212 295 : mpLoadedFactories->find(iFactory->second));
213 295 : if (iLoadedFactory != mpLoadedFactories->end())
214 0 : xFactory = Reference<XInterface>(iLoadedFactory->second, UNO_QUERY);
215 295 : if ( ! xFactory.is())
216 : {
217 : // Create a new instance of the factory.
218 : Reference<uno::XComponentContext> xContext =
219 295 : ::comphelper::getProcessComponentContext();
220 :
221 : // Create the factory service.
222 590 : Sequence<Any> aArguments(1);
223 295 : aArguments[0] <<= mxController;
224 : OSL_TRACE("creating resource %s",
225 : OUStringToOString(iFactory->second, RTL_TEXTENCODING_ASCII_US).getStr());
226 : try
227 : {
228 885 : xFactory = xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
229 295 : iFactory->second,
230 : aArguments,
231 590 : xContext);
232 : }
233 0 : catch (const Exception&)
234 : {
235 : OSL_TRACE("caught exception while creating factory.");
236 : }
237 :
238 : // Remember that this factory has been instanced.
239 590 : (*mpLoadedFactories)[iFactory->second] = xFactory;
240 295 : }
241 : }
242 477 : }
243 :
244 : //----- XInitialization -------------------------------------------------------
245 :
246 127 : void SAL_CALL ModuleController::initialize (const Sequence<Any>& aArguments)
247 : throw (Exception, RuntimeException, std::exception)
248 : {
249 127 : if (aArguments.getLength() > 0)
250 : {
251 : try
252 : {
253 : // Get the XController from the first argument.
254 127 : mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
255 :
256 127 : InstantiateStartupServices();
257 : }
258 0 : catch (RuntimeException&)
259 : {}
260 : }
261 127 : }
262 :
263 : } } // end of namespace sd::framework
264 :
265 :
266 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
267 127 : com_sun_star_comp_Draw_framework_module_ModuleController_get_implementation(
268 : ::com::sun::star::uno::XComponentContext* context,
269 : ::com::sun::star::uno::Sequence<css::uno::Any> const &)
270 : {
271 127 : css::uno::Reference< css::uno::XInterface > xModCont ( sd::framework::ModuleController::CreateInstance(context) );
272 127 : xModCont->acquire();
273 127 : return xModCont.get();
274 66 : }
275 :
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|